Fundamentals of computer operating system (15) — using fork system call to create process

Time:2020-8-1

introduction

This paper is the fifteenth part,Create process using fork system call。 Creating a process is a very important content. No matter what language it is, the bottom layer uses fork function when creating process. This paper uses C language to be familiar with fork system call to create process

Create process using fork system call

  • The fork system call is used toCreate processOf
  • Process created by forkInitialization statusIt is the same as the parent process (process has process space, memory, memory state, etc.)
  • The system will allocate new resources (including memory resources, CPU resources, etc.) for the fork process
  • Fork system callNo parameters
  • Fork meetingGo back twice, return the child process ID and 0 respectively (the first time is returned by the parent process, the second time it is returned by the child process, so it is returned twice)
  • The parent process returns the child process ID, and the child process returns 0

After calling fork, we can judge whether it is returned by the parent process or the child process 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 < < this is a sub process < endl;
    }
    else if(pid > 0) {
        Cout < < this is a parent process < endl;
        Cout < < sub process ID: "PID < < endl;
    }
    else if(pid < 0 ){
        Cout < < create process failed < endl;
    }
    return 0;
}

Operation results:

Fundamentals of computer operating system (15) -- using fork system call to create process

From the running results, we can see that fork has indeed returned twice, and both if’s have been reached

As mentioned earlier, when fork creates a child process, the initialization memory state of the child process is the same as that of the parent process. The following is also verified with code:

#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 < < this is a sub process < 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 < < this is a parent process < endl;
        Cout < < sub process 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 < < create process failed < endl;
    }
    return 0;
}

Operation results:

Fundamentals of computer operating system (15) -- using fork system call to create process

As can be seen from the figure, the initial value of the child process is the same as that of the parent process, and then as the logic of the parent process and the child process is different, their num value will go their separate ways. in other wordsIn the initialization state, the memory space of the child process is the same as the memory space of the parent process, but with their different logical direction, their memory space will go different

It is the core competitiveness of a technical person to look for invariance in the rapidly changing technology. Unity of knowledge and practice, combination of theory and Practice

Fundamentals of computer operating system (15) -- using fork system call to create process