Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C pthread_join()导致分段错误_C_Segmentation Fault_Pthreads_Coredump_Pthread Join - Fatal编程技术网

C pthread_join()导致分段错误

C pthread_join()导致分段错误,c,segmentation-fault,pthreads,coredump,pthread-join,C,Segmentation Fault,Pthreads,Coredump,Pthread Join,我正在努力理解线程,遇到了这个我似乎无法纠正的分段错误。我已经将错误范围缩小到pthread\u join()函数,但似乎不能再进一步了。我的理解是for循环需要与pthread\u create()的循环相同,传入的变量是传递到pthread\u exit()函数的解引用指针。任何指示都将不胜感激 #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <math.h&g

我正在努力理解线程,遇到了这个我似乎无法纠正的分段错误。我已经将错误范围缩小到
pthread\u join()
函数,但似乎不能再进一步了。我的理解是for循环需要与
pthread\u create()
的循环相同,传入的变量是传递到
pthread\u exit()函数的解引用指针。任何指示都将不胜感激

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

//void *runner(void *param);

void *pFactor(void *param)
{
        int *pFArray = malloc(sizeof(int) *32);
        int myNum = atoi(param);
        int count = 0;

        printf("I'm getting here");

        while (myNum % 2 == 0)
        {
                pFArray[count] = 2;
                myNum = myNum/2;
                count++;
        }

        for (int i = 3; i < sqrt(myNum); i += 2)
        {
                while (myNum % i == 0)
                {
                        pFArray[count] = i;
                        myNum = myNum/i;
                        count++;
                }
        }

        if (myNum > 2)
        {
                pFArray[count] = myNum;
        }

        //int *arrPtr = (int*) malloc(10);
        //arrPtr = pFArray;
        //return (void *) arrPtr;
        //return fprint("I made it to the end of the pFactor function")
        pthread_exit(pFArray);

}

int main(int argc, char *argv[])
{
        pthread_t tid[argc];
        pthread_attr_t attr;

        if (argc == 0)
        {
                fprintf(stderr, "usage: a.out <integer value>\n");
                return -1;
        }
        if (argc < 0)
        {
                fprintf(stderr, "%d must be >= 0\n", atoi(argv[1]));
                return -1;
        }

        /* Get the default attributes */
        pthread_attr_init(&attr);
        /* Create the thread */
        for (int i = 1; i < argc; i++)
        {
                tid[i] = i;
                pthread_create(&tid[i], &attr, pFactor, &argv[i]);
                printf("still working\n");
        }
        printf("still working\n");
        /* Wait for the thread to exit */
        for (int i = 1; i < argc; i++)
        {
                tid[i] = i;
                void *my_Ptr;
                printf("still working 1\n");
                pthread_join(tid[i], &my_Ptr);
                printf("still working 2\n");
                for (;;)
                {
                        printf("%d", atoi(argv[i]));
                        printf(": %d",((int*) my_Ptr)[i-1]);
                }
                //printf("%d", atoi(argv[i]));
                //printf(": %d",((int*) my_Ptr)[i-1]);
        } /*This is where you want to pass in the sum variable*/
}
#包括
#包括
#包括
#包括
//void*runner(void*param);
void*p因子(void*param)
{
int*pFArray=malloc(sizeof(int)*32);
int myNum=atoi(参数);
整数计数=0;
printf(“我来了”);
而(myNum%2==0)
{
pFArray[count]=2;
myNum=myNum/2;
计数++;
}
对于(int i=3;i2)
{
pFArray[count]=myNum;
}
//int*arrPtr=(int*)malloc(10);
//arrPtr=pfr阵列;
//返回(无效*)arrPtr;
//return fprint(“我在pFactor函数的末尾完成了它”)
pthread_exit(pFArray);
}
int main(int argc,char*argv[])
{
pthread_t tid[argc];
pthread_attr_t attr;
如果(argc==0)
{
fprintf(stderr,“用法:a.out\n”);
返回-1;
}
如果(argc<0)
{
fprintf(stderr,“%d必须大于等于0\n”,atoi(argv[1]);
返回-1;
}
/*获取默认属性*/
pthread_attr_init(&attr);
/*创建线程*/
对于(int i=1;i
您的代码有一个越界写入错误

以下是错误消息

==狭窄DTS消息的开始==在此处复制开始==(56.127)==

运行时错误:[越界写入]
继续执行可能导致未定义的行为,中止

-
- Writing 4 bytes to 0x8fa0090 will corrupt the adjacent data.
- 
- The object to-be-written (start:0x8fa0010, size:128 bytes) is allocated at
-     file:/prog.c::10, 24
- 
-  0x8fa0010               0x8fa008f
-  +------------------------+
-  |the object to-be-written|......
-  +------------------------+
-                            ^~~~~~~~~~
-        the write starts at 0x8fa0090 that is right after the object end.
- 
- Stack trace (most recent call first) of the write.
- [1]  file:/prog.c::18, 17
- [2]  file:/musl-1.1.10/src/thread/pthread_create.c::168, 17
-

===狭窄DTS消息的结束===此处复制结束=====

valgrind
下运行程序可确认在调用
pthread\u join
时发生问题的诊断。这也可以通过在调试器中运行Valgrind来确定,但Valgrind通常对segfaults特别有用,因为它的内存使用分析通常也有助于确定问题的原因

但是,在本例中,主要问题很清楚:您使用无效的第一个参数调用
pthread\u join()
。你似乎有点误解:

我的理解是for循环需要与for循环相同 对于
pthread\u create()

不,不一定。与
pthread\u create
唯一重要的关联是,第一个参数,即线程ID,需要是一个由
pthread\u create()
存储的值,用于标识既没有分离也没有加入的线程。在类似于启动线程的循环中执行联接可能是合适的,但这是由应用程序的特定需求驱动的。通常,这些要求包括在主线程经过某个点之前完成所有工作线程,为了实现这一点,必须将每个工作线程连接起来

传入的变量是 正在传递到pthread_exit()的解引用指针 功能

当我知道
pthread\u join()
的行为时,我几乎可以将这些单词压缩成适合的形状。我自己,我会说得更像,“第二个参数,如果不是null,则是指向写入线程函数返回值的位置的指针,或者它传递给
pthread_exit()
的值。”

无论如何,主要问题是我在注释中已经表达的:在联接循环中,您正在覆盖
pthread\u create
提供的线程ID,并将您的任意值传递给
pthread\u join
这些不会识别线程,如果pthreads实现与我的类似,那么错误的线程ID实际上是无效的指针。您似乎不了解的是,生成和分配线程ID的是
pthread\u create
。这就是为什么必须向它传递一个用于存储ID的地址。因此,在将其地址传递给
pthread\u create
之前初始化线程ID变量是没有用的


清除该问题并再次在valgrind下运行程序会发现线程函数中还有一个问题,这在其他答案中也有提及。发生这种情况的部分原因是
pthread\u create
的第四个参数与线程函数对其参数的使用不匹配

T