Summary of operating system concepts and knowledge points -> process management

Chapter One Operating System Overview

1. Operating system definition: Operating System (Operating System) enables one of the computer systemssystem software, Is a collection of some program modules, it can be as effective and reasonable as possibleOrganize and manage computer software and hardware resources, Reasonably organize the work flow of the computer, control the execution of the program and provide users with various service functions, so that the computer system can run efficiently. (There is no widely accepted standard definition)
2. Explanation of some terms in the operating system

noun Explanation
ROM(Read-Only Memory) Read-only memory
RAM(Random Access Memory) Random access device, also called main memory, is internal memory that directly exchanges data with CPU
Processes A program loaded into memory and executed is called a process
User mode When the system executes user applications, the system is in user mode
Kernel mode When a user requests operating system services, the system must enter kernel mode from user mode to satisfy the request
Program counter Specify the next command to be executed
System call Provide operating system service interface

Insert picture description here

There are interrupts, sharing, concurrency, competition, etc. in the operating system
1. Interrupt handling process

step content
(1) The device sends an interrupt signal to the processor
(2) The processor responds to the interrupt after processing the current instruction with very short delay
(3) The processor detects the interrupt after processing the current instruction, determines the source of the interrupt, and sends a confirmation interrupt signal to the device that sent the interrupt. The confirmation signal enables the device to restore the interrupt device to the normal state
(4) The processor starts to prepare for the software to handle the interrupt: save the execution program context of the interrupt point, including the program status word PSW, the next instruction position of the program counter PC, and the value of some registers
(5) The processor queries the interrupt vector table according to the interrupt source, obtains the entry address of the handler associated with the interrupt, and sets the PC to this address. The processor starts a new instruction cycle and transfers control to the interrupt handler
(6) The interrupt handler starts to work, check the I/O related device information
(7) When the interrupt processing ends, the processor detects the interrupt return instruction, and the context of the interrupted program is restored from the system stack. The processor state is restored to its original state
(8) PSW and PC are restored to the values ​​before the interrupt, the processor starts a new instruction cycle, and the interrupt processing ends

Insert picture description here

Chapter 2: Process

Process (process) definition: a process is a certain independent functionProgram or program segmentOne time on a set of dataDynamic execution
Process = Program + Execution
Process includes program, data and process control block
2.1 Process status

Process status Description
New Process is being created
Running Instruction is executing
Waiting (wating) The process waits for an event to occur
Ready Process waiting to allocate processor
Terminated The process has completed execution

Only one process can be running on the processor at a time;
however, multiple processes can be in a waiting or ready state.

Insert picture description here
2.1.3 Process Control Block
PCB includes process control status, process counter, CPU counter, etc.
2.1.4 Thread
A process has at least one thread, a process can run multiple threads, and multiple threads can share data
2.2 Process scheduling
When there are multiple processes, the process scheduler selects an available process to execute on the CPU, and the rest needs to wait for the CPU to be idle and reschedule.
2.2.1 Scheduling queue When a
process enters the system, it will be added to the job queue (job queue), which includes all processes in the system.
2.2.2 Context switch
Interrupt (intirrput) causes the CPU to change from executing the current task to executing the kernel program.
Switch the CPU to another process needs to save and restore the state of the current process to another process state context switch (context Switch)
2.3 Process Creation
2.3.1 processes running
most operating systems to identify process uses a unique process identifier ( process identifier,pid)

3 process synchronization

Insert picture description here

3.1 Explanation of terms on critical section issues

name Explanation
同步(process synchronization) There are certain types of events that occur in multiple processes in the systemTemporal relationship, Need to cooperate with each other to completeA task
Mutually exclusive Because all processes share resources, some resources need to be used exclusively, so the competition between processes to use these resources becomes mutually exclusive
Race condition Multiple processes concurrently access and manipulate the same data and the execution result is related to the specific access sequence
Entry area (rentry section) Before entering the critical area, each process requests permission. The code area that implements this request is called the entry area
Critical section Public variables may be modified when the process reaches this area. Features: only one process is allowed to execute in the critical section
Exit entry The process enters the exit area after executing the critical section code
Remaining area (remainder section) The area where the process executes other code

3.2 Requirements to be met by the solution to the critical section problem
① Mutual exclusion: If the process Pi is executed in the critical section, then other processes cannot be executed in the critical section.
②Synchronization (progress): If there is no process executing in the critical area, and there are processes that need to enter the critical area, then only those processes that are not executing in the remaining area can participate in the selection to determine who can enter the critical area next time, and this choice Cannot be postponed indefinitely
③Bounded wating: The time that the process waits to enter the critical zone has a range, not infinite.
3.3 Methods to Solve the Problem of Critical Section
① Preemptive kernel (preemptive kerbel): Allows the process in kernel mode to be preempted, and the process in kernel mode will run until it exits the kernel mode, blocking or voluntarily giving up CPU control.
②Nonpreemptive kernel (nopreemptive kerbel): the process in the kernel mode is not allowed to be preempted
3.4.1 Peterson solution (software method)
Insert picture description here
explanation: set two processes to share data items
int turn;
boolean flag[2];
j=1 -i;
two processes P0 and P1
process

My understanding Official explanation
① Initially set flag[i], that is, flag[0] is true, because turn is equal to j is equal to 1-i; so turn==1; it means that flag[0] wants to apply for entering the critical area. ②At this time, if flag[j ] That is, if flag[1] is equal to true and turn is equal to 1, then the statement will always be idling in the while loop.Note that there is a semicolon after the while loop and the judge condition③直到P1退出临界区并执行flag[j]=false后,P0才能进入临界区执行 考虑进程P0,一旦它设置flag[0]=true,则P1不能进入临界区。如果P1已经进入临界区,那么flag[1]=true,P0被阻塞不能进入临界区。另一方面,互相阻塞也避免了。假设P0在while里被阻塞了,表示flag[1]为true且turn=1,则此时P1可以执行

缺点:1,只适用于两个线程 2,现代计算机体系结构不支持
3.4.2硬件同步(硬件方法)
方法一 —>中断屏蔽方法
进入临界区前执行"关中断"指令
离开临界区后执行"开中断"指令
Insert picture description here优点:简单有效
缺点:不适用于多处理器(耗时)
方法二–>测试并设置指令
前提:test_and_set()时原子的(执行不可中断)
Insert picture description here
为每个临界资源设置一个lock,初始为false
解释:当一个临界资源未被使用时,lock等于false,程序可以执行while下面的语句,当lock等于true时,test_and_set(&lock)函数返回值为true,程序在while中空转
3.4.3互斥锁(mutex lock)(软件方法)
互斥锁:一个进程进入临界区时得到锁,在它退出临界区时释放锁,函数acquire()获得锁,函数release()释放锁,每个互斥锁都有一个布尔变量available,它表示锁是否可用
互斥锁实现要求qcquire()和release()的调用必须是原子地执行
Insert picture description here
进程互斥访问示例
Insert picture description here

缺点:忙等待(busy wating),一个进程在临界区中其他任何进程都必须等待
3.6信号量(semaphore)
信号量S是一个整型变量,它的初始化通过两个标准原子操作wait()和signal(),wait()也叫P操作,signal()也叫V操作
wait()函数体定义

wati(S)
{
    
    
while(S<=0);   //忙等待
S--}

signal()函数体定义

signal()
{
    
    
S++;
}

缺点:不满足让权等待(当进程不能进入自己的临界区时,应立即释放处理机,以免进程陷入“忙等”。)
3.4.5信号量的使用和实现
信号量使用改进(重新定义)
Insert picture description here

typedef struct
{
    
    
int value;
struct process *list;
}semaphore
//每一个信号量都有一个整数value和一个进程链表list,当一个进程必须等待信号量时,就被添加带进程链表,操作signal()从等待进程链表上取走进程,并加以唤醒
wait(semaphore *S)
{
    
    
S->value--;
if(S->value<0)
{
    
    
add this process to S->list;
block();
}
}

signal(semaphore *S)
{
    
    
S->value++;
if(S->value<=0)
{
    
    
remove a process P from S->list;
wakeup(P);
}
}

3.4.6死锁与饥饿
死锁:两个或多个进程无限等待一个事件,而该事件这些等待进程之一来完成,这些进程称为死锁
3.5经典同步问题
3.5.1生产者-消费者问题
3.5.2读者-作者问题
3.5.3哲学家就餐问题
3.6管程
管程定义:指关于贡献资源的数据及其在其上操作的一组过程或共享数据结构及其规定的所有操作(没听懂)
管程能够保证在任何时候最多只有一个线程(进程)操作管程中的代码

4进程调度

4.1 CPU-I/O执行周期
进程执行:包括周期进行CPU执行和I/O等待,进程在这两个状态下不断交替,进程执行从CPU执行开始,之后I/O执行,;接着另一个CPU执行,接着另一个I/O执行;
4.1.2 CPU调度程序
当CPU空闲时,操作系统就从队列中选择一个进程来执行并尾气分配CPU。
4.1.3抢占调度
需要CPU调度的四种情况
1,一个进程从运行态到等待状态
2,一个进程从运行状态到就绪状态
3,一个进程从等待状态到就绪状态
4,一个进程终止时
如果调度只发生在第1或4种情况,则调度是非抢占的,否则是抢占的,在非抢占调度下,一个进程在分配到CPU时,会一直运行直到终止或切换状态
4.1.4调度程序
调度程序功能:
1,切换上下文
2,切换到用户模式
3,跳转到用户程序的合适位置,以便重新启动程序

调度程序应尽可能快,调度程序停止一个进程而启动另一个进程所需要的时间称为调度延迟(dispatch latency)
4.2调度准则

名称 解释
CPU 使用率 应使 CPU 尽可能地忙碌。从概念上讲,CPU 使用率从 0% 到 100%。对于一个实际系统,它的范围应从 40%(轻负荷系统)到 90%(重负荷系统)。
吞吐量 如果 CPU 忙于执行进程,那么工作就在完成。一种测量工作的方法称为吞吐量,它是在一个时间单元内进程完成的数量。对于长进程,吞吐量可能为每小时一个进程;对于短进程,吞吐量可能为每秒十个进程。
周转时间 From the perspective of a particular process, an important criterion is how long it takes to run the process. The time period from process submission to process completion is called turnaround time. Turnaround time is the sum of all time periods, including waiting to enter the memory, waiting in the ready queue, executing on the CPU, and I/O execution.
waiting time The CPU scheduling algorithm does not affect the time for the process to run and perform I/O, it only affects the time required for the process to wait in the ready queue. The waiting time is the sum of the time spent waiting in the ready queue.
Response time For interactive systems, turnaround time is not the best criterion. Usually, a process can produce output quite early, and continue to calculate new results while outputting previous results to the user. Therefore, the other time is the time from submitting the request to generating the first response. This time is called response time, which is the time required to start the response, not the time required to output the response. The turnaround time is usually limited by the speed of the output device.

4.3 Scheduling algorithm
4.3.1 First-come, first-served scheduling
First-come, first-served scheduling (first-Come First_Served, FCFS)
processes that request the CPU first are allocated to the CPU first, and when a process enters the ready queue, its PCB will be linked to the queue At the end, when the CPU is idle, it will be assigned to the process at the head of the queue, and this running process removes the
FSFS strategy from the queue Advantages: simple, easy to understand
FSFS strategy Disadvantages: long average waiting time

Guess you like

Origin blog.csdn.net/qq_43475285/article/details/109188904