Solved

unix system programming....shmget error

Posted on 2007-11-17
5
2,816 Views
Last Modified: 2013-12-26
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~~~
0
Comment
Question by:shinymoon
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
PCableGuy earned 500 total points
ID: 20306356
I think the problem is in this part, what is "key" refer to? Check that first, and correct it if necessary, and report back.

shmget(key, sizeof(mySharedMem), IPC_CREAT|0666)
0
 

Author Comment

by:shinymoon
ID: 20306767
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 !?
0
 

Author Comment

by:shinymoon
ID: 20306771
and the error is...
Error in main: shmget: Invalid argument
0
 

Author Comment

by:shinymoon
ID: 20306782
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???
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 20307721
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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question