您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc,char *argv[])
{
int n, fd[2];
pid_t pid;
char buffer[25]; //缓冲区
if(pipe(fd)<0) //创建一个管道,两个文件描述符在fd数组中
{
printf("创建管道失败!\n ");
exit(0);
}
if((pid=fork())<0) //创建一个子进程
{
printf("创建子进程失败!\n ");
exit(0);
}
else if (pid>0) //父进程
{
close(fd[0]);
write(fd[1],"This is a pipe test!\n",22); //向管道写入数据,注意回车换行符
}
else //子进程
{
close(fd[1]); //关闭
n = read(fd[0],buffer,25); //从通道中读出数据
printf("%s",buffer); //将数据写到标准输出设备
}
exit(0);
}免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。