Problem with msgget()


 
Thread Tools Search this Thread
Top Forums Programming Problem with msgget()
# 1  
Old 09-13-2006
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 10:54 AM..
# 2  
Old 09-14-2006
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.
# 3  
Old 09-15-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Router problem or ISP problem ?

Hi everyone, I am experiencing discontinuity of Internet service, this started 1 month ago. Everything worked very well for 1 year of intensive use, but now, I have problems reaching my gateway. The gateway is not my router but a node belonging to my ISP and I share the same public IP with... (3 Replies)
Discussion started by: remic
3 Replies

2. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

3. Programming

Msgget(2) returns 0 - a workaround fix

Greetings: I am posting this because my searches for this problem only came up with two posts and no helpful suggestions. I have a "solution" (read work-around hack) and have not tried yet to find a root cause, and may never because I am busy doing other things (read working to pay the bills). ... (10 Replies)
Discussion started by: mr_bandit
10 Replies

4. Programming

msgget message queue always get permission denied

I want to use msgget() to obtain a message queue between two processes, here is my code: the first one create the mq, the second one open it and add a message to it. But when I execute the second one, I get permission denied. I've already desperately tried everything I can think of to solve this... (2 Replies)
Discussion started by: tefino
2 Replies

5. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

6. UNIX for Dummies Questions & Answers

DHCP problem and eth1 problem

At work I am trying to get this one Linux machine (let's call it ctesgm07) to behave like another Linux machine that we have (let's call it test007). test007 returns the following version info: cat /etc/debian_version: lenny/sid uname -a: Linux test007 2.6.27-7-generic #1 SMP Tue Nov 4... (0 Replies)
Discussion started by: sllinux
0 Replies

7. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

8. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies

9. UNIX for Advanced & Expert Users

SSH Problem auth problem

Hi, Just recently we seem to be getting the following error message relating to SSH when we run the UNIX script in background mode: warning: You have no controlling tty. Cannot read confirmation.^M warning: Authentication failed.^M Disconnected; key exchange or algorithm negotiation... (1 Reply)
Discussion started by: budrito
1 Replies

10. Programming

about msgget troble

hi,all i have in trouble about msgget. i create a queue and the program like blow: openMsg( pid_t key ) { .... int msgid; .... msgid=msgget(key,IPC_CREAT|IPC_EXCL|0666) if( msgid<=0 ){ fprintf( stdout,"%s,%d",strerror(errno),errno ); return -1; ... (9 Replies)
Discussion started by: subrain
9 Replies
Login or Register to Ask a Question