unix 父子进程间通信-无名管道
发表于:2025-02-11 作者:千家信息网编辑
千家信息网最后更新 2025年02月11日,#include #include #include #include #include #include #include #define MAXLINE 100char buf[MAX
千家信息网最后更新 2025年02月11日unix 父子进程间通信-无名管道
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #define MAXLINE 100
- char buf[MAXLINE];
- int main(int argc, char** agrv)
- {
- int pipefd[2];
- int ret;
- if(0 != pipe(pipefd))//创建管道
- {
- perror("pipe\n");
- exit(1);
- }
- //创建子程序
- if((ret=fork()) < 0)//创建失败
- {
- perror("fork\n");
- exit(1);
- }
- else if(ret == 0)//子程序
- {
- FILE* fp;
- close(pipefd[0]);//关闭管道可读端
- fp = fopen("./main.c","r"); //打开已存在可读文件
- if(NULL == fp)
- {
- perror("fopen\n");
- exit(1);
- }
- while(fgets(buf,MAXLINE,fp) != NULL)//将文件内容全部读入管道
- {
- int n = strlen(buf);
- if(n != write(pipefd[1],buf,n))
- {
- perror("write\n");
- exit(1);
- }
- }//end while
- if(ferror(fp))//在读的过程文件是否有报错
- {
- perror("ferror\n");
- exit(1);
- }
- close(pipefd[1]);//关闭文件可写端
- exit(0);//退出子程序
- }
- else
- {
- close(pipefd[1]);//关闭管道可写端
- if(-1 == wait(NULL))//等待子程序完全将文件内容读入到管道
- {
- perror("wait\n");
- exit(1);
- }
- if(dup2(pipefd[0],STDIN_FILENO) != STDIN_FILENO)//重新设置标准输入为管道可读端
- {
- perror("dup2\n");
- exit(1);
- }
- if(execl("/bin/more","more",(char*)0) < 0 )//分页程序 自动调用标准输入文件 显示管道内容
- {
- perror("execl\n");
- exit(1);
- }
- }
- return 0;
- }
管道
文件
子程序
内容
标准
输入
程序
过程
在读
父子
进程
通信
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
速来帮互联网科技
四川惠普服务器维修费用
计算机网络技术的高数
通过电脑改服务器ip
应用管理服务器原理
服务器相关书籍
系统数据库类型
中央网络安全和信息化委员会
网络安全的五大特点是什么
国家网络安全亮点在哪里
钢铁企业年鉴数据库
软件开发管理规定
服务器证书安全
数据库原理与应用绘图题
轻量数据库和access
linux下如何连通远程服务器
国家网络安全人民日报
mhp3数据库
常州电信服务器
重庆最大的软件开发公司
lol马来西亚服务器有多少人
计算机网络技术的高数
上海打造智慧校园软件开发
rpc数据库配置
数据库前台
2018陕西网络安全论坛
游戏软件开发区烤肉
数据库删除
什么是dtc数据库
登录框数据库连接失败