I must do this:
Write a program whose main thread creates 3 other threads. Each ot these threads (different from the main thread) shall write its pid and tid and terminate returning an integer number between 1 and 3 and different from the value returned by the other threads. The main thread shall print in the standard out its pid and the value returned by each of the other threads.
I think I'm doing it right, except that I don't understand how should I return the integer numbers between 1 and 3
I've the following code so 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);
}
Aucun commentaire:
Enregistrer un commentaire