【原创】《Linux高级程序设计》杨宗德著 - 进程管理与程序开发 - exec和system函数
【原创】《Linux高级程序设计》杨宗德著 - 进程管理与程序开发 - exec和system函数
1. execX系列函数
这些函数的区别是:使用文件名还是文件路径;使用参数列表还是使用argv[]数组的形式。
当进程调用这些函数中任意一个时,该进程用户空间资源(正文、数据、堆栈)完全由新程序代替。由于调用exec并不创建进程,如无特殊只是代码,进程内核信息基本不做修改。
函数用法示例:
execl(“/bin/ls”,”ls”,”-al”,”/etc/passwd”,(char * )0); execle(“/bin/ls”,”ls”,”-al”,”/etc/passwd”,(char * )0, env); execlp(“ls”,”ls”,”-al”,”/etc/passwd”,(char *)0); char * argv[ ]={“ls”,”-al”,”/etc/passwd”,(char*)0}; execv(“/bin/ls”,argv); char * argv[ ]={“ls”,”-al”,”/etc/passwd”,(char *)0}; char * envp[ ]={“PATH=/bin”,0} execve(“/bin/ls”,argv,envp); char * argv[ ] ={ “ls”,”-al”,”/etc/passwd”,0}; execvp(“ls”,argv);
2. system函数
system函数以新进程方式运行一个程序,然后结束,system函数用来创建新进程,并在此进程中运行新进程,知道新进程结束,才继续运行父进程。子进程退出后,会返回退出状态。
用法示例:
system(“ls -al /etc/passwd /etc/shadow”);
3. execX函数和system函数使用示例代码:
主函数如下
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<fcntl.h> #include<string.h> #include<stdlib.h> int main(int argc,char *argv[]) { int fd,status; pid_t pid; fd=open("test.txt",O_RDWR|O_APPEND|O_CREAT,0644); if(fd==-1) { perror("open"); exit(EXIT_FAILURE); } //fcntl(fd,F_SETFD,FD_CLOEXEC); //如果包含此行代码,则会导致错误 printf("befor child process write\n"); system("cat test.txt"); if((pid=fork())==-1) { perror("fork"); exit(EXIT_FAILURE); } if(pid==0) { char buf[128]; sprintf(buf,"%d",fd); execl("./newcode","newcode",buf,(char *)0); } else { wait(&status); printf("after child_process write\n"); system("cat test.txt"); } }
exec调用函数如下
#include<unistd.h> #include<stdio.h> #include<string.h> int main(int argc,char *argv[]) { int i; int fd; char *ptr="helloworld\n"; fd=atoi(argv[1]); i=write(fd,ptr,strlen(ptr)); if(i<=0) perror("write"); close(fd); }
运行结果:
第一次运行 $ ./fcntl_example befor child process write after child_process write helloworld 第二次运行 $ ./fcntl_example befor child process write helloworld after child_process write helloworld helloworld
以上代码运行结果可以看出,在执行exec系列函数时,默认情况下,新代码可以使用在原来代码中打开的文件描述符,即执行exec时,并不关闭原来打开的文件描述符。
但如果包含主函数中注释的代码,//fcntl(fd,F_SETFD,FD_CLOEXEC); 即关闭FD_CLOEXEC选项,则在执行exec后将关闭原来打开的文件描述符。
原文链接:http://blog.csdn.net/geng823/article/details/40682715