Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unix system programming....shmget error

Posted on 2007-11-17
5
Medium Priority
?
2,830 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
[X]
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
5 Comments
 
LVL 12

Accepted Solution

by:
PCableGuy earned 2000 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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

661 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