C Program to implement
Multi-threaded program
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 2
/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
int tid;
double stuff;
} thread_data_t;
/* thread function */
void *thr_func(void *arg) {
thread_data_t
*data = (thread_data_t *)arg;
fprintf(stdout, "hello from thr_func, thread id: %d\n", data->tid);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t
thr[NUM_THREADS];
int i, rc;
/* create a thread_data_t
argument array */
thread_data_t
thr_data[NUM_THREADS];
/* create threads */
for (i = 0; i
< NUM_THREADS; ++i) {
thr_data[i].tid = i;
if
((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) {
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
}
/* block until all
threads complete */
for (i = 0; i
< NUM_THREADS; ++i) {
pthread_join(thr[i], NULL);
}
return EXIT_SUCCESS;
}
output:
gcc thread.c -lpthread
neelkanth_surekha#ps -elfT | grep a.out
0 S neelkan+ 3273 3273 2526 0
80 0 - 22112 futex_ 01:37 pts/19 00:00:00 ./a.out
1 R neelkan+ 3273 3274 2526 99 80
0 - 22112 - 01:37 pts/19 00:00:06 ./a.out
1 R neelkan+ 3273 3275 2526 99 80
0 - 22112 - 01:37 pts/19 00:00:06 ./a.out
0 S neelkan+ 3277 3277 2526 0
80 0 - 3556 pipe_w 01:37 pts/19 00:00:00 grep
--color=auto a.out