Fundamentals of Computer Operating System (15) --- Use fork system call to create a process

introduction

This article is the fifteenth article, using the fork system call to create a process . Creating a process is a very important content. No matter which language, the bottom layer uses the fork function when creating the process. This article uses the C language to familiarize yourself with the fork system call to create the process.

Use the fork system call to create a process

  • fork system call is used to create a process of
  • The initialization state of the process created by fork is the same as the parent process (the process has process space, memory, memory state, etc.)
  • The system will allocate new resources (including memory resources, CPU resources, etc.) for the fork process
  • The fork system call has no parameters
  • Fork will return twice , returning the child process id and 0 respectively (the first time is returned by the parent process, and the second time is returned by the child process, so it returns twice)
  • The child process id is returned by the parent process, and the child process returned by 0 is the child process

After calling fork, we can judge whether the parent process or the child process returned according to whether the return value is 0

Code example:

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

using namespace std;

int main()
{
    pid_t pid;
    pid = fork();
    if(pid == 0) {
        cout << "这是一个子进程" << endl;
    }
    else if(pid > 0) {
        cout << "这是一个父进程" << endl;
        cout << "子进程id:" << pid << endl;
    }
    else if(pid < 0 ){
        cout << "创建进程失败" << endl;
    }
    return 0;
}

operation result:

It can be seen from the running results that fork did return twice, and both of the ifs have been reached.

As mentioned earlier, when fork creates a child process, the initial memory state of the child process is the same as that of the parent process. The following code is also used to verify:

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

using namespace std;

int main()
{
    pid_t pid;
    int num = 888;
    pid = fork();
    if(pid == 0) {
        cout << "这是一个子进程" << endl;
        cout << "num in son process:"<< num << endl;
        while(true) {
            num+=1;
            cout << "num in son process:"<< num << endl;
            sleep(1);
        }
    }
    else if(pid > 0) {
        cout << "这是一个父进程" << endl;
        cout << "子进程id:" << pid << endl;
        cout << "num in father process:"<< num << endl;
        while(true) {
            num-=1;
            cout << "num in father process:"<< num << endl;
            sleep(1);
        }
    }
    else if(pid < 0 ){
        cout << "创建进程失败" << endl;
    }
    return 0;
}

operation result:

It can be seen from the figure that the initial value of the child process is the same as the parent process, and then as the logic executed by the parent process and the child process are different, their num values ​​diverged. That is to say , the state of initialization, the memory space of the child process and the memory space of the parent process are the same, but as their logic goes different, their memory space will go different

It is the core competitiveness of a technical person to find the constant in the rapidly changing technology. Unity of knowledge and action, combining theory with practice

Guess you like

Origin blog.csdn.net/self_realian/article/details/107203209