multithreading - Creating multiple threads in C: write pid, tid and return integer -
i must this:
write program main thread creates 3 other threads. each ot these threads (different main thread) shall write pid , tid , terminate returning integer number between 1 , 3 , different value returned other threads. main thread shall print in standard out pid , value returned each of other threads.
i think i'm doing right, except don't understand how should return integer numbers between 1 , 3
i've following code far:
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <pthread.h> #define debug 0 // int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); // int pthread_join(pthread_t, void **value_ptr); void *printid(void *arg){ int i; unsigned long tid; tid = *((unsigned long *) arg); printf("thread tid: %lu thread pid: %d \n", tid, getpid()); } int main(int argc, char *argv[]) { pthread_t id1, id2, id3; pthread_create(&id1, null, printid, &id1); pthread_create(&id2, null, printid, &id2); pthread_create(&id3, null, printid, &id3); pthread_join(id1, null); pthread_join(id2, null); pthread_join(id3, null); printf("main thread pid: %d\n", getpid()); sleep(1); }
since threads in c can return void*. can allocate space int want return, read main, don’t forget free it.
Comments
Post a Comment