Linux Chapter [4]: Process and Operating System

Table of contents

1. Von Neumann architecture

Notice:

1. Why do we need memory?

a.Technical perspective

b. Cost perspective

2. The meaning of storage/memory

2. Operating system

1. Understanding of management 1: The essence of management - managing data.

2. Management understanding 2: How to manage a large amount of data!

The core concept of management: first describe (objects have various information), then organize (data structure)

3. Process (and PCB)

Are programs files? - Yes! Files are on disk!!

Processes and procedures, jobs

Process Control Block-PCB

2. Operating system OS

1.OS introduction

After analogy, the operating system also provides interfaces to users! The operating system also doesn’t trust anyone,

Are the system interfaces of Windows and Linux the same? ? They are definitely different.

Official diagram of operating system management structure:  

2.Why does the OS provide services to me?

3. Viewing the process

1. The first and most common way to view processes: ps axj | grep 'mytest'

ctrl+c and kill -9 process pid kill process

2.pid means the current process, ppid means the parent process

bash process and init process:

3. Code creates subprocess fork()

Example: printf prints 2 times

Example 2:

4. Why does fork() return the pid of the child process to the parent process and 0 to the child process?

5. Why does fork return twice?

What did the OS do after the fork? Is there an extra process in the system?

When calling a function, when the function is ready to return, has the core function of the function been completed? ?

fork will return two explanations:

3. Process state (running state, terminated state, blocked state, suspended state)

1. Process status based on operating system principles

2. Process status in the Linux kernel

(1)R,S,D

(2)Z (zombie) 和 X(dead)

(3) Orphan process

 S+: with + is the foreground process. S: Without +, it is a background process.

The orphan process will be adopted by process No. 1 (init process):

(4) T and t

3. Daemon process/elf process

(1) Introduce various names such as PGID, SID, etc.

(2) Conversation

4. Correspondence between process status in operating system and Linux kernel

4. Process priority

1. What is priority? Priority VS permissions (what is it)

2. Why does priority exist? (Why) - Not enough resources!

3. Concepts and operations related to priority under Linux (what to do) ps -la to view priority

The priority consists of two values: priority+nice

PRI and NI

PRI vs NI

(3) Modify priority operation

5. Switching between processes (register)

1. Context switching:

Register VS temporary data in register:

2. Competition and independence, parallelism and concurrency

Parallel explanation::

Concurrency explanation: (The switching process is done by a software scheduler in the operating system)

3. Preemptive kernel

Process priorities and queues

In preemptive multitasking, when a process is preempted, which running environment needs to be saved?

4.Register

Importance of data on registers:

It is necessary to accurately distinguish: register vs. the data in the register!


1. Von Neumann architecture

Our common computers, such as laptops. Most of our uncommon computers, such as servers, follow the von Neumann system.

Peripherals such as input and output devices do not deal directly with the CPU, but store data in memory. The CPU also reads data directly from memory.

Input: keyboard, microphone, camera, disk , network card...
Output: monitor, audio, disk , network card, graphics card... (input and output are peripherals)
(calculator + controller) [CPU]: arithmetic calculation + Logical computing
memory: just memory

Notice:

1. The disk can both read data from the hard disk and write data to the hard disk.

2. Necessary components of von Neumann architecture:

CPU calculator and controller, RAM memory (memory), ROM disk (input and output device)

Cache Cache is a technology that is not a necessary part of the von Neumann architecture.

3. The basic principles of von Neumann architecture computers are stored programs and program control

1. Why do we need memory?

a.Technical perspective

The computing speed of the CPU > the speed of the register > L1~L3Cache > memory >> peripherals (disk) >> optical disk and tape
Data perspective: Peripherals do not interact directly with the CPU, but interact with the memory. The same is true for the CPU.
        From our perspective, the memory , it is a large cache of the architecture that adapts to the problem of uneven peripheral and CPU speeds!

b. Cost perspective

        Register>>Memory>>Disk (Peripheral)

Computers are spreading all over the world! Computers must have two characteristics: effective and cheap
 

2. The meaning of storage/memory

Using lower cost of money, you can get higher performance. Balance slow input and output speed with fast CPU speed

Almost all hardware can only passively complete certain functions and cannot actively complete certain functions. Generally, it must be completed in conjunction with software (OS+CPU)

After the software we write ourselves is compiled, it must be loaded into the memory first before running. Why? ? The
architecture stipulates! The principle of locality

2. Operating system

1. Understanding of management 1: The essence of management - managing data.
 

An operating system is a piece of software, a management software

The essence of management: managing data.
 

The principal doesn't even see me, how can he manage me?
To manage you, do you need to deal with you or meet with you?
How does he do it?
The essence of management: It is not to directly manage the objects being managed, but to manage them as long as they are managed. After getting all the relevant data of the managed objects, our management of data can reflect the management of people! ! ! ! !

2. Management understanding 2: How to manage a large amount of data!

How to manage large amounts of data!

The way people understand the world:
through attributes!
Everything is an object.
All things can achieve the purpose of describing the object by extracting the attributes of the object.
class OBJ { //Member attributes        

}

The core concept of management: first describe (objects have various information), then organize (data structure)

 The management of the process has become the addition, deletion, checking and modification of the linked list.

3. Process (and PCB)

What is a process? A process is a dynamic process, a running program. Process = executable program (for example: test.exe in memory) + the kernel data structure (PCB) corresponding to the process. Process = kernel process data structure (task_struct , mm_ struct, page table) + process code and data

task_struct is a specific PCB. The PCB process control block is called task_struct in Linux.

Are programs files? - Yes! Files are on disk!!

There may be a large number of processes existing in the operating system at the same time. The operating system must manage all processes.
The essence of process management is the management of process data. First describe (objects have various information), then organize (data structure)

Processes and procedures, jobs

  • There is not necessarily a one-to-one correspondence between processes and programs. A program can run multiple times at the same time, resulting in multiple processes.
  • Processes and jobs do not necessarily have a one-to-one correspondence, because the completion of a job task can be composed of multiple processes and must be composed of at least one process.

Process Control Block -PCB

Process information is placed in a data structure called a process control block, which can be understood as a collection of process attributes.
It is called PCB (process control block) in the textbook. The PCB under the Linux operating system is: task_struct
task_struct-PCB type
The structure describing a process in Linux is called task_struct.
task_struct is a data structure of the Linux kernel. It is loaded into RAM (memory) and contains process information.
task_ struct content classification
        Identifier: A unique identifier that describes this process and is used to distinguish other processes.
        Status: task status, exit code, exit signal, etc.
        Priority: Priority relative to other processes.
        Program counter: The address of the next instruction to be executed in the program.
        Memory pointers: including pointers to program code and process-related data, as well as pointers to memory blocks shared with other processes.
        Context data : The data in the processor's registers when the process is executed [example of leave of absence, please add the figure of CPU, registers].
I/O status information: includes displayed I/O requests, I/O devices assigned to the process and a list of files used by the process.
Accounting information: may include total processor time, total number of clocks used, time limits, accounting accounts, etc.
other information
organizational process
It can be found in the kernel source code. All processes running in the system are stored in the kernel in the form of task_struct linked lists.

2. Operating system OS

1.OS introduction

Operating system: memory management, process management, file management, driver management

What is an operating system (OS)? Analogous to a bank management system, the operating system is the president of the bank. 

The way the bank provides services to everyone is through the window:

All banks we see are closed bodies with some windows exposed. Because banks don’t trust anyone! To prevent some criminals from breaking in, we must prevent the few and provide services to the many.

After analogy, the operating system also provides interfaces to users! The operating system also doesn’t trust anyone ,

To prevent a few people from maliciously modifying the code, services must be provided to the majority. The kernel is written in C language! Interface: Provides us with function calls in the underlying C language. The system can call various functions.

The operating system does not trust anyone! It will not directly expose any of its own data structures, code logic, and other data-related details!!

The operating system provides interface services to the outside world through system calls!
The Linux operating system is written in C language. The so-called "interface" here is essentially a C function!

The essence of our learning system programming is to learn the system interface here

Are the system interfaces of Windows and Linux the same? ? They are definitely different.

Official diagram of operating system management structure:  

2.Why does the OS provide services to me?

Computers and OS are designed to provide services to people

printf or cout prints to the monitor, which is hardware -> the so-called printing is essentially writing data to the hardware.
Our own C program is not qualified to write to the hardware.
 

3. Viewing the process

The code we write ourselves is compiled into an executable program and becomes a process after it is started!
So what about the code written by others? Is it a process after it is started? It definitely counts!!

1. The first and most common way to view processes: ps axj | grep 'mytest'

ps axj selects all processes, ps axj | grep 'mytest': filters out the process of mytest from all processes

ps axj | grep 'mytest' | grep -v grep: Filter out the process of mytest from all processes and hide the process grep (we are using grep, grep is also a process)

ps axj | head -l: Filter out the first line from all processes. The first line is PPID, PID....COMMAND, these names.

ps axj | grep 25979: Filter out process 25979 from all processes

 

Current path?: The path where the current process is located, which will be maintained by the process itself.

Every process in the system will have a unique identifier (pid) ! Just like students in school, every student has a student ID!
pid (process id)

ps ajx | head -1 && ps axj | grep' mytest '| grep -v g

ctrl+c and kill -9 process pid kill process

-9 is Order No. 9, we will talk about it later

2.pid means the current process, ppid means the parent process

getpid(): get the identity of the current process, getppid(): get the parent process identity of the current process (if you want to get the child process identity of the current process, you can only use fork)

Almost all the instructions we execute on the command line (your cmd) are child processes of the bash process (the pid of bash is 29673)!
 

bash process and init process:

Bash is the first program we run every time we open a terminal. It will capture your keyboard input and see what instructions you input. Then it will parse and execute the corresponding instruction program. The process corresponding to the execution program running in the terminal defaults to The parent process is this bash process (because the processes corresponding to these instruction programs are all created by this bash process). The
init process (also called process No. 1, which is the operating system) is a very important management process in the system. There is only one such process in the system. Process (bash process will run as many as the terminals are open), the process PID is 1
 

3. Code creates subprocess fork()

The fork function is used to create a child process. It has two return values: the parent process returns the pid of the child process, and returns 0 to the child process.

How can fork have different return values?

Example: printf prints 2 times

Why does printf print twice? : After fork, the parent process and the child process will share code and generally execute subsequent code.

 

Example 2:

After fork, the parent process and the child process will share code, and generally execute the subsequent code.
After fork, the parent process and the child process have different return values. You can judge by different return values ​​and let the father and son execute different code blocks, that is, both if and else. implement 

 

4. Why does fork() return the pid of the child process to the parent process and 0 to the child process?

Father:Son= 1:n (n>=1)
A father can have multiple sons, so the parent process must have a plan to identify the child process. After fork, the pid of the child process is returned to the parent process! The
most important thing about the child process is You need to know that you have been created successfully, because the cost for the child process to find the parent process is very low! So there is no need for an identifier, just return 0. Getppid() can get the parent process of the child process.

5. Why does fork return twice?

What did the OS do after the fork? Is there an extra process in the system?

task_ struct + process code and data
task_ struct + code and data of the
child process. Where does the data inside the task_ stuct object of the child process come from: basically it is the execution code of the child process inherited from the parent process
, and the calculation data is of the child process. Where does the code come from? : Execute the same code as the parent process. After fork, the code of the parent and child processes is shared! The data must be independent.
(Father-child process code sharing: different return values, allowing different processes to execute different codes)

When calling a function, when the function is ready to return, has the core function of the function been completed? ?

Completed!
1. The child process has been created
2. Put the child process into the run queue! (That is, the process is run)

How to understand 2. The process is run:

The run queue runqueue is a chain queue

fork will return two explanations:

Because the child process is put into the run queue before return, there are two processes running, the parent process and the child process. Because the return pid is also code, and the father and son share the code, the return pid will be executed once by the father and son respectively, so there are two return values.

3. Process state ( running state, terminated state, blocked state, suspended state )

1. Process status based on operating system principles

Running state: As long as the process is called running state in the run queue, it means that it is ready and can be scheduled at any time!! ( It is not a process running on the CPU)

Termination state: The process is still there, but it will never be scheduled and run, waiting to be released at any time! ( It is not a process that has been released)

The processes have terminated. Why not release the corresponding resources immediately instead of maintaining a terminated state?
Answer: Releasing takes time! It is possible that your operating system is currently busy and cannot handle it immediately.

Blocked state: The process is waiting for a certain resource (non-CPU). When the resource is not ready, the process needs to be queued in the waiting queue of the resource. At this time, the code of the process is not running, and the state of the process is called blocked! ( Only accessing peripherals will block, such as while(1) printf("11"); accessing the display means accessing peripherals will block, but a single while(1) will not block)

1. When a process uses resources, it is not just applying for CPU resources!
2. The process may apply for more other resources: disk, network card, graphics card, monitor resources, sound card/audio.
If we apply for CPU resources, we will not be able to get them temporarily. Satisfied, what needs to be queued - running queue
So what if we apply for resources from other slow devices? It also needs to be queued! (task_ struct is queued in the process)

When a process accesses certain resources (disk network card), if the resource is not ready for the time being, or is providing services to other processes, at this time: 1. The operating system will remove the current process from the runqueue 2. The operating system will remove the current process from the runqueue Put it into the waiting queue in the description structure of the corresponding device!

For example: the CPU executes a process A in the run queue, and the code executing process A needs to access the disk resource disk, but the disk is not ready for the time being, or is providing services to other processes, then process A will not occupy CPU resources, etc. disk, but the operating system puts process A into the waiting queue of the disk to wait. When our process is waiting for the disk , the code of the process will not be executed . What we see is: My process It's stuck. This is called process blocking.

Wait until the disk is ready and then put it back into the CPU's run queue to run the code.

pending

What should I do if the memory is insufficient?
The operating system has to help us move around
processes that will not be scheduled in the short term (the resources you are waiting for will not be ready in the short term). Its code and data are still in the memory! It’s all in vain A waste of space! The OS will replace the code and data of the process to the disk (swap partition)!

 

2. Process status in the Linux kernel

R running status (running): does not mean that the process is definitely running, it indicates that the process is either running or in the run queue. If you do not access external resources (for example: printf accesses peripherals such as the display), then you are basically applying for CPU resources, which is the R state.
S sleeping state (sleeping): means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep
(interruptible sleep)). Both S and D are in blocking state. The difference is that S can be awakened/killed, while D can only wait for its own execution to finish before waking up.
D disk sleep state (Disk sleep) is sometimes called uninterruptible sleep state (uninterruptible sleep). The process in this state usually waits for the end of IO.
T stopped state (stopped): The (T) process can be stopped by sending the SIGSTOP signal to the process. The suspended process can continue running by sending the SIGCONT signal.
X Death state (dead): This state is just a return state, you will not see this state in the task list

(1)R,S,D

R running state: If you do not access external resources (for example: printf accesses peripherals such as the display), then you are basically applying for CPU resources, which is the R state, for example, as follows:

S blocking state: Apply for external resources, such as a printf, or an infinite loop. Although the screen is frantically refreshing the printed content, in fact the CPU prints extremely fast, and most of the time is waiting for the monitor resource, so the state It is the S blocking state, such as the picture below: The S state is also called light sleep/ interruptible sleep, because this state can be awakened (the operating system changes the S state to the R state to wake up), and can also be awakened by us at any time Kill with kill -9 (pid)

D (disk sleep): Generally speaking, in Linux, if we are waiting for disk resources (disk), the state in which our process is blocked is D

D explanation: The process asks the disk to write its own data to the disk. The process has to wait for the disk to finish writing and return a value, which means the writing is successful or failed. In this state, the process is waiting for the disk to write data, and the operating system has no right to kill the process. Otherwise, the process cannot be found when the disk is finished writing, which will cause data loss. The state of this process is called D state. (If you want to kill the D process, you can only shut down the machine)

(2)Z (zombie) 和 X(dead)

The X death state is the terminal state mentioned before , and its resources can be recycled at any time.

Introduction to zombie process (zombie) : A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the status information of the child process, the process descriptor of the child process is still saved in the system. This process is called a zombie process.

A zombie process refers to a child process program that exits before the parent process and is no longer running. However, because the exit reason needs to be saved, the zombie process will not completely release resources and exit .

It will not automatically exit to release all resources, nor will it be killed again by the kill command

Zombie processes will cause resource leaks . It is necessary to avoid the generation of zombie processes and use process waiting (wait/waitpid) to complete the process.

Z (zombie) zombie status:

When a process in Linux exits, it generally does not enter the x state directly (x is the death state and resources can be recycled immediately), but enters the Z state. Process: After the process exits, it enters the zombie state and must be recycled by the parent process/operating system. The parent process/operating system will read the exit information of the process before the process can enter the X state and be released.
Why? Why is the process created? Answer: Because there must be a task for this process to execute. When the process exits, how do we know how well this process has completed the task for us? Answer: Generally The execution results of the process need to be informed to the parent process. Process Z is to maintain the exit information (put it in the task_ struct, the code and data of the process can be released, but the task_ struct cannot be released), and can be read by the parent process or OS ! How to read it? : Read by process waiting! How to wait? More on that later

 —How to simulate a zombie process?

If a child process is created and the child process exits, the parent process does not exit or wait for the child process. The state of the child process after exiting is Z.

Example: The following child process will be in the zombie state after exiting, but the parent process does not control it, so the child process has always been in the zombie state.


— Long time zombie, what’s the problem? ?

 If no one recycles the zombies of the child process, this state will be maintained forever! The related resources of the process (task_ struct) will not be released! This will lead to memory leaks! Generally, the parent process must be required to recycle (discussed later!)

The zombie state ( Zombies ) is a relatively special state. When the process exits and the parent process (using the wait() system call , discussed later)
A zombie process will be generated when the return code of the child process exit is not read.
A zombie process remains in the process table in a terminated state and is waiting for the parent process to read the exit status code.
Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the child process status, and the child process enters the Z state.

—Zombie processes must use the waitpid/wait interface to wait

Zombie processes will cause resource leaks and must be processed using the wait/waitpid interface.

(3) Orphan process

Introduction to orphan processes: If a parent process exits and one or more of its child processes are still running, those child processes will become orphan processes. (Orphan processes will be adopted by the init process (process number is 1), and the init process will complete status collection for them. Orphan processes will not cause a waste of resources )

————————————————————————————————————————

 S+: with + is the foreground process. S: Without +, it is a background process.

The background process cannot be terminated by ctrl+c. It can only be killed by killing -9 pid.

————————————————————————————————————————

The orphan process runs in the background. When the orphan process exits, it will not become a zombie process, so there will be no resource leakage.

Why does the parent process not have Z? But it just disappeared? ? The reason why
the parent process does not have Z is because the parent process of the parent process is bash, and it was recycled by bash (the child process we wrote above was not recycled because the parent process did not write waitpid for recycling).

The orphan process will be adopted by process No. 1 (init process):

If the parent process exits early and the child process is still running, the child process will be adopted by process No. 1 (init process)! The orphan process will not cause a waste of resources (process No. 1 is the operating system!)

Orphan process example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
	while (1)
	{
		sleep(1);
		printf("hello bit!\n");
	}
	pid_t id = fork();
	if (id == 0)
	{
		//child
		int cnt = 5;
		while (1)
		{
			printf("我是子进程,我还剩下 %d S\n", cnt--);
			sleep(1);
		}
		printf("我是子进程,我已经僵尸了, 等待被检测\n");
		exit(0);
	}
	else {
		//father
		int cnt = 3;
		while (cnt)
		{
			printf("我是父进程,我:%d\n", cnt--);
			sleep(1);
		}
		exit(0);
	}
}

(4) T and t

T (stopped) regular pause, has nothing to do with blocking

kill -19 pid is a pause command (sigstop No. 19); kill -18 pid is a wake-up command (sigcont No. 19 means continue);

The starting and stopping of the process can be controlled through signals:

 t (tracing stop): (tracing-tracing) is also the state in which a breakpoint is encountered when the paused process is being debugged.

3. Daemon process/elf process

These two are different translations of the same process. They are special orphan processes. Not only do they run in the background, but the most important thing is that they are separated from all connections with the terminal and login sessions. That is, they run silently in the background without being affected by anything, and It will not become a zombie process after exiting.


Generally, it works as a server. Servers that provide services to the outside world work in the server as a daemon process (elf process). Once started, unless the user actively shuts it down, the daemon process will always be running. Features: ① The parent process is system.

(1) Introduce various names such as PGID, SID, etc.

PPID: The ID of the parent process. PID: The id of the process itself. PGID: The process group to which the current process belongs. (The first process in the same group of processes is generally the leader of the process group) SID: The session id of the current process.

 COMMAND : command. TIME: running time. UID: The number after user name mapping, user UID.

TPGID : Information related to the current process group and the terminal. -1 indicates that the process has nothing to do with the terminal, and non-1 indicates that it is related.

TTY: which terminal

(2) Conversation

When a user logs in, a session will be established. Inside the session, there will be a foreground process group and zero or more background process groups. The foreground process will load bash for us.

Start a process group (can have only one process) in a session to complete a certain task. All processes in the session fork to create child processes, which generally still belong to the current session! ! Let a background process leave the current session and make it
independent Form a new session in the computer and form its own process group. This process is called a daemon process/elf process.

 

4. Correspondence between process status in operating system and Linux kernel

sheet:

                              Process status in the operating system   Linux kernel

        Running status R status

        Termination state Z or X state

        Process blocking S or D state

        The process hangs in S or D or T state

4. Process priority

1. What is priority? Priority VS permissions (what is it)

Priority is
the order in which processes obtain resources. Permissions: It talks about whether you can or can't
. Priority: It talks about whether you can, but whether first or last.

2. Why does priority exist? (Why) - Not enough resources!

The essence of queuing is called confirmation priority.
In the system, it is always the case that processes account for the majority and resources are the minority! --> It is normal for processes to compete for resources! --> The order must be confirmed.

3. Concepts and operations related to priority under Linux (what to do) ps -la to view priority

The priority consists of two values: priority+nice

PRI and NI

PRI is also relatively easy to understand, that is, the priority of the process, or in layman's terms, the order in which programs are executed by the CPU . The smaller this value is.
The higher the priority of the process
What about NI ? It is the nice value we are talking about , which represents the modified value of the priority at which the process can be executed.
The smaller the PRI value, the faster it will be executed. After adding the nice value, the PRI will become : PRI(new)=PRI(old)+nice
In this way, when the nice value is negative, the priority value of the program will become smaller, that is, its priority will become higher, and the faster it will be executed.
Therefore, adjusting the process priority, under Linux , means adjusting the process nice value .
The value range of nice is -20 to 19 , with a total of 40 levels.

PRI vs NI

It needs to be emphasized that the nice value of a process is not the priority of the process. They are not the same concept, but the nice value of the process will affect the progress of the process.
Process priority changes.
It can be understood that the nice value is the correction data of the process priority.

ps -la to view priorities

The priority consists of two values: priority+nice

The default priority of priority is 80. The smaller the number, the higher the priority; the larger the number, the lower the priority.

nice: Correction data for process priority!

To change the process priority, you need to change not pri, but NI, nice

(3) Modify priority operation

Ordinary users cannot modify the process priority at will. They can only be modified by the super user. Enter the super user command sudo top to run it, then enter r, enter the pid of the process, press Enter, and enter the value of the priority you want to change. (The minimum priority value is -20 and the maximum value is 19.)

Linux does not allow processes to set priorities uncontrollably. nice[-20, 19], prio [60,99]
priority value prio = prio_old + nice
. Every time the priority is set, the prio_old priority will be restored to 80, because nice[-20, 19], so prio[60,99].

5. Switching between processes (register)

The main means of process switching is through context switching.

1. Context switching:

There are a large number of registers inside the CPU, and there will be a lot of temporary data in the registers. The temporary data in these registers is called the context of the current process; the context is stored and moved out of the register, that is, the process of context protection and recovery is called context switching .

Register VS temporary data in register:

Registers are a bunch of hardware devices.

2. Competition and independence, parallelism and concurrency

Other concepts
Competition: There are many system processes, but there are only a few, or even 1, CPU resources, so there is competition between processes. In order to complete tasks efficiently and compete for related resources more reasonably, priority is given
Independence: Multi-process operation requires exclusive use of various resources, and multi-process operations do not interfere with each other during operation.
Process operation is independent, and it will not cause problems in other processes because one process hangs up or is abnormal!
Process: kernel structure + code and data.
How to achieve process independence? ?——Process address space

 
Parallelism: Multiple processes run on multiple CPUs at the same time. This is called parallelism.
Concurrency: Multiple processes use process switching under one CPU to advance multiple processes within a period of time, which is called concurrency.

Parallel explanation::

If there are multiple CPUs, at any time, there may be two processes running at the same time (that is,
running on the CPU). Such processes running on different CPUs at the same time is called parallelism .

Concurrency explanation: (The switching process is done by a software scheduler in the operating system)

My computer has a single CPU, but there are various processes running on my computer.

——Multiple processes are running in your system! = Multiple processes are running in your system at the same time
. Don’t think that once a process occupies the CPU, it will be executed until the end before the CPU resources are released! The big problem we encountered Some operating systems are time-sharing! The operating system will give each process a concept of time slice rotation
in a scheduling cycle ! For example: process 1 is given 20ms (20ms is the time slice of process 1), process 2 is given 30ms Within a period of time, multiple processes will switch and cross, allowing the codes of multiple processes to be advanced within a period of time. This phenomenon is called concurrency . (For example, if each process switches after 20ms, 5 processes will take 100ms. Each process can be executed 10 times in 1 second, so multiple processes are promoted)

3. Preemptive kernel

Does the operating system simply schedule according to the queue? Is it possible that a process with a higher priority suddenly comes??
- Today's computers support preemptive kernels !
Explanation: A running low-priority process , but if a process with a higher priority comes, our scheduler will directly peel the process off the CPU and put it on the process with a higher priority. This is called process preemption .
        

Process priorities and queues

Assume that there are 5 priorities: a. The operating system allows processes of different priorities to exist. b: There may be multiple processes with the same priority!

The head of the queue cannot be inserted. If a process with a higher priority comes, how to reflect the priority?

Assuming there are 5 priorities, the bottom layer must maintain an array of pointers: task struct * queue[5], a total of 5 elements, each element represents a priority, from top to bottom: the highest priority is 0, the priority 1, priority 2, priority 3, priority 4; each element stores a pointer of type task struct *. This pointer is linked to a queue. According to different priorities , Put specific processes into different queues , and the CPU traverses the queues that are not empty from top to bottom, from high priority to low: O(1) scheduling algorithm , for example, in the following figure, it is traversed and the priority is found to be 0 If the queue is not empty, the process at the head of the queue with priority 0 will be taken first.

Understand the content: In fact, the operating system will make the above hash table task struct * queue[5] into a bitmap form, and traverse it by scanning the bitmap; and the running queue will implement two such hash table bitmaps, and the operation The system first processes the first bitmap called active, then puts the newly added process into the second bitmap called old. Finally, after processing the first bitmap, swap(active,old) is performed, and then the new process is processed in a centralized manner. This batch of processes

 struct runqueue

{
        hash_ queue* active
        hash queue* old
}

In preemptive multitasking, when a process is preempted, which running environment needs to be saved?

Preemption is when multiple processes seize the CPU to run their own programs, so what should be saved? What is to be saved is the data about the current process in the CPU, such as the data or instructions and addresses to be processed in the registers, and what are the global variables? It is a variable. The data is in memory and has nothing to do with process switching, so it is not within the scope of saving.

So what needs to be saved are: the contents of all CPU registers (data being processed on the CPU), page table pointer (the starting address of the page table will be loaded into the register when the program switches), program counter (the instructions to be executed in the next step of the program) address)

4.Register

The registers in the CPU are a bunch of memories, components, and their functions are: they can temporarily store data (very few, but very important!) Registers:
① Visible registers (registers for us, such as: eax, ebx, ecx, General registers such as edx; ebp, esp, eip, etc. in the stack)
② Invisible registers (registers used by the operating system)

When a process is being executed, there will definitely be a large amount of temporary data, which will be temporarily stored in the registers in the CPU!

Importance of data on registers:

Example in real life: I went to school to join the military halfway, so I have to keep my student status . After I finish serving in the military and continue to go to school, my student status will be restored .
We call the various register data generated during the running of the process the hardware context data
of the process . When the process is stripped off: the context data needs to be saved ( student status is retained )
. When the process is restored: the previously saved context data needs to be restored to the registers. ( Restore student status )
Where is the context data of the process stored? - It is stored in the PCB of the process. The PCB under Linux is called task struct

It is necessary to accurately distinguish: register vs. the data in the register!

There is only one set of registers, and there can be multiple copies of the data in the registers. (The library is a CPU, and the registers are each seat in the library. The data is like classmates and their schoolbags. After self-study, just pack up and leave. This seat can be used by hundreds or thousands of classmates)

Guess you like

Origin blog.csdn.net/zhang_si_hang/article/details/126486650