Shared Memory
Killer B
pthonda at bgsuvax.UUCP
Fri Mar 1 11:36:27 AEST 1991
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 <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#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);
}
}
More information about the Comp.unix.programmer
mailing list