简体   繁体   中英

Why is the output printed twice with write() and not with print() in IPC using pipe?

My question is simple and straight forward.Here i am trying to send a data at the one end of pipe and trying to read from the other end.I am trying to learn IPC mechanism and i got stuck while doing this simple program.if i am using print()[1] in the Parent process then ,

o/p is 

In the child process
IN the parent process and its sleeping
SUBI IS IN LOVE WITH PUTHALATH

But if i am using write()[2 commented in the below program] in the parent process

 o/p is 

 In the child process
 IN the parent process and its sleeping
 SUBI IS IN LOVE WITH PUTHALATHIN the parent process and its sleeping

Why is the line "IN the parent process and its sleeping" got printed twice?

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main(){

   int fd[2];
   pipe(fd);

  if(!fork()){
    printf("In the child process\n");
    close(1);
    dup(fd[1]);
    close(fd[0]);
    write(1,"SUBI IS IN LOVE WITH PUTHALATH", 200);

   } else {
      sleep(1);
      printf("IN the parent process and its sleeping \n");
      char* stream;
      close(fd[1]);
       read(fd[0],stream,200);
       printf("%s",stream);------>(1)
      // write(1,stream,200);---->(2)
    }
     return 0;
    }

Any help pls because i am stuck here.

BUGS

   It is not advisable to mix calls to output functions from the stdio library
   with low-level calls to write(2) for the file descriptor associated with
   the same output stream; the results will be undefined and very probably not
   what you want.

http://man7.org/linux/man-pages/man3/puts.3.html

And as others pointed out, you didn't allocate memory for your stream..

In the child, you

write(1,"SUBI IS IN LOVE WITH PUTHALATH", 200);

write 200 bytes to the pipe, starting from where the string literal begins.

When you

write(1,stream,200);

in the parent (after having allocated memory to stream ), you write the 200 bytes written to the pipe by the child, while the printf stops at the 0 byte terminating the string literal "SUBI IS IN LOVE WITH PUTHALATH" .

So whatever bytes follow that string literal in memory get printed out too. The string literal "IN the parent process and its sleeping \\n" is apparently located within that memory section.

When I tried compiling this, gcc says:

22:12: warning: ‘stream’ may be used uninitialized in this function
[-Wuninitialized]

Which is a major indication!

Change char *stream to char stream[200]; and it works as expected. But if you call that write at the end, you'll write well beyond the string with whatever happens to be in memory after it, and as it's not initialised to 0, it'll probably be random rubbish. You can correct that with this:

write(1, stream, strlen(stream)); //correct length, not all 200 bytes

Really, though, you should not be writing 200 bytes in the parent, because you're writing from memory you haven't allocated. The number should be equal to the length of the string (plus one for \\0 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM