Operating System (OS) and System Processes

von Neumann architecture

To understand the operating system, the von Neumann architecture must be mastered, which determines the most basic design ideas at the bottom of the computer.

image-20230903203703062

First of all, the von Neumann architecture consists of four major components, input devices, memory, central processing unit (CPU), and output devices. The CPU is composed of two major devices: a controller and an arithmetic unit.

image-20230903204205899

The memory here refers to the memory , regardless of the cache situation. The ** CPU here can and can only read and write to the memory **, and cannot access peripherals (input or output devices). Peripherals (input or output devices) need to be input Or output data can only be written to or read from memory. In a word, all devices can only deal directly with memory.

Therefore, for a program to run, it must be loaded into memory. The reason is that the von Neumann architecture design dictates that this must be the case.

So why must it be memory? The CPU cannot directly read data from peripherals, such as hard disks. The reason is that storage is hierarchical.

image-20230903204856033

The short-board effect of wooden barrels is well understood, and efficiency must be taken into account.

Operating system (Operator System)

Any computer system contains a basic collection of programs called an operating system (OS). In a general understanding, the operating system includes the kernel (process management, memory management, file management, driver management), and other programs (such as function libraries, shell programs, etc.).

The purpose of designing the OS: The essence of the operating system is a set of software responsible for interacting with the underlying hardware resources. Interact with hardware and manage all hardware and software resources
to provide a good execution environment for user programs (applications) .

The essence of the operating system is a piece of software responsible for management, managing all hardware and software resources.

image-20230903210501697

It is impossible for users to directly manage the underlying hardware resources, because the operating system does not trust anyone, and the only way to interact with the underlying resources is through the system call interface and through the operating system.

From the perspective of development, the operating system will appear as a whole to the outside world, but it will expose part of its interface for upper-layer development. This part of the interface provided by the operating system is called a system call.
In the use of system calls, the functions are relatively basic, and the requirements for users are relatively high. Therefore, interested developers can properly encapsulate some system calls to form a library. With a library, it is very beneficial for higher-level users or Developers carry out secondary development.

Summarize:

How the operating system manages hardware: first describe, then organize .

Description: Integrate the properties of the object to be managed with a structure description

Organization: organize each structure with a specific data structure

process

basic concept

Textbook concepts: an execution instance of a program, a program being executed, etc.
Kernel point of view: Acts as the entity that allocates system resources (CPU time, memory).

image-20230903211633392

Simply understand, it is the programs loaded into the memory one by one. The simplest and intuitive way is to open the task manager, which means that there are many, many processes.

The concept is simple to understand. What is really difficult to understand is the various attributes of the process and the management of the process by the operating system.

Process description (PCB)

A process usually consists of a process information (PCB) and its data and code.

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 , and the PCB under the Linux operating system is: task_struct.

The structure describing the process in Linux is called task_struct. task_struct is a data structure of the Linux kernel that is loaded into RAM (memory) and contains process information.

Information contained in the PCB:

Identifier: Describe the unique identifier of this process, which 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 in the program to be executed.
Memory pointer: pointers including program code and process-related data, as well as pointers to memory blocks shared with other processes Context
data: data in the registers of the processor when the process is executing [Example of suspension of study, add pictures of CPU, registers].
I/O status information: including displayed I/O requests, I/O devices assigned to the process and a list of files used by the process.
Billing information: May include sum of processor time, sum of clocks used, time limits, billing account number, etc.
other information

The way processes are organized: This 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 list

In Linux, task_struct is organized by way of double linked list.

image-20230903223325072

view process

Process information can be found in the /proc directory. To obtain process information with PID 1, you need to view the /proc/1 folder

image-20230903212649190

Most process information can also be obtained using user-level tools such as top and ps, and is recommended.

For example to view all processes.

image-20230903212910050

How to find a process?

image-20230903213801291

Just need to know how to find the process. The details of the above line of process need to be understood slowly.

Get the process identifier (PID) through a system call

image-20230903214509446

You can get the PID or PPID of your own process through system calls, getpid() and getppid().

image-20230903215353681

Create a process through a system call (fork)

fork is very magical. It has two return values. Yes, it is a function, but it has two return values. It returns the PID of the child process to the parent process, and the child process returns 0 directly.

image-20230904143150246

image-20230903222924321

image-20230903222812313

This time you will find a magical phenomenon, the if else structure written in the code is executed at the same time. Could it be that id is both 0 and a number greater than 0?

How did fork do it, and what exactly did it do?

image-20230904192212594

The description of fork in the Linux documentation is as follows:

image-20230903224205835

Fork is to create a child process with its own PID, but ** the child process and the parent process share a code **.

But our purpose of creating a subprocess is to make the two processes do different things, so we need to use a certain method to distinguish the two, so fork uses two return values, but how can one id represent two value?

After the code is executed and finally returns, the parent process and the child process each return once, which realizes the two return values ​​of fork.

But there is still a problem that the child process and the parent process share a code , so the data is also shared. The problem is what if the data needs to be modified?

The solution given under Linux is that when the child process wants to modify the data, the operating system will block it and open up a separate space for the child process to copy the data to the child process. This technique is called copy-on- write .

Process status (primary knowledge)

image-20230904191519196

In order to figure out what a running process means, we need to know the different states of the process. A process can have several states (in
the Linux kernel, a process is sometimes called a task), the following is the definition in the Kernel source code:

/*
\* The task state array is a strange "bitmap" of
\* reasons to sleep. Thus "running" is zero, and
\* you can test for combinations of others with
\* simple bit tests.
*/
static const char * const task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */
};

R running status (running): It does not mean that the process must be running, it indicates that the process is either running or in the run queue.
S sleeping state (sleeping): means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep (interruptible sleep)).

D Disk sleep state (Disk sleep) is sometimes called uninterruptible sleep state (uninterruptible sleep). Processes in this state usually wait for the end of IO.
T stop state (stopped): A process can be stopped (T) by sending a SIGSTOP signal to the process. The suspended process can be resumed by sending the SIGCONT signal.
X dead state (dead): This state is just a return state, you will not see this state in the task list.

There are many kinds of process states. The first contact with the process state is to simply understand the three major states, running, blocking, and suspending .

image-20230904193840354

A process to execute must be queued in the run queue first.

A process on the CPU does not necessarily have to wait for its execution to end before it is taken down. Each process has a time slice. If the time is up but not over, it will still be taken down and put back in the queue to prevent a process from occupying resources all the time. Condition.

image-20230904195020472

And because the subsequent processes are all queuing, but still occupying resources, in order to ensure that the processes can be queued normally and save a lot of resources, only the PCB is left to queue, and the corresponding code and data are exchanged to the disk, and wait for the turn. Then exchange the corresponding code and data, and the process of only PCB in the middle is a kind of suspended state.

image-20230904195554700

Guess you like

Origin blog.csdn.net/weixin_73223794/article/details/132676590