.: Most Read :.

Join Churp Churp

Join Churp Churp
Click on the image to find out more

Friday, November 08, 2013

pthread and C program in Linux (Basic)

This example is suitable for those who are very new to pthread in C in Linux. You will learn on how to use pthread in C program. There is an additional code to be included during compiling the source code.


  1. Linux Machine
  2. gcc
  3. pthread header file

Pthread header file is a standard file. You can just download from the internet from any sources available. (the file name is usually pthread.h)


create a new directory, pthreadProgram

mkdir pthreadProgram

Go into the directory that you have created.

cd pthreadProgram

Place your downloaded pthread header file (pthread.h) into this directory.

now, create a new c file

vi tprog1.c

Write down the following codes:

//Program Name: tprog1
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
    long tid;
    tid = (long)threadid;
    printf("Hello World! It's me, thread #%ld!\n", tid);
int main (int argc, char *argv[])
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0; t<NUM_THREADS; t++){
        printf("In main: creating thread %ld\n", t);
        rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
        if (rc){
            printf("ERROR; return code from pthread_create() is %d\n", rc);
    /* Last thing that main() should do */

Done with writing the source code.

Press Esc

Then :wq [write and quit vi tool]

In this example, we have fix the number of threads to be 5. You can adjust the

To compile, you can do this in many ways. For this time, we are going to use direct compilation method.

gcc -pthread tprog1.c -o tprog1

Note that you need to include “-pthread” option during compiling the program.

If there is no error, you may now execute your program


Thursday, November 07, 2013

Tutorial: Simple C in Linux (Ubuntu)

Create, and compile simple C file on Linux terminal.


You have updated and upgraded your Linux OS.

sudo apt-get update

sudo apt-get upgrade

Installed gcc

sudo apt-get install gcc

Installed make (optional)

sudo apt-get install make



Login to your Linux machine.

vi sampleC.c

Press I to enter Insert mode.

Type the following C source code:


void main(void)


printf("Hello world\n");


Done with writing the source code.

Press Esc

Then :wq [write and quit vi tool]

To compile, you can do this in many ways;

Method 1 – direct compilation

gcc sampleC.c -o sampleC


Method 2 – compile using makefile

create a new make file

vi sampleMakefile

Type the following commands in makefile:


all: sampleC

#.o file

   sampleC: sampleC.o

   gcc -o sampleC.o

#.c file

   sampleC.o: sampleC.c

   gcc -o sampleC.c

Done with writing the commands in makefile.


You may replace “sampleC” by using your own filename.

# is used for command.

Press Esc

Then :wq [write and quit vi tool]

Execute the make file

make -f sampleMakefile

Make sure there is no error.

Now, your program is ready to be executed.


Sample output:

Hello world