C language fork a Linux process

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/mrtwenty/article/details/98798161

Process knowledge notes:

       Process is the execution of a program. Multiple processes CPU time division multiplexing, when the end of the time slice allocated to the process, the kernel process to recover the right to use the CPU to perform other processes in favor of the system, and the process before you need to enter the sleep state, the relevant first data will be saved, waiting for CPU scheduling. So the life cycle of a process, there are many different states, the state of the process is usually divided into five kinds: the initial state, ready state, running state, sleep state, termination state.

       In the system, each process has a process id, referred to as the pid (process id), the process has parent-child relationship, the process of brotherhood, has a father, parent process of each process id, called the ppid. So the system which processes will form a process tree, use the command to view the process tree pstree current system. Top previous process is the init process, process 1, and now some linux system uses systemd as the top processes on the system, the process is the number 1, as the difference between the two, Google can access relevant information when we fork a process code were executed from the fork consists of two processes, the execution order of parent and child can not be predicted. When executing the child process, the parent process is not subject to recycling, there will be zombie (defunct process) , when executing the parent process, the child process has not been performed, the child becomes an orphan process , it is claimed will systemd , responsible for recycling.

Here is an example of the use of a fork function to create child process:

#include <stdio.h>  //标准的输入输出函数
#include <stdlib.h> //standard library标准库函数头文件
#include <unistd.h> //对于类 Unix 系统,unistd.h 中所定义的接口通常都是大量针对系统调用的封装 fork、

int main()
{
    pid_t pid;
    
    pid=fork(); //创建一个进程,下面的代码, 由两个进程分别执行,父子进程执行顺序无法预测
    // pid 在两个不同的进程中,返回的结果不一样
    
    // 这是异常情况
    if (pid==-1)
    {
        perror("fork失败!");
        exit(1);
    }

    //返回大于0的进程就是父进程
    if(pid>0)  //父进程
    {
        printf("父进程: pid= %d , ppid=%d,子进程: %d \n", getpid(),getppid(),pid);

        sleep(1); //这里延迟父进程程序,等子进程先执行完。
    }
    else if(pid==0)  //子进程
    { 
        printf("子进程: pid= %d , ppid=%d \n", getpid(),getppid());
    }

    printf("执行完成!\n"); //由两个进程执行,当然输出两次

    return 0; //养成好习惯,返回程序执行状态码
}

Please note that sleep (1); this code after the comment and try to re-compile a then execute them and see the difference between a plus and no sleep,

Plus sleep (1); outputs:

       The parent process: pid = 17571, ppid = 16415 , sub-processes: 17572 
       child process: pid = 17572, ppid = 17571 
       execution is complete!
       Execution is complete!

Note: executing the first child process, the parent process is not dead, then the child process the parent process pid is ppid

Plus no sleep; may be output:

       The parent process: pid = 17580, ppid = 16415 , sub-processes: 17581 
       execution is complete!
       Child process: pid = 17581, ppid = 1 
       execution is complete!

Note: before executing the parent process, the child process has not, the child is receiving systemd, ppid child process becomes 1.

 

Reference herein Linux programming based on the book.

Guess you like

Origin blog.csdn.net/mrtwenty/article/details/98798161