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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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