我正在使用 BSPlib,当在多个线程(以及许多其他线程)上运行的函数上添加“int i”的简单定义时,我收到一条消息,如“进程 2 捕获信号 11 分段错误”。重要的是要注意我检查了很多,没有它我不会得到分段错误,而且我几乎一直都得到它。int 定义如何导致它?是否有我可能导致的堆栈溢出?谢谢。
int P;
int main( int argc, char* argv[] )
{
/** sequentail - process 0 */
P=bsp_nprocs(); /// maximum number of process avialble (must do that on sequential part ,need for bsp begin)
bsp_begin(P);
char* str1;
char* str2;
int n;
int** table;
int thread=bsp_pid();
int num_threads=bsp_nprocs();
if(thread == 0)
{
ifstream file1(argv[1]);
ifstream file2(argv[2]);
// check if the strings are the same size RDBG
string string1((istreambuf_iterator<char>(file1)), istreambuf_iterator<char>());
string string2((istreambuf_iterator<char>(file2)), istreambuf_iterator<char>());
n=string1.length();
str1= (char*)malloc(sizeof(char)*(n+1));
str2= (char*)malloc(sizeof(char)*(n+1));
strcpy(str1,string1.c_str());
strcpy(str2,string2.c_str());
}
if (thread!=0)
{
str1= (char*)malloc(sizeof(char)*(n+1));
str2= (char*)malloc(sizeof(char)*(n+1));
}
bsp_push_reg(&n,SZINT);
bsp_sync();
bsp_get(0,&n,0,&n,SZINT);
bsp_sync();
if (thread==0)
{
table=(int**)malloc(sizeof(int)*(n+1));
for (int i=0; i<n+1; i++)
table[i]=(int*)malloc(sizeof(int)*(n+1));
}
bsp_push_reg(str1,SZCHAR*(n+1));
bsp_push_reg(str2,SZCHAR*(n+1));
bsp_push_reg(table,n*n*SZINT);
bsp_sync();
if (thread==0)
{
for(int t=1; t<num_threads; t++)
for (int k=0; k<=n; k++)
{
bsp_put(t,str1+k,str1,k*SZCHAR,SZCHAR);
bsp_put(t,str2+k,str2,k*SZCHAR,SZCHAR);
}
}
bsp_sync();
cout << thread << "!!!" << str1 << ";" << str2 << endl;
int i;
bsp_sync();
bsp_pop_reg(table);
bsp_pop_reg(str2);
bsp_pop_reg(str1);
bsp_pop_reg(&n);
bsp_sync();
free(str1);
free(str2);
bsp_sync();
bsp_end();
return 0;
}