LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

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

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


Hi,
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?

Thanks
--Jasdeep
 
Old 01-02-2007, 07:59 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
  


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


All times are GMT -5. The time now is 05:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration