码迷,mamicode.com
首页 > 其他好文 > 详细

关于signal和fork的思考

时间:2017-12-25 22:15:21      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:include   pes   created   process   and   sig   没有   二次   i++   

fork可以在linux中创建子进程。先看man手册里面的东西:

SYNOPSIS
       #include <unistd.h>

       pid_t fork(void);

DESCRIPTION
       fork()  creates  a new process by duplicating the calling process.  The
       new process, referred to as the child, is an  exact  duplicate  of  the
       calling  process,  referred  to as the parent    ......
RETURN VALUE
       On success, the PID of the child process is returned in the parent, and
       0  is returned in the child.  On failure, -1 is returned in the parent,
       no child process is created, and errno is set appropriately.

函数的作用的创建一个进程,这个函数会返回两次,可能有三种不同的返回值。 
1. 出错返回-1 
2. 返回0,表示是子进程 
3. 返回大于0,表示是父进程

 

下面的一个简单的实例:

#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>

#define CHILDCOUNT 1

int main()
{ 
    pid_t fpid[CHILDCOUNT ];
    int i = 0;
    for(i = 0; i < CHILDCOUNT; i++)
    {
        fpid[i] = fork();
        if(fpid[i] < 0)
        {   
            perror("fork");
            return -1;
        }
        else if(fpid[i] == 0)
        {
            printf("This is child process, id:%d, my father:%d\n", getpid(), getppid());
            getchar();
            return 0;
        }
        else
        {
            printf("This is parent process, id:%d\n", getpid());
        }
    }

    getchar();
    printf("main() ------\n");
    return 0;
}

补充一点知识

  • fork出来的子进程复制了父进程的内存空间(处理代码区都复制了),和父进程共享代码区
  • fork之后,父子进程谁先执行不确定
  • 子进程中新定义的变量和父进程没有任何关系
  • 子进程也会复制父进程文件描述符,但是不会复制文件表。而是共用一个offset
  • 如果父进程先挂了,子进程就变成孤儿进程了,它爹就会变成1号进程
  • 子进程挂了会给父进程发信号(SIGCHLD),如果父进程收到该信号没有及时处理。子进程就变成僵尸进程了,直到父进程处理了该信号或父进程也退出了。


如何避免僵尸进程的出现?
如之前所说,父进程应该及时处理子进程发出来的信号,并且去获取子进程的退出码。 
在父进程中使用wait或waitpid参数等待子进程退出。 
也可以在收到子进程退出的信号时用wait或waitpid等待。

#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>

void sig_handle(int sig)
{
    int ret = 0;
    printf("%s() +++\n", __func__);
#if 0
    wait(NULL);
#else
    while(ret = waitpid(-1, NULL, WNOHANG))
    {
        if(ret < 0)
        {
            printf("waitpid failed\n");
        }
        else
        {
            printf("waitpid success\n");
            break;
        }
    }
#endif
}

int main()
{
    signal(SIGCHLD, sig_handle); // 标记A
    pid_t fpid[5];
    int i = 0;
    for(i = 0; i < 5; i++)
    {
        fpid[i] = fork();
        if(fpid[i] < 0)
        {   
            perror("fork");
            return -1;
        }
        else if(fpid[i] == 0)
        {
            printf("This is child process, id:%d, my father:%d\n", getpid(), getppid());
            return 0;
        }
        else
        {
            printf("This is parent process, id:%d\n", getpid());
        }
    }

    getchar();
    printf("main() ------\n");
    return 0;
}

假如没有signal(SIGCHLD, sig_handle);这个语句,函数运行时,5个子进程马上就结束了(父进程不能退出了)。这时候去查看可以看到有5个僵尸进程。 
如下:

xcy@xcy-virtual-machine:~/test/sock4$ ps -ef | grep test
xcy        5573   2863  0 16:53 pts/12   00:00:00 ./test
xcy        5574   5573  0 16:53 pts/12   00:00:00 [test] <defunct>
xcy        5575   5573  0 16:53 pts/12   00:00:00 [test] <defunct>
xcy        5576   5573  0 16:53 pts/12   00:00:00 [test] <defunct>
xcy        5577   5573  0 16:53 pts/12   00:00:00 [test] <defunct>
xcy        5578   5573  0 16:53 pts/12   00:00:00 [test] <defunct>
xcy        5580   3126  0 16:53 pts/0    00:00:00 grep --color=auto test
xcy@xcy-virtual-machine:~/test/sock4$

加上那句话就没有了

xcy@xcy-virtual-machine:~/test/sock4$ ps -ef | grep test
xcy        5587   2863  0 16:57 pts/12   00:00:00 ./test
xcy        5595   3126  0 16:57 pts/0    00:00:00 grep --color=auto test
xcy@xcy-virtual-machine:~/test/sock4$

 

再来一些有意思的东西
我们知道fork会出现两个进程,两个进程都会往下执行,看下面的代码:

#include<stdio.h>
#include<unistd.h>
int main()
{
    fork();
    fork();
    fork();
    printf("+\n");
}

这样就相当于最开始主进程创建两个进程A和B。 
A又创建两个进程A1,A2。B又创建两个进程B1,B2。 
最后,A1,A2,B1,B2都创建两个进程,所以最后会有8个进程,+会打印8次。 
这样就想细胞的二次分裂一样,3次分裂周期之后就有了8个细胞。


再来看个更难的:

#include<stdio.h>
#include<unistd.h>

void fun()
{
    fork();
    // fork() || fork() && fork(); // 第一个fork返回大于0,后面两个就不执行了
    fork() && fork() || fork();
    fork();
    printf("+\n");
}

int main()
{
    fun2();
}

这样一共能创建几个进程呢,+会打印几次呢。我们先一步一步来分析,假定所有的fork都会成功: 
第一次分裂成A和B。A进程创建的进程和B创建的进程数目肯定是一样的。

就先像下面这样分析:

fork() && fork() || fork(); // line 1
fork(); // line 2

要注意&&运算符和||运算符: 
对于&&来说:第一个表达式如果为0,后面的表达式就不用算了 
对于||来说:第一个表达式如果为1,后面的表达式也不用算了。 
了更好分析,上面的fork一次标号为fork1,fork2,fork3,fork4。 
先看line1。fork1会返回一个非0,和一个0.

  • 1.返回0,则直接进入line2,fork4创建两个进程
  • 2.返回大于0,执行fork2
  • 2.1 fork2返回0,则需要执行fork3,再接着执行fork4.这里会创建4个进程
  • 2.2 fork2返回大于0,也会执行fork3fork4。相当于也创建4个进程。

综上,第一次的A进程会产生10个进程,同理,B也会有10个进程。所以一共会有20个进程。

 

关于signal和fork的思考

标签:include   pes   created   process   and   sig   没有   二次   i++   

原文地址:https://www.cnblogs.com/xcywt/p/8111314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!