Message queue items not being removed
I've tried adding items to a message queue, and it works.
When I tried to remove it, it does not remove it. The code I've used is given below. Isn't msgrcv() supposed to remove the items from the queue? Code:
#include <stdio.h> |
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
When you write a message, part of that message is the message type. Ordinarily, one uses as the message, a struct, with the first element being the long int mtype field, followed by the message payload. This field must agree with the mtype parameter (4th param) in the call to msgrcv().
You seem to be using Beej's Guide (good), so go back and read more carefully. --- rod. |
I agree. Another good source of information is the man-pages.
I threw this little program together using the information I gathered (from the man-page): Code:
#include <sys/msg.h> |
Quote:
I referred your code and was able to make corrections in mine. Took a long time though... Corrected code: Code:
#include <stdio.h> |
Just a couple of comments...
In many/most cases, the queue will be used by multiple processes, as distinct from your example code, where the producer and consumer are in a single process, and share the same queue ID variable. So, all processes will have to open a message queue, and acquire their own message queue IDs. Each process can open the queue with different permissions, as appropriate. Along the same lines, again in most cases, the consumer will expect that the queue already exists, and not use the O_CREAT flag to open the queue. If this flag is not used, the msgget() call will fail, and return -1, which the caller can use to detect that there is probably no producer process, and act accordingly. The key returned by ftok() will probably be -1, if the path argument is a pathname of a non-existant file (as I suspect is the case in your example code). You should check the return values from these two functions, and take the appropriate branch as necessary. There exists the handy tool ipcs, for examining and managing the state of queues and other IPC methods on a host. --- rod. |
Thanks for the info theNbomr. I just remembered that one thing I forgot to do in my sample program was to remove the message queue once I was done with it. This was quite apparent when I ran "ipcs".
@ the OP: Make sure you insert this call when you are done using your message queue: Code:
... |
Quote:
@dwhitney67: Thanks for the msgctl(). Needed that one too. Works like a charm! :) Code:
#include <stdio.h> |
All times are GMT -5. The time now is 11:27 PM. |