#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
void server(int readfd,int writefd)
{
char sendbuf[128]="from child process:";
char recvbuf[128];
write(writefd,sendbuf,strlen(sendbuf)+1);
read(readfd,recvbuf,128);
printf("child process read from pipe:>%s\n",recvbuf);
}
void client(int writefd,int readfd)
{
char sendbuf[128]="from parent process:";
char recvbuf[128];
read(readfd,recvbuf,128);
printf("parent process read from pipe:>%s\n",recvbuf);
write(writefd,sendbuf,strlen(sendbuf)+1);
}
int main()
{
char sendbuf[128];
char recvbuf[128];
int fd1[2],fd2[2];
pipe(fd1);
pipe(fd2);
pid_t pid=fork();
if(pid==-1)
{
perror("pipe error!\n");
return -1;
}
else if(pid==0)
{
close(fd1[1]);
close(fd2[0]);
server(fd1[0],fd2[1]);
exit(0);
}
else
{
close(fd1[0]);
close(fd2[1]);
client(fd1[1],fd2[0]);
int status;
wait(&status);
}
return 0;
}
Linux C 关于无名管道(PIPE)的实例
猜你喜欢
转载自blog.csdn.net/Henry313/article/details/88926891
今日推荐
周排行