Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site drivax.UUCP Path: utzoo!linus!decvax!decwrl!sun!amdahl!drivax!holloway From: holloway@drivax.UUCP (Bruce Holloway) Newsgroups: net.unix-wizards Subject: Shared Memory Problem Message-ID: <367@drivax.UUCP> Date: Mon, 31-Mar-86 19:15:19 EST Article-I.D.: drivax.367 Posted: Mon Mar 31 19:15:19 1986 Date-Received: Wed, 2-Apr-86 07:39:12 EST Distribution: net Organization: Digital Research, Monterey, CA Lines: 141 I'm having a problem with the shared memory routines under System V, and I wonder if any gurus out there could help me.... I'm trying to create a shared memory segment, and put its base address into a structure pointer. Everything seems fine until I actually try to use the memory, when I get a core dump. What am I doing wrong? (My code included). #include #include #include #include #include #include #define FALSE 0 #define TRUE 1 #define WAITING 1 #define GOING 2 #define MES struct _mes MES{ int mes_flags; char mes_mes[80]; } *mes; extern int errno; extern char *shmat(); main(acnt,avar) int acnt; char *avar[]; { extern key_t ftok(); key_t key; int shmid, fatal; char *mm; printf("Calling path is: %s\n",avar[0]); key = ftok(avar[0],'B'); if(key == (key_t)-1){ printf("Couldn't make key\n"); return; } else printf("Key is %ld\n",(long)key); shmid = shmget(key, sizeof(MES), IPC_CREAT | IPC_EXCL); if(shmid<0){ fatal = FALSE; switch(errno){ case EINVAL: mm = "Size is out of legal bounds"; fatal = TRUE; break; case EACCES: mm = "Access not granted"; fatal = TRUE; break; case ENOENT: mm = "Not creating a shmid, and one does not exist"; fatal = TRUE; break; case ENOSPC: mm = "Too many shmids already."; fatal = TRUE; break; case ENOMEM: mm = "Not enough memory to fill request."; fatal = TRUE; break; case EEXIST: mm = "A shmid already exists. We are not first."; break; default: mm = "Undefined error code."; fatal = TRUE; break; } printf("ERROR(%d): %s\n",errno,mm); if(fatal) return; shmid = shmget(key, sizeof(MES), IPC_CREAT); receive(shmid); return; } send(shmid); } send(shmid) int shmid; { int mesnum = 1; printf("We are first. Sending first message.\n\n"); mes = (MES *)shmat(shmid, (char *)0, 0); if(!mes){ printf("SHMAT failed!\n"); return; } /* Core is dumped on next statement */ mes->mes_flags = 0; putchar('!'); while(1){ sprintf(mes->mes_mes, "Message #%d!\n", mesnum++); mes->mes_flags |= WAITING; while(mes->mes_flags & WAITING); } } receive(shmid) int shmid; { printf("We are second. Waiting for messages.\n\n"); mes = (MES *)shmat(shmid, (char *)0, 0); if(!mes){ printf("SHMAT failed!\n"); return; } while(1){ /* Core is dumped on this statement. */ while(!(mes->mes_flags & WAITING)); printf(mes->mes_mes); mes->mes_flags &= ~WAITING; } } -- +----------------------------------------------------------------------------+ |Whatever I write are not the opinions or policies of Digital Research, Inc.,| |and probably won't be in the foreseeable future. | +----------------------------------------------------------------------------+ Bruce Holloway ....!ucbvax!hplabs!amdahl!drivax!holloway (I'm not THAT Bruce Holloway, I'm the other one.)