[Linux] Process concept, simple operation, pcb, process status, zombie process

Process concept

For the operating system, a process is a description of program operation, which is used by the operating system for program scheduling operation management.
In fact, for the system, the process is the pcb-process control block.

pcb-process control block

The description of a program running on the same platform, called the pcb process control block of the program, is a task_struct structure under Linux.
The pcb saves the location of the process in the memory, and the instruction address of the process running, which can be regarded as the running description of the program.
For which program to run, the operating system finds the pcb of the corresponding program, takes out the necessary information for the program to run, and loads it onto the cpu to start running.

Description information contained in pcb

Memory pointer, context data, program counter, process ID-pid, IO information, process priority, process status, accounting information...

Simple operation of the process

fork function

The fork function is a very important function in linux, it creates a new process from an existing process. The new process is the child process, and the original process is the parent process.

#include <unistd.h>
pid_t fork(void);
Return value: 0 is returned in the child process, the parent process returns the id of the child process, and the error returns -1. The
process calls fork. When the control is transferred to the fork code in the kernel, the kernel does :
■Allocate a new memory block and kernel data structure to the child process
■Copy part of the data structure content of the parent process to the child process
■Add the child process to the system process list
■Fork return and start the scheduler scheduling

Conventional usage:
■A parent process wants to copy itself so that the parent and child processes execute different code segments at the same time. For example, the parent process waits for a client request and spawns a child process to handle the request.
■A process needs to execute a different program. For example, the child process calls the exec function after returning from the fork.
Reasons for the failure of fork call:
■There are too many processes in the system
■The number of processes of the actual user exceeds the limit

Create process

A process is a pcb, which is a task_struct structure (only in Linux). To create a process is to create a task_struct structure.
pid_t fork(void)-Create process interface-Create a new process (child process) by copying the process that calls this interface (parent process).
Fork () function: For the parent process, it will return the pid of the created child process ; For the child process, it will return 0

Illustration:
Insert picture description here
Insert picture description here
pid3586 is the parent process and
pid3587 is the child process

ps instruction

ps: Command to view the currently running process in Linux. Can list the processes running in the system, including process numbers, commands, CPU usage, memory usage, etc.
ps -a: list all running/activated processes
ps -ef |grep: list required processes
ps -aux: display More detailed information about the process, including: USER, PID, %CPU (cpu occupancy rate), %MEM (memory occupancy rate), program running time, operating instructions, etc.

Process status under Linux

Running state (R): It is running or can be run immediately after turning to the time slice.
Interruptible sleep state (S): Sleep state that can be interrupted (enter the running state when the wake-up condition is met, or the sleep is interrupted).
Uninterruptible sleep state (D): Sleep state that cannot be interrupted (you can enter the running state only after the wake-up conditions are met)
Stop state (T): The program stops running state (it can still be scheduled, but does nothing)
Death state (X) : This state is only a return state, and you will not see it in the task list.
Zombie state (Z): The process has exited, but the resources of the process have not been fully released yet, a state waiting to be processed

Zombie process

Zombie process: A process in a zombie state is a process that has exited, but the resources are not completely released.
Cause: The child process exits before the parent process, but the parent process does not notice the exit of the child process, so the system is not complete Release the resources of the child process, and the child process enters the zombie state. After the child process exits, it saves its exit return value in the pcb. If the parent process does not pay attention to processing, the pcb resource will not be released.
Harm: Resource leakage (resources occupied by pcb cannot be recovered), resource occupation (the number of processes that a user can create is limited)
Solution:

Processing: exit the parent process
Avoid: process waiting

Orphan process

Orphan process: the parent process exits before the child process, the child process will become an orphan process, running in the background, and the parent process will become the No. 1 process (refers to the init process, that is, the orphan process will be taken over by the init process), and then the No. 1 process Recycling resources
Note: In centos7, process No. 1 is renamed as systemd process

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/114703507