Location>code7788 >text

Linux_Process Understanding, Status and Priority (detailed version)

Popularity:29 ℃/2024-10-21 17:30:29

1. The concept of process

Textbook concepts: an executed instance of a program, the program being executed, etc.

Kernel view: the entity that allocates system resources (CPU time, memory).

Actually:Process = kernel's associated management data structures (task_struct, page tables, etc.) + program's code and data

image

task_struct:is a structure that describes a process, a data structure in the Linux kernel that is loaded into RAM (memory) and contains information about the process.

task_struct content classification:

1, identifier: a unique identifier that describes the process and is used to distinguish it from other processes.
2、Status: task status, exit code, exit signal and so on.
3、Priority: the priority of the process relative to other processes.
4. Program Counter: The address of the next instruction to be executed in the program.
5, memory pointers: including program code and process-related data pointers, and other processes to share the memory block pointers
6, context data: process execution processor registers in the data [Hugh example, to add the map CPU, registers].
7, I / O status information: including the display of the I / O request, allocated to the process of the I / O device and the process used by the file list.
8, bookkeeping information: may include the total processor time, the total number of clocks used, time limits, bookkeeping number.
9, ...

2. Process identifier PID

image

How to view process properties:

You can use ps ajx, or you can ls /proc (proc is a file that holds process properties)
Example:
ps ajx | head -1 && ps ajx |grep myproc
or
ls /proc

Let's write a program.

image

Then make it run

At this point we query the PID

image

If we shut down the process and then query it, we will not be able to find the PID printed by the process, because the proc is updated in real time

Methods for obtaining PIDs within a program:

#include<> //header files

getpid();//return the PID of the process

getppid();//returns the PID of the process's parent.

image

output result

image

How to kill a process.

kill -9 +PID of the process

Let's write a dead-end program that continually prints PIDs

Then use the kill -9 command to shut down the process

image

3. System calls to create processes: a primer on the fork command

fork has two return values:
If the return value is 0, it is a child process;
If the return value is greater than 0, it is the parent process;
Parent and child process code sharing, data each open space, a private copy (using copy-on-write)

Let's write some code to understand fork better.

image

run up

image

We found that both if and else if actually ran and made changes to val in the child process without changing the parent's value of val

From this we know thatParent and child processes share code and are data independent

4. Process status

The Linux process state is essentially represented by a variable that takes on different values;

image

A linux process has the following states

image

R status (operational status)

A process is ready to be dispatched by the system at any time when it is running;

If only one process, the cpu can directly allocate enough resources to execute the process, but when there are a lot of processes are scheduled, the cpu's resources are not enough to allocate, then each process needs to compete for cpu resources, and the cpu can not be allocated to so many processes at the same time; this time, there should be a run queue (runqueue) to organize the PCB;

The process in this runqueue is the R state

image

Once a process holds a cpu, does it keep running?

It doesn't.
When a process is taken to the cpu for execution, it doesn't wait for that process to finish executing before switching to the next process.
Rather, all of these multiple processes execute (concurrently) in a time slot, and
After each process executes for a time slice of time, it is taken off the cpu and switched to the next process. (Process switching)
A time slice is usually about 10ms

When looking at the state of the process, we see that there is an x state and an x+ state, which distinguishes whether the process is executing in the foreground or in the background;

. /process: the process is executed in the foreground
. /process &: processes run in the background

S, D status (dormant)

S-state: essentially waiting for the "resource" to be ready, S-state can be interrupted;

D state: also a hibernation state, although the D state is not interruptible;

image

A process will be 1GB of data placed on the disk, waiting to read, we know that the peripheral reading speed is relatively slow, then the process will wait; but during the waiting period, the OS memory is seriously insufficient (OS has the authority to kill the process to free up space), this time, if the OS sees this waiting process, kill him, then this will cause a serious problem: the disk in the reading of data problems! If the OS sees the waiting process and kills it, it will cause a serious problem: the disk will have problems reading the data, and it will go back to the process that scheduled it, but then it will find that the process is gone. Then it will cause the loss of the 1GB of data.

How can this be avoided?

Set the process to deep hibernation state, D state, then the process will not be killed by OS, you have to wait until the process wakes up. Note that the D state can not be killed by kill, unless reboot, sometimes need to power off.

T, t state

T status: Stopped process

t state: pause when a breakpoint is encountered

kill -19 PID: Suspend process
kill -18 PID: continue process

Z-state (zombie process)

State Z: A process has finished executing and the process has exited, but the resources occupied by the process have not been fully released.

Let's look at the following code

image

Run the program for a while

After the child process finishes executing, the parent process is still executing.

image

At this point we query the status of the process

image

The child process has finished running, but needs to maintain its own exit information, and will record its own exit information in its own process's task_struct for the parent process to read, and if the parent process doesn't read it, the process will always exist.

Orphan process

Orphan process: the parent process completes while the child process is still executing;
Orphaned processes are generally adopted by process #1 (the OS itself);

Why should the orphan process be adopted by OS?

To ensure that child processes are properly recycled

X state (dead state)

The process has finished executing and all the resources it occupied have been released;
kill -9 PID
killall Process name

5. Process priorities

What is prioritization?

The order in which cpu resources are allocated is the priority of the process (priority).

Essentially, it specifies the order in which the process acquires a certain resource (one or more simple numbers)
task_struct process control block -->struct ----->internal field ---->int prio=xxx
The smaller the number in Linux, the higher the priority;

Difference between priority and authority

Authority: the question of whether it can or can't
Priority: already can, question of order of access to resources

Why Prioritization?

Processes always have limited access to resources (cpu).
And most of the processes in the system are more, so there will be competition between processes, in order to efficiently complete the task, more reasonable competition for the relevant resources, it has a priority.

Characteristics of Linux Priorities and How to View Them

PRI : represents the priority at which the process can be executed, the smaller the value the earlier it will be executed.
NI : represents the nice value of this process.

image

PRI: Process Priority

NI: Corrected data for process priority.niceValue;

New priority = priority + nice value. This leads to a dynamic modification of the process priority

Attention:The nice values are in the range: [-20,19] -----40 numbers and all have a priority starting at 80;

How do I change the priority of a process?

command
2. Enter top and press 'r', enter the PID of the process you want to modify, and enter the NI value;