僵死进程实例(2)

版权声明:私藏源代码是违反人性的罪恶行为!博客转载无需告知,学无止境。 https://blog.csdn.net/qq_41822235/article/details/81591838

目录

一、  支撑知识 

二、  示例代码

三、  运行结果 


一、  支撑知识 

 阻塞运行:当条件未发生时,函数等待条件发生;当条件发生后,函数才会返回。

二、  示例代码

#include<unistd.h>    //fork()
#include<stdio.h>    //printf()
#include<assert.h>    //assert()
#include<stdlib.h>    //exit()

void main()
{
    pid_t n = fork();
    pid_t pid;
    assert(n != -1);    //The child process is not created successfully!!

    if(n == 0)
    {
        printf("The child process starts\n");
        sleep(2);    
        printf("The child process is over!\n");
        exit(0);    //The child process is over!
    }
    else
    {
        /*
        *The wait at the end of the script will wait for 
        *    the child process to terminate or complete!
        */
        pid = wait(NULL);   
 
        printf("pid=%d\n", pid);
        while(1)
        {
            sleep(1);
            printf("The father process is running!\n");
        }

        //Although this statement will never be executed, it must be written out!    
        exit(0);   
    }
}

三、  运行结果 

运行结果

关于运行结果的说明,从运行结果我们可以看到,调用fork()之后,理论上,子进程、父进程运行是互不影响的。但是父进程执行到wait()时,会一直等待直到子进程完全运行结束。

 

 

阅读更多
换一批

没有更多推荐了,返回首页