代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int pd[2], n, i;
char line[256];
int main()
{
pipe(pd); // create a pipe
printf("pd=[%d, %d]\n", pd[0], pd[1]);
if(fork()){ // fork a child to share the pipe
printf("parent %d close pd[0]\n", getpid());
close(pd[0]); // parent as pipe WRITER
while(i++ < 10){ // parent writes to pipe 10 times
printf("parent %d writing to pipe\n", getpid());
n = write(pd[1], "I AM YOUR PAPA", 16);
printf("parent %d wrote %d bytes to pipe\n", getpid(), n);
}
printf("parent %d exit\n", getpid());
}
else{
printf("child %d close pd[1]\n", getpid());
close(pd[1]); // child as pipe READER
while(1){ // child read from pipe
printf("child %d reading from pipe\n", getpid());
if((n = read(pd[0], line, 128))){ //try to read 128 bytes
line[n]=0;
printf("child read %d bytes from pipe: %s\n", n, line);
}
else // pipe has no data and no writer
exit(0);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。