操作系统进程管理 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:
Chrome
andiTerm
are two processes.- There are many threads in
Chrome
for 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
ready
andrunning
statues can transition to each other. The process inrunning
status will transition toready
while it runs out of allocated CPU time. In converse, aready
process will transition torunning
while it is allocated CPU time. - Transition from
Running
towaiting
is because of lacking neccesary resources, excluding CPU time. A shortage of CPU time will cause a transition fromrunning
toready
.
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)