What is a process?
The code written by development is called program, so run the developed code. We call it a process.
Understand: when we run a program, the program we run is called a process.
- The process is to apply for a piece of memory space and put data into the memory space. It is the process of applying for data and the smallest resource management unit
- A process is a container for threads
Difference between program and process
- A program is a collection of data and instructions. It is a static concept. It is a pile of code that can be stored in the system for a long time
- Process is the process of program running. It is a dynamic concept. Process has a life cycle, that is, the process will be destroyed with the termination of the program and will not exist permanently in the system
Interaction between processes
- Processes are implemented through TCP / IP ports
What is a thread
- Thread is the smallest unit that the operating system can schedule operations.
- It is included in the process and is the actual operation unit in the process.
- A thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread executes different tasks in parallel.
It is a pipeline of a process, which is only used to execute the program without involving the application of resources. It is the smallest execution unit of the actual executor of the program
Interaction between threads: multiple threads share the same block of memory and interact through the shared memory space
Relationship between process and thread
- Example: we open a chat software, which starts a process; When we open some functions in the software, such as space, scanning, setting, These operations are threads
Therefore, it can be said that “process” includes “thread”, and “thread” is a subset of “process”
A process is a container for threads
Example of factory assembly line:
Process: refers to an application running in the system; Once a program runs, it is a process; Process – the smallest unit of resource allocation.
Thread: the basic unit in which the system allocates processor time resources, or a unit execution flow executed independently within a process. Thread — the smallest unit of program execution.
The process allocates a large part of memory, while the thread only needs to allocate a part of the stack
A program has at least one process, and a process has at least one thread
One thread can create and revoke another thread, and multiple threads in the same process can execute concurrently
Parallel, concurrent, serial
- Concurrency: multiple tasks appear to be performed simultaneously, which is a kind of pseudo parallelism
- Implementation using multichannel technology under single core
- Parallel: multiple tasks are performed simultaneously
- Parallelism can only be realized with multiple cores, otherwise it can only realize concurrency (pseudo parallelism)
- Serial: a program runs completely before running the next process
Three states of task operation
A process changes its running state constantly during its running process
Generally, a running process must have three states: ready state, running state and blocking state.
After the process has allocated all necessary resources except CPU, the program can be executed as long as the CPU is obtained. The state of the process at this time is called ready state,
There may be multiple processes in the ready state in a system. They are usually arranged in a queue, which is called the ready queue
When a process has obtained CPU operation permission and its program is running, it is called running state
In a single core operating system, only one process is running, and in a multi-core operating system, multiple processes are running
When an executing process cannot execute because it waits for an event, it is deprived of CPU operation time by the operating system, which is called blocking state
There are many reasons for blocking, such as waiting for I / O operations, higher priority tasks stealing CPU permissions, etc
Transition between three states of a process
When a process is running, it will constantly switch from one state to another
It can be in ready state, running state or blocked state for many times. The following figure shows the transition diagram of the three states
Ready state ➠ ➠ running state
For a process in ready state, when the process scheduler allocates CPU time slices to it, the process will change from ready state to running state
Running state ➠➠ ready state
When a process in the running state runs out of allocated time slices, it loses the permission to use the CPU, and the running state will change to the ready state again
Operating state ➠➠ blocking state
The running process cannot continue to execute because it encounters I / O operations or is robbed of CPU permissions by higher priority tasks, so it changes from running state to blocking state
Blocking state ➠➠ ready state
A process in a blocked state changes from a blocked state to a ready state if its waiting has been processed
Two methods of task submission
Synchronization refers to the communication mode in which the sender sends data and the receiver sends a response before sending the next datagram
Synchronization means that the operation of two programs is related. One thread needs to wait when blocking, and the other thread runs
Asynchrony refers to the communication mode in which the sender sends data without waiting for the receiver to send back the response, and then sends the next datagram
Asynchrony means that two threads are unrelated and run their own
- ❶ you asked me to go to dinner. When I heard it, I went to dinner with you immediately. If I didn’t hear it, you kept shouting until I told you to hear it
- ❷ calling is like synchronization. Both sides are at the same time. You can’t call another person again
- ❶ you asked me to go to dinner, and then I went to dinner myself. I may leave immediately or later when I get the news
- ❷ sending a message is like asynchronous. After sending a message to one person, you may send a message to another person
What is a process pool?
- Process pool is the application of technology composed of resource process and management process
Why should there be a process pool?
- There will be thousands of tasks to be executed in busy time, and there may be only sporadic tasks in idle time.
- So when thousands of tasks need to be executed, do we need to create thousands of processes?
- First, it takes time to create a process and time to destroy a process.
- Second, even if thousands of processes are started, the operating system can’t let them execute at the same time, which will affect the efficiency of the program.
Therefore, we cannot start or end the process according to the task without limitation. So what are we going to do?
Concept of process pool
- Define a pool and put a fixed number of processes in it. When there is a demand, take the processes in a pool to process tasks
- After processing, the process does not close, but puts the process back into the process pool to continue waiting for tasks
- If there are many tasks to be executed and the number of processes in the pool is not enough, the task must wait for the previous process to return after executing the task, and the idle process can continue to execute.
- In other words, if the number of processes entering the pool is fixed, at most a fixed number of processes are running at the same time
- This will not increase the scheduling difficulty of the operating system, save the time of switching processes, and realize the concurrency effect to a certain extent.
- For pre created idle processes, management processes (such as pools) will distribute work to idle processes for processing.
- The management process is responsible for creating the resource process, handing over the work to the idle resource process for processing, and recycling the resource process that has processed the work.
Interaction between resource process and management process
- How can the management process effectively manage the resource process and assign tasks to the resource process?
- Interact through IPC, semaphore, semaphore, message queue, pipeline, etc.