一、exec函数簇
1,被加载的文件的参数列表必须以自身名字为开始,以 NULL 为结尾。比如要加载执
行当前目录下的一个叫做 a.out 的文件,需要一个参数”abcd”(可多个参数,往后增加参数即可),那么正确的调用应该是:
execl(“./a.out”, “a.out”, “abcd”, NULL);
或者:
const char *argv[3] = {“a.out”, “abcd”, NULL};
execv(“./a.out”, argv);
2,exec 函数簇成功执行后,原有的程序代码都将被指定的文件或脚本覆盖,因此这些
函数一旦成功后面的代码是无法执行的,他们也是无法返回的。
二、wait/waitpid函数
注意:上面所说的退出状态不是退出值,退出状态包括退出值。如果wait和waitpid成功获得子进程的退出状态,则需要使用宏来进一步解析,解析在下图:
注意:正常退出是指调用exit()和_exit(),或者在主函数中调用return、或者是最后一个线程调用了pthread_exit()。wait()阻塞等待,waitpid()可阻塞可不阻塞
示例代码
父进程代码(test.c):
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "stdlib.h"
#include <wiringPi.h>
void routine1(void) // 退出处理函数
{
printf("parent exit handler.\n");
}
int main(int argc, char *argv[])
{
pid_t pid;
int process = 0;
pid = fork();
if(pid ==0)//child process code second
{
execl("./execl", "./execl", "123", "abc", NULL);
//char *buf[] = {"./execl", "./execl", "123", "abc", NULL};
//execv("./execl", buf); //等效于execl()
//execlp("ls", "ls", "-l", NULL); //执行脚本文件
printf("child execl failure\n"); //如果execl()执行成功,将不会执行execl()后的代码
}
if(pid >0)//parent process code first
{
int status = 0xffff;
int i = 0;
process = 1;
wait(&status); //等待子进程结束
printf("child exit: %d", WEXITSTATUS(status) ); //打印子进程退出值
atexit(routine1); // 注册退出处理函数,执行exit()退出前,执行routine1函数
for(i=0; i<3; i++)
{
printf("parent process=%d\n",process);
delay(1000); //ms
}
exit(0); //0表示正常退出,同时冲洗缓冲区数据,并执行退出处理函数
}
}
子进程文件代码(execl.c):
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "stdlib.h"
#include <wiringPi.h>
void routine1(void) // 退出处理函数
{
printf("child exit handler.\n");
}
int main(int argc, char *argv[])
{
int i = 0;
atexit(routine1);// 注册退出处理函数,执行exit()退出前,执行routine2函数
for(i=0; i<argc; i++)
{
printf("argv[%d]:%s\n", i, argv[i] );
delay(1000); //ms
}
exit(0); //0表示正常退出,同时冲洗缓冲区数据,并执行退出处理函数
}
效果图如下: