Bus error in C Program on Unix machine -


i'm unexperienced c , running "bus error" cannot understand cause of. had never heard of gdb came across on forum , tried using on problem program , got following output:

% gdb proc1 gnu gdb 5.0

...

this gdb configured "sparc-sun-solaris2.8"...

(no debugging symbols found)...

(gdb) run

starting program: /home/0/vlcek/cse660/lab3/proc1

(no debugging symbols found)...

(no debugging symbols found)...

(no debugging symbols found)...

program received signal sigsegv, segmentation fault. 0x10a64 in main ()

i have no idea means, saying there's error in line 10 in code? if so, line 10 in code merely "int main()" i'm not sure issue there... when try running program says "bus error" i'm not sure go here. tried putting printf right after main , doesn't print string, gives me bus error.

below code:

// compilation command: gcc -o proc1 proc1.c ssem.o sshm.o  #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include "ssem.h" #include "sshm.h"  // code of proc1 int main() {int i, internal_reg;  int key1 = 111111, key2 = 222222, key3 = 333333, key4 = 444444;  /* here create , initialize semaphores */  int sem1 = sem_create(key1, 1);  if (sem1 < 0) {    perror("sem failed");  }  int sem2 = sem_create(key2, 1);  if (sem2 < 0) {    perror("sem failed");  }  int sem3 = sem_create(key3, 1);  if (sem3 < 0) {    perror("sem failed");  }  int sem4 = sem_create(key4, 1);  if (sem4 < 0) {    perror("sem failed");  }  /* here created: shared memory array account of size 3 */  int *account;  int shmid = shm_get(123456, (void**) &account, 3*sizeof(int));  if (shmid < 0) {    perror("shm failed");  }  account[0]=10000;  account[1]=10000;  account[2]=10000;  /* synchronize proc2, proc3 , proc4 (4 process 4 way synchronization)*/   (i = 0; < 1000; i++)    {      sem_signal(sem1);      sem_signal(sem1);      sem_signal(sem1);   internal_reg = account[0];      internal_reg = internal_reg - 200;      account[0] = internal_reg;       /* same thing, except we're adding $100 account1 now... */      internal_reg = account[1];      internal_reg = internal_reg + 200;      account[1] = internal_reg;       if (i % 100 == 0 && != 0) {        printf("account 0: $%i\n", account[0]);        printf("account 1: $%i\n", account[1]);      }       if (i == 300 || == 600) {        sleep(1);      }       sem_wait(sem2);      sem_wait(sem3);      sem_wait(sem4);    }  /*     here add code prints contents of each account  , sum after 100th, 200th, 300th, ...., , 1000th iterations*/   }  /*in code above include wait , signal operations on semaphores. no t over-synchronize. */ 

here documentation ssem , sshm:

/*  * ssem.c  *   * version 1.0.0  * date : 10 jan 2002  *  */   #include <sys/ipc.h> #include <sys/sem.h> #include <sys/types.h>  #include "ssem.h"  #define perms 0600  static struct sembuf op_lock[1] = {         0, -1, 0 };  static struct sembuf op_unlock[1] = {         0, 1, ipc_nowait  };    int sem_create(int key,int initval) {         int semid,i;         semid = semget((key_t)key, 1, ipc_creat | perms);          for(i=0;i<initval;i++)                 semop(semid,&op_unlock[0],1);         return semid;  }  int sem_open(int key) {         int semid;         semid = semget(key,0,0);         return semid; }   int sem_wait(int semid) {         return semop(semid,&op_lock[0],1); }   int sem_signal(int semid) {         return semop(semid,&op_unlock[0],1); }   int sem_rm(int semid) {         return semctl(semid, 0, ipc_rmid, 0); }    /*  * sshm.c  *   * routines simpler shared memory operations  * version : 1.0.0.  * date : 10 jan 2002  *  */  #include <sys/shm.h> #include <sys/ipc.h> #include <sys/types.h>  #include "sshm.h"  #define perms 0600  int shm_get(int key, void **start_ptr, int size) {         int shmid;         shmid = shmget((key_t) key, size, perms | ipc_creat);         (*start_ptr) = (void *) shmat(shmid, (char *) 0, 0);         return shmid;  }   int shm_rm(int shmid) {         return shmctl(shmid, ipc_rmid, (struct shmid_ds *) 0);  } 

after compiling proc1.c -ggdb flag , running gdb got following:

program received signal sigsegv, segmentation fault. 0x10a64 in main () @ proc1.c:36

36 account[0]=10000

why cause segmentation fault?

after changing declaration of account to

int *account = 0; 

and adding

printf("account == %p\n", account); 

before account[0] = 10000;

i following upon running proc1:

account == ffffffff bus error 

in order more sensible results gdb should compile program -ggdb option. include debugging information (like line numbers) program.

what seeing memory address (0x10a64) of program counter. not unless can correlate assembly instructions find there part of c program yourself.

it looks using shm_get properly. think library designer has made terrible mistake in naming function shmget.

it's thought. account pointer ending invalid value (aka 0xffffffff (aka (void *)(-1))) in it. value (void *)(-1) indicates sort of error, , explicitly mentioned in manpage shmat. indicates shmat call inside library failed. here how can tell if failed:

 if (account == (void *)(-1)) {      perror("shmat failed");  }  account[0] = 10000;  // ... 

now, why failed interesting mystery. apparently shmget call succeeded.

personally, think system v ipc deprecated @ point , should avoid using if can.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -