Re: Working with message queues

From: Pascal Bourguignon (spam_at_thalassa.informatimago.com)
Date: 09/29/03

  • Next message: Andreas Kahari: "Re: Help needed about "finger" command"
    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.
    

  • Next message: Andreas Kahari: "Re: Help needed about "finger" command"

    Relevant Pages

    • Re: Working with message queues
      ... >> I am new to unix programming and now I am going through message queues ... it does not take into account the terminating ... >number of bytes needed to store a '\0' terminated string is one more ... I don't see anything that flushes out the queue after it's been read. ...
      (comp.unix.programmer)
    • Re: [Lit.] Buffer overruns
      ... > i've heard various stories about the null terminating by unix (say ... > compared to multics pli and buffer headers with lengths that were ... > one string to another ... ...
      (sci.crypt)
    • Re: Frage zu strchr()
      ... # a char) in the string pointed to by s. ... The terminating null character ... Prev by Date: ... Next by Date: ...
      (de.comp.lang.c)
    • Re: Portable EOL?
      ... That standard, like the C standard for C ... Now, since the string is a C string, I would follow the terminating new ... the same thing as a null character, ...
      (comp.lang.c)
    • Re: Will the real Altair Basic please stand up?
      ... If you look at the code, he's not putting an 80h before the terminating 00h. ... using the high bit of a character to signal the end of the ... string, was common enough, back when, byte counting counted. ... Monitor, and voila! ...
      (comp.os.cpm)