Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shmget: Invalid argument. Why i get this error?

I get a "shmget: Invalid argument error" while i try to execute this part of code

  int *nFS, *spb, *cell1, shmid;
  key_t key = 5768; 

  //i need a shared memory segment in which i can put 3 ints
  if ((shmid = shmget(key, (sizeof(int) * 3), IPC_CREAT | 0666)) < 0 ) {
             perror("shmget");
             exit(1);
       } 
 if ((spb = (int)shmat(shmid, NULL, 0))== -1 ){  
            perror("shmat");
            exit(1);
      }
  cell1= spb + 1 ;
  nFS= cell1 + 1;
  //i try to assign here 7 to  nFS
  *nFS=7;

there is something wrong here but i can't figure out what. Can you help me?

Thanks, Alex.

like image 668
Sicioldr Avatar asked Dec 08 '11 10:12

Sicioldr


People also ask

Why do we need key in Shmget?

A key of IPC_PRIVATE (0x00000000) guarantees that a unique shared memory segment is created. A key can also be specified by the caller or generated by the ftok() function. (Input) The size of the shared memory segment being created. If an existing shared memory segment is being accessed, size may be zero.

What is the use of Shmget?

The shmget function is used to create a new shared memory segment or to locate an existing one based on a key. Shared memory segments are memory areas which can be shared by several processes and which, once created, continue to exist until explicitly deleted using the shmctl function.


1 Answers

From the shmget(1) man page:

EINVAL A new segment was to be created and size < SHMMIN or size > SHMMAX, or no new segment was to be created, a segment with given key existed, but size is greater than the size of that segment.

You should check whether you still have an segment for this key using ipcs and remove it with ipcrm.

like image 104
stacker Avatar answered Nov 07 '22 20:11

stacker