当前位置: 首页 > ai >正文

嵌入式第二十九课!!!回收子进程资源空间函数与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)

内核内置命令都是不可以通过文件调用的

http://www.xdnf.cn/news/17878.html

相关文章:

  • Deepoc具身智能模型如何重塑康复辅助设备
  • 如何理解AP中SM中宿主进程?
  • 另类的pdb恢复方式
  • 第十四节:物理引擎集成:Cannon.js入门
  • python自学笔记9 Seaborn可视化
  • 【100页PPT】数字化转型集团信息化总体解决方案(附下载方式)
  • 【机器人-基础知识】ROS1和ROS2对比
  • shell脚本实现sha256sum校验并拷贝校验通过的文件
  • OpenCV Python——图像查找(特征匹配 + 单应性矩阵)
  • Kotlin作用域函数全解:run/with/apply/let/also与this/it的魔法对决
  • fs模块_写入文件
  • Layers(图层)
  • OpenSCA开源社区每日安全漏洞及投毒情报资讯|14th Aug. , 2025
  • 【车联网kafka】常用参数及其命令总结(第八篇)
  • Backblaze 2025 Q2硬盘故障率报告解读
  • Web攻防-大模型应用LLM安全提示词注入不安全输出代码注入直接间接数据投毒
  • CAXA电子图板2026(国产CAD之光)
  • C# 中的 string / StringBuilder / 值类型 / 引用类型 / CLR 总结
  • Design Compiler:使用IC Compiler II Link
  • Tasking编译问题: E207: syntax error - token “<integer>“ delete的解决方案
  • 【机器学习】核心分类及详细介绍
  • 电视同轴电缆全面指南:从基础到应用,批量测量一键计量
  • 【博客系统测试报告】---web界面自动化测试
  • 使用PyTorch实现手写数字识别系统:从理论到实践
  • 附045.Kubernetes_v1.33.2高可用部署架构二
  • 介绍大根堆小根堆
  • C++——分布式
  • 从 0 到 1 玩转Claude code(蓝耘UI界面版本):AI 编程助手的服务器部署与实战指南
  • Unity 绳子插件 ObjRope 使用简记
  • C#文件复制异常深度剖析:解决“未能找到文件“之谜