Note that there are some explanatory texts on larger screens.

plurals
  1. POPthread_join() Causing segment default error
    text
    copied!<p>What the following code trying to accomplish is just to compute the Matrix Multiplication of A and B to get matrix C. It uses nXn threads to compute each entry of C independently. So the code works on Cygwin, but not on linux. I keep getting segment default with the Pthread_join calls.</p> <pre><code>#define _REENTRANT // Make sure the library functions are MT (muti-thread) safe #include &lt;stdio.h&gt; #include &lt;pthread.h&gt; #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include &lt;string.h&gt; #define BUFFER_SIZE 512 // Declare a structure data type that will be used to pass arguments to the worker threads typedef struct args_for_thread_t{ int *rowA; int rowIdx; int *columnB; int columnIdx; int **matrixC; } ARGS_FOR_THREAD; /* Global Variables */ int numRows,numColumns; /*Function Prototype*/ void *computeC(void *this_arg); void printMatrix(int** matrix,int numRows,int numColumns); int main(void){ const char filename[] = "input_data.txt"; FILE *file = fopen(filename,"r"); char *delims = " "; int **matrixA,**matrixB,**matrixC; int flagB = 0; //Indicate wether the program should process matrixB int i,j; if (file){ char line[BUFFER_SIZE]; int rowIdx = 0; while (fgets(line,sizeof(line), file)){ char substr[BUFFER_SIZE], *result; //fputs(line,stdout); result = strtok(line, delims); int columnIdx = 0; //Once we reach a line break, we start the processing of matrix B if (!strcmp(line,"\n")){ flagB = 1; rowIdx = 0; //Reset the rowIdx continue; //Skip the new line, and start to read data into matrix B } while (result != NULL){ if (!strcmp(result,"ROWS")){ //To retrieve the number of rows result = strtok(NULL,delims); numRows = atoi(result); matrixA = (int **) malloc(numRows*sizeof(int*)); matrixB = (int **) malloc(numRows*sizeof(int*)); matrixC = (int **) malloc(numRows*sizeof(int*)); rowIdx = -1; result = strtok(NULL,delims); } else if (!strcmp(result,"COLUMNS")){//To retrieve the number of Columns result = strtok(NULL,delims); numColumns = atoi(result); for (i=0;i&lt;numRows;i++){ //Malloc the columns matrixA[i] = (int *) malloc(numColumns*sizeof(int)); matrixB[i] = (int *) malloc(numColumns*sizeof(int)); matrixC[i] = (int *) malloc(numColumns*sizeof(int)); } rowIdx = -1; result = strtok(NULL,delims); } else if (!flagB){ //Processing Matrix A matrixA[rowIdx][columnIdx] = atoi(result); columnIdx++; result = strtok(NULL,delims); } else if (flagB){ //Processing Matrix B matrixB[rowIdx][columnIdx] = atoi(result); columnIdx++; result = strtok(NULL,delims); } } rowIdx++; } } else{ printf("No Such File exists!\n"); } //At this point, matrix A and matrix B are both ready for computation. We will start to compute the product of the two matrices int num_threads = numRows*numColumns; //The toal number of worker threads pthread_t *worker_thread = (pthread_t *) malloc(sizeof(pthread_t)*num_threads); ARGS_FOR_THREAD *args_for_thread; for(i = 0; i &lt; numRows; i++){ for(j = 0; j &lt; numColumns; j++){ args_for_thread = (ARGS_FOR_THREAD *)malloc(sizeof(ARGS_FOR_THREAD)); // Allocate memory for the structure that will be used to pack the arguments args_for_thread-&gt; rowA = matrixA[i]; //We need to allocate the corresponding column in B for multiplication int k; args_for_thread-&gt;columnB =(int *) malloc(sizeof(int)*numRows); for (k=0;k&lt;numRows;k++){ args_for_thread-&gt; columnB[k] = matrixB[k][j]; } //rowIdx and columnIdx gives the corresponding entry for matrix C args_for_thread-&gt; rowIdx = i; args_for_thread-&gt; columnIdx = j; args_for_thread-&gt; matrixC = matrixC; if((pthread_create(&amp;worker_thread[i], NULL, computeC, (void *)args_for_thread)) != 0){ printf("Cannot create thread \n"); exit(0); } } } // Wait for all the worker threads to finish for(i = 0; i &lt; num_threads; i++) pthread_join(worker_thread[i], NULL); //Print out the Final Matrix C printMatrix(matrixC,numRows,numColumns); //Clean up pointers for(i = 0; i &lt; numRows; i++){ free(matrixA[i]); free(matrixB[i]); free(matrixC[i]); } free(matrixA); free(matrixB); free(matrixC); } void printMatrix(int** matrix,int numRows, int numColumns){ int i,j; for (i=0;i&lt;numRows;i++){ for (j=0;j&lt;numColumns;j++){ printf("%d ",matrix[i][j]); if (j==numColumns-1){ printf("\n"); } } } } /* Function that will be executed by all the worker threads. It will compute the i,j entry for column C */ void *computeC(void *this_arg){ ARGS_FOR_THREAD *arg = (ARGS_FOR_THREAD *) this_arg; int rowIdx = arg-&gt;rowIdx; int columnIdx = arg-&gt;columnIdx; int *rowA = arg-&gt;rowA; int *columnB = arg-&gt;columnB; int **matrixC = arg-&gt;matrixC; int i; int sum = 0; for (i=0;i&lt;numRows;i++){ //Compute entry for matrix C. Since A,B are nxn square matrix, we can use either numRows or numColumns as the size sum += rowA[i]*columnB[i]; } matrixC[rowIdx][columnIdx] = sum; free((void *) arg); // Free up the structure pthread_exit(NULL); } </code></pre> <p>What is the issue here? Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload