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

C语言多进程创建和回收

一、多进程创建和回收

  • 孤儿进程:父进程先退出了,子进程没有退出,成为孤儿进程,父进程变成1号进程。
  • 僵尸进程:父进程没有退出,子进程退出了,但是父进程没有回收子进程资源,导致子进程变成僵尸进程。

1. fork()

创建子进程函数,一个进程可以创建多个子进程。

pid_t fork(void)
返回值小于0 创建失败等于0 是子进程大于0 是父进程
 
 

示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>int main()
{pid_t ret = fork();if (ret < 0){perror("fork error");return -1;}else if (ret > 0){printf("here is father,pid is [%d], child pid is [%d]\n", getpid(), ret);sleep(1);}else if (ret == 0){printf("here is child pid is [%d],father is [%d]\n", getpid(), getppid());}return 0;
}
 
 

2. wait()

父进程回收子进程,其中分为wait()和waitpid()函数

pid_t wait(int *status);
返回值大于0 返回值为回收的进程id返回-1 回收失败参数 status 进程回收状态可以使用下面的宏来说明当前的回收状态WIFEXITED(wstatus) 如果正常返回,返回trueWEXITSTATUS(wstatus) 用于输出正常返回的状态 用 %d 格式化输出WIFSIGNALED(wstatus) 如果被信号杀死,返回trueWTERMSIG(wstatus) 用于输出被哪个信号杀死 用 %d 格式化输出WIFSTOPPED(wstatus) 如果子进程停止了,返回trueWSTOPSIG(wstatus) 用于输出进程停止是由于哪个信号WIFCONTINUED(wstatus) 如果进程被信号SIGCONT重启,返回trueWCOREDUMP(wstatus) 如果子进程发生核心转储,返回true
 
 

示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>// pid_t wait(int *status);int main()
{pid_t ret = fork();if (ret < 0){perror("fork error");return -1;}else if (ret > 0){int waitstatus;printf("here is father,pid is [%d], child pid is [%d]\n", getpid(), ret);pid_t waitid = wait(&waitstatus);if (waitid == -1){perror("wait error");return -1;}else if (waitid > 0){printf("child pid [%d] is over\n", waitid);}if (WIFEXITED(waitstatus)){printf("exited, status=%d\n", WEXITSTATUS(waitstatus));}else if (WIFSIGNALED(waitstatus)){printf("killed by signal %d\n", WTERMSIG(waitstatus));}else if (WIFSTOPPED(waitstatus)){printf("stopped by signal %d\n", WSTOPSIG(waitstatus));}else if (WIFCONTINUED(waitstatus)){printf("continued\n");}}else if (ret == 0){printf("here is child pid is [%d],father is [%d]\n", getpid(), getppid());}return 0;
}//输出
here is father,pid is [24290], child pid is [24291]
here is child pid is [24291],father is [24290]
child pid [24291] is over
exited, status=0
 
 

3. waitpid()

waitpid中的第三个参数可以让回收进程变成非阻塞的。

pid_t waitpid(pid_t pid, int *status, int options);
返回值大于0 成功,返回的是回收的进程id等于0 返回0是因为第三个参数设置为了 WNOHANG ,等待是非阻塞的,并且这个时候没有子进程需要被回收小于0 回收失败参数 pid 进程id小于-1 :等待该数字绝对值的所在组所有子进程,例如-21328,则等待21328组内所有子进程-1 :等待所有子进程0 :等待组ID等于调用进程的组ID的子进程大于0 :等待某个子进程
参数 status 进程回收状态同wait函数的
参数 options 回收选项0 :不添加选项WNOHANG :如果没有子进程需要被回收,就立即返回WUNTRACED :如果一个子进程被停止了,就返回WCONTINUED :如果一个子进程被SIGCONT从停止转变为运行,就返回
 
 

示例:

创建多个子进程并回收它们

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>// pid_t waitpid(pid_t pid, int *status, int options);int main()
{int i = 0;for (i = 0; i < 3; i++){pid_t ret = fork();if (ret < 0){perror("fork error");return -1;}else if (ret > 0){printf("here is father,pid is [%d], child pid is [%d]\n", getpid(), ret);// pid_t waitid = wait(&waitstatus);}else if (ret == 0){sleep(3);printf("here is child [%d] pid is [%d],father is [%d]\n", i, getpid(), getppid());break;}}if (i == 3){int waitstatus;int num = 0;do{pid_t waitid = waitpid(-1, &waitstatus, WNOHANG);if (waitid == -1){perror("wait error");return -1;}else if (waitid > 0){printf("child pid [%d] is over\n", waitid);num++;}else if (waitid == 0){// 没有进程被回收,可以做其他事情sleep(1);continue;}if (WIFEXITED(waitstatus)){printf("[%d] exited, status=%d\n", waitid, WEXITSTATUS(waitstatus));}else if (WIFSIGNALED(waitstatus)){printf("[%d] killed by signal %d\n", waitid, WTERMSIG(waitstatus));}else if (WIFSTOPPED(waitstatus)){printf("[%d] stopped by signal %d\n", waitid, WSTOPSIG(waitstatus));}else if (WIFCONTINUED(waitstatus)){printf("[%d] continued\n", waitid);}if (num == i){break;}} while (1);}return 0;
}
//输出
here is father,pid is [24553], child pid is [24554]
here is father,pid is [24553], child pid is [24555]
here is father,pid is [24553], child pid is [24556]
here is child [0] pid is [24554],father is [24553]
here is child [2] pid is [24556],father is [24553]
here is child [1] pid is [24555],father is [24553]
child pid [24554] is over
[24554] exited, status=0
child pid [24555] is over
[24555] exited, status=0
child pid [24556] is over
[24556] exited, status=0
http://www.jsqmd.com/news/25366/

相关文章:

  • 2025年长沙除甲醛公司权威推荐榜单:甲醛检测/办公室除甲醛/新房测甲醛源头服务商精选
  • 2025年口碑好的床垫面料行业内知名厂家排行榜
  • 达梦数据库-人大金仓数据库参考手册
  • 2025年质量好的大型工业压榨机最新TOP品牌厂家排行
  • 【GIS】常见网格集
  • 局域网内连接mysql
  • 2025年评价高的充磁夹具厂家最新权威推荐排行榜
  • 2025年化工造粒机生产厂家权威推荐:永磁材料造粒机/PC改性造粒机/彩色母造粒机源头厂家精选
  • 提升企业效率的优质文件同步软件推荐
  • 2025年质量好的硬齿面减速机实力厂家TOP推荐榜
  • 2025年10月无缝钢管推荐榜:五家主流厂商对比与排名
  • 2025年靠谱的成都中空板厂家最新用户好评榜
  • 2025 年廊坊门窗厂家最新推荐:香河 / 大厂系统窗、断桥铝及封阳台门窗精选榜单
  • 2025年靠谱的净化铝材TOP品牌厂家排行榜
  • 2025年知名的异型管缩管机。厂家最新实力排行
  • 2025 年天津门窗源头厂家最新推荐榜:五大实力品牌测评出炉,含华建铝材合作商及地标项目供应商
  • 2025年10月护眼台灯品牌推荐榜:优视光领衔全维度对比评测
  • 2025年钢结构厂房优质厂家权威推荐:二手钢构库房/钢结构库房/二手车间源头厂家精选
  • 2025年靠谱的齿轮齿条提升机厂家推荐及选购指南
  • 如何提高内外网文件传输效率:企业数字化转型的关键突破点
  • 2025 年点焊机厂家最新推荐榜,技术实力与市场口碑深度解析,助力企业精准选型
  • 2025年比较好的卸油扫仓转子泵厂家推荐及选择参考
  • 2025年口碑好的重型圆盘耙热门厂家推荐榜单
  • 2025年评价高的耐硫酸涂层厂家实力及用户口碑排行榜
  • 2025年成都殡仪一条龙服务权威推荐:丧葬一条龙/公墓/陵园墓地专业机构精选
  • 2025年比较好的RAYCEE氮气过滤器最新TOP品牌厂家排行
  • vmware ubuntu实现双向复制
  • P11956 「ZHQOI R1」树图 分析
  • 习惯用 Markdown 却要交 Word?零成本解决方案:DIY 脚本 + Pandoc 搞定
  • 2025年10月别墅全屋定制公司评价榜:五家深度对比