打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c语言pipe
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
void fun1() {
pid_t fd;
int pipefd[2];
if (0 > pipe(pipefd)) {
fprintf(stderr, "pipe errno is %s\n", strerror(errno));
} else {
}

if (0 > (fd = fork())) {
fprintf(stderr, "fork errno is %s\n", strerror(errno));
} else if (fd == 0) {
//子进程
close(pipefd[0]);
char buf[15] = { '\0' };
while (1) {
fgets(buf, sizeof(buf) - 1, stdin);
if (0 > write(pipefd[1], buf, sizeof(buf)-1)) {
fprintf(stderr, "write errno is %s\n", strerror(errno));
}
memset(buf, 0, sizeof(buf));
printf("chlid\n");
}
close(pipefd[1]);
exit(0);
} else {
//父进程
close(pipefd[1]);
char buf[10] = { '\0' };
memset(buf, 0, sizeof(buf));
while (1) {
int k=0;
while (0!=(k= read(pipefd[0], buf, sizeof(buf) - 1))) {
printf("parent = %s k=%d\n", buf,k);
memset(buf, 0, sizeof(buf));
k=0;
}
// fprintf(stderr, "read errno is %s\n", strerror(errno));
}
close(pipefd[0]);
exit(0);
}
}
int main(void) {
fun1();
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux Socket学习(二)
Ubuntu12.04下pulseaudio的安装以及API使用
IO多路复用之select全面总结(必看篇)
Linux下C语言编程--文件的操作
mmap内存映射操作
C 标准错误处理函数 fprintf 和 printf
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服