Reputation: 13
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
Upvotes: 1
Views: 80
Reputation: 140148
pthread_join
returns 0 when OK. You have a logic problem in your code, running next threads only if failed, and returning with errors at the same time!
The problem you get is that with all that wrong logic you try to join on thread5
which is not initialized.
Attempt to fix your code, just adding a couple of else
statements & changed tested values against 0, now it makes sense:
int main(void){
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
pthread_t thread4;
pthread_t thread5;
pthread_t thread6;
if (pthread_create(&thread1, NULL, thread_1, NULL))
{
perror("pthread_create 1");
return EXIT_FAILURE;
}
if (pthread_create(&thread2, NULL, thread_2, NULL))
{
perror("pthread_create 2");
return EXIT_FAILURE;
}
if (pthread_create(&thread3, NULL, thread_3, NULL))
{
perror("pthread_create 3");
return EXIT_FAILURE;
}
if(pthread_join(thread1, NULL)==0){
if (pthread_create(&thread4, NULL, thread_4, NULL))
{
perror("pthread_create 4");
return EXIT_FAILURE;
}
}
else
{
perror("pthread_join");
return EXIT_FAILURE;
}
if(pthread_join(thread2, NULL)==0){
if (pthread_create(&thread5, NULL, thread_5, NULL))
{
perror("pthread_create 5");
return EXIT_FAILURE;
}
}
else
{
perror("pthread_join");
return EXIT_FAILURE;
}
if(pthread_join(thread3, NULL)==0 && pthread_join(thread5, NULL)==0){
if (pthread_create(&thread6, NULL, thread_6, NULL))
{
perror("pthread_create 6");
return EXIT_FAILURE;
}
}
else
{
perror("pthread_join");
return EXIT_FAILURE;
}
if(pthread_join(thread4, NULL)==0 && pthread_join(thread6, NULL)==0){
printf("je suis l'etape 7 et la fin.\n");
}
else
{
perror("pthread_join");
return EXIT_FAILURE;
}
return 0;
}
Upvotes: 1