linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Detecting whether a Message Queue with a given qid exists?
@ 2005-10-31  7:11 Rajat Jain
  2005-10-31  7:30 ` Steve Graegert
  0 siblings, 1 reply; 6+ messages in thread
From: Rajat Jain @ 2005-10-31  7:11 UTC (permalink / raw)
  To: linux-c-programming

Hi,

This is the reagrding the message queue mecahnism of sys V IPC.

Is there a way to find out that whether a message queue with a
particular qid exists?

Thanks,

Rajat

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Detecting whether a Message Queue with a given qid exists?
@ 2005-10-31  7:15 JOSEPH ALPHONSE
  0 siblings, 0 replies; 6+ messages in thread
From: JOSEPH ALPHONSE @ 2005-10-31  7:15 UTC (permalink / raw)
  To: Rajat Jain, linux-c-programming

Hi all,
	send the command "ipcs -q "
	
	u will get a list of queues	
	grep from the list to get the required id

Thanks 
Joseph

-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of Rajat Jain
Sent: Monday, October 31, 2005 12:41 PM
To: linux-c-programming@vger.kernel.org
Subject: Detecting whether a Message Queue with a given qid exists?


Hi,

This is the reagrding the message queue mecahnism of sys V IPC.

Is there a way to find out that whether a message queue with a
particular qid exists?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


---------------------------------------------------------------------------
       "This e-mail and any files transmitted with it are for the sole use
of the intended recipient(s) and may contain confidential and privileged
information. If you are not the intended recipient, please contact the
sender by reply e-mail and destroy all copies of the original message.

       Any unauthorized review, use, disclosure, dissemination, forwarding,
printing or copying of this email or any action taken upon this e-mail is
strictly prohibited and may be unlawful."
---------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Detecting whether a Message Queue with a given qid exists?
  2005-10-31  7:11 Detecting whether a Message Queue with a given qid exists? Rajat Jain
@ 2005-10-31  7:30 ` Steve Graegert
  2005-11-01  6:22   ` Rajat Jain
  0 siblings, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2005-10-31  7:30 UTC (permalink / raw)
  To: linux-c-programming

On 10/31/05, Rajat Jain <rajat.noida.india@gmail.com> wrote:
> Hi,
>
> This is the reagrding the message queue mecahnism of sys V IPC.
>
> Is there a way to find out that whether a message queue with a
> particular qid exists?

msgget returns a valid queue identifier.  To see if the queue still
exists when you try to place a message into the queue via msgsend,
check for EINVAL, which is returned if the queue identifier is not
valid (any more):

int result, msqid;

struct m {
	long type;
	char text[64];
} msg;

m.type = 1;
strcpy(m.text, "Hi, I am a message");

if ((queue_id = msgget(KEY, 0666)) == -1) {
	/* handle error */
}
... /* do some stuff here */
result = msgsnd(queue_id, (void *)&m, sizeof(m.text), IPC_NOWAIT);
... /* check error here */
	
Hope this helps.



	\Steve

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Detecting whether a Message Queue with a given qid exists?
  2005-10-31  7:30 ` Steve Graegert
@ 2005-11-01  6:22   ` Rajat Jain
  2005-11-01  9:11     ` Steve Graegert
  0 siblings, 1 reply; 6+ messages in thread
From: Rajat Jain @ 2005-11-01  6:22 UTC (permalink / raw)
  To: Steve Graegert; +Cc: linux-c-programming

> 
> msgget returns a valid queue identifier.  To see if the queue still
> exists when you try to place a message into the queue via msgsend,
> check for EINVAL, which is returned if the queue identifier is not
> valid (any more):
> 
> int result, msqid;
> 
> struct m {
>        long type;
>        char text[64];
> } msg;
> 
> m.type = 1;
> strcpy(m.text, "Hi, I am a message");
> 
> if ((queue_id = msgget(KEY, 0666)) == -1) {
>        /* handle error */
> }
> ... /* do some stuff here */
> result = msgsnd(queue_id, (void *)&m, sizeof(m.text), IPC_NOWAIT);
> ... /* check error here */
> 
> Hope this helps.
> 

Hi,

Thanks for the help. I understood this. But the above method uses msgsnd. So if the Queue actually exists, then a message will ACTUALLY be inserted. Given a qid, I want to check if a queue with that ID exists, without actually inserting a message. Is there a way or the only way out is try to insert a message, and if successful, remove that message :-(

The background og this is that I am using message queues to communicate between 2 independent programs (say A & B). Program "A" creates a message queue, writes a message in the queue and tells the qid to program B via a file. Program "B" reads the queue ID, and gets the message from the Message queue. After one message has been successfully sent, "A" wants to send another message, but before that just wants to check if the message queue it had created earlier already exists. If it does, it would insert messages in the old queue, otherwise would create a new queue and tell the new qid to "b" via the same file.

TIA,

Thanks & Best Regards,

Rajat


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Detecting whether a Message Queue with a given qid exists?
  2005-11-01  6:22   ` Rajat Jain
@ 2005-11-01  9:11     ` Steve Graegert
  2005-11-02  3:00       ` Rajat Jain
  0 siblings, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2005-11-01  9:11 UTC (permalink / raw)
  To: Rajat Jain; +Cc: linux-c-programming

On 11/1/05, Rajat Jain <rajat.noida.india@gmail.com> wrote:
> >
> > msgget returns a valid queue identifier.  To see if the queue still
> > exists when you try to place a message into the queue via msgsend,
> > check for EINVAL, which is returned if the queue identifier is not
> > valid (any more):
> >
> > int result, msqid;
> >
> > struct m {
> >        long type;
> >        char text[64];
> > } msg;
> >
> > m.type = 1;
> > strcpy(m.text, "Hi, I am a message");
> >
> > if ((queue_id = msgget(KEY, 0666)) == -1) {
> >        /* handle error */
> > }
> > ... /* do some stuff here */
> > result = msgsnd(queue_id, (void *)&m, sizeof(m.text), IPC_NOWAIT);
> > ... /* check error here */
> >
> > Hope this helps.
> >
>
> Hi,
>
> Thanks for the help. I understood this. But the above method
> uses msgsnd. So if the Queue actually exists, then a message
> will ACTUALLY be inserted. Given a qid, I want to check if a
> queue with that ID exists, without actually inserting a message.
> Is there a way or the only way out is try to insert a message,
> and if successful, remove that message :-(

Try msgctl with the IPC_STAT command:

	#include <sys/msg.h>
	int msgctl(int msqid, int cmd, struct msqid_ds *buf);

If the queue ID is not valid this call fails with EINVAL and buf is not filled.

	\Steve

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Detecting whether a Message Queue with a given qid exists?
  2005-11-01  9:11     ` Steve Graegert
@ 2005-11-02  3:00       ` Rajat Jain
  0 siblings, 0 replies; 6+ messages in thread
From: Rajat Jain @ 2005-11-02  3:00 UTC (permalink / raw)
  To: Steve Graegert; +Cc: linux-c-programming

>
> Try msgctl with the IPC_STAT command:
>
>        #include <sys/msg.h>
>        int msgctl(int msqid, int cmd, struct msqid_ds *buf);
>
> If the queue ID is not valid this call fails with EINVAL and buf is not filled.
>
>        \Steve
>

Thanks a lot. I think it would solve my problem

:-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-11-02  3:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-31  7:11 Detecting whether a Message Queue with a given qid exists? Rajat Jain
2005-10-31  7:30 ` Steve Graegert
2005-11-01  6:22   ` Rajat Jain
2005-11-01  9:11     ` Steve Graegert
2005-11-02  3:00       ` Rajat Jain
  -- strict thread matches above, loose matches on Subject: below --
2005-10-31  7:15 JOSEPH ALPHONSE

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).