unix system programming....shmget error

Posted on 2007-11-17
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"

Question by:shinymoon
  • 3
  • 2
LVL 12

Accepted Solution

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)

Author Comment

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");

which argument is invalid !?

Author Comment

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

Author Comment

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???
LVL 12

Expert Comment

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.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
monitor and log every file access on redhat/linux 4 143
fizzArray  challenge 1 109
Is there a simple front-end menu system. 9 122
php56-php-mcrypt for rhel7 php56 1 111
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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