I want to create a program that creates 2 child process, and each of them creates 2 threads, and each thread prints its thread id. I0ve allread done that the outuput isn't the outuput i want.
When a run the following comand "$./a.out | sort -u | wc -l" I have the folowing output
2
$:
It should be 6.
Thanks in advance
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "debug.h"
#define FFORK 1
#define FTHRE 2
void* identify(void *arg);
int main(int argc, char *argv[]){
pthread_t th_id[2];
short int i = 0;
(void)argc; (void)argv;
setvbuf(stdout,NULL,_IONBF,0);
switch (fork()){
case -1:
ERROR(FFORK,"Fork Failed\n");
break;
case 0:
if (pthread_create(&th_id[0],NULL,identify,NULL) != 0 || pthread_create(&th_id[1],NULL,identify,NULL) != 0)
ERROR(FTHRE,"FTHRE\n");
if (pthread_join(th_id[0],NULL) != 0 || pthread_join(th_id[1],NULL) != 0)
ERROR(FTHRE,"FTHRE\n");
exit(0);
break;
default:
switch(fork()){
case -1:
ERROR(FFORK,"FFORK\n");
break;
case 0:
if (pthread_create(&th_id[0],NULL,identify,NULL) != 0 || pthread_create(&th_id[1],NULL,identify,NULL) != 0)
ERROR(FTHRE,"FTHRE\n");
if (pthread_join(th_id[0],NULL) != 0 || pthread_join(th_id[1],NULL) != 0)
ERROR(FTHRE,"FTHRE\n");
exit(0);
break;
default:
switch(fork()){
case -1:
ERROR(FFORK,"FFORK\n");
break;
case 0:
if (pthread_create(&th_id[0],NULL,identify,NULL) != 0 || pthread_create(&th_id[1],NULL,identify,NULL) != 0)
ERROR(FTHRE,"FTHRE\n");
if (pthread_join(th_id[0],NULL) != 0 || pthread_join(th_id[1],NULL) != 0)
ERROR(FTHRE,"FTHRE\n");
exit(0);
break;
default:
for (i=0;i < 3; i++)
wait(NULL);
}
}
}
return 0;
}
void* identify(void *arg){
(void) arg;
printf("%lu\n", (unsigned long int)pthread_self());
pthread_exit(NULL);
}