Process concept [linux]

process basics

Before learning the process, you must first have a certain foundation in computer hardware and software.

Hardware basis:Von Neumann architecture

As shown in the figure, it is the architecture of the computer in terms of hardware.​ 

Here are some common input and output devices (some devices are only output devices or only input devices; while some devices can be used as both output devices and input devices)

Input devices: microphone, camera, keyboard, network card, disk, mouse, etc.

Output devices: sound card, graphics card, network card, disk, monitor, printer, etc.

The purpose of connecting these devices together: data flow between devices. The essence is frequent data copying between devices! Therefore, the overall speed of copying data is an important indicator that determines computer efficiency.

Classic scenario: When the program is running, the program must be loaded into memory first.

Program files are filled with instructions and data. These instructions and data are ultimately executed by the CPU. After the program files are generated, they will be stored in the disk. The files in the disk must first be loaded into the memory before they can interact with the CPU. Therefore, when the program is running, the program must first be loaded into memory.

Software foundation:Operating system

The operating system is a computer that managessoftware and hardwareresources software, and it is also the first software to be loaded when the computer is turned on. It can provide users with a stable, efficient and safe operating environment

Management: Making decisions based on data (operating system)

Managed: implements the manager’s decisions (software and hardware)

Computer modeling of data management: First convert the heap data management scenario into the addition, deletion, checking and modification of specific data structures, and then convert specific problems into computer-level modeling,Describe first, then organize

So what exactly is a process?

Let’s take a program as an example. The essence of an executable program is a binary file, which is saved on the disk. When running, the program must be loaded into the memory first; software on the computer, such as WeChat and QQ, is also essentially an .exe Executable programs must first be loaded into memory when starting.

These executable programs, after being loaded into the memory, will become processes!

After loading into memory, how to manage these processes?

For each process, it consists of a kernel PCB and an executable program. This kernel PCB is also called the process control block (process ctrl block).

This kernel PCB can be seen as a structure, which contains the process's status, priority, identifier, and memory pointer etc. contains all attribute fields of process information. Management of processes can be seen as management of the PCB of each process. All control and operations of the process are only related to the PCB of the process and have nothing to do with the executable program of the process!

Summary: Process management can be roughly viewed as adding, deleting, checking, and modifying nodes in a linked list formed by multiple PCB objects.

Almost all instructions are programs, and they also become processes after they are loaded into memory and run. The main job of the CPU is to loop within the cycle of fetching instructions from memory -> analyzing instructions -> executing instructions .

There is also a memory PC in the CPU . The instruction pointer IP/EIP in the PC stores the address of the next instruction being executed, and which process the instruction pointer points to. The code indicates which process is about to be scheduled! The essence of judgment, loop, and function jump instructions is to modify the instruction pointer pointing in the memory PC.

process status

If you create a process, there will be one more process in the system! In Linux, a normal process has its parent process. Each process will have a number called pid. The pid of the process will almost change every time it is started, because this process is a brand new process!

The following generation tests the newly created process through the process of parent process creating child process.

fork() function, header file #include<unistd.h>

The fork() function is used to create a child process and has two return values. returns 0 to the child process and returns the id of the child process to the parent process. (In Linux, you can use the same variable name to represent different memories) 

 #include<stdio.h>                                                                                                           
 #include<unistd.h>
 #include<stdlib.h>
 #include<sys/types.h>
 #include<sys/wait.h>
 int main()
 {
   pid_t id = fork();
   if(id == 0){
       printf("我是子进程,my_pid:%d,my_ppid:%d,return id:%d\n",getpid(),getppid(),id);
       exit(0);
   }
   else{
       printf("我是父进程,my_pid:%d,my_ppid:%d,return id:%d\n",getpid(),getppid(),id);
   }
 }  

When a child process is created, it actually uses the parent process as a template: the child process will shallowly copy the field information of the parent process, and use copy-on-write (open space deep copy when writing, otherwise shallow copy) to optimize creation Process efficiency.​ 

operation result: 

If one process crashes, will it affect the operation of other processes?​ 

From an operational perspective, this is not possible. Any process is independent and does not affect each other.

Test: If the parent process crashes, will it affect the operation of the child process?

  #include<stdio.h>                                                                                                                   
  #include<unistd.h>
  #include<stdlib.h>
  #include<sys/types.h>
  #include<sys/wait.h>
  int main()
  {
    pid_t id = fork();
    while(1)
    {
      if(id==0)
      {
  
        printf("我是子进程,my_pid:%d,my_ppid:%d,return id:%d\n",getpid(),getppid(),id);
        sleep(1);
      }
      else
      {
         printf("我是父进程,my_pid:%d,my_ppid:%d,return id:%d\n",getpid(),getppid(),id);
         sleep(1);
      }
    }
    return 0;
  }

Use the kill -9 command to kill the parent process 

You can see that after killing the parent process, the child process is still running, but its ppid has changed and it has become an 'orphan process'.

Process queuing

Even if the process is placed on the CPU, the process will not always be running!

There is a concept of time slice in CPU, that is, the CPU will allocate a time slice of time (very short) to the current process to run, and subsequent processes will be queued in order of start time. Once the time on the time slice is exhausted, the current process is still not running. When it ends, the CPU will switch processes, and the unfinished processes will continue to be queued and wait for the next scheduling. This method is fair and efficient, and can effectively prevent the process from being blocked or hung, causing a waste of CPU resources!

From a macro perspective, we can open multiple software at the same time, as if there are many programs running at the same time. But in fact, the CPU can only process the content of one process at a time, but the rotation time is extremely short, and we did not notice it.

Process blocking

The concept of process blocking was mentioned above, so what is the general scenario of process blocking?

For example, when we run a C language program, the moment the program starts, it is loaded into the memory and forms a process. There are parts of this program that require the user to input using the keyboard, but the user has yet to enter. During the waiting process, the process seems to be running, but the CPU cannot always wait for user input. At this time, the process will set itself to a blocking state, queue up in different waiting queues according to different blocking types, and wait for software or hardware resources to be ready before continuing to run.

So to sum up: when a process is waiting for software and hardware resources, and the resources are not ready, the process will set itself to the blocking state. The process in the blocking state will be connected to multiple waiting queues according to the different waiting resources!

Process hangs

When computer resources are tight, the operating system will set some low-priority processes to a suspended state and move them to external memory. When conditions permit, the process will be transferred back to the memory.

zombie process

Each process is created to complete certain tasks required by users, so these processes must have results and data (process status). After the process exits, its process status still needs to be maintained by itself for the upper layer to read.

When a process dies (exits) and its process status is not read by its parent process, the current status of the process is the zombie state. The process in the zombie state will always exist, and the memory will not be released, which will cause a memory leak!

Orphan process

If the parent process of a process dies (exits) before the child process, then the child process will be adopted by process No. 1 and become an orphan process.

Guess you like

Origin blog.csdn.net/zyb___/article/details/134958123