一、fork函数的使用
fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事。
一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同。相当于克隆了一个自己。
#include <stdio.h>
#include <unistd.h>
int main ()
{
int fpid;//the return of fork function
int count=0;
fpid=fork();
if (fpid < 0)
printf("error in fork!");
else if (fpid == 0) {
printf("i am the child process, my process id is %d\n",getpid());
count++;
}
else { //pid bigger than 0;
printf("i am the parent process, my process id is %d\n",getpid());
count++;
}
printf("result count: %d\n",count);
return 0;
}
返回结果为:
i am the child process, my process id is 7067 result count: 1 i am the parent process, my process id is 7066 result count: 1
fork虽然仅被调用了一次,但是却可能有两次返回,它的返回值有三种:
(1)在父进程中,fork返回值大于零的子进程id。
(2)在子进程中,fork返回值为零。
(3)fork进程创建失败时返回负值。
fork出错可能有两种原因:
1)当前的进程数已经达到了系统规定的上限,这时errno的值被设置为EAGAIN。
2)系统内存不足,这时errno的值被设置为ENOMEM。
创建新进程成功后,系统中出现两个基本完全相同的进程,这两个进程执行没有固定的先后顺序,哪个进程先执行要看系统的进程调度策略。
二、循环中的fork函数
#include <stdio.h>
#include <unistd.h>
int main ()
{
int fpid;
int i;
for(i = 0; i < 3; i++)
{
fpid = fork();
if(fpid>0)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
}
else if(!fpid)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
}
}
return 0;
}
运行结果:
loop 0: my process id is 7281, my father process id is 7280, fpid 0 loop 1: my process id is 7282, my father process id is 7281, fpid 0 loop 1: my process id is 7281, my father process id is 7280, fpid 7282 loop 0: my process id is 7280, my father process id is 6968, fpid 7281 loop 1: my process id is 7285, my father process id is 7280, fpid 0 loop 1: my process id is 7280, my father process id is 6968, fpid 7285 loop 2: my process id is 7287, my father process id is 7280, fpid 0 loop 2: my process id is 7280, my father process id is 6968, fpid 7287 [root@localhost threadTest]# loop 2: my process id is 7284, my father process id is 7281, fpid 0 loop 2: my process id is 7281, my father process id is 1, fpid 7284 loop 2: my process id is 7286, my father process id is 7285, fpid 0 loop 2: my process id is 7285, my father process id is 1, fpid 7286 loop 2: my process id is 7283, my father process id is 7282, fpid 0 loop 2: my process id is 7282, my father process id is 1, fpid 7283
从结果可以看到当最上层的父进程死亡之后,未死亡的子进程会继续运行到结束,但是此时他们的父进程id变为1。id为1的进程为init进程,init进程是系统中的一个特殊进程,通常程序文件为/sbin/init,在系统启动时负责启动各种系统服务,之后就负责清理子进程。只要有子进程终止,init就会调用wait函数清理它。
三、fork函数产生的死进程(僵尸进程)
我们所运行的程序,并不是都会结束的,有些程序(如服务器等)需要它一直执行下去。这些程序中如果调用到fork函数就有可能产生“僵尸进程”。
#include <stdio.h>
#include <unistd.h>
int main ()
{
int fpid;
int i;
for(i;; i++)
{
fpid = fork();
if(fpid>0)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
}
else if(!fpid)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
exit(0);
}
sleep(10);
}
return 0;
}
运行10秒后用ps -e命令可以看到结果如下:
[root@localhost root]# ps -e | grep test 22928 pts/5 00:00:00 test 22929 pts/5 00:00:00 test <defunct>
而且每隔10秒钟,再运行一次ps -e命令,都可以看到多增加了一个test <defunct>进程。状态栏为defunct的进程,就是所谓的“僵尸”进程。“僵尸进程”是一个已经死亡的进程,但是其在进程表中仍占了一个位置。因为进程表的容量有限,所以defunct进程不仅占用系统的内存资源,影响系统的性能,而且如果数量太多还会导致系统瘫痪。
当以fork()系统调用建立一个新的进程后,核心进程就会在进程表中给这个新进程分配一个进入点,然后将相关信息存储在该进入点所对应的进程表内。这些信息中有一项是其父进程的识别码。当这个进程走完了自己的生命周期后,它会执行exit()系统调用,此时原来进程表中的数据会被该进程的退出码(exit code)、执行时所用的CPU时间等数据所取代,这些数据会一直保留到系统将它传递给它的父进程为止。由此可见,defunct进程的出现时间是在子进程终止后,但是父进程尚未读取这些数据之前。
当父进程执行终止后,再用ps -e命令观察时,我们会发现defunct进程也随之消失。这是因为父进程终止后,init 进程会接管父进程留下的这些“孤儿进程”(orphan process),而这些“孤儿进程”执行完后,它在进程表中的进入点将被删除。如果一个程序设计上有缺陷,就可能导致某个进程的父进程一直处于睡眠状态或是陷入死循环,那么当该子进程执行结束后就变成了defunct进程,这个defunct 进程可能会一直留在系统中直到系统重新启动。
僵尸进程无法调用kill清除,因为kill命令式用来终止进程的,而僵尸进程已经终止了。可以使用wait或者waitpid函数来清除僵尸进程。
四、用wait或waitpid避免fork函数产生僵尸进程
wait和waitpid的函数原型是:
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
若调用成功则返回清理掉的子进程id,若调用出错则返回-1。父进程调用wait或waitpid时可能会:
- 阻塞(如果它的所有子进程都还在运行)。
- 带子进程的终止信息立即返回(如果一个子进程已终止,正等待父进程读取其终止信息)。
- 出错立即返回(如果它没有任何子进程)。
这两个函数的区别是:
- 如果父进程的所有子进程都还在运行,调用wait将使父进程阻塞,而调用waitpid时如果在options参数中指定WNOHANG可以使父进程不阻塞而立即返回0。
- wait等待第一个终止的子进程,而waitpid可以通过pid参数指定等待哪一个子进程。
可见,调用wait和waitpid不仅可以获得子进程的终止信息,还可以使父进程阻塞等待子进程终止,起到进程间同步的作用。如果参数status不是空指针,则子进程的终止信息通过这个参数传出,如果只是为了同步而不关心子进程的终止信息,可以将status参数指定为NULL。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main ()
{
int fpid;
int stat_val;
int i;
for(i;; i++)
{
fpid = fork();
if(fpid>0)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
}
else if(!fpid)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
exit(0);
}
waitpid(fpid, &stat_val,0);
if(WIFEXITED(stat_val))
printf("child exited with code %d\n", WEXITSTATUS(stat_val));
else if (WIFSIGNALED(stat_val))
printf("child terminated abnormally, signal %d\n", WTERMSIG(stat_val));
sleep(10);
}
return 0;
}
运行结果:
loop 134514078: my process id is 31080, my father process id is 31079, fpid 0 loop 134514078: my process id is 31079, my father process id is 6968, fpid 31080 child exited with code 0 loop 134514079: my process id is 31089, my father process id is 31079, fpid 0 loop 134514079: my process id is 31079, my father process id is 6968, fpid 31089 child exited with code 0
这时在使用ps -e查看就不会产生僵尸进程了。但是会引起阻塞。以下是不会引起阻塞的代码
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void sig_chld_handler(void) {
while (waitpid(-1, NULL, WNOHANG) > 0);
}
int main ()
{
int fpid;
int stat_val;
int i;
#ifdef __USE_GNU
signal(SIGCHLD, (sighandler_t )sig_chld_handler);
#endif
#ifdef __USE_BSD
signal(SIGCHLD, (sig_t )sig_chld_handler);
#endif
for(i;; i++)
{
fpid = fork();
if(fpid>0)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
}
else if(!fpid)
{
printf("loop %d: my process id is %d, my father process id is %d, fpid %d\n",
i, getpid(), getppid(), fpid);
exit(0);
}
}
return 0;
}
子进程的终止信息在一个int中包含了多个字段,用宏定义可以取出其中的每个字段:如果子进程是正常终止的,WIFEXITED取出的字段值非零,WEXITSTATUS取出的字段值就是子进程的退出状态;如果子进程是收到信号而异常终止的,WIFSIGNALED取出的字段值非零,WTERMSIG取出的字段值就是信号的编号。