Re: Working with message queues
From: Pascal Bourguignon (spam_at_thalassa.informatimago.com)
Date: 09/29/03
- Previous message: prashna: "Working with message queues"
- In reply to: prashna: "Working with message queues"
- Next in thread: Scott Kelly: "Re: Working with message queues"
- Reply: Loic Domaigne: "Re: Working with message queues"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Sep 2003 08:32:12 +0200
vashwath@rediffmail.com (prashna) writes:
> Hi all,
> I am new to unix programming and now I am going through message queues
> and I have written two programs send.c and receive.c. I am creating
> MessageQueue in send.c and writing 3 messages to it.In receive.c I am
> reading the messges and printing them on stdout.Here are the 2
> programes..
> The output is :
> message is Good morning world
>
> message is Good Afternoon world
>
> message is Good Evening world
> d
> ^^^----------------,
> My question: why this extra d is being printed on screen?
I don't know, you've send twice send.c!
But in anycase, you have a problem because you don't send the length
of your message! Since you're telling it that the message size is
strlen(message.mtext), it does not take into account the terminating
'\0'. So msgrcv will only report and copy strlen(message.mtext)
characters, not copying any terminating '\0'. I assume your receive.c
program won't do anything more, so you're getting a string with
whatever is aready present in the buffer.
Unfortunately, or hopefully, this buffer seems to be full of '\0', so
the first message is '\0' terminated where you expect it. Since the
second is larger, it too is '\0' terminated where you expect it. But
then, the third is smaller, so "garbage" from the second message
appears after the last character, the '\n' of the second message.
So, either you pass the length along with your message (or take into
account the message size given by msgrcv, or be concious that the
number of bytes needed to store a '\0' terminated string is one more
the length of the string!
-- __Pascal_Bourguignon__ http://www.informatimago.com/ Do not adjust your mind, there is a fault in reality.
- Previous message: prashna: "Working with message queues"
- In reply to: prashna: "Working with message queues"
- Next in thread: Scott Kelly: "Re: Working with message queues"
- Reply: Loic Domaigne: "Re: Working with message queues"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|