操作系统进程管理 Process Management
Process and Thread
Process
Process is the basic unit of resource allocation.
Process Control Block describes the basic info and running state of process. The so-called creation and switch of process, are operations to PCB.
In the following pic, four programs create four processes, that are executed concurrently.

Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant. For example, multitasking on a single-core machine.
Parallelism is when tasks literally run at the same time, e.g., on a multicore processor.

Thread
Thread is the basic unit of scheduling.
One process can have multiple threads, they share the resources of process.
For example:
ChromeandiTermare two processes.- There are many threads in
Chromefor different purposes, such as the HTTP request thread, rendering thread, event resposing thread. These threads can be executed concurrently, so it allows rendering the page while requesting HTTP resources.

Diff between Thread and Process
- Resources
- Process: The basic unit of resource allocation.
- Thread: Does not own the resources; Can share the data in the same process.
- Scheduling
- Process: The switch of process will cause the switch of PCB.
- Thread: The basic unit of schedule. The switch of threads in same process does not cause switch of PCB.
- Overhead
- Porcess: The overhead of creation, switch, and close of a process is high. The system needs to allocate memory space, I/O devices, etc.
- Thread: The effort taken to create/cancel a thread is more smaller than that of process.
- Communication
- Process: Use IPC (Inter-process Communication)
- Thread: Read/Write the data of one process to communicate.
Process Status Switching
Status
- Created
- Ready.
- Ready to run.
- Running
- Waiting.
- Hang on, waiting for resources.
- Terminated

Note
- Only
readyandrunningstatues can transition to each other. The process inrunningstatus will transition toreadywhile it runs out of allocated CPU time. In converse, areadyprocess will transition torunningwhile it is allocated CPU time. - Transition from
Runningtowaitingis because of lacking neccesary resources, excluding CPU time. A shortage of CPU time will cause a transition fromrunningtoready.
Process Scheduling Algorithm
A process scheduling algorithm is to schedule the process to be assigned to CPU to execute. Likewise,
The goal of the scheduling algorithm is different for different systems.
Goals of Different Systems
- Batch Processing System
- Batch processing systems have minimal user interaction. In these systems, the goal of the scheduling algorithm is to ensure throughput and turnaround time (the time from submission to completion).
- Interaction System
- An interaction system needs to process numerous user operations and thus requires quick response time.
- Real-time System
- A real-time system requires a request to be responded to within a definite time frame.
- Real-time systems are divided into hard real-time and soft real-time. The former needs to meet absolute deadlines, while the latter can tolerate some delays.

There are six popular process scheduling algorithms.
- First Come, First Served (FCFS)
- Shortest Job First (SJF)
- Shortest Remaining Time Next (SRTN)
- Round Robin (RR)
- Multilevel Queue Scheduling (MLQ)
- Multilevel Feedback Queue Scheduling (MLFQ)
1. First Come First Served (FCFS)
2. Shortest Job First (SJF)
3. Shortest Remaining Time Next (SRTN)
4. Round Robin (RR)
5. Multilevel Queue Scheduling (MLQ)
6. Multilevel Feedback Queue Scheduling (MLFQ)
Process Synchronization
- Critical Section (临界区段)
- Synchronization and Mutual Exclusion
- Semaphore
- Monitors (管程)
Typical Synchronization Problem
Dining Philosophers Problem
五个哲学家围着一张圆桌,每个哲学家面前放着食物。哲学家的生活有两种交替活动:吃饭以及思考。当一个哲学家吃饭时,需要先拿起自己左右两边的两根筷子,并且一次只能拿起一根筷子。
Background:
Five philosophers are sitting around a round table, there is food in front of each philosopher. Philosopher needs to take both of the chopsticks to start eating, and they can take up only one chopstick at a time.
- The problem is that, while one philosopher took one chopstick

Dining Philosophers Problem
- Several philosophers are at the same table and ready to eat.
- There is one chopstick on their right-hand and left-hand side.
- Each person needs to take both of chopsticks to start eating.
- The problem is that, while one philosopher took one chopstick
Consumer-Producer Problem
Todo
Inter-Process Communication
- Pipe
- FIFO Pipe
- Message Queue
- Shared Storage
- Socket