19重定向管道与popen模型
重定向 dup2
int dup(int fd)
重定向文件描述符 int newFd = dup(STDOUT_FILENO)
newFd 指向 stdout
int dup2(int fd1, int fd2)
重定向文件描述符 dup2(newFd, STDOUT_FILENO)
stdout 指向 newFd
重定向输入输出到管道
例子1: 父进程标准输入后,有子进程进行标准输出
void testDup()
{
int fds[2];
pid_t pid;
char buf[128];
if(pipe(fds))
{
printf("file:%s,line:%d",__FILE__,__LINE__);
perror("fail pipe!");
return ;
}
pid=fork();
if(pid<0)
{
printf("file:%s,line:%d",__FILE__,__LINE__);
perror("fail fork!");
return ;
}
else if(pid==0)
{
close(fds[1]);
//直接输出
dup2(STDOUT_FILENO,fds[0]);
}
else
{
close(fds[0]);
dup2(STDIN_FILENO,fds[1]);
//父进程接受键盘输入
while(1)
{
fprintf(stderr,"parent send:");
memset(buf,0,sizeof(buf));
read(STDIN_FILENO,buf,sizeof(buf));
write(fds[1],buf,strlen(buf));
}
close(fds[1]);
}
return ;
}
例子2: ls | more 类似实现
思路:
1.ls 的输出,是输出到 STDOUT
2.more 的输入,是从 STDIN 接受
3.我们要实现 ls 的输出,作为 more 的输入
4.所以我们要创建管道,然后,把 STDOUT 和 STDIN 重定向到管道
5.这样 我们就实现了: 连接 ls 和 more
void testDup2()
{
int fds[2];
pid_t pid;
if(pipe(fds))
{
printf("file:%s,line:%d",__FILE__,__LINE__);
perror("fail pipe!");
return ;
}
pid=fork();
if(pid<0)
{
printf("file:%s,line:%d",__FILE__,__LINE__);
perror("fail fork!");
return ;
}
else if(pid==0)
{
close(fds[1]);
dup2(fds[0],STDIN_FILENO);
printf("\n");
execlp("more","more","-d",NULL);
close(fds[0]);
}
else
{
close(fds[0]);
dup2(fds[1],STDOUT_FILENO);
execlp("ls","ls","-b",NULL);
close(fds[1]);
}
}
popen 模型
FILE *popen(const char *cmd, char *type)
解析:
创建子进程,并调用exec,指向cmd命令
同时建立管道,用于父子进程标准输入输出
type取值 “ r ” : 数据由子进程流到父进程
type取值 “ w ”: 数据由父进程流到子进程
int pclose(FILE *stream)
popen第一参数与exec的区别:
popen第一参数包括了命令的名称,参数。如:popen(”ps -ef”,”r”)作为第一参数。
exec是路径、名称、参数分开多个部分作为exec的参数传入。如exec(“ps”,”ps”,”-ef”);
例子:排序
void testPopen()
{
int arr[]={2,4,5,77,11,22,633,21,4};
FILE *file=popen("sort -n","w");
if(!file)
{
printf("file:%s,line:%d",__FILE__,__LINE__);
perror("fail open file!");
return ;
}
int i=0;
for(;i<sizeof(arr)/sizeof(arr[0]);++i)
{
fprintf(file,"%d\n",arr[i]);
}
pclose(file);
}
例子2:通过 popen 实现 ps -ef | grep pts
思路:
1.使用popen模型分别调用ps –ef, grep pts:
pRead = popen("ps -ef", "r")
pWrite= popen("grep pts", "w")
2.此时pRead,pWrite就是两个文件指针,进行文件读写操作。
3. 从 pRead中读取数据,写入到 pWrite中。
void testPopen2()
{
FILE *pRead=popen("ps -ef","r");
FILE *pWrite=popen("grep pts","w");
if(!(pRead&&pWrite))
{
printf("file:%s,line:%d",__FILE__,__LINE__);
perror("fail open file!");
return ;
}
char buf[1024];
while(fgets(buf,sizeof(buf),pRead))
{
fputs(buf,pWrite);
}
pclose(pRead);
pclose(pWrite);
}
19重定向管道与popen模型的更多相关文章
- Asp.Net MVC 的19个管道事件
httpApplication调用ProcessRequest方法,内部执行19个管道事件,如下 BeginRequest 开始处理请求 AuthenticateRequest 授权验证请求开始,获 ...
- 管道与popen函数与重定向
转自:http://www.tldp.org/LDP/lpg/node12.html Pipes the Easy Way! LIBRARY FUNCTION: popen(); PROTOTYPE: ...
- 进程同步控制(锁,信号量,事件), 进程通讯(队列和管道,生产者消费者模型) 数据共享(进程池和mutiprocess.Pool模块)
参考博客 https://www.cnblogs.com/xiao987334176/p/9025072.html#autoid-1-1-0 进程同步(multiprocess.Lock.Semaph ...
- 命令行环境下简单实用的工具——重定向&管道
如果你对管道和重定向应用自如了,无需继续往下看.本文虽然以windows上cmd命令行环境演示,但同样适用于Unix/Linux等平台. 引言 关于管道和重定向,最初是在刘汝佳的<算法竞赛入门经 ...
- Shell基础:常用技巧&重定向&管道操作
Shell脚本介绍和常用工具 Shell脚本 Shell脚本:实际就是windows里的批处理脚本,多条可一次执行的Shell命令集合.Linux上的脚本可以用很多种语言实现,bash shell是比 ...
- Python 3 利用 Dlib 19.7 和 sklearn机器学习模型 实现人脸微笑检测
0.引言 利用机器学习的方法训练微笑检测模型,给一张人脸照片,判断是否微笑: 使用的数据集中69张没笑脸,65张有笑脸,训练结果识别精度在95%附近: 效果: 图1 示例效果 工程利用pytho ...
- 进程间通信(队列、管道)、消费者模型和进程池(apply,apply_async,map)
一.队列(先进先出) 进程间通信:IPC(Inter-Process Communication) 队列是使用管道和锁定实现,所以Queue是多进程安全的队列,使用Queue可以实现多进程之间的数据传 ...
- Python和其他进程的管道通信方式--popen和popen2的比较
目前有一个查询程序 get_user_id 是用C写的,python需要调用这个程序:使用 get_user_id "用户名" 可以得到输出: "ID0002451&q ...
- 基于共享内存、信号、命名管道和Select模型实现聊天窗口
问题模型 A.B两个进程通过管道通信,A 进程每次接收到的数据通过共享内存传递给A1进程显示,同理,B进程每次接收到的数据通过共享内存传递给B1进程显示: 对于A.B 进程,采用ctrl+c(实际为S ...
随机推荐
- 【RF库Collections测试】Get From List
Name:Get From ListSource:Collections <test library>Arguments:[ list_ | index ]Returns the valu ...
- MongoDB启动报错
启动mongodb的时候报错: [root@localhost bin]# ./mongod --dbpath /usr/java/mongoNode/data/db --logpath /usr/j ...
- Google Inc.:Google APIs:23' 解决方案
在导入一个项目是,出现 Unable to resolve target 'Google Inc.:Google APIs:6'第一种解决方法: compileSdkVersion 23 改成 com ...
- Swift 高级运算符
本文转载至 http://my.oschina.net/sunqichao/blog?disp=2&catalog=0&sort=time&p=2 除了基本操作符中所讲的运算符 ...
- 微信accesstoken回调
errcode=-1的时候,开发文档中说明是系统异常,至于具体原因不明 不过有一种原因是AppID以及AppSecret错误 其它可能原因还待发现
- LeetCode——Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- 关于PreparedStatement.addBatch()方法 (转)
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...
- 【APIO2016】Fireworks[DP 可并堆维护凸包优化]
4585: [Apio2016]烟火表演 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 100 Solved: 66[Submit][Status] ...
- 为listview的item中的元素设置onclick事件
表达能力比较差,所以现在解释一下标题的意思:listview的列表项,点击的时候触发的是itemOnClick事件,点击后转向到A页:那么,假如在子项中有一个连接是想转到B页,我们该怎么办呢.这样能明 ...
- Swift - WebKit示例解读
如果你曾经在你的App中使用UIWebView加载网页内容的话,你应该体会到了它的诸多不尽人意之处.UIWebView是基于移动版的Safari的,所以它的性能表现十分有限.特别是在对几乎每个Web应 ...