1

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

4
  • 1
    I'm not getting the logic. There's a strong possibility that thread5 isn't initialized. Commented Oct 17, 2016 at 21:56
  • 3
    "If successful, the pthread_join() function shall return zero; otherwise, an error number shall be returned to indicate the error." Commented Oct 17, 2016 at 21:57
  • 1
    On windows, I get the following:je suis le thread 1. je suis le thread 2. je suis le thread 3. je suis l'etape 7 et la fin. pthread_join: No error Commented Oct 17, 2016 at 21:58
  • 4
    The second call to phread_join using the argument thread1 will cause undefined behavior. Commented Oct 17, 2016 at 22:00

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, mostly, but you cannot join thread1 twice, as your code does. But once the main thread joins thread1, it doesn't need to do so again anyway.
I saw it the first time, but got overwhelmed by the flawed logic. Thanks (I actually did not test that)
thank you, the code works perfectly now, but i still dont understand why you say that thread5 is not initialised??
in your original code, since return codes were reversed, you never created thread5, but you tried to join it, thus using the thread5 variable uninitialized for join, which explains the crash (thread3 was OK)
ahh, thank you again, i start getting the logic of this now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.