popen和system问题

popen和system问题

1. 问题描述

C的代码里面去调用命令启动一个shell脚本,分别使用了下面两个途径。
其中一个是: func1(cmd) { popen(cmd,type); pclose(); }
另一个是: func2() { system(cmd); } 其中cmd类似于“./xxx.sh &”。
问题就是,func1这种调法,会导致xxx.sh运行阻塞,脚本最开始添加打印信息都没有显示,但是top后显示进程存在;而func2调用后脚本运行正常。

2. 问题分析

通过分析popen,pclose,system的源码,两者的流程分别如下:
首先看popen,pclose的流程:
父进程调用popen,
popen(cmd, type)
{ 返回一对fd(分别用来重定向子进程的读和写,但不是同时,根据type父进程和子进程最后分别只能用一个)
fork
1. 子进程重定向fd,将标准输出或者标准输入定向到一个fd,然后执行execl cmd,
返回;
2. 父进程收到子进程返回的fd
}
父进程调用pclose
pclose { 1.等待popen出来的子进程结束; 2.关闭fd }
 
然后是system的流程:
system { fork; 1. 子进程execv cmd; 2. 父进程wait子进程退出; 返回; }
 
 
上面的流程简单点,总结出来就是摘自stackoverflow上的一个解释:
popen gives you a pair of file handles you can use to read and write input and output to and from stdin/stdout of the running process in an interactive manner. The system call is merely executing that and redirecting stdin of the current process to that of the invoked child process, and stdout of that process to stdout of the current (host) process. It depends what you are trying to achieve, in general. If your goal is simply to run a command, system works fine. If you're interested in reading its output in a programmatic manner and processing it (and possibly generating more input), then popen is going to work better.

子进程的标准输入和标准输出,和跟着开启他的父进程的配置来的,当执行一个命令会后台运行的时候,比如./test.sh &, 其实又是fork出来一个子进程,然而内部封装的接口my_system接口中是连续调用的popen和pclose,这就导致了最终后台运行起来的那个test.sh子进程,它里面被popen中重定向的fd已经被关闭了,所以后面所有的echo打印都会报错“broken pipe”。表现出来就是在串口中看不到任何脚本里面的打印信息。

3. 问题总结

封装一个popen和pclose连续调用的接口,对于执行不依赖任何标准输入输出的命令是可以使用的,但是实际上这样做还不如直接调用system; 代码中频繁调用system和popen这种系统调用都会有一个问题,就是进程的切换都会带来性能上的一些开销,这个是c和shell混合编程的一个隐患,所以项目后期还是需要尽量将shell脚本中的业务集成进来,减少这种性能开销,也减少这种类似的调用上的一些异常。

猜你喜欢

转载自www.cnblogs.com/doctors/p/10110967.html
今日推荐