Operating system Learning notes----process/threading Model----Coursera Course note process/threading model 0. Overview 0.1 Process Model
Multi-Channel program design
Concept of process, Process control block
Process status and transitions, process queues
Process Control----process creation, revocation, blocking, wake-up 、...
0.2 threading Model
Why threading is introduced
The composition of the thread
Implementation of threading mechanism
User-level threads, core-level threads, mixed-mode
1. Basic concept of the process 1.1 multi-channel program design
Allows multiple programs to run in memory at the same time to improve CPU system efficiency
1.2 Concurrent environments and concurrent programs
Concurrency Environment :
Over a period of time, a single processor has two shipments of more than two programs at the same time to start running but not yet closed, and the order is not predetermined.
Concurrent Programs :
Programs that execute in a concurrency environment
1.3 Definition of the process
Definition: Process
A process is a program that has a separate function for a single run activity on a data set, which is an independent unit of system resource allocation and CPU scheduling.
Process is also called task
The process of executing a program
is the abstract that is running the program
Abstraction of the CPU, changing a CPU into multiple virtual CPUs
system resources are allocated in process units, such as memory, file 、...
Each process has a separate address space
The operating system schedules the CPU to the required process
See how many processes are in the current system:
Linux:ps command
1.4 Process Control block PCB
Pcb:process Control Block
Also known as: Process descriptor, process properties
A specialized data structure used by the operating system to manage the control process
Document the various properties of a process, describing the process's dynamic change process
PCB is the only sign of system-aware process existence
The process is one by one corresponding to the PCB
Process table: PCB collection for all processes
The size of the process table is generally fixed
Concurrency: Maximum number of processes supported
what the contents of the PCB contain :
Process description Information
Process Control Information
The resources and usage that you have
CPU Thread Information
Process description Information
The process ID is unique, usually an integer
The name of the process, usually given the executable file name, not unique
UserID (User ID)
Process Group Relationships
Process Control Information
Current status
Priority level
Code Execution Entry Address
The disk address of the program
Run statistics (Execution time, page schedule)
Inter-process synchronization and communication
Queue pointers for processes
Message Queuing pointers for processes
The resources and usage that you have
Status of the virtual address space
List of open files
CPU Field Information
Register value (General Register, program counter PC, program status word PSW, stack pointer)
Pointer to the process also marked
Linux:task_struct
2. Process status and state transitions 2.1 process status
Running state : The process consumes the CPU and runs on the CPU
The process goes into a running state after the process is dispatched from the Ready state
Ready state : The process is ready, allocated to the required resources, and can be run as soon as it is allocated to the CPU
waiting state (or blocking state): The executing process is temporarily unable to run due to certain events (I/O requests, failed request buffers) and the process is blocked.
When the process waits for a condition to be satisfied, enter the ready state to wait for the system to call
Create a State :
Completed the work necessary to create a process, PID, PCB
However, the implementation of the process has not been agreed because of limited resources
termination State : When the process executes, the process enters the terminating state
Can do some data statistics work
Resource Recycling
suspend State (Suspend): Used to adjust the load
The process does not consume memory space, and its process image is exchanged to disk to save
2.2 Process State transitions
- ready -to-run: Scheduler selects a new process to run
run-ready : There are only two cases
1. Running the process ran out of time slices
2. A high-priority process enters a ready state to preempt a running process
- Run --wait: When a process waits for an event to occur
1) Request OS Server
2) Access to resources is not yet available
3) Waiting for I/O results
4) Wait for another process to provide information
wait-ready : The awaited event has occurred
The following two state transitions cannot occur
Wait-and-run: The operating system does not pick from the blocking (waiting) queue when it is scheduled to execute, but from the ready queue.
Ready-to-wait: The ready state is ready, but it is not executed and cannot enter the waiting state.
2.3 Process State Model
Light sleep can receive signals, while deep sleep can not receive signals
2.4 Process Queue
Process queue:
The operating system establishes one or more queues for each type of process
The queue element is a PCB
As the process state changes, its PCB enters another queue from one queue
Waiting state queue, multiple waiting queue conditions are different
A ready-to-state queue can also be multiple
3. Process Control
The Process Control operation completes the transition between the states of the process, which is done by a primitive with a specific function.
Primitive Language (Primitive)
A program that accomplishes a particular function, which is indivisible or non-disruptive. That is, the execution of the primitive must be sequential and not allowed to be interrupted during execution. ----Atomic operation (Atomic)
3.1 Creation of the process
Assigning a unique identity and process control block to a new process
Assigning an address space to a process
Initializing the Process Control block
Set the default value (for example: The state is new, ...). )
Set the appropriate queue pointer
Example: Adding a new process to the Ready queue list
3.2 Revocation of the process
Resources occupied by the recovery process
Close open files, disconnect network connections, reclaim allocated memory
Cancel the PCB of the process
3.3 Process Blocking
A running process that expects a certain time to occur during its run, such as waiting for keyboard input, waiting for disk data to complete, and waiting for other processes to send messages. When the awaited event does not occur, the process itself executes the blocking primitive, making itself a blocking state from the running state.
Wait ();
3.4 Several Process Control operations for UNIX
Fork () Creates a new process by replicating the calling process (parent process), which is the most basic process-building process
EXEC () includes a series of system calls that enable the conversion of process execution code by overwriting the original address space with a new program code
Wait () provides a primary process synchronization operation that allows a process to wait for the end of another process
Exit () to terminate the run of a process
3.5 Unix's fork () implementation
Assigning a free process descriptor to a child process
Proc Structure
Assign to child process uniquely identify PID
Copy the parent process address space one page at a time
Cons: The contents of Unix that are copied from the parent process to the child process are not needed much. Linux uses the write-time replication technology cow speed up the creation process Copy-on-write
Inherit shared resources from the parent process, such as open files and current working directory
Set the status of the child process to ready and insert it into the ready queue
Child process return identifier 0
PID that returns a child process to the parent process
#include <sys/types.h>#include <stdio.h>#include <unistd.h>int main(int argc, char *argv[]){ pid_t pid; pid = fork(); // 创建一个子进程 if (pid < 0) { // 出错 fprintf(stderr, "fork failed\n"); exit(-1); } else if (pid == 0) { // 子进程 execlp("/bin/ls", "ls", NULL); } else { wait(NULL); // 父进程等待子进程结束 printf("Child process complete\n"); exit(0); } return 0;}
The parent Process space Fork () returns the PID number of the child process.
The child Process Space Fork () returns 0
4. In-depth understanding of the process Concept 4.1 discussion on the process
Classification of processes
系统进程:操作系统为管理一些资源而设定的进程,特点是优先级比较高,相对于用户进程优先被调度用户进程前台进程:用户打交道的进程后台进程:操作系统初始化后设定的进程,在后台为客户提供服务CPU密集型进程:需要大量计算的进程,如:游戏、画面渲染等I/O密集型进程:经常需要输入输出、读盘等操作
Process hierarchy
Unix进程家族树:init为根
4.2 The difference between a process and a program
Processes are more accurate in describing concurrency, and programs cannot
The program is static and the process is dynamic
The process has a life cycle, there is birth and death, is short, and the procedure is relatively long
A program can correspond to multiple processes
The process has the ability to create other processes, and the program does not
4.3 Process address space
The operating system assigns an address space to each process
Each process has its own independent address space, and the address of the different address space is the virtual address
4.4 Process Image (image)
Static description of the entire Process execution activity:
Consists of the process address space content, the hardware register content, and the kernel data structure and kernel stack associated with the process.
User-Related: process address space (including code snippets, data segments, heaps and stacks, shared libraries ...) )
Register Related: Value of program counter, instruction register, program status register, stack pointer, universal register, etc.
Kernel-Related:
Static section: PCB and various resource data structures
Dynamic part: Kernel stack (different processes use different kernel stacks after entering the kernel)
4.5 Context Switch
The process of swapping CPU hardware state from one process to another is called context switching.
while the process is running , its hardware status is stored in registers on the CPU
Register: Value of program counter, program status register, stack pointer, universal register, other control register
when the program is not running , the values of these registers are stored in the Process Control block PCB; When the operating system is running a new process, the relevant values in the PCB are sent to the corresponding registers.
5. Introduction of Thread 5.1 threads
three ways to construct a server :
Model |
features |
Multithreading |
There are concurrent, blocking system calls |
Single thread process |
No concurrency, blocking system calls |
Finite state machine |
There are concurrent, non-blocking system calls, interrupts |
Consideration of expenses
Process-related actions:
Create a process
Undo Process
Process Communication
Process switching
The time/space overhead is large, which limits the increase of concurrency.
Low overhead for threads
Create a new thread with less time, the revocation is
Two thread switching takes less time
Threads communicate with each other without calling the kernel (shared memory and files from threads in the same process)
Performance considerations
Multiple threads, some compute, some I/O
Multiple processors
5.2 Basic concepts of threading
Two basic properties of a process
The owner of the resource----process or the owner of the resource
The CPU dispatch unit----thread inherits this property
Threads: A running entity in a process, which is the dispatch unit of the CPU, sometimes referred to as a lightweight process.
Multiple execution sequences (threads) were added to the same process.
5.3 Properties of a thread
Thread:
has identifier ID
Stateful and state transitions
Contexts that need to be protected when not running
Contextual: Registers such as program counters
Has its own stack and stack pointers
Different threads of the same process, sharing the address space and other resources of the process
You can create and revoke another thread
The program starts as a single-threaded process.
6. Implementation of thread mechanism 6.1 thread
User-level threads
Establish line libraries in user space: Provides a set of procedures for managing threads.
Runtime System: Completing the management of threads (operations, thread tables)
Kernel-managed or process, without knowing the existence of a thread
Thread switching does not require kernel state privileges
Multithreaded programming interface, which is provided to the user in line libraries mode, pthread
Pthread_create ()
Pthread_exit ()
Pthread_join ()
Pthread_yield ()
6.2 User-level thread summary
Advantages :
Thread switching fast
Scheduling algorithms are used by program-specific
User-level threads can run on any operating system (only thread libraries is required)
Disadvantages :
The kernel only assigns the processor to a process, and two threads in the same process cannot run concurrently with two CPUs
Most system calls are blocked, so all threads in the process are blocked because of the kernel blocking process
6.3 Core-level threads
The kernel manages the threads and provides API interfaces to the application
Kernel maintains the context of processes and threads
Kernel support required for thread switching
Scheduling on a thread basis:
such as: Windows
Hybrid model
Thread creation completed in user space
Thread scheduling and so on nuclear mentality complete
Multiple user-level threads multiplexing multiple kernel-level threads
7. This chapter highlights the 7.1 process
Concurrency any process can move forward with other processes to execute
A dynamic process is an instance of an executing program
Process is dynamically generated, the dynamic extinction of
Process transitions between three basic states during its life cycle
Independence process is an independent unit of resource allocation
The address space of each process is independent of each other
Interactivity refers to a process that may have a direct or indirect relationship with other processes in the course of execution
Async Each process advances at a relatively independent, unpredictable speed
Process Imaging program + data + stack (user stack, kernel stack) + PCB
7.2 Threads
Multithreaded Application Scenarios
Thread Basic Concepts, properties
Thread Implementation Mechanism
Re-entry program (can be re-entered):
A program that can be called concurrently by multiple processes, having the following properties:
is purely code, i.e. it does not change itself during execution, and the process that invokes it should provide the data area
8. Key Concepts
process, process state and state transitions, process control, Process Control block (PCB), process address space, process context
threads, thread properties, user-level threads, core-level threads, pthreads, reentrant programs, primitives, Web servers
Operating system Learning notes----process/threading Model----Coursera Course notes