调用 pthread_join() 后出现分段错误

发布于 2024-11-04 17:37:30 字数 880 浏览 1 评论 0原文

我使用 POSIX pthread 库编写了以下代码:

#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);
}

在执行代码时,我得到以下输出:

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

正如我所研究的,调用 pthread_join 的线程(pid2)将阻塞,直到传入参数(pid1)的线程调用 pthread_exit()。 pthread_exit() 用于停止特定线程的执行,让所有其他线程继续执行。

我想知道为什么我最后出现了分段错误。

请正确地向我解释一下。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明月松间行 2024-11-11 17:37:36

您正在使用未初始化的变量,void **end;,这会导致未定义的行为:

pthread_join(pid1,end);

您应该做的是:

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

即将一个有意义的指针传递给您想要结果的变量,而不是未初始化的指针。

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.

妄想挽回 2024-11-11 17:37:36

我认为问题在于您传递给 pthread_join() 的 end 指针实际上并未指向任何地方。请尝试以下操作:

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);
}

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);
}
风筝在阴天搁浅。 2024-11-11 17:37:36

分段错误仅仅意味着您尝试进行内存访问或跳转到操作系统不允许您执行代码或读取/写入的内存中的某个位置。在这种情况下,您生成的子线程应该在 pthread_join() 调用之后返回到哪里,因为操作系统已经清理了主父进程并回收了主父进程使用的所有内存(此包括执行代码以及堆栈空间、堆空间等)? ...这绝对不是用户态线程可以访问的内存,因此操作系统会抛出分段错误。

A segmentation fault merely means that you've tried to make a memory access or jump to some location in memory that the OS hasn't allowed you to either execute code from or read/write from. In this case where is your spawned child thread suppose to return to after the pthread_join() call since the OS has cleaned up the main parent process and reclaimed the all the memory used by the main parent process (this includes execution code as well as stack-space, heap-space, etc.)? ... It's definitely not memory that a user-land thread has access to, hence the OS throws a segmentation fault.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文