#include<stdio.h>
#include<wait.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char ch01[]="hellolinux";
char ch02[100]="\0";
int fd1[2];
int fd2[2];
pipe(fd1);
pipe(fd2);
pid_t *status;
pid_t pid = fork();
if(pid < 0)
{
perror("进程操作错误");
exit(1);
}
else if(0 == pid)
{
printf("子进程创建成功\n");
close(fd1[1]);
read(fd1[0],ch02,sizeof(ch02));
for(int i=0;i<strlen(ch02);i++)
ch02[i] -= 32;
close(fd1[0]);
close(fd2[0]);
write(fd2[1],ch02,strlen(ch02));
close(fd2[1]);
}
else
{
printf("父进程创建成功\n");
close(fd1[0]);
write(fd1[1],ch01,strlen(ch01));
close(fd1[1]);
wait(status);
close(fd2[1]);
int n = read(fd2[0],ch02,sizeof(ch02));
printf("%d\n", n);
write(1,ch02,strlen(ch02));
printf("\n");
close(fd2[0]);
}
return 0;
}
父子进程对两个无名管道的读写操作案例分享
最新推荐文章于 2023-05-22 21:48:01 发布