Link to home
Start Free TrialLog in
Avatar of shinymoon
shinymoon

asked on

unix system programming....shmget error

Hello experts,

I have a really urgent question. I'm doing a system programming for my os class. When I use shmget function, I get an error saying invalid argument....I don't understand. Can anyone help~~~?

struct mySharedMem{
...
...
}

mySharedMem* shm;                                                            

int main(int argc, char* argv[]){
  mySharedMem* shm;
  int shmId;
  key_t key = KEYVALUE;

  int numProcesses;
  pid_t pid;

  if((shmId = shmget(key, sizeof(mySharedMem), IPC_CREAT|0666)) <0){
    perror("Error in main: shmget");     //I get error message from this line!!!!
    exit(1);                                            // error is "Error in main:shmget: invalid argument"
  }

help~~~
ASKER CERTIFIED SOLUTION
Avatar of PCableGuy
PCableGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shinymoon
shinymoon

ASKER

Thanks for responding.

at the begining of my program,  I defined KEYVALUE like below
/#define KEYVALUE 5678

but, I changed my code as your advice and run it again..I get the exactly same error
my code looks like .....

int main(int argc, char* argv[]){
  mySharedMem* shm;
  int shmId;
  key_t key = 5678;

  if((shmId = shmget(key, sizeof(mySharedMem), IPC_CREAT|0666)) <0){
    perror("Error in main: shmget");
    exit(1);
  }

which argument is invalid !?
and the error is...
Error in main: shmget: Invalid argument
oh, I just tried a different value for key that is 123. It worked. Thanks ^^
Is there any limitation to assign the value for the key???
Hi shinymoon,

Most of the code I've seen usually uses IPC_PRIVATE as the key. Otherwise, I think you might need to use the ftok function to get a key. In other words, I don't think it can be an arbitrary number. Perhaps, that's a good question for your programming class instructor.

http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/shm/key.html