LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2007, 12:21 AM   #1
navalbabu
LQ Newbie
 
Registered: Dec 2006
Posts: 1

Rep: Reputation: 0
how to clear a posix message queue


hello friends,

I am working on a project, which is having many modules communicating through posix message queues. Now i am facing a problem. If one of the module goes down and the other modules try to read and write to that dead module's message queue becomes zombie. I want to clear that message queue without closing it. i couldnt find any system calls or commands for that.The project is time critical and I would not able to read the whole message queue for clearing it. please help me.

Last edited by navalbabu; 12-09-2007 at 01:30 PM.
 
Old 12-08-2007, 06:33 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"ipcrm" should work:

http://linux.about.com/library/cmd/blcmdl8_ipcrm.htm
 
Old 12-09-2007, 12:26 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
Quote:
Originally Posted by paulsm4 View Post
"ipcrm" should work
No it shouldn’t. It’s used for the older SysV-style message queues (i.e., msgsnd() and msgrcv()), not the newer POSIX message queues (i.e., mq_send() and mq_receive()).

When you’re dealing with POSIX message queues, they can last longer than processes. For example, this scenario works:
  1. Process A creates and opens a message queue.
  2. Process A exits.
  3. Processes B and C open the same queue.
  4. Processes B and C use the message queue they just opened.
Also, all message queues are “closed” upon the exiting of a process which had opened one. In this sense, they are very similar to named fifos or even plain, ordinary text files (since I can create a named fifo in one process and intend for its use in two unrelated processes).

The problem is, how should the kernel implement this functionality. It seems the easiest way is to keep the creator process open as a zombie process after its death. It will only “pass on” when the message queue descriptor is unlinked from the system and all instances of it are closed.

So there is no way to “clear” a message descriptor (short of receiving all the messages). The other option is to unlink and close the message descriptor and then re-open it. A third option is to use something implementation-specific. For example, in linux, there is a message queue filesystem. I am not quite sure about the specifics of the filesystem, except that you can mount it like this:
Code:
mount -t mqueue none /path/to/mount/point
Of course you can always make a process tell other processes that it’s “going down” by creating a signal handler and/or registering a function with atexit(). The relevant function would send a special message with a high priority to notify others of its death. The other processes might use this message to take subsequent action.
 
Old 12-09-2007, 04:13 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
c'mon, orsor - give me a break!

As you know, SysV message queues (along with its brethren SysV IPCs "shared memory" and "semaphores") have been around for a long, long time; are available and trustworthy on just about every *nix (including, of course, Linux) ... and use "ipcs" and "ipcrm". This thread suggested that the new Posix APIs (only available in Linux 2.6.6 and later, I understand) should also support the same commands:

http://lists.linuxcoding.com/rhl/2005/msg61711.html

navalbabu -
osor is correct about "all message queues are closed upon the exiting of a process which had opened one". Based on the information you provided, I thought you might be looking for a scriptable solution. AFAIK (see the above citation), "ipcrm" *should* work. Please try it.

Otherwise, the default behavior should also work. Much depends on a) which programming language you're using, b) what you mean by "module" (Linux process? Posix thread? Something else entirely?), and c) what precisely you mean by "zombie" (for example, is the initiating process still alive?)

You might even wish to consider a different mechanism than Posix message queues - for example: SysV message queues (which are more mature, and generally available on more platforms), named pipes (which are also more mature, and a lot simpler), or any number of other choices.

IMHO .. PSM
 
Old 12-09-2007, 06:32 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
Quote:
Originally Posted by paulsm4 View Post
c'mon, orsor - give me a break!
Sorry man.
Quote:
Originally Posted by paulsm4 View Post
This thread suggested that the new Posix APIs (only available in Linux 2.6.6 and later, I understand) should also support the same commands:

http://lists.linuxcoding.com/rhl/2005/msg61711.html
That thread (at least that particular message) is incorrect. The most current, stable linux version of ipcrm (available as part of util-linux) has no provisions for POSIX message queues. You can try for yourself, but I doubt it will magically understand that you are talking about POSIX message queues (and not the SysV variety). Even if it were made to do so, that would make the linux version (or one’s use of it) non-portable, since the only type of message queues specified by POSIX for ipcrm is the SysV type. It’s kind of confusing that in addition to “POSIX message queues” being specified in POSIX itself, the SysV message queues are also specified in POSIX (I believe the spec calls them “XSI message queues”).

Along the lines of a non-portable replacement for forcefully unlinking POSIX message queues, there is this message (part of the same thread), in which Ulrich Drepper explains how to handle the situation using linux’s mqueue filesystem.
Quote:
Originally Posted by paulsm4 View Post
osor is correct about "all message queues are closed upon the exiting of a process which had opened one".
Yes, but let me reiterate that the queue is not unlinked upon the exiting of the creating thread. In fact, even when you unlink a POSIX message queue (with mq_unlink()), it won’t actually be removed until all open instances of it are closed. This is much similar to files—I can open a file in process A, unlink the file in process B, and process A will still have access to it.
Quote:
Originally Posted by paulsm4 View Post
You might even wish to consider a different mechanism than Posix message queues - for example: SysV message queues (which are more mature, and generally available on more platforms), named pipes (which are also more mature, and a lot simpler), or any number of other choices.
Yes, I was thinking this as well. At least you might try to redesign the message queuing so that all processes know when the leader dies.
 
Old 01-11-2008, 02:37 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Posix Message Queues culin Programming 3 04-08-2007 08:01 AM
POSIX message queues(Solaris) to SYS V message queues(Linux) devershetty Programming 1 01-22-2007 11:15 AM
Posix Message queues? gdipierro Programming 20 07-26-2005 05:59 PM
does rh9.0 support POSIX message queue? simon_qwl Programming 1 07-22-2005 06:25 PM
POSIX message queues... infamous41md Programming 6 05-25-2004 03:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration