jeudi 28 septembre 2017

Segmentation fault using rand(): no such file or directory

I'm trying to implement a Fibonacci program that uses fork() and message queues while using getopt to set command line options for the nth Fibonacci number and 'm' computing threshold. So far, I get my program to compile but when I try to run it I get either Segmentation fault (11 or core dumped). I tried using gdb to debug it but I'm not having any luck. I'm just starting to learn about segmentation faults and gdb so any guidance will be greatly appreciated. Thanks!

main.c

    #include <stdlib.h>
    #include <stdio.h> 
    #include <unistd.h> 
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h> 
    #include <sys/stat.h> 

    int fib_seq(int x);

    int main(int argc, char **argv)  
    {

      int c, n, m, Fflag, Sflag; int x=0;

      pid_t pid; 

      //interprocess communication
      const int size=4096; 
      char *shared_memory; 

           int segment_id=shmget(IPC_PRIVATE, size, S_IRUSR|S_IWUSR);
           shared_memory= (char *)shmat(segment_id, NULL, 0); 

      //command line option entry
      while ((c=getopt(argc, argv, "F:S:")) != -1) 
           switch(c) 
             {
             case 'F':
               Fflag = 1;
               printf("test\n");
               n= atoi(optarg);
               break;
             case 'S':
               Sflag = 1;
               m= atoi(optarg);
               printf("n=%d\nm=%d\n", n, m);
               break;
             default:
                abort ();
             }

      //begin fibonacci sequence
      for(int i=0; i<=n; i+=1){

           fib_seq(x);
           x+=1;

           if (((x-1)>m)||((x-2)>m)){

           pid=fork();

           fib_seq(x); 
           x+=1; 

           }  
       }
    }

fib_seq.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int extern x;

    int fib_seq(int x) { 

         srand(time(NULL));
         int i, rint = (rand() % 30); 
         double dummy; 

         for (i = 0; i < rint*100; i++) { 
           dummy = 2.3458*i*8.7651/1.234;
         }

         if (i == 0) return(0);
         else if (x == 1) return(1); 
         else return(fib_seq(x-1)+fib_seq(x-2));
    }

Makefile:

    # make all: compiles and links all files
    # make test: runs executable for prob1
    # make clean: cleans up .o and *~ files


    CC = gcc
    CFLAGS = -g -Wall -Werror -c
    OBJ = main.o fib_seq.o

    ################################  Make All  ####################################

    # Compiles all files
    all: main fib_seq
        $(CC) $(OBJ) -o myfib

    # Compiles object files
    main: main.c
        $(CC) $(CFLAGS) $@.c

    fib_seq: fib_seq.c
        $(CC) $(CFLAGS) $@.c

    ################################ Test ##########################################

    test: myfib
        ./myfib -F 6 -S 3

    #############################  Clean  ##########################################

    clean:
        $(RM) *.o *~ prob* $(SO)

gdb run:

    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7a47b0f in __random () at random.c:293
    293 random.c: No such file or directory.

gdb list/where:

    (gdb) list
    288 in random.c
    (gdb) where
    #0  0x00007ffff7a47b0f in __random () at random.c:293
    #1  0x00007ffff7a47f69 in rand () at rand.c:27
    #2  0x00000000004008e5 in fib_seq (x=-135319) at fib_seq.c:8
    #3  0x000000000040098d in fib_seq (x=-135318) at fib_seq.c:17
    #4  0x000000000040098d in fib_seq (x=-135317) at fib_seq.c:17
    #5  0x000000000040098d in fib_seq (x=-135316) at fib_seq.c:17
    #6  0x000000000040098d in fib_seq (x=-135315) at fib_seq.c:17
    #7  0x000000000040098d in fib_seq (x=-135314) at fib_seq.c:17
    #8  0x000000000040098d in fib_seq (x=-135313) at fib_seq.c:17
    #9  0x000000000040098d in fib_seq (x=-135312) at fib_seq.c:17
    #10 0x000000000040098d in fib_seq (x=-135311) at fib_seq.c:17
    #11 0x000000000040099c in fib_seq (x=-135309) at fib_seq.c:17
    #12 0x000000000040098d in fib_seq (x=-135308) at fib_seq.c:17
    #13 0x000000000040098d in fib_seq (x=-135307) at fib_seq.c:17
    #14 0x000000000040098d in fib_seq (x=-135306) at fib_seq.c:17
    #15 0x000000000040098d in fib_seq (x=-135305) at fib_seq.c:17
    #16 0x000000000040098d in fib_seq (x=-135304) at fib_seq.c:17
    #17 0x000000000040098d in fib_seq (x=-135303) at fib_seq.c:17
    #18 0x000000000040098d in fib_seq (x=-135302) at fib_seq.c:17
    #19 0x000000000040098d in fib_seq (x=-135301) at fib_seq.c:17
    #20 0x000000000040098d in fib_seq (x=-135300) at fib_seq.c:17
    #21 0x000000000040098d in fib_seq (x=-135299) at fib_seq.c:17
    #22 0x000000000040098d in fib_seq (x=-135298) at fib_seq.c:17




Aucun commentaire:

Enregistrer un commentaire