0

I've had three different lab TAs look at my code and none of them have been able to help me, so I've decided to try here. Unless I delete all code relating to both gettimeofday and any semaphores, I get a "Segmentation fault (core dumped)​" error. I've boiled down my code to only the main thread with simple declarations to attempt to get to the root of the problem.

My code:

#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/time.h>  

void *threadN (void *); /* thread routines */

pthread_t tid[1]; /* array of thread IDs */

int main() 
{
    int i,j;
   /* here create and initialize all semaphores */ 
   int mutex = sem_create(777777, 1);

   int MatrixA[6000][3000];

   for (i=0; i < 6000; i++) {
       for (j=0; j < 3000; j++) {
           MatrixA[i][j]=i*j;
       }
   }
   int MatrixB[3000][1000];

   for (i=0; i < 3000; i++) {
       for (j=0; j < 1000; j++) {
           MatrixB[i][j]=i*j;
       }
   }

   int MatrixC[6000][1000];
   struct timeval tim;
   gettimeofday(&tim, NULL);
   float t1=tim.tv_sec+(tim.tv_usec/1000000.0);  
   gettimeofday(&tim, NULL);  
   float t2=tim.tv_sec+(tim.tv_usec/1000000.0);  
   printf("%.2lf seconds elapsed\n", t2-t1); 

   sem_rm(sem_open(777777, 1));
   return 0;
}​

I'm completely stumped here.

JamesENL
  • 6,400
  • 6
  • 39
  • 64
user2785277
  • 345
  • 4
  • 19
  • 3
    Can you please post an explanation of what your code is attempting to do? What are the expected outputs when you input certain values? Without this information it is difficult to debug your question. – JamesENL Mar 31 '15 at 03:36
  • At this point the code doesn't do much of anything. This is only the beginning o what will eventually use multithreading to quickly assign values to an array, but before I reach that point I need this first section to be able to run without simply giving a segmentation fault and nothing else. – user2785277 Mar 31 '15 at 03:48
  • In that case, can you narrow down exactly which line your seg fault is being thrown from. Threading in C is a notoriously tricky business. – JamesENL Mar 31 '15 at 03:49
  • The fault doesn't happen so long as I delete the gettimeofday functions, the float declarations, and the semaphores. If any of them are left in I get the fault. – user2785277 Mar 31 '15 at 03:58
  • The code crashes in which line? – alk Mar 31 '15 at 05:27
  • You are using `l` as size modifier in `printf("%.2lf....)` which is not correct. Maybe you wanted `L` for `long double` but your variables are just `float`s. Otherwise I got 0.00 with `float`s and correct output if I change `t1` and `t2` do `double` using `printf("%2f...)`. – Anto Jurković Mar 31 '15 at 06:27
  • possible duplicate of [Understanding memory allocation, test program crashing](http://stackoverflow.com/questions/8611198/understanding-memory-allocation-test-program-crashing) – Anto Jurković Mar 31 '15 at 07:47

2 Answers2

3

You eat your stack. See Radix sort for 10^6 array in C, comment from @Joachim Pileborg:

Local variables are usually stored on the stack, and the stack is usually limited to single-digit megabytes. On Windows for example, the default is 1MB per process, on Linux the default is 8MB...

I tried your code on Windows and it was dead with just MatrixA defined: 6000*3000*4 (for int)...

So you will have to move matrix data out of stack: define matrix as static or allocate on heap.

Community
  • 1
  • 1
Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
1

A useful thing I have found for tracking seg faults is with using gdb.

gcc -g -o a.out -c program.c

-g generates source level debug information

gdb a.out core

this starts up gdb with a.out

(gdb) run

this should run the program and show the line where the seg fault is happening.

Sweetness
  • 116
  • 7
  • 1
    This does not answer the question at all. It would have suited as comment however ... – alk Mar 31 '15 at 05:35
  • GDB answers all C debug questions! OP if GDB does not work for you I suggest running "man sem_open" we may have different versions but the first argument for sem_open is a char * so if it sem_open is looking at address location 777777 it would be a source for a potential seg fault. – Sweetness Mar 31 '15 at 14:45