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

嵌入式第二十九课!!!回收子进程资源空间函数与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.lryc.cn/news/622011.html

相关文章:

  • 大模型——如何让 AI 绘图的中文呈现更稳定和准确
  • Spring 条件注解与 SPI 机制(深度解析)
  • LeetCode 面试经典 150_数组/字符串_最长公共前缀(20_14_C++_简单)(暴力破解)(求交集)
  • Docker 实战:情感分析系统-容器化部署全流程(sa-logic、sa-webapp、sa-frontend )
  • Highcharts Dashboards | 打造企业级数据仪表板:从图表到数据驾驶舱
  • CUDA 编程笔记:GPU 硬件资源
  • 敏捷数据开发实践:基于 Amazon Q Developer + Remote MCP 构建本地与云端 Amazon Redshift 交互体系
  • mysql-条件查询案例
  • C++从入门到实战(十九)C++ vector容器及其常用接口
  • dockerfile自定义镜像,乌班图版
  • 【开源大模型和闭源大模型分别有哪些?两者的对比?部署私有化模型的必要性有哪些?】
  • 解决zabbix图片中文乱码
  • Spring Boot 拦截器详解
  • HarmonyOS Camera Kit 全解析:从基础拍摄到跨设备协同的实战指南
  • 开源 Arkts 鸿蒙应用 开发(十六)自定义绘图控件--波形图
  • 成品电池综合测试仪:一站式评估性能与安全
  • Flutter 以模块化方案 适配 HarmonyOS 的实现方法
  • 嵌入式学习日记(29)进程、线程
  • 一分钟了解EtherCAT 分支器
  • Web攻防-大模型应用LLM搭建接入第三方内容喂养AI插件安全WiKI库技术赋能
  • Linux操作系统从入门到实战(二十三)详细讲解进程虚拟地址空间
  • 【数据可视化-90】2023 年城镇居民人均收入可视化分析:Python + pyecharts打造炫酷暗黑主题大屏
  • Redis 知识点与应用场景
  • Web 开发 15
  • webrtc编译arm/arm64
  • C# 中的 string / StringBuilder / 值类型 / 引用类型 / CLR 总结
  • KNN算法:从电影分类到鸢尾花识别
  • 标准电子邮件地址格式(RFC 5322 里的 mailbox 语法)
  • 机器学习之PCA降维
  • 大模型系列——从训练到推理:网页数据在大语言模型中的新角色