im beginner in C system programing, im trying to write a code in C that use threads to realise the graph in the photo,click here to see the graph so i have write this
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void *thread_1(void *arg){
printf("je suis le thread 1.\n");
(void) arg;
pthread_exit(NULL);
}
void *thread_2(void *arg){
printf("je suis le thread 2.\n");
(void) arg;
pthread_exit(NULL);
}
void *thread_3(void *arg){
printf("je suis le thread 3.\n");
(void) arg;
pthread_exit(NULL);
}
void *thread_4(void *arg){
printf("je suis le thread 4 et je suis lance apres la fin du thread 1.\n");
(void) arg;
pthread_exit(NULL);
}
void *thread_5(void *arg){
printf("je suis le thread 5 et je suis lance apres la fin du thread 1 et le thread 2.\n");
(void) arg;
pthread_exit(NULL);
}
void *thread_6(void *arg){
printf("je suis le thread 6 et je suis lance apres la fin du thread 3 et le thread 5.\n");
(void) arg;
pthread_exit(NULL);
}
int main(void){
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
pthread_t thread4;
pthread_t thread5;
pthread_t thread6;
pthread_create(&thread1, NULL, thread_1, NULL);
pthread_create(&thread2, NULL, thread_2, NULL);
pthread_create(&thread3, NULL, thread_3, NULL);
if(pthread_join(thread1, NULL)){
pthread_create(&thread4, NULL, thread_4, NULL);
perror("pthread_join");
return EXIT_FAILURE;
}
if(pthread_join(thread1, NULL) && pthread_join(thread2, NULL)){
pthread_create(&thread5, NULL, thread_5, NULL);
perror("pthread_join");
return EXIT_FAILURE;
}
if(pthread_join(thread3, NULL) && pthread_join(thread5, NULL)){
pthread_create(&thread6, NULL, thread_6, NULL);
perror("pthread_join");
return EXIT_FAILURE;
}
if(pthread_join(thread4, NULL) && pthread_join(thread6, NULL)){
printf("je suis l'etape 7 et la fin.\n");
perror("pthread_join");
return EXIT_FAILURE;
}
else{
return EXIT_FAILURE;
}
return 0;
}
when this code is excuted i get this message
je suis le thread 2.
je suis le thread 1.
je suis le thread 3.
Segmentation fault (core dumped)
what i want it to be is that the threads are created in sort to be like the graph mean 1-2-3 then 4 after 1 ends and 5 after 1 and 2 ends, 6 will be created after 3 and 5 ends and the last instruction will be excuted after 6 and 4 ends, can anyone show me what i did wrong
thread5isn't initialized.