c - 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 %d\n", 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_threads\n",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.\n\n");
return 0;
}
解决方案
C 是一门硬语言,尤其是因为默认情况下运行时错误不包含有用的调试信息。这就是调试器的用武之地。
我如何用 docker/alpine 调试它:
- 将代码放入
~/gcc/t.c
以共享给我的 docker docker run --rm -it -v
ls -d~/gcc:/code alpine
apk add build-base gdb musl-dbg
安装 gcc & friends、gdb 和 musl-dbg,我需要在标准库中调试 segfault。cd /code
gcc -g t.c -o t
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
,您可以开始break
在特定行中添加 gdb 语句并解决任何剩余的错误。我的程序似乎没有完成,但我没有给它太多时间。
推荐阅读
- python - 特定容器硒python中的所有元素属性
- python - 确定对象是向上还是向下
- javascript - jQuery - 仅在 ajax 加载特定 div 后运行脚本
- roblox - Roblox 脚本不运行
- kubernetes - Kubernetes yaml 文件 - env 和 envFrom 中的相同属性
- r - 分组数据上带有 slide_dbl() 的滚动窗口
- flutter - 使 ListView 上方的标题与 ListView 中的数据保持一致的最有效方法是什么?
- c++ - 相同的功能,不同的性能结果使用谷歌基准测试
- javascript - 如何根据配置对象创建过滤条件以过滤数组的数据项?
- apache-spark - 连接数据框中的列并根据数字对它们进行排序