关闭

fork创建进程

291人阅读 评论(0) 收藏 举报
分类:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>


int main(int argc,char *argv[])
{
        pid_t child;
        int count  = 100;
        printf("Parrent process [%d] start...\n",getpid());
        printf("Parrent [%d] start create child process:\n",getpid());
        child = fork();
        //fail
        if(child < 0)
        {
                printf("Parrent process [%d] create child fail!\n",getpid());
                printf("%s\n",strerror(errno));
                return -1;
        }
        else if (child == 0)
        {
                printf("count = %d",++count);
                printf("Child process [%d] ,parrent [%d]\n",getpid(),getppid());
                printf("child process [%d] finish,quit!\n",getpid());
                exit(0);
        }
        else
        {
                sleep(1);
                printf("Parrent process [%d] create child [%d] sucess\n",getpid(),child);
        }
        printf("Only parrent do......[%d],count = [%d]\n",getpid(),count);
        return 0;
}
-------------------------------------解决僵尸程----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>


int main(int argc,char *argv[])
{
        pid_t child;
        int count  = 100;
        printf("Parrent process [%d] start...\n",getpid());
        printf("Parrent [%d] start create child process:\n",getpid());
        child = fork();
        //fail
        if(child < 0)
        {
                printf("Parrent process [%d] create child fail!\n",getpid());
                printf("%s\n",strerror(errno));
                return -1;
        }
        else if (child == 0)
        {
                sleep(3);
                printf("count = %d",++count);
                printf("Child process [%d] ,parrent [%d]\n",getpid(),getppid());
                printf("child process [%d] finish,quit!\n",getpid());
                exit(0);
        }
        else
        {
                //sleep(1);
                wait();
                printf("Parrent process [%d] create child [%d] sucess\n",getpid(),child);
        }
        printf("Only parrent do......[%d],count = [%d]\n",getpid(),count);
        return 0;
}
--------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

void signal_handler(int sig_num)
{
        switch(sig_num)
        {
                case SIGCHLD:
                {
                        wait();
                }
                break;
        }
}


int main(int argc,char *argv[])
{
        pid_t child;
        int count  = 100;
        signal(SIGCHLD,signal_handler);
        printf("Parrent process [%d] start...\n",getpid());
        printf("Parrent [%d] start create child process:\n",getpid());
        child = fork();
        //fail
        if(child < 0)
        {
                printf("Parrent process [%d] create child fail!\n",getpid());
                printf("%s\n",strerror(errno));
                return -1;
        }
        else if (child == 0)
        {
                sleep(3);
                printf("count = %d",++count);
                printf("Child process [%d] ,parrent [%d]\n",getpid(),getppid());
                printf("child process [%d] finish,quit!\n",getpid());
                exit(0);
        }
        else
        {
                //sleep(1);
                //wait();
                while(1)
                {
                        printf("Parrent process [%d] create child [%d] sucess\n",getpid(),child);
                        sleep(1);
                }
        }
        printf("Only parrent do......[%d],count = [%d]\n",getpid(),count);
        return 0;
}
---------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

void signal_handler(int sig_num)
{
        switch(sig_num)
        {
                case SIGCHLD:
                {
                        wait();
                }
                break;
        }
}


int main(int argc,char *argv[])
{
        pid_t child;
        int count  = 100;
        //signal(SIGCHLD,signal_handler);
        printf("Parrent process [%d] start...\n",getpid());
        printf("Parrent [%d] start create child process:\n",getpid());
        child = fork();
        //fail
        if(child < 0)
        {
                printf("Parrent process [%d] create child fail!\n",getpid());
                printf("%s\n",strerror(errno));
                return -1;
        }
        else if (child == 0)
        {
                sleep(3);
                printf("count = %d",++count);
                printf("Child process [%d] ,parrent [%d]\n",getpid(),getppid());
                child = fork();
                if(child == 0)
                {
                        while(1)
                        {
                                printf("child child [%d] [%d]\n",getpid(),getppid());
                                sleep(1);
                        }
                }
                else
                {
                        printf("child process [%d] finish,quit!\n",getpid());
                        exit(0);
                }
        }
        else
        {
                //sleep(1);
                //wait();
                while(1)
                {
                        printf("Parrent process [%d] create child [%d] sucess\n",getpid(),child);
                        sleep(1);
                }
        }
        printf("Only parrent do......[%d],count = [%d]\n",getpid(),count);
        return 0;
}

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:53091次
    • 积分:2914
    • 等级:
    • 排名:第13653名
    • 原创:230篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    最新评论