我有这段代码,但它有一个错误:Segmentation Fault(核心转储),它不能与更多的2个线程一起工作。你知道我做错了什么吗?
此代码用于通过莱布尼兹公式计算圆周率
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
#define NUM_HILOS 2
struct datos
{
int inicio;
int fin;
float *pi;
}
*calcPi (void *datos){
struct datos *datos_proceso;
datos_proceso = (struct datos *) datos;
int i = datos_proceso -> inicio;
int end = datos_proceso -> fin;
printf("inicio %d n", i);
printf("fin %d n", end);
float *pi = datos_proceso -> pi;
int signo = 1;
do{
*pi = *pi +(signo*4.0)/((2*i)+1);
i++;
signo *= -1;
//printf("%f n", *pi);
}while(i<end);
}
int main()
{
int error, i;
float *pi;
int j = -1;
/*variable para hilos*/
我认为错误已经过去了,但我不知道如何修复
struct datos hilo_datos[NUM_HILOS];
pthread_t idhilo[NUM_HILOS];
//printf("este es pi %f n", *pi);
for(i=0; i<NUM_HILOS; i++)
{
hilo_datos[i].inicio =j+1;
hilo_datos[i].fin =j+1000;
hilo_datos[i].pi = pi;
printf("%d n", hilo_datos[i].inicio);
printf("%d n", hilo_datos[i].fin);
j += 1000;
}
for(i=0; i<NUM_HILOS; i++)
{
error=pthread_create(&idhilo[i], NULL, (void *)calcPi, &hilo_datos[i]);
}
for(i=0; i<NUM_HILOS; i++)
{
pthread_join(idhilo[i], NULL);
}
printf("este es pi %f n", *pi);
return 0;
}
您的错误主要是忘记了变量初始化等简单的事情。当像float *pi;
这样的指针在初始化之前被访问时,它几乎总是会引起问题。至少它应该引发编译器警告。顺便说一下,打开所有编译器警告GCC选项
以下是获得干净构建的一些细节。。。
1 将return语句添加到calcPi函数
...
return 0;
}
2使用;
终止结构数据
struct datos {
...
};
^
3功能:
* calcPi (void *datos){...}
应该是:
void * calcPi (void *datos){...}
或者更好:
void calcPi (struct datos *d){...} //passing a struct pointer
4在使用变量之前初始化它们。例如:
float *pi; //uninitialized
float *pi = NULL;//initialized pointer
pi = malloc(sizeof(float)*NUM_HILOS);//array of pi with NUM_HILOS elements
然后,在下面的赋值语句中使用pi[i]。。。
hilo_datos[i].pi = pi[i];
或者,只需创建一个简单的浮点:(在这种情况下不需要指针)
float pi = 0;//works just fine for what you are doing
//no further initialization is needed
其他问题包括线程应用错误、创建不合适的变量类型(即,就您使用它的方式而言,float *pi;
可能只是float pi;
,而不需要malloc()
)
下面包含了一个使用此算法计算pi(无线程)的非常简单的示例,用于说明:
#include <stdio.h>
#include <stdlib.h>
#define NUM_HILOS 10 //array elements (no threads)
struct datos //modified for use as array, no threads
{
float pi; //PI
};
void calcPi (struct datos *d)
{
struct datos *datos_proceso = d;
float pi = datos_proceso[0].pi;
int signo = 1;
int i;
for(i=0;i<NUM_HILOS;i++)
{
pi = pi + (signo*4.0)/((2*i)+1);
signo *= -1;
d[i].pi = pi; //change values for NUM_HILOS to see how
//values for pi converge here.
}
}
int main()
{
int error, i;
float pi = 0;
int j = -1;
//your comment: I think that the error...
//... (The error you were seeing here was
//caused by an uninitialized float *pi; which has been
//changed to float pi = 0; in this example)
struct datos hilo_datos[NUM_HILOS];
for(i=0; i<NUM_HILOS; i++)
{
hilo_datos[i].pi = 0; //initialize all to zero
}
calcPi(hilo_datos);//send array of struct, check all elelments when returned
for(i=0; i<NUM_HILOS; i++)
{
printf("este es pi %f n", hilo_datos[i].pi);
}
getchar();
return 0;
}