Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation fault after the call of pthread_join()

I have written the following code using the POSIX pthread library:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

pthread_t pid1,pid2;

void *test(void *arg)
{
void **end;
printf("\nNew Thread ID: 0x%x\n",(unsigned int)pid2);
pthread_join(pid1,end);
printf("\nNew Thread going to go off\n");
printf("\nNew Thread ID: 0x%x\n",(unsigned int)pid2);
return ((void *)NULL);
}    

int main()
{
pid1 = pthread_self();
pthread_create(&pid2,NULL,test,NULL);
printf("\nMain Thread ID: 0x%x\n",(unsigned int)pid1);
sleep(2);
printf("\nI am going off\n");
pthread_exit(0);
}

On executing the code I got the following output:

Main Thread ID: 0xb7880b30
New Thread ID: 0xb787eb70
I am going off
Segmentation fault

As I studied, the thread(pid2) calling the pthread_join will block until the thread passed in argument(pid1) calls pthread_exit(). And pthread_exit() is used to stop the execution of a particular thread letting all others to keep on executing.

I want to know why I got Segmentation Fault at last.

Please explain me properly.

like image 326
pradeepchhetri Avatar asked May 03 '11 19:05

pradeepchhetri


People also ask

What is the use of pthread_join () function?

The pthread_join() function waits for a thread to terminate, detaches the thread, then returns the threads exit status. If the status parameter is NULL, the threads exit status is not returned.

What is the use of pthread_join () and pthread_exit () function?

The pthread_exit() function terminates the calling thread and returns a value via retval that (if the thread is joinable) is available to another thread in the same process that calls pthread_join(3).

What happens if pthread_join is not called?

After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated. Joining with a thread that has previously been joined results in undefined behavior. Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread".

Is pthread_join a blocking call?

Description: The pthread_join() function blocks the calling thread until the target thread thread terminates, unless thread has already terminated. If value_ptr is non-NULL and pthread_join() returns successfully, then the value passed to pthread_exit() by the target thread is placed in value_ptr.


2 Answers

You're using an uninitialized variable, void **end;, which results in undefined behavior:

pthread_join(pid1,end);

What you should instead be doing is:

void *end;
pthread_join(pid1, &end);

i.e. passing a meaningful pointer to a variable in which you want the result, rather than an uninitialized pointer.

like image 141
R.. GitHub STOP HELPING ICE Avatar answered Oct 03 '22 19:10

R.. GitHub STOP HELPING ICE


I think the problem is that your end pointer passed to pthread_join()isn't actually pointing anywhere. Try the following:

void *test(void *arg)
{
    void *end;    // <===
    printf("\nNew Thread ID: 0x%x\n",(unsigned int)pid2);
    pthread_join(pid1,&end);  // <===
    printf("\nNew Thread going to go off\n");
    printf("\nNew Thread ID: 0x%x\n",(unsigned int)pid2);
    return ((void *)NULL);
}
like image 36
Michael Burr Avatar answered Oct 03 '22 18:10

Michael Burr