Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!rice!uw-beaver!milton!dali.cs.montana.edu!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!magnus.ircc.ohio-state.edu!tut.cis.ohio-state.edu!bgsuvax!pthonda From: pthonda@bgsuvax.UUCP (Killer B) Newsgroups: comp.unix.programmer Subject: Shared Memory Keywords: SHMGET Message-ID: <7070@bgsuvax.UUCP> Date: 1 Mar 91 00:36:27 GMT Organization: Bowling Green State University B.G., Oh. Lines: 105 The program given below is running fine on SPARC station, however when I tried to execute the same program on Vax 785 with Unix 4.3 BSD,Iam encountering the message "Shmget failed :Invalid argument".I tried to typecast the argumnets to call 'shmget' to the right types but the problem is still persistent. However after going through manuals I figured out that 'shmget' gives this whenever the programs specifies a shared memory segment size which is less than or greater than the system imposed limits.Could anyone tell me how to get the system information about default allowable shared memory segment sizes. Thanx in advance. Pratap. And here is the code.. /*=========================================================================*/ #include #include #include #include #include #define SHMKEY (key_t)0x10 /* Establish a key for shared memory */ #define IPCFLAGS IPC_CREAT | IPC_EXCL #define RDWR 0666 #define ERROR -1 struct shmid_ds *buf ; static int shmId ; static int prId; long *shmPtr ; main(argc,argv) int argc ; char *argv[] ; { char *shmat(), *malloc() ; int retValue ; /* Get the shared memory segment */ shmId = shmget(SHMKEY,sizeof(long),RDWR|IPCFLAGS) ; if(shmId < 0) { /* It's failing here. *********/ perror("shmget failed") ; exit(ERROR) ; } /* Set the required permissions */ /* buf = (struct shmid_ds *) malloc(sizeof(struct shmid_ds)) ; buf->shm_perm.cuid = getpid() ; buf->shm_perm.mode = 0600 ; if(shmctl(shmId,IPC_SET,buf) < 0) { perror("Shmctl failed"); exit(ERROR) ; } */ /* Attach the shared memory segment */ shmPtr = (long *) shmat(shmId,0,0) ; if((char *) shmPtr == (char *)-1) { perror("Shmat failed") ; exit(ERROR) ; } /* Initialize to a value for testing ....*/ *shmPtr = 120L ; if((prId = fork()) < 0) { perror("Fork failed") ; exit(ERROR) ; } if(prId == 0) consumer() ; else producer() ; shmdt(shmPtr) ; shmctl(shmId,IPC_RMID,0) ; } int producer() { int i ; for(i = 0 ; i < 100 ; i++) { ++*shmPtr ; printf("Producer incremented. Current value is %ld\n",*shmPtr); } } int consumer() { int k ; for(k = 0 ; k < 100 ; k++) { --*shmPtr ; printf("Consumer decremented. Current value is %ld\n",*shmPtr); } }