#include "include/apue.h"
static int pfd1[2],pfd2[2];
void TELL_WAIT(void)
{
//创建管道
if (pipe(pfd1) < 0 || pipe(pfd2) < 0)
err_sys("pipe error");
}void TELL_PARENT(pid_t pid)
{
//把数据写进pfd2管道(写端-标准输出)
if (write(pfd2[1],"c",1) != 1)
err_sys("write error");
}void WAIT_PARENT(void)
{
char c;
//从pfd1管道(读端-标准输入)中读数据
if (read(pfd1[0],&c,1) != 1)
err_sys("read error");
if (c != 'p')
err_quit("WAIT_PARENT:incorrect data");
}void TELL_CHILD(pid_t pid)
{
if (write(pfd1[1],"p",1) != 1)
err_sys("write error");
}void WAIT_CHILD(void)
{
char c;if (read(pfd2[0],1) != 1)
err_sys("read error");
if ( c != 'c' )
err_quit("WAIT_CHILD: incorrent data");
}
int main()
{
pid_t pid;TELL_WAIT();
if ((pid = fork()) < 0)
{
err_sys("fork error!");
}
else if (pid == 0)
{
TELL_PARENT(getpid());
WAIT_PARENT();
printf ("2222222222\n");
}
else
{
TELL_CHILD(pid);
WAIT_CHILD();
printf ("1111111111\n");
}
return 0;
}
执行结果:
2222222222
11111111111
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。