嵌入式第二十九课!!!回收子进程资源空间函数与exec函数
wait函数
这个函数是这么用的:
#include <sys/types.h>
#include <sys/wait.h>pid_t wait(int *wstatus);
功能:阻塞等待回收子进程的资源空间
参数:
wstatus :保存子进程消亡状态的变量地址
NULL:不保存子进程退出的状态
返回值:
成功:返回回收到的子进程的PID号
失败:-1
*wstatus指的就是在子进程消亡后的返回值;
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>int main(int argc, const char *argv[])
{pid_t pid = fork();if (pid > 0){int wstatus;pid_t pidtmp = wait(&wstatus);printf("pidtmp = %d\n", pidtmp);if (WIFEXITED(wstatus)){printf("%d over normall, state : %d\n", pidtmp, WEXITSTATUS(wstatus));}else if (WIFSIGNALED(wstatus)){printf("%d ober by signal,signal num = %d\n", pidtmp, WTERMSIG(wstatus));}while (1){printf("I am father : pid= %d\n", getpid());sleep(1);}}else if (0 == pid){int i = 20;while (i--){printf("I am son : pid = %d\n", getpid());sleep(1);}exit(9);//return 9;}else{perror("fork error");}return 0;
waitpid函数
这个函数是这么用的:
pid_t waitpid(pid_t pid, int *status, int options);
功能:回收指定进程的资源
和wait功能相似,比wait更灵活
参数:
pid:
<-1 回收指定进程组内的任意子进程 (-100.等待GID=100的进程组中的任意子进程)
-1 回收任意子进程,组内外
0 回收和当前调用waitpid一个组的所有子进程,组内
> 0 回收指定ID的子进程
status 子进程退出时候的状态,
如果不关注退出状态用NULL;
options 选项:
0 表示回收过程会阻塞等待
WNOHANG 表示非阻塞模式回收资源。
返回值: 成功 返回接收资源的子进程pid
失败 -1
设定为非阻塞且没有回收到子进程返回0
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>int main(int argc, const char *argv[])
{pid_t pid = fork();if (pid > 0){while (1){int wstatus;//pid_t pidtmp = waitpid(-1, &wstatus, 0); ==>wait(&wstatus)pid_t pidtmp = waitpid(-1, &wstatus, WNOHANG);printf("pidtmp = %d\n", pidtmp);if (0 != pidtmp){if (WIFEXITED(wstatus)){printf("%d over normall, state : %d\n", pidtmp, WEXITSTATUS(wstatus));}else if (WIFSIGNALED(wstatus)){printf("%d ober by signal,signal num = %d\n", pidtmp, WTERMSIG(wstatus));}}printf("I am father : pid= %d\n", getpid());sleep(1);}}else if (0 == pid){int i = 20;while (i--){printf("I am son : pid = %d\n", getpid());sleep(1);}exit(9);//return 9;}else{perror("fork error");}return 0;
}
总结:
子进程资源空间回收策略:
1. wait 阻塞回收:一般情况下,父进程专门负责资源回收;
2. waitpid 非阻塞方式回收:搭配轮询方式回收。
3. 不回收:子进程的任务需要一直执行
4. 异步回收:当子进程结束时通知父进程进行回收
exec函数族
这个函数是在一个进程里面执行另外一个文件(可执行文件),其本质是将文本去的指令代码替换成exec要执行的文件的指令。
这里重点介绍两个函数:
int execl(const char *path, const char *arg, ...
/* (char *) NULL */);
参数:
path:要执行的可执行文件的路径和名称
arg:执行该可执行文件时需要传递的参数
NULL :参数传递结束标志
返回值:
出错:-1
int execlp(const char *file, const char *arg, ...
/* (char *) NULL */);
功能:从PATH指定的系统路径下寻找该可执行文件
参数:
file:需要执行的可执行文件的名称(系统路径下已有的文件)
arg : 执行该可执行文件时需要传递的参数
int execle(const char *path, const char *arg, ...
/*, (char *) NULL, char * const envp[] */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],
char *const envp[ ]);
l:list 列表
p:path 路径 : 系统路径(通过命令env查看)
v:vector 容器
e:env 环境变量
我们可以通过exec函数调用系统命令(只要是可以用“whereis +命令”查找到的命令都可以调用):
#include <stdio.h>
#include <unistd.h>int main(int argc, const char *argv[])
{/*printf("exec : pid = %d\n", getpid());execl("./hello", "./hello", NULL);printf("after exec\n");*/// execl("/bin/ls", "ls", "-l", NULL);char *arg[] = {"ls", "-l", NULL};
// execv("/bin/ls", arg);// execlp("ls", "ls", "-l", NULL);execvp("ls", arg);return 0;
}
Linux下的命令:
1. 内核内置命令:cd
2. 外部命令:ls、rm、 touch
whereis 命令
3. 别名:ll (ls -l)
内核内置命令都是不可以通过文件调用的