process, thread

The operating system is a management software that provides a stable operating environment for various applications and manages various hardware devices.​ 

process

An operating system consists of a kernel and supporting applications. Theprocess is one of the many key concepts in the operating system kernel. In layman’s terms, a process is a program that has already started running.

Each process consumes a certain amount of system resources (CPU, GPU, memory...). The operating system allocates resources according to processes, that is:The process is the basic unit of system allocation of resources.

PCB

There is aspecial structure in the operating system (the operating system kernel is implemented in C/C++) to describe the attributes of the process. This structure is collectively called "Process Control Block" also called PCB (Process Control Block) (the pcb here is not the pcb board in hardware) ).

A process can be represented by one or more PCBs, and a data structure similar to a doubly linked list is used in the operating system to manage and organize multiple PCBs.

  • The creation of the process is to create the PCB and insert it into the linked list;
  • Destroying the process is to delete the PCB from the linked list and release it;
  • Displaying the process list is equivalent to traversing the linked list.

pid

pid is the identity of the process. Each process will have a pid, and the system will automatically assign a unique PID to the process as soon as the program is run. After the process is terminated, the PID is recycled by the system and may continue to be assigned to newly running programs.

Memory pointer (a set of properties)

The memory pointer describes what memory resources the process holds.

Each process is allocated a piece of memory when it is created.

  • The process requires a special memory to store the instructions that need to be executed. For example, when we open an exe file on the computer, the system reads the file content and loads it into the memory, and then the CPU retrieves and executes these instructions from the memory;
  • It also stores some temporary data generated during runtime.

file descriptor table

The file descriptor table is a data structure similar to a sequence table. Describes what kind of hard disk resources the process holds.

Each process has its own file descriptor table. Through the file descriptor table, you can know which files are associated with the current program and can operate those files.


PCB also references some attributes to support the operating system in implementing process scheduling.

process status

Ready state: The process is always ready to be executed on the CPU, that is, the process is called by the CPU at any time, and it can come and go as soon as it is called.

specific situation:

  • The process is executing on the CPU;
  • Although the process is not executing on the CPU at this time, it is always ready to be executed on the CPU.

 

Blocking state: When certain execution conditions in the process are not met, the process cannot participate in the scheduling execution of the CPU. For example, the program is waiting for user input.

The process has a total of 5 states, namelycreation, ready, running (execution), terminated, and blocked.

 

process priority

When multiple processes are scheduled in the operating system, they are not treated equally. Some processes will be given higher priority by the operating system. For example: when you play games, the priority of the game is higher than QQ.

  • Each process has a corresponding priority,the priority determines when it runs and how much CPU time it receives.
  • The priority of the process can be changed dynamically. High-priority processes run first, and have the same priority. ’s processes run in turn according to time slices .

 

context

The process needs to save the scene before leaving the CPU and record the status of various registers in the current CPU in the memory. When the process returns to the CPU for execution next time, the memory information will be restored and execution will continue from the last time. To put it simply, it is archiving and loading.

 

Accounting information

Because the operating system allocates different hardware resources to processes of different priorities through the process priority mechanism. This may cause one or several processes to occupy too many resources, causing one or several processes to cause exceptions because they cannot allocate resources.

The accounting information records the CPU status held by the current process, and resources are dynamically allocated based on this information.


virtual address space

Early computers did not have virtual memory. At that time, the operating system allocated physical memory to processes.

1b801f1995ab4c1dbd73c29c796be2ff.png

There will be some problems at this time. If process A crosses the boundary when accessing the memory, it may change the data in the memory of process B, causing both processes A and B to become abnormal.

Later, the operating system abstracted the memory and introduced the concept of "virtual address space". At this time, the operating system allocated not the real physical memory address but the allocated virtual memory address.

0c8a501778464c12a26d8da5bc56970d.png

 At this time, because the process cannot obtain the real physical memory address, the above-mentioned situation of incorrectly modifying the memory of other processes will not occur.

But at this time, a new problem was introduced: at this time, the processes were completely isolated from each other. In the past, if two processes wanted to communicate, they could directly make modifications in each other's memory. However, the introduction of After obtaining the virtual memory, because the real memory address cannot be obtained, it cannot be modified in the same way.

At this time, communication between processes needs to be implemented with the help of an additional public memory space: for example, A buys a computer online, and then the courier delivers it to Cainiao Station, and then A goes to Cainiao Station to get the computer.

In fact, there are many communication methods here, but the overall core idea iswith the help of an additional public memory space.


thread

Here, a factory is used to illustrate the relationship between threads and processes:

At this time, A is the owner of a factory. Now he has a factory, but there is only one assembly line in the factory. A can earn 100 yuan a day.

be105f1789004132b7ca436c255be493.png

At this time, A wants to make 200 yuan a day, so he now has two solutions. The first is to build a new factory with only one assembly line; the second is to add an assembly line to the old factory.

5b036866bad64c998f65491810a99461.png

2b68be1911204b0581b68ce819892b73.png

The pipeline here is equivalent to a thread; and the factory is equivalent to a process.

In JAVAencourage the use of multi-threadingbut notdiscourage the use of multiple processes to implement concurrent programming.

As mentioned aboveThe process is the basic unit for the system to allocate memory, and allocating memory is not easy for the system, so it leads to: The process is too heavy and inefficient.

  • Creating a process takes more time;
  • Destroying a process also consumes more time;
  • Scheduling also takes a lot of time.

Threads are relatively lightweight and are also called "lightweight processes". Just like the example given above, building a new factory will definitely not save time and effort by adding a new assembly line.

Althoughthreads are created, destroyed, and scheduled fasterbutthreads cannot leave the process alone. There is, that is, the process contains threads. It can contain multiple threads but must contain at least one thread.

Threads are responsible for executing code. If necessary, multiple threads can be created, and each thread can independently execute a piece of code, thereby achieving the effect of concurrent programming.

Threads are the basic unit of scheduling execution.

The PCBs and threads mentioned above have a one-to-one correspondence, which means that a process can have multiple PCBs and each PCB corresponds to a thread. However, pid, memory pointer, and file descriptor table are common between the same process. That is to say, except for the first thread, the remaining threads do not need to re-apply for resources when they are created.

Andthe more threads the better, because the size of the factory is certain, if If there are too many threads, the efficiency will not only not be improved but may also be reduced:

ec5404c6880640edad88dc630feaf324.png

Characteristics of threads

  • Each thread can be scheduled and executed independently on the CPU;
  • Multiple threads in the same process share the same memory space and file resources.

 

Summarize:

The process contains threads (the number of threads is at least 1)=> Each PCB corresponds to a thread=> Each thread has its ownstate, context, priority level, accounting information=> Each thread can be independently scheduled and executed on the CPU=> Multiple threads in the same process share the same memory space and file resources=> Creating a thread does not require re-application for resources => Threads are more lightweight

  • Process is the basic unit of resource allocation;
  • Threads are the basic unit of scheduling execution.

Processes are independent of each other. If one process hangs, it will not affect other processes; and ifA thread hangs, if Failure to handle it properly will affect other threads.

 

 

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/133758405