Solved

shmget returning 0

Posted on 2008-10-26
1
1,025 Views
Last Modified: 2008-10-28
I have written this code

The call to CreateSharedMem creates and returns the id of shared memory segment but when i call GetSharedMem it returns 0?

Any idea what i am doing wrong ?

Thank You
int CreateSharedMem(char* key_file, char id, int seg_size)
{
        int shm_id;
        key_t keyval;
        if ((keyval = ftok(key_file,id))==-1)
        {
        	perror("ftok");
        	return -1;
        } 
        // Can use IPC_EXCL which will fail and returns -1 if a segment with the same key exists
        if((shm_id = shmget( keyval, seg_size, IPC_CREAT | 0666 | IPC_EXCL )) == -1)
        {
        		perror("shmget");
                return(-1);
        }
   
        return(shm_id);
}
 
int GetSharedMem(char* key_file,char id, int seg_size)
{
        int shm_id;
        key_t key_val;
        if ((key_val = ftok(key_file,id))==-1)
        {
        	perror("ftok");
        	return -1;
        } 
        printf("Key is %d",key_val);
        // Can use IPC_EXCL which will fail and returns -1 if a segment with the same key exists
        if((shm_id = shmget( key_val , seg_size, IPC_CREAT | 0666 ) == -1))
        {
        		perror("shmget");
                return(-1);
        }
        return(shm_id);
	
}
 
main()
{
.
.
 
 /* Create a shared memory */
	int my_shm_id = CreateSharedMem("MySharedMem.c",'A',1000);
	printf("%d : Shared mem is created\n",my_shm_id);
	
	
/* Get the same shared memory */
       int my_shm_id = GetSharedMem("MySharedMem.c",'A',1000);	
       printf("%d is Shared mem that is created\n",my_shm_id);
.
.
}

Open in new window

0
Comment
Question by:Novice_
[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
1 Comment
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 22810811
Due to operator precedence, this :

        if ((shm_id = shmget( key_val , seg_size, IPC_CREAT | 0666 ) == -1))

is the same as this :

        if ((   shm_id = (shmget( key_val , seg_size, IPC_CREAT | 0666 ) == -1)   ))

So, you check whether the return value of shmget is == -1, and then you place the result of that comparison in shm_id (0 if false, non-zero if true).

shm_id being 0 means that the shmget call succeeded.


I would suspect that you intended this instead :

        if ((shm_id = shmget( key_val , seg_size, IPC_CREAT | 0666 )) == -1)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 147
Problem to show menu 5 107
IIS Log files on Exchange 2013 server 6 346
.NET Core supports which cell phone platforms? 3 38
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

710 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