多进程
一、进程状态
二、创建子进程 - fork
1、函数接口
#include <unistd.h>
pid_t fork(void);
2、基本概述
成功后,子进程的 PID 在父进程中返回,在子进程中返回 0。
失败时,在父进程中返回 -1,不创建子进程,并正确设置 errno。
3、错误码
三、监控子进程 - wait/waitpid
1、函数接口
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *wstatus);
pid_t waitpid(pid_t pid, int *wstatus, int options);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
2、wait 执行动作
如果调用进程并无被等待的子进程终止,调用进程一直阻塞等待,直到某个子进程终止。
如果调用时已有子进程终止,wait() 则立即返回。
如果 wstatus 非空,wstatus 表示子进程如何终止的信息。
将子进程的 id 作为 wait() 返回结果。出错时,wait() 返回 -1。
如果没有子进程了,函数立刻返回,返回-1;如果子进程都已经结束了,也会立即返回,返回-1,并且会回收子进程的资源。
3、错误码
ECHILD:调用进程没有子进程,wait() 返回 -1,并将 errno 置为 ECHILD。
4、缺陷
如果使用 wait 阻塞回收子进程资源,可能影响其他业务处理。
如果使用 wait 非阻塞轮训调用回收子进程资源,会增加程序设计的复杂性。
四、孤儿进程
父进程已经结束,子进程仍在运行。运行的子进程称为孤儿进程。
孤儿进程由众进程之祖(init进程)接管。
五、僵尸进程
子进程已经结束,父进程未使用 wait/waitpid 对子进程资源进行回收,未回收资源的子进程称为僵尸进程。
僵尸进程无法通过信号杀死。
六、父进程使用 SIGCHLD 信号建立子进程资源回收机制
子进程终止,系统都会向其父进程发送 SIGCHLD 信号。系统对 SIGCHLD 默认处理是忽略。
当调用信号处理程序时,会暂时将引发调用的信号阻塞起来(除非为sigaction()指定SA_NODEFER标志),且不会对 SIGCHLD 标准信号进行排队。如果 2 个子进程相继终止,即产生2次 SIGCHLD 信号,父进程只能捕捉到1个 SIGCHLD,SIGCHLD 信号造成丢失情况。
解决 SIGCHLD 信号丢失问题方案一:使用循环以 WNOHANG 标志来调用 waitpid(),直至再无其他终止的子进程需要处理为止。
while(waitpid(-1,NULL,WNOHANG) > 0){
continue;
}
注意:如果信号处理函数耗时较长,仍会出现信号丢失问题。
七、忽略终止的子进程
将 SIGCHLD 处置显示设置为 SIG_IGN,系统将终止的子进程立即删除,避免子进程的产生。子进程状态也将被删除,故而 wait() 调用不会返回子进程的任何信息。
八、僵尸进程处理方法
1、通过信号机制:通过重新实现 SIGCHILD 信号处理函数。这种方法有信号丢失情况。
2、2次fork:让init进程负责子进程资源回收工作。
3、父进程调用wait进行子进程资源回收。
4、通过 signal(SIGCHLD, SIG_IGN) 通知内核不关心子进程结束,让内核回收。
附录一:多进程
#include "stdio.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
pid_t childPid = 0;
pid_t waitPid = 0;
int i = 0;
int processNum = 5;
/* 1、创建子进程 */
for(i=1;i<processNum;i++){
switch(childPid = fork()){
case -1:
exit(-1);
break;
case 0:
break;
default:
break;
}
}
printf("ppid = %ld pid = %ld %s\r\n",(long)getppid(),(long)getpid(),(childPid == 0) ? "(child)" : "(parent)");
return 0;
}
附录二:监控子进程 - 使用 wait/waitpid 回收子进程资源
#include "stdio.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
pid_t childPid = 0;
pid_t waitPid = 0;
int i = 0;
int processNum = 5;
/* 1、创建子进程 */
for(i=1;i<processNum;i++){
switch(childPid = fork()){
case -1:
exit(-1);
break;
case 0:
break;
default:
break;
}
}
printf("ppid = %ld pid = %ld %s\r\n",(long)getppid(),(long)getpid(),(childPid == 0) ? "(child)" : "(parent)");
sleep(3);
/* 2、回收子进程资源 */
while(1){
waitPid = wait(NULL);
if(waitPid == -1){
if(errno == ECHILD){
// printf("No more children\r\n");
exit(0);
}else{
printf("other errrno\r\n");
exit(-1);
}
}
printf("pid: %ld children return\r\n",(long)waitPid);
}
return 0;
}
测试:
ppid = 3476 pid = 14118 (parent)
ppid = 14118 pid = 14123 (child)
ppid = 14118 pid = 14121 (parent)
ppid = 14118 pid = 14120 (parent)
ppid = 14119 pid = 14129 (child)
ppid = 14118 pid = 14119 (parent)
ppid = 14120 pid = 14128 (child)
ppid = 14119 pid = 14122 (parent)
ppid = 14119 pid = 14124 (parent)
ppid = 14127 pid = 14132 (child)
ppid = 14121 pid = 14126 (child)
ppid = 14122 pid = 14127 (parent)
ppid = 14120 pid = 14125 (parent)
ppid = 14122 pid = 14130 (child)
ppid = 14125 pid = 14133 (child)
ppid = 14124 pid = 14131 (child)
pid: 14123 children return
pid: 14129 children return
pid: 14128 children return
pid: 14126 children return
pid: 14132 children return
pid: 14133 children return
pid: 14121 children return
pid: 14130 children return
pid: 14131 children return
pid: 14125 children return
pid: 14127 children return
pid: 14124 children return
pid: 14122 children return
pid: 14120 children return
pid: 14119 children return
附录三:孤儿进程
#include "stdio.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
pid_t childPid = 0;
pid_t waitPid = 0;
int i = 0;
int processNum = 5;
switch(childPid = fork()){
case -1:
exit(-1);
break;
case 0:
break;
default:
exit(0);
break;
}
printf("ppid = %ld pid = %ld %s\r\n",(long)getppid(),(long)getpid(),(childPid == 0) ? "(child)" : "(parent)");
while(1);
return 0;
}
附录四:僵尸进程
#include "stdio.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
pid_t childPid = 0;
pid_t waitPid = 0;
int i = 0;
int processNum = 5;
switch(childPid = fork()){
case -1:
exit(-1);
break;
case 0:
exit(0);
break;
default:
break;
}
printf("ppid = %ld pid = %ld %s\r\n",(long)getppid(),(long)getpid(),(childPid == 0) ? "(child)" : "(parent)");
while(1);
return 0;
}
附录五:多进程示例
#include "stdio.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
int main(int argc, char* argv[])
{
pid_t childPid = 0;
pid_t waitPid = 0;
int i = 0;
int processNum = 5;
signal(SIGCHLD, SIG_IGN);
switch(childPid = fork()){
case -1:
exit(-1);
break;
case 0:
exit(0);
break;
default:
break;
}
printf("ppid = %ld pid = %ld %s\r\n",(long)getppid(),(long)getpid(),(childPid == 0) ? "(child)" : "(parent)");
while(1);
return 0;
}