Linux应用进程控制三(进程调用外部脚本exec函数簇、system、popen)
一、exec函数簇
exec函数族的作用是根据指定的文件名找到可执行文件,并用它来取代调用进程的内容,换句话说,就是在调用进程内部执行一个可执行文件。这里的可执行文件既可以是二进制文件,也可以是任何Linux下可执行的脚本文件。
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
execl与execv区别在于参数传递方式不同,execl将参数存放在一个列表中,execv将参数存放在一个字符串数组中。
execlp和execvp增加了文件查找的路径,优先查找path参数的文件,找不到则到环境变量PATH中去查找。
execle增加了给可执行程序传递环境变量的字符串数组。
exec函数簇应用实例:
execl("/bin/ls", "ls", "-a", "-l", NULL);
char *const arg[] = {"ls", "-l", "-a", NULL};
execv("/bin/ls", arg);
execvp("ls",arg);<br><br>
execle使用实例:
exec.c编译生成exec
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char *const envp[] = {"name=scorpio", "host=192.168.6.200", NULL};
execle("./hello", "hello", NULL, envp);
return 0;
}
hello.c:编译生成hello
#include <stdio.h>
#include <unistd.h>
extern char **environ;
int main(int argc, char **argv)
{
printf("hello\n");
int i = 0;
for(i = 0; environ[i] != NULL; i++)
{
printf("%s\n", environ[i]);
}
return 0;
}
运行./exec:
hello
name=scorpio
host=192.168.6.200
exec程序中定义的环境变量传递给了调用的hello程序。
常见错误:
A、找不到文件或路径,此时errno 被设置为ENOENT
B、数组argv和envp忘记用NULL结束,此时errno被设置为EFAULT
C、没有对要执行文件的运行权限,此时errno被设置为EACCES
二、system函数
#include <stdlib.h>
int system(const char *__command);
system()函数调用/bin/sh来执行参数指定的命令,/bin/sh一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令。
system函数的源码:
int system(const char * cmdstring)
{
pid_t pid;
int status;
if(cmdstring == NULL)
{
return (1); //如果cmdstring为空,返回非零值,一般为1
}
if((pid = fork())<0)
{
status = -1; //fork失败,返回-1
}
else if(pid == 0)
{
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
_exit(127); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的进程就不存在啦~~
}
else //父进程
{
while(waitpid(pid, &status, 0) < 0)
{
if(errno != EINTR)
{
status = -1; //如果waitpid被信号中断,则返回-1
break;
}
}
}
return status; //如果waitpid成功,则返回子进程的返回状态
}
因此,想用system()函数调用外部程序便十分简单,例如调用/home/usrname/hello/目录下的helloworld可执行文件:
system("/home/usrname/hello/helloworld");
在该command执行期间,SIGCHLD是被阻塞的,好比在说:hi,内核,这会不要给我送SIGCHLD信号,等我忙完再说;
在该command执行期间,SIGINT和SIGQUIT是被忽略的,意思是进程收到这两个信号后没有任何动作。
实际上system()函数执行了三步操作:
1.fork一个子进程;
2.在子进程中调用exec函数去执行command;
3.在父进程中调用wait去等待子进程结束。
system()函数执行过程为:fork()->exec()->waitpid()
对于fork失败,system()函数返回-1。
如果exec执行成功,也即command顺利执行完毕,则返回command通过exit或return返回的值。
(注意,command顺利执行不代表执行成功,比如command:"rm debuglog.txt",不管文件存不存在该command都顺利执行了)
如果exec执行失败,也即command没有顺利执行,比如被信号中断,或者command命令根本不存在,system()函数返回127。
如果command为NULL,则system()函数返回非0值,一般为1。
使用system()函数的建议:
1、建议system()函数只用来执行shell命令,因为一般来讲,system()返回值不是0就说明出错了;
2、建议监控一下system()函数的执行完毕后的errno值,争取出错时给出更多有用信息;
3、建议考虑一下system()函数的替代函数popen();
三、popen函数
#include <stdio.h>
FILE * popen ( const char * command , const char * type );
int pclose ( FILE * stream );
参数:
command:是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用-c 标志,shell 将执行这个命令。
type :只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 "r" 则文件指针连接到 command 的标准输出;如果 type 是 "w" 则文件指针连接到 command 的标准输入。
popen 的返回值是个标准 I/O 流,必须由 pclose 来终止。
实例:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char buffer[80];
fp = popen("cat /etc/passwd", "r");
fgets(buffer, sizeof(buffer), fp);
printf("%s", buffer);
pclose(fp);
}
四、popen和system的区别
4.1、popen
与 system
的区别与联系
特性 | system | popen |
---|---|---|
执行方式 | 执行命令后阻塞等待命令执行完成 | 执行命令并返回一个文件指针,允许读取或写入命令的标准输入/输出 |
输出捕获 | 无法捕获输出,只返回命令的退出状态 | 可以读取命令的标准输出或向命令的标准输入写入数据 |
与 shell 的关系 | 会调用 shell 来执行命令,支持 shell 特性(如管道、重定向等) | 也可以执行命令,但不能像 system 一样直接使用 shell 的复杂特性 |
用途 | 用于执行简单的命令,获取命令的退出状态 | 用于获取命令输出,或者向命令的输入传递数据 |
返回值 | 返回命令的退出状态码 | 返回文件指针(FILE * ),用于读写标准输入输出 |
效率 | 启动一个 shell 来执行命令,相对较慢 | 直接创建管道和子进程,相对较高效 |
阻塞性 | 阻塞,直到命令执行完毕 | 阻塞,直到管道关闭 |
4.2、联系
- 执行命令:二者都能用于执行外部命令。
- 阻塞执行:二者都在执行命令时阻塞当前进程,直到子进程执行完毕。
- 与子进程的交互:二者都创建子进程执行命令,
popen
提供了与子进程之间更强的交互能力,可以捕获标准输出或向标准输入写入数据,而system
只是简单地执行命令并返回退出状态。