The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with dd command or maybe AFS problem Anta Shell Programming and Scripting 0 08-25-2006 07:10 AM
SSH Problem auth problem budrito UNIX for Advanced & Expert Users 1 03-17-2004 07:12 AM
about msgget troble subrain High Level Programming 9 12-28-2001 06:52 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-13-2006
Registered User
 

Join Date: Sep 2006
Posts: 1
Problem with msgget()

Hi,

I am having problem with msgget() function.

Here is the problem that I am having on Unix :



I have two processes sender and receiver. Sender generates queue (msgget()) with some key e.g. 938, for output.

Receiver reads from the same queue. i.e. receiver also tries to get queue id calling msgget() function and using same key, 938 that sender used.



But the qid that sender is receiving from msgget() function is 16777219 and receiver is getting different qid. Ideally it should get the same qid i.e. 16777219.



I tried to remove this queue using



ipcrm –q <qid>



And I tried to bring up sender with key as 338. But still it is building qid as such huge number and receiver is getting different qid.


Now I am able to read from this queue.

But still not sure whether this qid is correct or not



Please let me know how do I resove this.

Thanks,
Ashwini

Last edited by Ashwini; 09-14-2006 at 06:54 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 09-14-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
While not extremely experienced in this subject, it strikes me that if the id's were guaranteed to be the identical, they wouldn't need keys to find them.
Reply With Quote
  #3  
Old 09-14-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
test1.c:
Code:
#include<stdio.h>
#include<sys/msg.h>

int main() {
        int msqid;

        msqid=msgget(938,666|IPC_CREAT);
        fprintf(stdout,"msqid from program 1: %d\n",msqid);
        fprintf(stdout,"now sleeping for 30 seconds\n");
        sleep(30);
        msgctl(msqid,IPC_RMID,NULL);
}
test2.c:
Code:
#include<stdio.h>
#include<sys/msg.h>

int main() {
        int msqid;

        msqid=msgget(938,666|IPC_CREAT);
        fprintf(stdout,"msqid from program 2: %d\n",msqid);
        msgctl(msqid,IPC_RMID,NULL);
}
I used the above bits of test code to check if at any point the message queue id changed. It didn't
In fact, the man page of msgget makes it quite clear:
Quote:
The msgget() argument returns the message queue identifier
associated with key.

A message queue identifier and associated message queue and
data structure (see intro(3)) are created for key if one of
the following are true:

o key is IPC_PRIVATE.

o key does not already have a message queue identifier
associated with it, and (msgflg&IPC_CREAT) is true.
Remember that msgget will always return a queue id that is associated with the key. This means that if there is already a queueid that is associated with the key, thats what you'll get. A new queue id is only created under the circumstances specified (key is IPC_PRIVATE or queueid does not already exist).

So if you have a program that created a message queue with a certain key, then another program that tries to create a message queue with the same key will *always* get the existing message queue id ,unless one of the programs removes the queue from the system.

In your case, if you have two programs, is one of the programs removing the message queue before the second one can access it? If this happens, then the queue id associated with your key will no longer exist and you will get a new queue id.

And don't worry about the size of the queue id. It makes no difference to your program.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0