C语言中的多线程错误分割错误

C语言中的多线程错误分割错误,c,linux,multithreading,pthreads,C,Linux,Multithreading,Pthreads,我正在尝试使用多线程将两个矩阵相乘。在这里,我在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> #

我正在尝试使用多线程将两个矩阵相乘。在这里,我在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;

}
#包括
#包括
#包括
#包括
int SIZE=10;//按大小排列的矩阵
int num_thrd;//线程数
INTA[2000][2000],B[2000][2000],C[2000][2000];;
//初始化矩阵
void init_矩阵(int m[SIZE][SIZE])
{
int i,j;
对于(i=0;ifor(int ini=0;iniC)是一种硬语言,特别是因为默认情况下运行时错误不包含有用的调试信息。这就是调试器的作用

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

  • 将代码放入
    ~/gcc/t.c
    以共享给我的docker
  • docker-run-rm-it-v
    ls-d~
    /gcc:/code-alpine
  • apk添加build base gdb musl dbg
    以安装gcc&friends、gdb和musl dbg,我需要在标准库中调试segfault
  • cd/code
  • gcc-gtc-ot
  • 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
    
    我们可以看一看这行代码,做一些思考,最终来看看
    outFile
    的定义:

    outFile = fopen("Algorithm3_Times.txt", "r");
    
    啊哈!我们本想写外文,但我们打开了它来阅读!我改为打开来写:

      outFile = fopen("Algorithm3_Times.txt", "w");
    
    并且您的程序运行(注意下面我只显示前10行)


    现在您已经了解了
    gdb
    ,您可以开始在特定行中输入gdb
    break
    语句,并解决任何剩余的bug。我的程序似乎从未完成,但我没有给它太多时间。

    如果您想用C做任何远程复杂的事情,您确实需要开始使用调试器。当我终于意识到我浪费了多少生命,我真的踢了我自己,因为我没有学习<代码> GDB < /C> >。不要犯同样的错误。我使用了调试器!为什么你要标记C++?这不是主要问题,而是调用<代码> iITIX矩阵(A);< /COD>和<代码>;
    没有初始化您认为是的数组部分。是否收到编译器关于不兼容指针类型的警告?
      outFile = fopen("Algorithm3_Times.txt", "w");
    
    /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