#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int _pipe[2];
int ret = pipe(_pipe);
if (ret < 0)
{
perror("pipe");
return 1;
}
pid_t id = fork ();
if (id<0)
{
perror("fork");
return 2;
}
else if (id == 0)
{
// child
int count =5;
close (_pipe[0]);
char* msg = "hello bit";
while (count --)
{
write(_pipe[1],msg,strlen(msg));
sleep(1);
}
close (_pipe[1]);
exit(123);
}
else
{
// Father
close(_pipe[1]);
char buf[128];
while(1)
{
int count =5;
ssize_t s = read ( _pipe[0],buf,sizeof(buf)-1);
if (s<0)
{
perror("read");
}
else if(s==0)
{
printf("write is close\n");
return 2;
}
else
{
buf[s] ='\0';
printf ("child >> father: %s\n",buf);
}
count --;
if (count == 0)
{
close (_pipe[0]);
break;
}
}
int status = 0;
pid_t _wait = waitpid (id, &status,0);
if (_wait > 0)
{
printf("exit code is %d, signal is %d\n",
WIFEXITED(status), status & 0xff);
}
}
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有