Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 01-02-2007, 01:15 AM   #1
LQ Newbie
Registered: Jan 2007
Posts: 2

Rep: Reputation: 0
IPC V message queue --msgsnd() call

I am using RH9- 2.4.21.
I am interested in IPC among pthreads using message queue. I was going through the MAN pages of msgsnd(2) command --
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

It says - "The msgsnd() system call appends a copy of the message pointed to by msgp to the message queue whose identifier is specified by msqid."
Now suppose in my case msgp is a pointer to the following structure
struct ptrmsg {
long mtype;
void *ptr;

i am assigning the memory for ptr using malloc
ptr =(char *)malloc(100);

can i free the memory pointed by ptr after calling msgsnd(),in the sending thread itself, because the man page says "it appends a COPY OF THE MESSAGE to the message queue"? OR

should i go for the traditional approch of releasing the memory after reading the message from the queue in receiver thread?

Old 01-02-2007, 06:59 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your message does not include the memory allocated with malloc(), so it is not replicated within the queue. msgsnd() sees your message as simply an array of bytes, and knows nothing about it's makeup. Moreover, if you free the memory, it will invalidate the copy of the pointer (the one now on the queue) to it, since it will then point to memory which may have been re-used. You should free the memory in the consumer thread, once it has been de-referenced, and no longer used.
Somehow this seems like a clumsy way of sharing data, when the memory, and the pointers used to reference it are already visible to both the producer and consumer threads (because it uses pthreads). Any process outside the main thread will be unable to safely de-reference the pointer to the malloc()'d memory.

--- rod.


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
linux ipc programming message queues mariogarcia Programming 2 05-28-2006 03:37 PM
IPC in socket using select system call Gomathy Linux - Networking 0 01-28-2005 06:37 AM
IPC message between process (Sync) davidho3 Linux - Networking 1 07-24-2004 08:20 AM
IPC message between process (Sync) davidho3 Programming 1 07-24-2004 07:37 AM
IPC (Message Queues vs. FIFOs vs. Unix-domain Sockets) TedMaul Programming 0 03-18-2004 04:53 AM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:56 AM.

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