A preliminary understanding of the process of the operating system (1)

1. Concepts related to processes

A process is a running activity of a program with independent functions on a certain data set. It can apply for and own system resources. It is a dynamic concept and an active entity . It's not just the code of the program, but also the current activity, represented by the value of the program counter and the contents of the processing registers.

1.1 Definition of process

Narrow definition : a process is an instance of a computer program that is being executed

Broad definition : A process is a running activity of a program with certain independent functions on a certain data set. It is the basic unit of the dynamic execution of the operating system. In the traditional operating system, the process is not only the basic allocation unit, but also the basic execution unit.

1.2 The concept of process (1)

A process is an entity . Each process has its own address space , in general, including the text region (text region), data region (data region) and stack (stack region). The text area stores code executed by the processor; the data area stores variables and dynamically allocated memory used during process execution; the stack area stores instructions and local variables for active procedure calls.

1.3 The concept of process (2)

A process is a "program in execution" . A program is an inanimate entity. Only when the processor gives life to the program ( when the operating system executes ), it can become an active entity . We call it a process (we will explain why a process is an active entity below).

To understand in a simple way, we can regard the running program as a process , such as the QQ we usually use, when we open the qq app, it is equivalent to starting a process, and the operating system will allocate memory space for the process .

Let's look at the picture below:

insert image description here

2. The difference between a process and a program

First of all, we need to understand what is the program? A program is a piece of code that we write down, and a process is created because the code starts to execute.

Like the picture below:
insert image description here

At this point, we can use this picture to understand the difference between a program and a process:

1. The process is dynamic and the program is static: a program is a collection of ordered codes, and a process is the execution of the program. The process has kernel state/user state.
It is easy to understand that the program is static, so the code you write is of course static. It is easy to understand that the process is dynamic. When the code starts to execute, it will definitely be executed. After the execution is completed, the process will die, so the process is dynamic.

2. The process is temporary, and the program is permanent; the process is a process of state change, and the program can be saved for a long time
. Based on the first explanation, the second is the same understanding.

3. The composition of the process and the program is different. The composition of the process includes the program, data and process control block (that is, the process state information). Since the
process is dynamic and needs to be executed intermittently, then the state information of the process needs to be saved.

3. Process management:

The operating system itself is a big topic, and it is a very complex software. It has many functions, among which there is a functional module closely related to us, process management !

Why do we need process management?

Everyone look at the picture:

insert image description here
It can be seen that because we have a lot of processes, we need to manage them. If there are only a few processes, of course it will not involve process management.

How to manage the process?

  1. To describe a process, use the structure/class to express the information of a process.
  2. Organize this process, use a certain data structure, and put these structure objects together (usually using the data structure of a double-linked list)

3.1 What are the attributes of the structure of the process

We have another name for the structure of the process, which is PCB (process control block)

(1) Pid (the process identification number in the operating system)

PID (Process Identification) refers to the process identification number in the operating system, that is, the process identifier. Every time a program is opened in the operating system, a process ID, or PID, is created.

As long as a program is run, the system will automatically assign a logo

Is temporarily unique : when the process terminates, the number is recycled and may be assigned to another new process.

As long as no other program is successfully run, this PID will continue to be assigned to the currently running program.

If a program is successfully run and then another program is run, the system will automatically assign another PID .

(2) memory pointer

What part of the memory is currently used by this process. (When the process is running, which memory resources are used)

(3) File descriptor table

As shown below:

insert image description here
All the files in the red box above are files,

File : For example, the data stored on the hard disk is often organized in units of files.

Every time a process opens a file, it will generate a 'file descriptor' (indicating that the file is opened). Since a process may open many files, it naturally corresponds to a set of file descriptors , and these file descriptors are placed in a similar order In a data structure such as a table , a file descriptor table is formed !

The file descriptor table describes: which files are used by your process, or which hard disk resources are used.

At this time: we can know from the combination of memory pointer and file descriptor table that the operation of the process needs to apply for resources from the operating system. At this time, it can also be understood that the process is the basic unit of resource allocation by the operating system!

The following attributes are attributes related to describing a cpu resource. These attributes are auxiliary.

4. Process scheduling:

Our programs can run, all rely on the cpu, each program is equivalent to a set of ' binary instructions ', these instructions are run by the cpu.

cpu has a concept ( number of cores )

Take Xiaoyu as an example:

insert image description here

My computer has only 8 cores, each of which is equivalent to an employee, and each employee can perform the work of two people at the same time, which can be understood as my eight cores can run 16 processes at the same time. I have seen that there are no less than 100 processes running in our task manager, so my small 8 cores are too tired to run more than 100 processes at the same time.

So the following involves why the computer can run so many processes at the same time.

(1) Parallel

Parallelism means that the system has the characteristics of performing calculations or operations at the same time , and can complete two or more tasks at the same time. Parallelism requires the support of related hardware, such as pipeline or multi-processing hardware environment.

for example:

Suppose I am a good-looking and talented girl, and I have a standard for choosing a spouse, which is (1) rich (2) good-looking (3) able to lick me (will make me happy and help me relieve stress).

But, looking around, I didn't find boys who meet these three conditions (we call them ABC), the rich ones are not handsome (A), the handsome ones have no money (B), and the ones who can lick are neither handsome nor rich ( C ). But I am a girl who pursues perfection. I just want to find a boy who meets my mate selection criteria. At this time~ I plan to date three boys at the same time, because I am dating them at the same time. time is parallel .

(2) concurrent

Concurrency means that two or more events occur within the same time interval . The concurrency of the operating system means that there are multiple running programs " simultaneously " in the computer system, so it has the ability to process and schedule multiple programs to execute at the same time. The purpose of introducing programs in the operating system is to enable programs to execute concurrently . In a multi-program environment, for a period of time, macroscopically, multiple programs are executing "simultaneously" , but at each moment, only one program can actually , so microscopically these programs are time-sharing executed alternately . The concurrency of the operating system is achieved through time sharing .

Still using the above example as an example:

Because I'm dating three boys at the same time, so for my reputation, I definitely don't want them to meet.

So, I planned a schedule:

monday, wednesday, friday tuesday, thursday Saturday sunday
rich ( A ) Handsome ( B ) Will lick ( C ) give yourself a break

Why the rich can accompany him for three days a week, of course it is because of the power of money!!!

So, I will distribute the weekly time according to the above table. When we adjust the length of time to 1 week, we will find that I accompany them and different people at different times, but when I adjust the length of time When it is adjusted to 1 year and 2 years, we can think that I am " simultaneously " with ABC. The nature involved at this time is what we call " concurrency "

5. Attributes required for process scheduling

(1) Process status

Simply think that the state of the process is mainly these two:

Ready state: The process is ready and can be executed on the CPU at any time

Blocked state: The process cannot be executed on the CPU temporarily

We still demonstrate through the above example:

One day, A said that I need to go on a business trip for a month, so during his one-month business trip, I can’t be with him. At this time, A is in a " blocked state ", but although A is on a business trip, I still have B , C , at this time, I can go to these two boyfriends at any time and stay with them. At this time, these two boyfriends are " ready ".

(2) Process priority

Scheduling between processes is not necessarily fair, some need priority scheduling~~

For example:

Why I will allocate more time to A to accompany me, because A has money, and having money reflects his importance. Of course, A should be given priority, so why only let C accompany me once a week, of course because , he has no other purpose except to make me happy. He has no money and is not handsome, so the priority is naturally the last.

(3) Process context

The context is an "archive record" that describes where the current process has been executed.

When the process leaves the ccpu, it needs to "archive" the intermediate results of the current operation, wait for the next time the process returns to the cpu, and then restore the previous "archive", and continue to execute from the last result.

The so-called context specifically refers to the values ​​of a series of registers inside the cpu during the process running .

Register :

There are many kinds of registers, the most typical function of which is to save the intermediate results of the current process execution, including which instruction is executed.

Archive: When the process leaves the cpu, the value of the register needs to be saved in the PCB context field~~

File reading: When the process returns next time, the cpu restores the value in the PCB to the register.

Example :

One day A told me that you are going to prepare for our trip to Sanya next month, so what I need to prepare at this time is a sexy swimsuit! After that, when I was with B, he told me that my mother will celebrate next month Birthday, remember to prepare a birthday present! Then I thought, buy a mobile phone for my aunt’s birthday present.

Later, A asked me if I was ready, but because I couldn’t remember for a while, I said that the mobile phone had been bought, and B asked me if I was ready? I replied that the sexy swimsuit was already bought. Isn’t it just wearing help at this time? Well, so we need the context of the process to record where our things are going!

(4) Accounting information of the process

Counting how long each process has been executed on the cpu~~
can be used as a reference for scheduling.

For example: I only accompany C one day a week, which caused C to lick me less attentively. At this time, I looked at the number of days I spent with C and found that the number of times I accompanied him was too little, so I needed to spare a few more days a week Stay with him, so that he will lick me hard again~ Of course, this situation is only temporary, after he licks me hard again, I still only stay with him one day a week...

6. Commonly used data structures of the operating system

The operating system often uses a structure such as a doubly linked list to organize the PCB~~

1. To create a process is to create a node of a linked list
2. To destroy a process is to delete the nodes in the linked list
3. To traverse a process is to traverse a doubly linked list

おすすめ

転載: blog.csdn.net/xiaoyubuhuiqiche/article/details/129559266