为什么在IPC中使用管道使用write()而不使用print()将输出打印两次? [英] Why is the output printed twice with write() and not with print() in IPC using pipe?

查看:67
本文介绍了为什么在IPC中使用管道使用write()而不使用print()将输出打印两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单明了.在这里,我试图在管道的一端发送数据,并试图从另一端读取数据.我正在尝试学习IPC机制,在做这个简单的程序时遇到了麻烦.如果我在父进程中使用print()[1],

o/p is 

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

但是如果我在父进程中使用write()[2在下面的程序中注释]

 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

为什么在父进程及其睡眠中"这一行被打印了两次?

#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;
    }

任何帮助都因为我被困在这里.

解决方案

在孩子中,您

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

从字符串文字开始的地方向管道写入200个字节.

当你

write(1,stream,200);

在父级中的

(已为stream分配了内存之后),您将200字节写入子级写入管道中,而printf在0字节处停止,终止字符串字面量"SUBI IS IN LOVE WITH PUTHALATH".

因此,内存中该字符串文字后面的任何字节也会被打印出来.字符串文字"IN the parent process and its sleeping \n"显然位于该内存部分中.

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.

解决方案

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.

这篇关于为什么在IPC中使用管道使用write()而不使用print()将输出打印两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆