C中的多线程错误分割故障



我正在尝试使用多线程将两个矩阵相乘。在这里,我在linux中使用gcc编译程序,并通过输入线程数来运行。

gcc multiThread.c -o test -lpthread
./test 4

在这里,我对N*N的矩阵进行乘法运算,其中N从10开始到1000,间隔为10,并计算每次迭代的执行时间。当我运行程序时,它会出现分段错误。请帮忙。

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include<time.h>
int SIZE = 10;   // Size by SIZE matrices
int num_thrd;   // number of threads
int A[2000][2000], B[2000][2000], C[2000][2000];
// initialize a matrix
void init_matrix(int m[SIZE][SIZE])
{
int i, j;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
m[i][j] = rand() % 100 + 1;
}
// thread function: taking "slice" as its argument
void* multiply(void* slice)
{
int s = (int)slice;   // retrive the slice info
int from = (s * SIZE)/num_thrd; // note that this 'slicing' works fine
int to = ((s+1) * SIZE)/num_thrd; // even if SIZE is not divisible by num_thrd
int i,j,k;
printf("computing slice %d (from row %d to %d)n", s, from, to-1);
for (i = from; i < to; i++)
{  
for (j = 0; j < SIZE; j++)
{
C[i][j] = 0;
for ( k = 0; k < SIZE; k++)
C[i][j] += A[i][k]*B[k][j];
}
}
printf("finished slice %dn", s);
}
int main(int argc, char* argv[])
{
FILE *outFile;
outFile = fopen("Algorithm3_Times.txt", "r");
pthread_t* thread;  // pointer to a group of threads
for(int ini=0; ini<100; ini++)
{

int i;
if (argc!=2)
{
printf("Usage: %s number_of_threadsn",argv[0]);
exit(-1);
}
num_thrd = atoi(argv[1]);
init_matrix(A);
init_matrix(B);
clock_t start = clock();
thread = (pthread_t*) malloc(num_thrd*sizeof(pthread_t));
// this for loop not entered if threadd number is specified as 1
for (i = 1; i < num_thrd; i++)
{
// creates each thread working on its own slice of i
if (pthread_create (&thread[i], NULL, multiply, (void*)i) != 0 )
{
perror("Can't create thread");
free(thread);
exit(-1);
}
}
// main thread works on slice 0
// so everybody is busy
// main thread does everything if threadd number is specified as 1
multiply(0);
// main thead waiting for other thread to complete
for (i = 1; i < num_thrd; i++)
pthread_join (thread[i], NULL);
clock_t end = clock();
float time = (end - start)*1000 / CLOCKS_PER_SEC;
fprintf(outFile,"time taken for Multiplication using %d", num_thrd);
fprintf(outFile," threads =  %f", time);
fprintf(outFile," milliseconds n");
if (thread != NULL)
{
free(thread);
thread = NULL;
}
SIZE += 10;
}
printf("calculation completed.nn");
return 0;
}

C是一种硬语言,尤其是因为默认情况下运行时错误不包含有用的调试信息。这就是调试器的用武之地。

我是如何用docker/alpine:调试的

  1. 将代码放入~/gcc/t.c中,以便与我的docker共享
  2. docker run --rm -it -vls-d~/gcc:/code alpine
  3. CCD_ 4安装gcc&friends、gdb和musl dbg,我需要它们来调试标准库中的segfault
  4. cd /code
  5. gcc -g t.c -o t
  6. gdb t

现在是我的调试器会话:

GNU gdb (GDB) 8.0.1
[ ... preamble removed for brevity ... ]
Reading symbols from t...done.
(gdb) run 1
Starting program: /code/t 1
warning: Error disabling address space randomization: Operation not permitted
computing slice 0 (from row 0 to 9)
finished slice 0
Program received signal SIGSEGV, Segmentation fault.
vfprintf (f=0x0, fmt=0x560896553020 "time taken for Multiplication using %d",
ap=ap@entry=0x7ffcb18b29f8) at src/stdio/vfprintf.c:671
671 src/stdio/vfprintf.c: No such file or directory.
(gdb) bt
#0  vfprintf (f=0x0,
fmt=0x560896553020 "time taken for Multiplication using %d",
ap=ap@entry=0x7ffcb18b29f8) at src/stdio/vfprintf.c:671
#1  0x00007fa6ef0c056f in fprintf (f=<optimized out>, fmt=<optimized out>)
at src/stdio/fprintf.c:9
#2  0x0000560896552ee2 in main (argc=2, argv=0x7ffcb18b2b68) at t.c:87

太好了,现在我们有一个行号要看。87号线是这条线:

fprintf(outFile,"time taken for Multiplication using %d", num_thrd);

我们可以看一看这条线,做一点思考,最终得出outFile:的定义

outFile = fopen("Algorithm3_Times.txt", "r");

啊哈!我们希望写入outFile,但我们打开它进行读取!我改为开放式写作:

outFile = fopen("Algorithm3_Times.txt", "w");

你的程序运行(注意下面我只显示前10行(

/code # ./t 10  |head
computing slice 1 (from row 1 to 1)
finished slice 1
computing slice 2 (from row 2 to 2)
computing slice 3 (from row 3 to 3)
finished slice 3
finished slice 2
computing slice 4 (from row 4 to 4)
finished slice 4
computing slice 5 (from row 5 to 5)
finished slice 5

既然您已经了解了gdb,那么您就可以开始在特定的行中放入gdbbreak语句,并解决任何剩余的错误。我的节目似乎一直没有结束,但我没有给它太多时间。

最新更新