如何解决编写C程序以创建未命名管道
写一个用于创建未命名管道的C程序。父进程创建一个新的子进程,并且两个进程将相互通信。
我尝试过的解决方案。
这是使用未命名管道的正确方法吗?
#include<stdio.h>
#include<unistd.h>
#include<string.h>
int main() {
int fd[2],ret = 0;
char buffer[] = "Marvellous InfoSystems";
char readBuff[BUFSIZ];
pid_t Childpid;
pipe(fd); //create a oneway communication channel i.e pipe i.e unnamed pipe
Childpid = fork();
if(Childpid == 0) //child process
{
close(fd[0]); /*close the ipnut side of the pipe after entering the child process*/
printf("Child intiaites communication by writing the data at output side of the pipe \n");
write(fd[1],buffer,sizeof(buffer)); /*Child process writes at the output side of the pipe*/
}
else //parent process
{
close(fd[1]); /*Close output side of the pipe after the entering the parent proces*/
ret = read(fd[0],readBuff,sizeof(readBuff));/*Parent proces reads the data i.e written by the child at output side of the
pipe*/
printf("The data read by Parent process is : %s \n",readBuff);
}
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。