Operating System Principles

directory

One level

The secondary directory

Three directories

The basic concept

Computer hardware system

composition

Central processing unit

The CENTRAL processing Unit (CPU) is the computing Core and Control Unit of a computer.

  • Arithmetic logic unit: one or more arithmetic units
  • Register components: including general purpose registers, control and status registers, and Cache
  • Control unit: an internal bus for data, control, and state of connection between components; The part responsible for decoding instructions, sending out control signals for the operation to be performed for each instruction, and realizing data transmission, etc
Processor and register

Main memory

DRAM

Peripheral equipment
Device type
  • Input devices

    Keyboard mouse

  • Output devices

    display

  • The storage device

    1. Hard disks such as solid state disks, mechanical disks, and USB flash drives

    2. drive

  • Network communication equipment

    The network card etc.

Equipment control mode
  • Polling mode: BUSY CPU control + data exchange
  • Interrupt mode: CPU start/interrupt + data exchange
  • DMA mode: CPU start/interrupt, DMA data exchange
The bus
Introduction to the

Bus is the public communication trunk line that transmits information between various functional parts of the computer. It is the public channel for CPU, memory, input and output devices to transmit information

Each part of the computer is connected through the bus, and peripheral devices are connected with the bus through the corresponding interface circuit, thus forming a computer hardware system

A bus consists of a set of control lines, a set of data lines, and a set of address lines, depending on the kind of information to be transmitted

Bus type

Internal bus: used to connect the CPU chip internal components

System bus: used to connect CPU, memory and various I/O modules and other major components

The system bus also has tiers, as shown in the figure below

Communication bus: Used for communication between computer systems

Von Neumann structure

Stored program computer

Von Neumann et al. summarized and explicitly proposed in 1946, called the von Neumann computer model stored program computer in the architecture of the main characteristics

  • The control flow is generated by instruction flow with the operation unit as the center
  • Using the principle of stored program, data flow is organized for main memory
  • Main memory is addressable, linearly addressed space
  • The instruction consists of opcodes and address codes
  • Data is encoded in binary
structure

The organizational level of storage

The memory includes the cache in the CPU, the main memory, the hard disk in the peripheral device and the remote memory in the network

Computer software system

Different layers of software development

  • Computer hardware systems: Machine languages
  • Resource management for operating systems: Machine language + generalized instructions (extended hardware resource management)
  • Operating system file system: Machine language + system call (extended information resource management)
  • Database management system: + database language (expanded with more powerful information resource management)
  • Language handlers: Problem oriented languages

The execution of a computer program

The operating system

Operating System (OS

OS is the most basic system software of a computer system. It manages hardware and software resources, controls program execution, improves human-machine interface, reasonably organizes computer workflow, and provides a good operating environment for users to use computers

In short, an operating system is a collection of system programs that facilitate users, manage and control computer hardware and software resources

  • From the user’s point of view, OS manages various resources of the computer system, expands the function of the hardware, and controls the execution of the program
  • From the perspective of human-computer interaction, OS is the interface between the user and the machine, providing a good man-machine interface, convenient for users to use the computer, in the whole computer system has a link between the preceding and the following status
  • In terms of system structure, OS is a large software system with complex functions and huge system. It adopts hierarchical and modular program structure

Components of an operating system

  • Process scheduling subsystem
  • Process communication subsystem
  • Memory management subsystem
  • Equipment management subsystem
  • File management subsystem
  • Network communication subsystem
  • Job control subsystem

type

Operation control mode

  • Multichannel batch operating system, offline control mode
  • Time-sharing operating system, interactive control mode
  • Real time operating system

Application field

  • Server operating system, parallel operating system
  • Network operating system, distributed operating system
  • PC operating system, mobile operating system
  • Embedded operating system, sensor operating system

Resource management

Computer resources contain

  • Hardware resources Processor, memory, peripherals
  • Information resources data, procedures
Mask the low-level details of resource usage

Driver: The lowest level part that directly controls and monitors various hardware (or file) resources

The job is to hide the details of the underlying hardware and provide an abstract, generic interface to other parts such as printing a piece of text or a file, without knowing the details of how the file information is stored on the hard disk, and without knowing the specific printer type and control details

Resource Sharing Mode
  • Exclusive mode of use
  • Concurrent usage mode
Resource Allocation Policy
  • Static allocation
  • Dynamic allocation
  • Resource Preemption mode

System control

The mismatch between CPU speed and I/O speed is striking. Only by allowing multiple programs to enter the memory at the same time to compete for the CPU, can the CPU and peripheral devices fully parallel, thus improving the efficiency of the computer system

Realization of multi – channel program system
  • Create an administrative entity for programs that enter memory for execution: processes

  • The OS should be able to manage and control the execution of process programs

  • The OS coordinates and manages the use of resources between processes

    • Management and scheduling of processors
    • Management and scheduling of main storage
    • Manage and schedule other resources
  • How to use resources: Invoke service routines provided by the operating system (how to get stuck in the operating system)

  • How to reuse CPU: scheduler (let other programs run when CPU is idle)

  • How to make cpus fully parallel with I/O devices: Device Controllers and channels (dedicated I/O processors)

  • How to make a running program give up CPU: Interrupt (interrupt a running program, introduce OS processing)

The control mode
Offline operation control mode
  1. OS: Provides job description language
  2. User: write the operation manual, determine the operation control steps, and submit it together with the program data
  3. Operator: Input jobs through the console
  4. OS: Automatically controls the execution of jobs through job control programs

Example: batch OS job control mode, UNIX shell program, DOS bat file

Online job control mode
  1. Computer: Provide terminal (keyboard/monitor)
  2. User: Log in to the system
  3. OS: Provides command interpreters
  4. User: Input commands online to directly control the execution of job steps

Example: the interactive control mode of time-sharing OS

Command interpreter

Either offline control or online control requires a command interpreter

Command interpreter: Accept and execute a user-requested processing command for a job. When a new batch job is started or a new interactive user logs into the system, the system automatically executes the command interpreter, which is responsible for reading the control card or command line, interpreting it, and executing it

Conversational language: a programmable command interpreter

The process
  1. OS starts command interpreter, outputs command prompt, waits (keyboard interrupt/mouse click/multi-channel recognition)
  2. Request an interrupt whenever the user enters a command (a command buffer exists temporarily) and presses Enter to wrap
  3. When the CPU responds, it gives control to the command interpreter, reads the contents of the command buffer, analyzes the command, accepts the parameters, and executes the processing code
  4. After the foreground command is executed, it outputs the command prompt again and waits for the next command
  5. After background command processing starts, the next command can be received

Foreground commands: Enter commands only after they are executed

Background commands: After a command is started, the next command can be started without affecting the execution of the command in the background

The system interface

The program interface of the operating system – system call

A process implemented by an operating system to perform a specific function; Provides an interface to the operating system for all running programs

The operating system provides interfaces for operations that applications call when they need to perform them

implementation
  • Write a system call handler

    The operating system provides these system call handlers ahead of time

  • Design a system call entry address table, each entry address points to a system call handler, and contains the number of system call parameters

    Through software initiation, the system looks up the system call entry address table and finds the corresponding system call handler

  • Trapped processing requires a field preserve to preserve the processor scene when a system call occurs

    The current state of the processor is stored before the system call is called so that the processor scene can be restored after the call

The system structure

The structure design

OS component kernel, process, thread, pipe, etc

Kernel design is the most complex part of OS design

Modular design concept, hierarchy, virtualization

The kernel

Single kernel: A hybrid form of a kernel, widely used since the 1960s; Such as Unix/Linux, and Windows(which boasts a hybrid CS architecture)

Microkernel: Since the 1980s, most OS research has focused on the separation of structural and functional components

Hybrid kernel: a compromise between microkernel and single kernel, with more components running in a core mentality for faster execution

External kernel: Minimize the software abstraction of the kernel and the messaging mechanism of the traditional microkernel, allowing developers to focus on hardware abstraction, partially used by embedded systems

Processor management

Processor and register

Processor unit

Instruction decoder ID: responsible for the execution of specific interpretation instructions

Instruction register IR: Is responsible for the storage of instructions

Program counter PC: points to the address where the instruction is to be executed at the next hop

After the arithmetic logic unit completes the calculation, it summarizes the contents into the Flag register

MAR, MDR: Used to access memory data

User program visibility register

Programmers can reduce the number of access to the main memory, improve the efficiency of instruction execution

All programs are available, including applications and system programs

  • Data register: Also called general purpose register
  • Address register: index, stack pointer, segment address, etc

Control and status registers

Used to control the operation of the processor; Used primarily by privileged operating system programs to control program execution

  • Program counter PC: Stores the address of the instruction to be fetched
  • Instruction register IR: Stores recently used instructions
  • CC: a bit set by the CPU for the result of an instruction operation, indicating positive/negative/zero/overflow, etc
  • Flag bit: interrupt bit, interrupt permit bit, interrupt mask bit, processor mode bit, memory protection bit,… , etc.
The program status word PSW

PSW is the concept of an operating system, which records the dynamic information of the current program running, usually including:

  • Program counter, instruction register, condition code
  • Interrupt word, interrupt permit/disable, interrupt masking, processor mode, memory protection, debugging control

The PSW is also a register in the computer system

  • A set of control and status registers is usually set up
  • You can also specify a PSW register

Instruction and processor

Machine instructions

Machine instructions are the basic commands executed by the computer system and the basic unit of execution by the CPU

An instruction consists of one or more bytes, including an opcode field, one or more operand address fields, and some status words and characteristic codes that represent the state of the machine

Instructions to complete all kinds of arithmetic logic operations, data transmission, control flow jump

Instruction execution process

The CPU takes out the instruction according to the PC (program counter), puts it into IR, decodes the instruction, and then issues various control commands to execute the microoperation series, thus completing the execution of an instruction

One instruction is executed as follows

  1. Fetch: Fetch instructions from memory or cache to IR according to PC
  2. Decode: Interprets instructions in IR to determine their execution behavior
  3. Execution: connect to CPU component, perform operation, produce result and write back, at the same time set operation conclusion flag in CC; Jump instructions operate on PC, other instructions increment THE VALUE of PC
Instruction execution cycle and instruction pipeline

Privileged and non-privileged directives

Not all machine instructions can be used by user programs. Special instructions related to the core resources of the computer are protected. Instructions related to the core resources can only be used by operating system programs

Such as: start I/O instructions, PC instructions, and so on

Suppose program A wants to output 123, while program B wants to output 456. If the I/O control is handed over to the computer, the output is likely to be 142536, which does not meet the requirements

So instructions are classified as privileged and non-privileged

  • Privileged directives: Directives that can only be used by the operating system kernel
  • Nonprivileged instruction: an instruction that can be used by all programs
Processor mode

The computer implements privileged instruction management by setting processor mode

Generally, there are four operating modes: 0 OS kernel, 1 system call, 2 shared library programs, and 3 user programs

In general, modern operating systems use only two modes, 0 and 3, corresponding to kernel mode and user mode

Processor mode switch

Including “user mode → kernel mode” and “kernel mode → user mode” conversion

Events such as interrupts, exceptions, or system exceptions cause user programs to switch to the OS kernel, triggering user mode → kernel mode

  • The program requests operating system services
  • An exception occurred while the program was running
  • Interrupts occur and respond to program execution

After the OS kernel processing is complete, the call interrupt return instruction (such as Intel IRET) is triggered: kernel mode → user mode

interrupt

concept

Interrupt refers to the process of temporarily suspending the running of the current program on the CPU when an event needs to be dealt with urgently in the process of program execution, switching to the execution of the corresponding event handler, and then returning to the place where the original program was interrupted or scheduling the execution of other programs after the processing is completed

Operating systems are “interrupt-driven”; In other words, interrupts are the only way to activate the operating system

For example, when a user program is running, it can only be switched by interrupting

Interrupt has broad sense and narrow sense points, the interrupt refers to the broad sense interrupt

Interrupts, exceptions, and system exceptions

  • In the narrow sense, interrupt refers to interrupt events outside the processor, that is, interrupt events irrelevant to the current running instructions, such as I/O interrupt, clock interrupt, external signal interrupt, etc

  • Exception refers to the interrupt event caused by the current running instruction, such as address exception, arithmetic exception, processor hardware failure, etc

  • System exception refers to the interrupt event caused by system call triggered by executing the trap instruction, such as request device, request I/O, create process, etc

    The system exception has nothing to do with hardware

The interrupt source

Processor hardware failure interrupt event

It is caused by hardware failure of processor, internal memory, bus, etc

The handling principles are: protect the site, stop the equipment, stop the CPU, report to the operator, and wait for human intervention

A procedural interrupt event

Caused by the processor executing machine instructions

  • Arithmetic exceptions, such as zero divisor and operand overflow, are handled simply and reported to the user. It can also be handled by user – written intermittent procedures
  • Illegal instruction, user mode privilege instruction, address out of bounds, and illegal access instruction Exception: The process is terminated
  • Termination process instruction: Terminates a process
  • If the virtual address is abnormal, execute the command again after adjusting the memory
Voluntary interruption events

Processor execution is caused by an instruction to request OS services. In operating systems, it is commonly referred to as a system call

  • Request allocation peripherals, request I/O, and so on
  • The processing flow is as follows: get stuck in the OS, protect the site, check the address of the entrance according to the function number, and jump to the specific processing program
I/O interruption event

Interrupt event from peripherals reporting I/O status

  • I/O completed: Adjusts the process status and releases the waiting process
  • I/O error: Wait for human intervention
  • I/O exception: Wait for manual intervention
External interrupt event

An interrupt event caused by a signal from a peripheral device

  • Clock interrupt, interval clock interrupt: time and time slice processing
  • Device registration and termination interrupt: Adjust the device table
  • Keyboard/mouse signal interruption: respond accordingly to the signal
  • Shutdown/restart interrupt: write back files, stop device and CPU

The interrupt system

Interrupt system is the system which responds and deals with interrupt in computer system, including hardware subsystem and software subsystem

The interrupt response is done by the hardware subsystem

Interrupt reason software subsystem complete

Interrupt response processing and instruction execution cycles

Add a microoperation at the end of the instruction execution cycle in response to an interrupt

Interrupt device

A hardware device in a computer system that detects and responds to interrupts/anomalies is called an interrupt device. Due to the diversity of interrupt sources, there are various interrupt devices implemented by hardware to handle different types of interrupts. These interrupt devices vary from computer to computer, and usually include:

  • Out-of-processor interrupts: Detected and responded to by the interrupt controller
  • Exceptions within the processor: detected and responded to by the control logic and implementation circuit of the instruction, the corresponding mechanism is called trap
  • System exception requesting OS services: triggered directly when processor execution falls into instruction, the corresponding mechanism is called system trap
Interrupt controller

A control unit in a CPU that includes interrupt control logic lines and interrupt registers

  1. The external device sends an interrupt request IRQ to it and sets the interrupt that has occurred in the interrupt register
  2. At the end of instruction processing, the interrupt register will be checked. If there is an unmasked interrupt, the order of operations in the processor will be changed to lead to the interrupt handler in the operating system

This process is asynchronous, the register is set and the interrupt is made by looking at the register at the end of this instruction

Traps and system traps

Traps and system traps: The logic of instructions and part of the implementation circuit

  • If an exception occurs during command execution, the system turns to the exception handler of the operating system based on the exception
  • After the virtual address exception occurs, the instruction needs to be re-executed, often bypading the trap of setting the page exception handler independently
  • After executing the trap instruction, the trap processing is passed, triggering the system trap and activating the system call handler

This process is synchronous, an interruption that occurs during the execution of an instruction

Interrupt response procedure
  1. An interrupt source is found and an interrupt request is made

    1. Found the interrupt recorded in the interrupt register
    2. Decide whether these interrupts should be masked
    3. When there are multiple interrupt sources to respond to, one is selected according to the specified priority
  2. Interrupts execution of the current program

    Saves the PSW/PC of the current program to the core stack

  3. Switch to the operating system’s interrupt handler

Interrupt handler

An operating system control program for handling interrupt events. Its main task is to handle the interrupt events and restore normal operations

Interrupt handling procedure
  • Protects the state of the processor that is not protected by hardware

  • The interrupt source can be identified by analyzing the PSW interrupt code field of the interrupted process

  • Handle the occurrence of interrupt events separately

  • Return to normal operation

    Case 1: For some interrupts, you can directly return to the process that was just interrupted. Case 2: For other interrupts, you need to interrupt the running of the current process, adjust the process queue, start the process scheduling, select the next process to execute, and resume its execution

Multiple interrupt response and handling
Interrupt mask

When the computer detects an interrupt, the interrupt device determines whether to respond to the interrupt that has occurred through the interrupt shielding bit, and selectively responds to the interrupt

Interrupt priority

When the computer detects multiple interrupts at the same time, the interrupt device responds to the interrupt in the order of priority

One possible processing order:

  1. Processor hardware failure interrupt event
  2. Voluntary interruption events
  3. A procedural interrupt event
  4. External interrupt events such as clock interruption
  5. Input/output interrupt event
  6. Restart and shutdown interruption events

Different types of operating systems have different interrupt priorities

Nested handling of interrupts

When the computer responds to an interrupt, it can respond to other interrupts during the interrupt processing

Operating system is a performance-critical program system, and interrupt response processing has hardware requirements. Considering system efficiency and implementation cost, interrupt nested processing should be limited to a certain number of layers, such as 3 layers

The nesting of interrupts alters the order in which interrupts are processed, leading to the possibility that those who respond first will be processed later

process

Process is a program with a certain independent function about a data set running activities, but also an independent unit of the operating system for resource allocation and scheduling

A process consists of five physical parts

  1. The data structure in which the OS manages running programs P
  2. Memory code C for running a program
  3. Memory data D of a running program
  4. General register information R (for running a program)
  5. (OS control program execution) program status word information PSW

Different programs

  • Run on different data sets: constitute two unrelated processes
  • Running on the same data set: Constitute two communicative processes that share data

The same procedure

  • Running on different data sets: Independent processes that make up two shared code

Process status

  • Running state: the process occupies the processor running
  • Ready: the process is ready to run and waits for the processor to run
  • Wait state: a process is not ready to run because it is waiting for resources, inputs, outputs, signals, etc

  1. Running state → Waiting state: Waiting for resources, I/O, and signals
  2. Waiting state → Ready state: The resource is satisfied, I/O is finished, and the signal is complete
  3. Running state → Ready state: running time slice to the process with higher priority
  4. Ready state → running state: when the processor is idle, the process with higher priority is preempted
Process a hang

The OS cannot predict the number of processes and resource requirements, and the computer system may run out of resources

Insufficient running resources are manifested as low performance and deadlock

Solution: Deprive some processes of memory and other resources, switch to the SWAP zone managed by the OS, do not participate in the process scheduling, wait until the appropriate time to transfer memory, restore resources, participate in the running, this is suspended process

The pending state is essentially different from the pending state. The pending state occupies the applied resources and is waiting, while the pending state has no resources

Process suspension selection and recovery

  • Generally, the process in the wait state enters the pending wait state
  • You can also select a ready process to enter a pending ready state
  • A running process can also suspend itself
  • After the wait event ends, the pending wait state enters the pending ready state
  • Generally, suspend the ready process to resume

Data description

Process control block

Process control block PCB is a data structure used by OS to record and depict process state and environment information

It is divided into identification information, site information and control information

Identity information

Stores information that uniquely identifies the process

  • System assigned id
  • Id of a process group assigned by the system
  • User-defined process name
  • User-defined process group name
The site information

Used to store processor field information while the process is running

  • User visible register contents: data register, address register
  • Control and status register contents: PC, IR, PSW
  • Stack pointer contents: core stack and user stack Pointers
Control information

Stores information related to management and scheduling processes

  1. Scheduling information: status, waiting event/cause, and priority
  2. Process composition information: code/data address, external memory image address
  3. Queue Pointers: process queue Pointers, parent and child process Pointers
  4. Communication related information: message queue, semaphore, lock
  5. Process privilege information: such as memory access permission, processor privilege
  6. Processor usage information: occupied processor, time slice, processor usage time/total time executed, billing information
  7. Resource list information: such as occupied resources, used resources
Process image

The set of contents and execution states of a process at a certain point in time is a memory-level physical entity, also known as a memory image of a process. It contains:

  • Process control block: Stores process identification information, status information, and control information
  • Process block: The program space in which a process executes
  • Process data block: The space of data processed by a process, including data, user stacks of processing functions, and modifiable programs
  • Core stack: The stack used by processes running in kernel mode, interrupts or system procedures

Process context

Process execution requires environment support, including the execution information in the CPU field and Cache, process physical entities in the OS, and the environment that supports process running. Process context includes the following:

  • User-level context: user block/user data area/user stack/user shared memory
  • Register context: PSW/ stack pointer/universal register
  • System-level context: PCB/ memory area table/core stack

Process context describes the execution of a process

Process management

Key process management software includes:

  • System call/interrupt/exception handler
  • Queue management module
  • Process control program
  • Process scheduler (mostly independent processes)
  • Process communication program (multiple packages)
  • Terminal login and job control procedures, performance monitoring procedures, audit procedures and other peripheral procedures
Queue management module

The operating system establishes multiple process queues, including ready queue and wait queue, which are organized into first-in, first-out queue and priority queue. A process in a queue can connect with a single/double guide or index through a queue guide in the PCB. Process and resource scheduling revolves around process queues.

Control and management
  • Process creation: add an entry to the process schedule, apply for a PCB and initialize it, generate an identity, create an image, allocate resources, move to the ready queue
  • Process undo: Remove from queue, return resource, revoke identity, recycle PCB, remove process table entry
  • Process blocking: Saves the field information, modifies the PCB, moves it to the waiting queue, and schedules the execution of other processes
  • Process wake up: wait to move out of queue, modify PCB, move to ready queue (this process has higher priority than running process trigger preemption)
  • Process suspension: modify the status and access to the relevant queue, memory and other resources sent to the swap area
  • Process activation: allocating memory, changing state, and logging in and out of relevant queues
  • Others: For example, modify process privileges
Primitives and process control primitives

The process control process involves the modification of OS core data structures (process schedule /PCB pool/queue/resource table). To prevent time-related errors, primitives should be used

A primitive is a program composed of several instructions to complete a specific function, and its execution is indivisible. Execution of primitives can be achieved by a close interrupt

The primitives used for process control are called process control primitives. Another common class of primitives are process communication primitives

Process switching

Process switchover refers to taking back the processor from the running process and letting the process to be run occupy the processor. Process switchover is essentially the context switch between the interrupted running process and the process to be run. The processing process is as follows:

  1. Saves the context of the interrupted process
  2. Steering process scheduling
  3. Restores the context of the process to be run

Process switching must be done in operating system kernel mode, which requires mode switching

The mode switch

Mode switching, also known as processor state switching, includes:

  1. User mode to kernel mode is triggered by an interrupt/exception/system call that interrupts user process execution

    1. Switch from processor mode to kernel mode
    2. Saves the PC/PSW value of the current process to the core stack
    3. Turn to interrupt/exception/system call handlers
  2. Kernel to user mode triggered when the OS executes an interrupt return instruction to return control to the user process

    1. Eject the PSW/PC value from the core stack of the pending process
    2. Switch from processor mode to user mode
Process switch working process
  1. Switch forward mode and press into PSW/PC
  2. Saves site information about interrupted processes
  3. Handle specific interrupts/exceptions
  4. Saves the system stack pointer SP value of the interrupted process to the PCB
  5. Adjust PCB information of interrupted process, such as process status
  6. Adds the PCB of the interrupted process to the relevant queue
  7. Select the next cpu-consuming process to run
  8. Modify the PCB information of the selected process, such as process status
  9. Set the address space of the selected process and restore storage management information
  10. Restores the SP value of the selected process to the processor register SP
  11. Restores the field information of the selected process to the handler
  12. (Interrupt return instruction triggered) Reverse mode conversion and eject PSW/PC
The time when a process switch occurs

A process switch must occur during an interrupt/exception/system call.

  • Blocked system calls and abnormal virtual addresses cause the interrupted process to enter the wait state
  • A process with a higher priority is found after the time slice or I/O is interrupted, and the interrupted process enters the ready state
  • Terminates an exception that cannot continue with a system call, causing the interrupted process to enter the terminated state

Some interrupts/exceptions do not cause a process state transition, do not cause a process switch, and just hand back control to the interrupted process after processing is complete.

  1. (Interrupt/exception triggered) Forward mode switch pressing into PSW/PC
  2. Saves site information about interrupted processes
  3. Handle interrupts/exceptions
  4. Restores site information about the interrupted process
  5. (Interrupt return instruction triggered) Reverse mode conversion pops PSW/PC

Multithreading technology

Traditional processes are single-threaded structured processes

Problems with concurrent programming for single-threaded processes:

  • Process switching costs a lot
  • Process communication costs a lot
  • Limits the granularity of process concurrency
  • It reduces the efficiency of parallel computing

The operating system restricts the communication between processes from the perspective of security, and the cost of scheduling between processes is higher

Multithreaded structured process

A thread is the execution path of a process and the basic unit of scheduling. All threads in the same process share the main storage space and resources acquired by the process

Thread states are running, ready, and sleep, with no suspension

The suspension is related to whether or not the resource is occupied, whereas the thread uses the resources of the process

Os-aware threading environment:

  • Processor scheduling objects are threads
  • A process has no three states (or only suspended states)

OS is not aware of thread environment:

  • The processor scheduling object is still a process
  • The user scheduler in user space schedules threads
advantages
  • Fast thread switching
  • Reduce administrative overhead
  • Communication is easy to implement
  • Increased parallelism
  • Save memory space
Implementation of multithreading
KLT, kernel-level Threads

All the work of thread management is done by the OS kernel

The characteristics of

  • If a thread in a process is blocked, the kernel can schedule other threads in the same process to occupy the processor
  • In a multiprocessor environment, the kernel can schedule concurrent execution of multiple threads in the same process
  • The kernel itself can also be implemented by multi-threading technology, which can improve the execution speed and efficiency of the operating system
  • Application thread runs in user mode, and thread scheduling and management are implemented in the kernel. In the same process, mode switching is required when the control is transferred from one thread to another, and the system overhead is large
ULT, User-level Threads

A thread library running in user space that provides a support environment for the development and running of multithreaded applications.

All the work of thread management is done by the application, and the kernel is unaware of the existence of threads.

The characteristics of

  1. All thread management data structures are in the user space of the process, and kernel mode is not required for thread switching, which can save mode switching overhead and valuable kernel resources
  2. Allows processes to select scheduling algorithms based on application specific needs, or even tailor scheduling algorithms based on application requirements
  3. It runs on any OS, and the kernel doesn’t need to do anything to support ULT
  4. Unable to take advantage of multiple processors, the OS schedules processes that only one ULT can execute
  5. An ULT block causes the entire process to block

Jacketing technology

Make the blocking system call non-blocking and execute the Jacketing program when the thread is trapped in the system call. Resource usage is checked by the Jacketing program to determine whether to perform a process switch or transfer control to another thread.

Hybrid multithreading

Multiple ULT for a single application can be mapped to several KLTS, which can be adjusted to achieve better parallelism

Thread state under the thread hybrid policy

  • Active ULT represents the three states bound to KLT
  • The active ULT runtime activates user scheduling
  • A non-blocking system call can use Jacketing to initiate user scheduling and adjust active ULT

Processor scheduling

Scheduling hierarchy

Advanced scheduling

Also known as long-range scheduling, job scheduling determines whether or not to be added to the pool of executing processes

In a time-sharing OS, high-level scheduling determines:

  • Whether to accept a connection from an end user
  • Whether a command is accepted by the system and constitutes a process
  • Whether to add a new state process to the ready process queue
Intermediate scheduling

Also known as balanced load scheduling, determines the set of processes available in main memory

  • Intermediate scheduling was introduced to improve memory utilization and job throughput
  • Intermediate scheduling determines which processes are allowed to reside in main memory to compete for processors and other resources, serving as short-term adjustments to system load
  • Intermediate scheduling smoothes the load on the system by swapping some processes out of main memory so that they enter a “pending” state and do not participate in process scheduling
Low-level scheduling

Also known as short-cut scheduling, process scheduling determines which available processes occupy the processor for execution

  • Process scheduler: also known as dispatcher, operating system implementation of processor scheduling program, is the most core part of the operating system
  • The processor scheduling strategy directly affects the performance of the whole system

The main function

  • Remember the state of process or kernel-level threads
  • Determine when and for how long a process or kernel-level thread gets the processor
  • Assign processors to process or kernel-level threads
  • Recall handler

Scheduling algorithm

Principles for selecting processor scheduling algorithms
  • Resource utilization: Making CPU or other resources as utilized as possible and working in parallel
  • Response time: Make interactive users’ response times as small as possible, or process real-time tasks as quickly as possible
  • Turnaround time: The time interval between submission to the system and completion of execution to obtain results is called turnaround time and should be kept as short as possible, or the average turnaround time
  • Throughput: As many processes as possible per unit time
  • Fairness: Ensure that each user and each process gets a reasonable share of CPU or other resources
Priority number scheduling algorithm

Processes are run based on the number of priorities assigned to them

classification
  • Preemptive priority number scheduling algorithm
  • Non-preemptive priority number scheduling algorithm
The number of priorities related to the time of entry to the system
  • Short computation time (job/process) is preferred
  • The process with short remaining calculation time is preferred
  • High response ratio (job/process) priority response ratio = wait-wait time incoming time response ratio = {wait time \over incoming time} Response ratio = Incoming time Waiting time
  • First come, first served: First team selected
  • Used for advanced scheduling; In low-level scheduling, computation-oriented processes are too superior
Time slice rotation scheduling algorithm
  • Take turns to occupy a CPU slice according to the time at which each process enters the ready queue
  • Time slice interrupt
  • Time slice determination: select the appropriate length of time slice. If the time slice is too long, it will degenerate into first-come-first-served algorithm; if the time slice is too short, the scheduling overhead will be high
  • Different process time slice length can be adjusted
Hierarchical scheduling algorithm

Also known as multi-queue strategy, feedback loop queue

The basic idea
  • Create multiple queues of ready processes with different priorities
  • Multiple queues of ready processes are scheduled by priority number
  • A high-priority ready process with a short time slice allocated
  • Processes in a single ready process queue have the same number of priorities and time slices

Example:

Lottery scheduling algorithm
The basic idea

Issue lottery tickets to processes for various system resources, such as CPU time. When the scheduler needs to make a decision, a lottery ticket is randomly selected, and the process holding the lottery ticket gets system resources

Lotteries can be exchanged between cooperative processes

Storage management

Main mode

Logical addresses

Also known as relative address, that is, the address space used by user programming

Logical addresses are numbered from 0 and can be in one of two forms:

  • One-dimensional logical address (address)
  • 2-d logical address (segment number: in-segment address)
Block programming
  • Design a program into segments (code segments, data segments, stack segments, etc.)
  • Users can apply segment coverage technique to expand memory space usage

Physical address

Also known as absolute address, that is, the address space used by the program execution, processor execution instructions according to the physical address

Reuse of main memory
Reuse by partition
  • Main memory is divided into multiple fixed/variable size partitions
  • One program/program segment occupies one partition
Reuse by page shelf
  • Main memory is divided into multiple fixed-size page shelves
  • A program/program segment occupies multiple page frames

Basic mode of storage management

If you compare logical addresses with physical addresses, there are 2 x 2=4 basic storage management modes

  • Single-contiguous storage management: Programs with a one-dimensional logical address space occupy a fixed or variable partition of main memory
  • Segment storage management: programs with a segment 2-d logical address space occupy multiple main memory variable partitions
  • Page storage management: programs with one dimensional logical address space occupy multiple main memory page shelves
  • Segmented page storage management: programs with segmented 2-d logical address space occupy multiple main memory page shelves

function

Address translation

Also weighing localization, that is, the logical address into absolute address

Static relocation

Address translation occurs when the program is loaded into memory

Executed by loaders and used by early small OS

Dynamic heavy position

Address translation occurs when the CPU executes the program

For efficiency, it depends on hardware address translation mechanism

Allocation and disallocation
distribution

When processes are installed into main storage, the storage management software allocates main storage space and sets a table to record the allocation of main storage space

Go to the match

When a process evacuates or proactively returns the main storage resources, the storage management software reclaims all or part of the storage space occupied by the process and adjusts the main storage allocation table

The Shared space

Multiple processes sharing main memory resources: Multiprogramming enables several programs to enter main memory simultaneously, each occupying a certain amount of storage space, and sharing the same main memory

Multiple processes share certain areas of main memory: several cooperating processes have a common block of main memory or data

Storage protection

In order to prevent multiple processes from interfering with each other, programs and data in main memory must be protected

  • Information in private main memory: readable and writable
  • Shared information in common areas: according to authorization
  • Non-process information: cannot be read or written

The CPU checks whether the access is allowed. If the access is not allowed, an address protection exception occurs, and the OS handles the exception.

Expansion of space

Storage expansion: Disk expansion as main storage, only part of the process or part of the process into memory

  1. Swap technique: call out part of a process that is not running
  2. Virtualisation: Only part of the process is called in

This work requires hardware and software collaboration

  1. The swap process decides to swap, and the hardware mechanism calls in
  2. When the CPU detects an address that is not in main memory, the VIRTUAL address is abnormal. The OS calls in the virtual address and re-executes the command

Virtual memory

Put forward

  • The main storage capacity limit brings many inconveniences

    • Users must consider main memory limits when writing programs
    • The number of tracks in multichannel programming is limited
  • User programming behavior analysis

    • Taking all things into account, execution is mutually exclusive
    • Spatial locality, such as sequence and cycle
    • The time locality of the execution of a phase

Consider partial call-in process content

The basic idea

  • Storage management puts all the process information in secondary storage. During execution, part of the information is first loaded into main storage and later called in according to the execution behavior
  • If there is not enough free space in main storage, storage management needs to call out temporarily unused information from main storage to secondary storage based on execution behavior
Implementation approach

Two address Spaces need to be established and automatically managed

  • (Auxiliary) Virtual address space: holds process loading
  • (Main storage) Actual address space: hosts process execution

For the user, the computer system has a much larger main storage space, called virtual memory. Virtual memory is an address space extension technology that is generally transparent to user programming

Hardware support for storage management

Storage Objects involved in storage management

  • In order to obtain better processing performance, some main memory programs and data (especially key performance data) are transferred to Cache. Storage management needs to manage them, even the management of associative memory
  • To obtain a larger virtual address space, storage management needs to manage virtual storage files stored on hard disks, solid state disks, and even network hard disks
Cache Memory
  • Cache is a high speed and small capacity memory between CPU and main memory. It is composed of static memory chip SRAM. It has smaller capacity but is more expensive and faster than main memory DRAM technology, which is close to the speed of CPU
  • The CPU often needs to read the same data block repeatedly. The introduction of Cache and the increase of Cache capacity can greatly improve the hit ratio of data read by the CPU and improve system performance
Composition of cache storage
  • Cache memory usually consists of high-speed memory, associative memory, address translation unit, replacement logic and so on
  • Associative memory: memory that is addressed by content
  • Address conversion component: through the association memory to establish a directory table to achieve fast address conversion. Cache is directly accessed upon a hit. On a miss, read from memory and add to Cache
  • Replace parts: Replace data blocks according to certain policies and modify address translation parts when the cache is full
Organization of cache storage
  • Caches are small due to CPU chip size and cost
  • According to cost control, it can be divided into L1, L2 and L3

L1 Cache: data Cache and instruction Cache; Built-in; It has the highest cost and the greatest impact on CPU performance. The value ranges from 32KB to 256KB

L2 Cache: built-in Cache and external Cache. The latter has lower performance. Usually between 512KB and 8MB

L3 Cache: Mostly external, useful in games and servers; However, for many applications, bus improvement is more beneficial than L3

Single-partition storage management

Each process occupies a physically completely contiguous storage space (area)

Single-user continuous storage management

  • The main storage area is divided into system area and user area, and a fence register boundary is set up to divide two areas, which is used by hardware to store and protect during execution

  • Generally, static relocation is used for address translation with low hardware cost

    Static relocation: When a job is loaded, the instruction and data addresses of the program in the job are all translated into absolute addresses

  • Applicable to single-user single-task operating system, such as DOS

Fixed partition storage management

  • Support for multiple partitions
  • The number of partitions is fixed and the size is fixed
  • Static relocation is available
  • Hardware implementation cost is low
  • Early OS adoption

Main memory allocation

Main memory allocation table

Address translation

Variable partition storage management

Partition dynamically according to the memory requirements of the process

When creating a process, check whether the main memory has enough free space based on the amount of main memory required by the process

  • If so, divide a partition according to the amount needed
  • If no, make the process wait for main storage resources

Since the partition size is determined according to the actual requirements of the process, the number of partitions is random

The table of allocated area and the table of unallocated area adopt linked list

Memory allocation
  • The allocation algorithm is used first
  • Neighborhood adaptive allocation algorithm
  • Optimal adaptive allocation algorithm
  • Worst adaptive allocation algorithm
Dynamic relocation

Fraction of memory
  • Fixed partition will result in memory oddment
  • Variable partitioning also creates small unusable memory partitions, known as out-of-memory odbs, as the process allocates memory
  • The optimal adaptive algorithm is most likely to produce external oddment
  • Any adaptation algorithm cannot avoid the occurrence of external oddment
Mobile technology (program float technology)

Moving partitions to address out-of-memory oddments requires dynamic relocation support

Page storage management

  • Paged memory divides main memory into equal-sized page shelves
  • Limited by the size of the page holder, the logical address of the program is naturally divided into pages
  • Different pages can be placed in different page frames and do not need to be contiguous
  • Page tables are used to maintain the main memory integrity of processes

Address in page storage management

Logical addresses

The logical address for page storage management consists of the page number and cell number. The logical address is as follows:

Physical address

The physical address managed by pager storage also consists of the pager number and cell number in the form of the physical address:

Address translation

The price

Base addresses for variable partition storage are stored in registers, which are fast, while page tables are stored in main memory, which must be accessed twice per address translation

  1. Read the corresponding page holder number in the page table by page number
  2. Read and write at the calculated absolute address

Problems: Reduced access speed

Solution: Cache part of the page table

Fast table

In order to improve the speed of address translation, a special high-speed memory is set up to store part of the page table, which is the fast table

Quick table entry: page number, page rack number

This high-speed memory is associative memory, which addresses by content rather than by address

Because you want to find the page number corresponding to the page holder number, know the page number, do not know the address, so it is content addressing

Eg: Assume that the main memory access time is 200 nanoseconds, the fast table access time is 40 nanoseconds, and the fast table lookup hit rate is 90%

The average address translation cost is $(200+40)*90%+(200+200)*10% =256 nanoseconds $

That’s 36% less than the time it took to access main memory twice (400 nanoseconds)

Address translation process

Lookup table by page number in logical address

  • If the page is already in the fast table, the absolute address is formed by the page rack number and cell number
  • If the page is not in the express table, it searches the main memory page table to form an absolute address and registers the page in the express table
  • When the express form is full and a new page needs to be registered, an old registration item needs to be eliminated in the express form according to a certain strategy
A process schedule in a multiprogram environment

A page table for each process is registered in the process table. When a process occupies a processor, its page table start address and length are entered into the page table control register

User job name Page table starting address Page table length
AB 0010 4
CD 0014 3
EF 0017 7
Address translation in multiprogram environment

Memory allocation/de-allocation

A bitmap can be used to record the allocation of main memory, and a process page table can be established to maintain the logical integrity of main memory

Use 1 bit for each page to record whether the page is occupied

Pages of Shared

Page storage management enables multiple processes to share programs and data

  • Data sharing: Different processes can share data pages using different page numbers

  • Program sharing: Different processes must share code pages using the same page number

    The location of different process code for the same program is determined

Page virtual storage management

All the pages of the process are loaded into virtual memory, and part of the pages are loaded into real memory first. Then, according to the execution behavior, the pages that are not in main memory are dynamically called in, and the necessary pages are called out at the same time

For the first time, only the first page of process information is loaded into main storage, which is called request page storage management

Page table of virtual storage management

Page entry to be expanded:

  • The virtual address and physical address of each page
  • Main memory resident flag, write back flag, protection flag, reference flag, removable flag

Implementation of pager virtual storage management

CPU processing address

  • If the page resides, the block number is obtained to form the absolute address
  • If the page is not in memory, the CPU issues a page-missing interrupt

OS handles missing page interrupts

  • If there is a free page frame, according to the auxiliary address to call in the page, update the page table and fast table, etc
  • If there is no free page holder, it decides to eliminate the page, call out the modified page, call in the page, update the page table and fast table

paging

When the main storage space is full and new pages need to be loaded, ppage virtual storage management must call out some pages already in main storage according to certain algorithms. The task of selecting pages to be eliminated is called page scheduling. The algorithm for selecting obsolete pages is called the page scheduling algorithm.

Page scheduling algorithm is not designed properly, will appear (just eliminated pages immediately to call in, and so repeatedly), this phenomenon is called jitter or jerking.

Page – missing interruption rate

Assume that process P has n pages and the system allocates m page racks. P The number of successful accesses is S, and the number of unsuccessful accesses is F. The total number of accesses is A=S+F. Page – missing interruption rate is defined as f=F/A

Factors affecting page – missing interruption rate

  • Number of frames allocated to a process: The more frames available, the lower the page-missing break rate
  • Page size: The larger the page size, the lower the rate of missing pages
  • User programming method: in the case of large data volume, it also has a great impact on the page – missing interrupt rate

Eg: The program sets the array to “0”, assuming that there is only one main memory page rack, the page size is 128 words, the array elements are stored in line, the first page is in main memory at the beginning

Procedure 1:

int A[128] [128];
for(int j=0; j<128; j++)for(int i=0; i<128; i++) A[i][j]=0;
Copy the code

The inner loop traverses by marching, resulting in a page-missing interrupt for every assignment, a total of (128× 128-1) page-missing interrupts

Program 2:

int A[128] [128];
for(int i=0; i<128; i++)for(int j=0; j<128; j++) A[i][j]=0;
Copy the code

The inner loop traverses the column, generating (128-1) page – missing interrupts

OPT page scheduling algorithm

The ideal scheduling algorithm is to first weed out pages that will not be accessed again when a new page is called, and then select pages that will be accessed after the longest time since now. This algorithm is proposed by Belady, called Belady algorithm, also called optimal algorithm (OPT)

OPT can only be emulated, not implemented

FIFO page scheduling algorithm

Always eliminate the first page that is added to main memory, or the page that is in main memory the longest (unless resident)

Simulation is the sequence of the execution of the program, there is a certain rationality

LRU page scheduling algorithm has not been used for the longest time

Eliminate pages that have not been accessed in a recent period of time, that is, pages that have been used recently and may be used again soon

It simulates the local properties of program execution, considering both circularity and sequence

Strict implementation is expensive (need to maintain special queues)

The simulation implementation of LRU algorithm builds a reference flag per page for hardware to use and sets a time interval interruption: when the interruption page reference flag is set to 0 during address conversion, when the page reference flag is set to 1, it is difficult to randomly select how long the time interval from the middle of the page reference flag is 0

Least commonly used LFU page scheduling algorithm

Weeding out pages that have been visited less frequently in a recent period of time simulates OPT better than LRU

Based on the interval interruption, and set a counter for each page, after the interval interruption occurs, all counters clear 0 every visit page 1 times to add 1 to the counter to select the smallest value of the page eliminated

CLOCK page scheduling algorithm
  • The page queue is constructed by the circular queue mechanism, which forms an annular table similar to the clock surface
  • The queue pointer is the equivalent of a hand on a clock, pointing to pages that might be obsolete
  • Use page reference flag bits

The working process

  • When a page is called into main memory, it references flag position 1

  • When the main memory page is accessed, its reference flag position is 1

  • When a page is weeded out, the loop queue is scanned starting with the page to which the pointer is currently pointing

    • Clear the reference flag bit 0 of pages with reference flag bit 1 encountered and skip
    • Discard pages encountered with reference flag bit 0 and advance the pointer one step

The inverted page table

Memory management unit (MMU) : THE CPU manages the control line of virtual and physical storage, maps virtual addresses to physical addresses, provides storage protection, and determines the elimination of pages when necessary. MMU is a specialized hardware organization configured for page storage management.

Inverted page table IPT: Data structure used by MMU

Why reverse page tables

In a paging system, each process has a page table. A modern system may have a large number of processes, each of which allows a large logical address space, and thus a process may have a large page table, which can take up a large amount of memory. By mapping physical pages to logical pages, you can save a lot of storage.

How to implement reverse page table

An entry is set for each physical page in the memory, which stores the process number and page number. Finally, the system maintains only one inverted page table.

Reverse the page entry of the page table

Process number Page number flag bit chain pointer

Process number: The process number that uses the physical page

Page number: indicates the virtual address page number of the physical page

Flag bits: valid, referenced, modified, protected, and locked flag information

Chain pointer: hash chain (when hashes collide, they are linked together by linked list)

Hash table of reverse page table

If it is to use the page table can be directly from the process of the page table query to the page frame number, and now the use of reverse page table, in the page table to the page frame number mapping need to use hash query, so to maintain a hash table.

Hash function input: process number and page number

Hash function output: pointer to reverse page table entry

Address translation process
  1. A pointer to an inverted page entry is queried from the hash table

  2. Compares the process number and page number of the pointed page entry

    • If so, the page is the required physical page

    • Not the same, continue along the chain pointer

      • If they are the same, the page is the required physical page
      • If not, the page is not in memory, and in virtual memory, page replacement is required

Segment storage management

Sections of storage is geared to the needs of developers, developers, according to their own need to program, the program of different parts in different period of address management, and physical of memory management is based on the variable partition storage management, only the variable partition storage management is a process of a paragraph, and sections of storage management is a process of multiple segments.

It is also different from page management, management is transparent to the developer, the system automatically on the software memory according to the cut. And page management each page is equal in size, evenly distributed. The size and location of a segment management segment are uncertain.

Block programming

Each program can be composed of several segments, each of which can be addressed starting with “0” and the addresses within the segments are contiguous

The logical address of segmented storage consists of two parts: segment number unit number

The basic idea of segmental storage management

Segment storage management is based on variable partition storage management. A process occupies multiple partitions

The hardware needs to add a set of user-visible segment address registers (code segment, data segment, stack segment, additional segment) for address translation

You need to set a segment table for storage management. Each segment occupies a segment entry, including the start address of the segment, the segment length limit, and the flags for storage protection, portability, and scalability

Period of Shared

This is achieved through entries in different process segment tables pointing to the same segment base address

The information on the shared segment must be protected. If the information can only be read but not written, the protection will be interrupted if the protection conditions are not met

Segment-page storage management

Section storage management can be implemented based on page storage management

Page management for each paragraph

Address translation for segment-page storage management

Equipment management

The basic concept

I/O devices

Modern computer systems are usually equipped with a large number of I/O devices for information exchange or storage between the computer system and the external world (such as users, other computers or electronic devices, etc.)

Divided by device management
  • Character device: Exchanging information on a character basis, sending or receiving a stream of characters

    • Most human-computer interaction devices are character devices, such as mouse and monitor
  • Block device: A fixed-size block of data (a block is an area of continuous information on a storage medium) for information exchange

    • Storage devices are usually block devices, such as disk drives
  • Network equipment: equipment used to communicate with remote devices

    • Machine-machine communication devices are network devices, such as network cards
    • Network devices can be abstracted as special character devices that transmit streams of characters, or as block devices that transmit continuous small pieces of data
Equipment management objectives
  • Overcome the problem caused by the mismatch between the speed of the device and that of the CPU, so that the host and the device can work in parallel and improve device efficiency

  • Abstract devices, mask physical details and operation processes of devices, configure drivers, and provide a unified interface for users and high-level software

    • Nodes in a file system are abstracted and managed in a unified manner

      Input and output to the device are abstracted to read and write files

    • Raw device: the device is not directly managed by the operating system and is read and written by applications, which improves I/O efficiency

Device management functions
  • Device Interrupt Processing
  • Buffer management
  • Equipment allocation and allocation
  • Device driver scheduling
  • Implementing virtual devices
Layers of device management
  • I/O hardware

    • I/O devices and their interface lines
    • Control unit
    • channel
  • I/O software

    • System I/O software
    • User space I/O software

I/O control mode

Device manager

A device controller is an entity in a computer whose primary responsibility is to control one or more I/O devices for data exchange between THE I/O devices and the computer. It is the interface between THE CPU and the I/O device, it receives the command from the CPU, and to control the I/O device work, so that the processor from the complex device control affairs. A device controller is an addressable device that has a unique device address when it controls only one device. If the control can be connected to more than one device, it shall contain more than one device address and make each device address correspond to one device.

Data exchange refers to data exchange between cpus and controllers and between controllers and devices. For the former, data is written to or read from the controller in parallel by the CPU through the data bus. In the latter case, it is the device that inputs data to and from the controller to the device.

Polling mode

  1. The processor sends an I/O command to the controller
  2. If the equipment is not ready, repeat the test process until the equipment is ready
  3. Perform data exchange
  4. You can perform other operations only after the I/O operation is complete

The interrupt way

  1. The processor issues an I/O command to the controller and continues to execute subsequent instructions

    • If the process does not need to wait for the I/O to complete, subsequent instructions can remain instructions in the process
    • Otherwise, the process hangs on the interrupt and the processor performs other work
  2. The controller checks the device status and initiates an interrupt when it is ready

  3. The CPU responds to the interrupt and turns to the interrupt handler

  4. Interrupt handlers perform data reads and writes

  5. Resume execution of the original program

Direct memory access (DMA)

DMA module: Mimics the processor to control the exchange of data between main memory and device controllers

Process:

  1. The processor issues I/O commands to the DMA module
  2. The processor continues to do other work, and the DMA module takes care of transferring all the data
  3. At the end of the data transfer, DMA interrupts the processor

Features:

  • The CPU does not terminate the execution of the original program

  • The CPU participates only at the beginning and end of the data transfer

    • To start, the CPU needs to initialize the DMA module
    • At the end, the CPU response is interrupted but does not have to be saved on-site
Cycle stealing in DMA mode

When DMA and CPU access memory across the bus at the same time, the CPU always cedes ownership of the bus to the DMA for one or more main memory cycles

  • Periodic stealing has little effect on data exchange between delayed CPU and main memory
  • The data transfer process is discontinuous and irregular
  • In most cases, the CPU exchanges data with the Cache

A comparison of the three ways

The device controller adopts polling mode

The CPU waits for the device to be ready and participates in data transfer

A device controller that uses interrupt mode

The CPU does not wait for the device to be ready, but participates in data transfer in response to an interruption

Direct memory control via DMA

The CPU participates at the beginning and end of data transfer, but not during data exchange with main memory

CPU function Waiting for the equipment Data transfer
Polling mode Need to be To participate in
The interrupt way Don’t need To participate in
DMA Don’t need Don’t participate in

I/O channels

The device controller contains its own dedicated processor and channel programs

  • I/O instructions are no longer executed by the processor, but are stored in main memory by
  • The I/O channel contains processor execution using four levels of connectivity: processor, channel, controller, and device
process
  1. When the CPU encounters an I/O request, it starts the specified channel. Once successfully started, the channel starts to control the I/O device to operate.
  2. The CPU performs other tasks
  3. After the I/O operation is complete, the I/O channel is interrupted. The CPU stops working and processes the EVENT that the I/O operation is complete

The CPU works in parallel with the channel

I/O channels and DMA

Unlike DMA, where the direction of data transfer, the location of memory where the data is stored, and the length of the data block transferred are all controlled by the CPU, which is controlled by the channel. In addition, DMA mode requires at least one DMA controller per device, and one channel controller can control multiple devices.

  1. DMA can only implement fixed data transfer control, while channels have their own instructions and procedures, with greater ability to handle data input and output independently.
  2. DMA can control only one or a few of the same kind of devices, while a channel can control many of the same or different kinds of devices.

See https://www.jianshu.com/p/d1542085afde

Bus and I/O

Single bus

Connect the CPU, main memory, and I/O modules to the same set of buses

Advantages: Simple structure, easy to expand

Disadvantages: Main memory needs to share bus with I/O module; The increase of devices will make the bus longer, which will increase the transmission delay. Not suitable for a large number of high-speed equipment

Traditional three-level bus

Main memory and Cache transfer data through the main memory bus, and data transfer between the main memory bus and I/O devices on the expansion bus buffer through the expansion bus interface

Advantages: Data transfer between main memory and I/O is separated from processor activity; More I/O devices are supported

Disadvantages: Does not apply to the situation where the data rates of I/O devices differ too much

Multi – stage bus of north – south bridge is adopted

Through the storage bus, PCI bus, E(ISA) bus respectively connected to the main memory, high-speed I/O devices and low-speed I/O devices

Advantages: Supports I/O devices with different data rates

A multistage bus using I/O channels

Supports data transfer between CPU, main memory, and multiple I/O channels

Supports I/O channels and I/O controllers, as well as data transfer between I/O controllers and devices

I/O software

Implementation level

Design goals
  • High efficiency: Improve the efficiency of equipment, especially disk I/O operations
  • Versatility: use uniform standards to manage all equipment
Design ideas

The software is organized into hierarchies, with low-level software shielding hardware details and high-level software providing a clean, user-friendly interface

The main consideration
  • Device independence: Writing programs to access files is device-independent
  • Error handling: Errors handled by low-level software are not perceived by higher-level software
  • Synchronous/asynchronous transmission: support blocking and interrupt drive two working modes
  • Buffering technology: establish data buffer to improve throughput

I/O software implementation

I/O interrupt handler

Located at the bottom of the operating system, closely related to the hardware, with as little contact as possible with the rest of the system

When a process requests an I/O operation, it is usually suspended until after the data transfer ends and an I/O interrupt occurs, the operating system takes over the CPU and turns to the interrupt handler

When the device makes an interrupt request to the CPU, the CPU responds to the request and goes to an interrupt handler

function
  1. Check the contents of the device status register
  2. Determine the cause of the interruption,
  3. Process the I/O operations according to the completion status

If an error occurs during data transmission, the system reports an error message to the upper-layer software and executes the data again

If it ends normally, the process waiting for transfer is woken up and converted to the ready state

If there is an I/O command waiting for transmission, notify the relevant software to start the next I/O request

Device driver

Includes all code that is closely related to the device, receives and executes I/O requests from software that is independent of the device

  • Converts logical I/O requests submitted by users into the startup and execution of physical I/O operations
  • Monitor the correct execution of equipment, manage data buffers, and correct errors as necessary
function
  • Device initialization

    • Preset device and controller and channel states during initial system startup or device data transfer
  • Execute device driver routines

    • Responsible for starting equipment and data transmission
    • For channel mode, it is also responsible for generating channel instructions and channel procedures, and starting channel work
  • Invoke and execute interrupt handlers

    • Responsible for handling all kinds of interrupts issued by equipment and controllers and channels
level

Each device driver deals with only one device, or a class of closely related devices

Device drivers are divided into integral drivers and layered drivers

  • The overall driver provides interfaces and control hardware directly to the operating system

    • It is suitable for simple drivers with high efficiency but difficult to migrate
  • Layered drivers divide drivers into multiple layers and put them on the stack. When the system receives an I/O request, it calls the driver on the top of the stack first. The driver on the top of the stack can directly process the request or call the driver on the lower level until the request is processed

    • It is used for drivers with complex functions and high reuse requirements. The structure is clear and easy to transplant, but it will increase part of the system overhead
Device-independent I/O software

Performs common I/O functions for all devices and provides a consistent interface to user-layer software

function
  • Device naming: Addresses devices by path name
  • Device protection: Checks whether the user has the right to access the device
  • Provides device-independent data units: number of characters, block size
  • Buffering technology: transmission rate, time constraint, cannot reach destination directly
  • Device allocation and status tracking: Assign different types of devices
  • Error handling and reporting: Errors that the driver cannot handle
User space I/O software

Library functions: Some I/O software can be implemented using library functions outside the operating system kernel and connected to applications at run time

Virtual device software: Emulation I/O software that uses one type of device to simulate another type of device, such as a virtual cd-rom drive

The I/O buffers

purpose

Solve the contradiction between CPU and device speed mismatch, coordinate the logical record size and physical record size inconsistent problem, high CPU and device parallelism, reduce I/O operations on THE CPU interrupt times, relax the CPU interrupt response time requirements

The buffer

A storage area in memory used to temporarily store data for I/O operations

operation

Write operation: sends data to the buffer until it is full, the process continues to compute, and the system writes the contents of the buffer to the device

Read operation: The system reads the physical record on the device to the buffer, reads the required data from the buffer and sends it to the process as required

A single buffer

The operating system creates a buffer in the system area of main memory

Input: Reads data to the buffer, the system sends the buffer data to the user area, the application processes the data, and the system reads the next data

Output: Copies data from the user area to the buffer, and after the system outputs the data, the application continues to request output

double-buffering

Use two buffers

Input: The device first enters the data into buffer 1, from which the system passes the data to the user area for processing by the application, while the device passes the data to buffer 2

Output: The application passes data from the user to buffer 1, the system passes data to the device, and the application passes data to buffer 2

Data can be entered from the device to the cache and from the cache to the user simultaneously

Circular buffer

The operating system allocates a set of buffers, each with a link pointer to the next buffer, to form a circular buffer

Resolve device and process speed mismatch.

These caches are common system resources that are shared by processes and uniformly allocated and managed by the system

Equipment independence

Users usually specify logical devices instead of physical devices to separate user jobs from physical devices, and then establish mappings between logical devices and physical devices in other ways

For example, when users write programs to achieve the printing function, when the printing function is not to specify a specific printer, but just declare the need for a printer, by the system to deploy a printer

To convert a logical device name into a physical device name, the system needs to provide a table corresponding to the logical device name and physical device name for device management

advantages

  • The application program is independent of physical devices. When the system adds or changes devices, the source program does not need to be modified
  • Easy to handle I/O device faults and improve system reliability
  • Increase the flexibility of equipment allocation, more effective use of equipment resources to achieve multi-channel programming

Device allocation method

Exclusive device: Can only be used exclusively by one process

allocation

  • Static allocation Allocates resources before programs run. This method is simple to implement and prevents system deadlocks, but reduces device utilization
  • Dynamic allocation improves device utilization

Device allocated data structure

Device class table
  • Each type of device corresponds to a column in the device class table
  • Includes device type, total number of devices, number of idle devices, and start address of device table
  • It is used to support device independence
Equipment list
  • Each type of device has its own device table, which registers each physical device in this type of device
  • Including: physical device name (id), logical device name (ID), process ID that occupies the device, whether to assign, good/bad flag, etc

disk

The physical structure

A disk consists of multiple disks

Each disk is divided into concentric circles of tracks

A cylinder of tracks at the same position on different plates is called a cylinder

Each track is divided into a fixed number or an unequal number of sectors

To address a large number of sectors, the operating system groups adjacent sectors into clusters to store files

Physical block (sector) address:

  1. Cylinder number, head number, sector number

  2. Plane X, channel Y, sector Z

    The “face” in “side X, track Y, sector Z” refers to the magnetic head (disk), not the cylinder, sector starts at 1

Read and write data

When reading or writing data, the magnetic head must be positioned at the beginning of the specified sector on the specified track

process
  1. Seek: Control the moving arm to the specified cylinder, select the magnetic head number
  2. Rotation: Wait for sectors to be read or written to rotate under the head
  3. Data transfer

Disk access time = Seek time + rotation time + Transfer time

Driven scheduling

Move the arm scheduling

To minimize the movement time of the moving arm, thus reducing the total path seeking time

First in first out
  • The moving arm moves randomly and the seeking performance is poor
  • Process requests in order, fair to all processes
Minimum search time is preferred
  • The request with the shortest search time is executed first, which has better seek performance
  • There is hunger
One-way scan
  • The mobile arm always sweeps in one direction and does not provide service on the way home
  • Suitable for continuous large number of uniformly distributed cylinder requests
Two-way scanning
  • The moving arm moves in one direction at a time, processes the nearest I/O request, and moves in the opposite direction when it reaches the last cylinder
  • Slow response to requests for the area covered by the most recent scan
The elevator dispatching
  • When there is no request, the moving arm stops, and when there is a request, it moves according to the rule of the elevator
  • Each time the nearest cylinder is selected along the moving direction of the moving arm
  • Change the direction of movement if there is no request in the current direction but there is a request in the opposite direction
Rotation scheduling

Minimizes the total time of rotation delay

Cycle sorting
  • By optimizing I/O request ordering, access requests located on the same cylinder are completed within the minimum number of turns
  • Rotation position measurement hardware and multi-head simultaneous reading and writing technology can improve the efficiency of rotation scheduling
To optimize the distribution of

Rotation delay is reduced by the way information is arranged in storage space

  • Cross factor If sectors are numbered in sequence along the track, the disk speed may be too fast. As a result, data in the current sector may be skipped before data in the next sector is processed, and data can be read only after a revolution is made. Therefore, sector numbers are numbered with intervals. For example, a crossover factor of 2:1 means that one sector will be separated between adjacent numbers, and 3:1 means that two sectors will be separated.
  • When continuous data is recorded on different tracks on the same cylinder and then the cylinder is changed, the arm shift operation during data reading and writing can be reduced.

Virtual device

The use of one physical device to simulate another

Eg: Memory card emulates disk, block device emulates character device

A classic SPOOLing system

To store input and output data, the system creates input and output Wells on disk

Wells serve as buffer storage areas

composition
  • Preinput program: transfers data from input devices to disk input Wells
  • Slow output program: transfer data from disk output Wells to output equipment
  • Well management program: Controls the exchange of data between operations and Wells
role
  • Preinput: The operating system preenters the input data required by the job in batches from the input device into the input buffer on disk temporarily
  • When a job is scheduled to execute, the data used by the job is read from the disk’s input buffer instead of starting the input device
  • Buffering: The job does not start the output device, but merely stores the output data temporarily to the output buffer on disk
  • After the job is executed, the operating system outputs the job in batches

Not only is the utilization of equipment improved, but the running time of jobs is shortened, and each job feels like it has the exclusive equipment it needs

File management

The file system

File system is the module of accessing and managing information in the operating system. It manages the storage, retrieval, update, sharing and protection of user and system information in a unified way, and provides users with a set of convenient and effective methods of using and operating files

The term file not only reflects the logical structure of the user’s concept, but is also closely related to the storage structure of the auxiliary storage (also known as file storage) in which it is stored

function

User-oriented functions of the file system
  • File access by name
  • File sharing and protection
  • File manipulation and use

To implement these features, the OS must consider:

  • Establish and maintain the file directory
  • Allocate and reclaim storage space
  • Data confidentiality and protection
  • Monitor users’ access to and modification of files
  • The realization of information representation, addressing method, storage order, and information retrieval in different storage media

File storage

Volume and block

  • A volume is a physical unit of storage media, corresponding to a tape, a floppy disk, a CD-ROM, and a hard disk partition

  • A block is an area of continuous information on a storage medium, also known as a physical record

    Block The physical unit of information exchange between main and auxiliary storage, always exchanging one or an integer block of information at a time

    For the requirement of starting and stopping mechanical action or identifying different blocks, there must be a gap between two adjacent blocks. The gap is the area between blocks that does not record user code information

File structure

Logical structure of the file

The logical structure of files can be divided into two forms, streaming files and recording files

Streaming file

The data in a file is no longer a record, but a stream of information consisting of a sequence of bytes

Such files often read the desired information by length, or they can be delimited by special characters inserted

Recording document

Is a structured file, which is a record flow file composed of several logical record information

A logical record is a unit of information in a file that is divided according to its logically independent meaning

For example, the salary information of each employee is a logical record; The wage information of whole unit worker formed the record type file of this unit wage information

The physical structure of the file

The physical structure and organization of files refer to the storage methods and organization relationships of files in the physical storage space

The file storage structure involves many problems, such as partition of blocks, arrangement of records, organization of indexes, search of information and so on

Sequential file

The sequential structure is formed by storing logically continuous information in a file in sequential adjacent blocks of the storage medium. This kind of file is called sequential file, also known as continuous file

Advantages: Fast sequential access to records

Disadvantages: Before creating a file, you need to determine the file length in advance to allocate storage space; Difficulty modifying, inserting, and adding file records

It’s like an array

Connection file

Connection file, also known as series file; The join structure is characterized by the use of join words to indicate the sequence of physical blocks in a file

The physical address of the first block of file information is given by the file directory, and the link word of each block indicates the location of the next physical block of the file. If the content of the connection key is 0, the file ends

It’s like a linked list

Advantages:

  • Easy to add, delete and change file records, easy to dynamically grow records
  • You don’t have to be sure of the file length in advance
  • The storage space usage is high

Disadvantages:

  • Additional storage space is required for storing Pointers
  • Since access must pass through the buffer, after obtaining the connection word, can find the address of the next physical block, therefore, only suitable for sequential access
The file directly

A direct file, also known as a hash file, establishes the corresponding relationship between the physical storage address and the computed record keywords

This transformation is usually done by hashing.

It’s like a hash table

Index file

An index file creates an index table for each file, where each table entry contains a record’s key (or logical record number) and its storage address

The address of the index table can be indicated by the file catalog. The index table can be consulted to find the corresponding record key (or logical record number), and then obtain the data storage address

access

Index files are divided into two areas of file storage: index area and data area

Accessing the index file requires two steps:

  1. Find the index table,
  2. Get the physical address of the record

Two accesses to secondary storage are required, and if the file index has been pre-called into main storage, one exchange of internal and external memory information is reduced

The characteristics of

Advantages: index structure can be considered as an extension of the connection structure, in addition to the advantages of connecting files, but also overcome the disadvantage of it can only make sequential access, with the ability to directly read and write any record, easy to add, delete, change files

De-click: increases the space overhead and lookup time of index tables

File directory

File directory structure

One of the basic functions of a file system is the establishment, maintenance, and retrieval of file directories. The directories should be easy to find and avoid conflicts

Primary directory structure

A linear table is constructed in the operating system, occupying a directory entry with the related attributes of each file, constituting a level 1 directory structure

Secondary directory mechanism

The first level is the master file directory, which is used to manage all user file directories. Its directory entry registers the name of the user accepted by the system and the address of the user file directory

The second level is the user’s file directory, which holds a registration bar for each file of the user, with the same contents as the directory entries in the first level directory

Each user is allowed to view only his or her own file directory

Tree directory structure

Each level of directory can register the next level of directory, also can register files, thus, the formation of hierarchical file directory structure

The hierarchical directory structure usually adopts the tree directory structure, which is a inverted tree with roots, and the root is the root directory. From the root down, each tree fork is a subdirectory; And leaves are documents

The full name of a file includes all subdirectory paths encountered along the path from the root directory to the file, also known as pathnames

Directory Management

To find the
File search

File access by name means that the system searches all levels of file directories according to the file path name provided by the user to find the file

  • You can look up from the root (absolute pathname)

  • You can also look up from “current directory” (relative pathname)

    The current directory is represented by.,… Parent directory

  • Modern operating systems have the change working directory command, that is, to change the current working directory

Directory entry lookup

When searching specific directory items, you can use the sequential search method, scan the directory items in the file directory in turn, compare the name of the directory items with the name of the file to find, you can use some optimization methods to speed up the search directory, such as dichotomy, hash

File directory processing

One problem with the tree directory structure is that it is inconvenient to use a file when it passes through many directory nodes. When the system is searching the directory along the path, it often needs to visit the file memory for many times, which greatly slows down the access speed. If the directories of all files are copied to the main memory, the access speed is accelerated, but the overhead of the main memory is increased.

An effective way is to copy the frequently used and currently used file directories into main storage, which does not increase the main storage overhead too much, and can significantly reduce the directory search time

Active file list

The system can establish an active file table for each user process. Before the user uses a file, the directory information about the file is copied to the specified main storage area through “open” operation, and the relevant information is filled into the active file table to establish the connection between the user process and the file index

When the file is no longer in use, use Close to disconnect the user process from the file. At the same time, if the directory has been modified, update the corresponding file directory in the auxiliary memory

File security and protection

File is an important resource of computer system, therefore, file system is required to have the means to ensure file security, provide file security measures, and effectively realize file sharing

File sharing

Refers to the common use of certain files by different users

Concurrency control

In systems that allow file sharing, the operating system should provide a means to synchronize control of shared files

  • Multiple processes may simultaneously access a file, and if they simultaneously read, the operating system should have common control over the file
  • If two processes perform write operations, for example, process A requires file modification and process B requires data read from the same file, the OPERATING system must provide A synchronization control mechanism to ensure file data integrity
Security measures

File confidentiality means that the file and its contents cannot be stolen by other users without the authorization of the file owner. The measures are as follows:

  1. Hidden file directory

  2. Set the password

  3. Using the key

    Setting a password means that you need to enter a password before opening the file, but the file is stored on disk, so you can directly use the key to encrypt the file itself, so that without the key, the content of the file itself is incomprehensible

File protection

To prevent files from being destroyed

File systems must have the ability to prevent hardware and software failures and preserve information integrity, and file copy is the main implementation mechanism

Dynamic multiple copy technology

A file that maintains the same content on multiple media and updates the content simultaneously

Dump, backup, and restore

Periodically copy and dump files to other media. When a media fails, the dumped files are recovered

  • One is that at a certain interval or the end of a unit of processing, the system automatically duplicate the updated files and data
  • The second is to copy all the file information every day or every week, and then RESTORE the system by loading the dump file when necessary, such as BACKUP, RESTORE and other commands

The file is confidential

Prevents files and contents from being stolen by other users

File access control matrix

The system sets the access properties for each user to access each file object

The access control matrix is a two-dimensional matrix composed of all users’ access attributes to all files

Access control table

Since the operating system has many users and many files, the ACCESS control matrix is a sparse matrix, which can be simplified into an access control table

Each line contains: user, file, and access properties

The access control table registers only those parts that have access attributes to a file

File protection based on Access control matrix/table

Access properties: can have access, read, write, execute, create, delete, authorize, etc

Authorization is when the owner of a file grants permissions on a file to other users

The system checks the user’s access to the file by consulting (matrix/table)

System management users (super users) are granted the rights to access system files

File attributes

A simplified approach to access control tables is to categorize users and specify file attributes for each class of users

  • Categories of users: owner, partner, others

  • File properties: Read, write, Execute…

  • File attributes can be placed in file directory entries, simplifying management

  • When using files, users can check file properties to protect them

    read write perform
    File the main 1 1 0
    partner 1 0 0
    It is the user 1 0 0

File access

The techniques and means provided by the operating system for user programs to use files depend to some extent on the physical structure of files

Sequential access

Read/write operations in record order

The read operation reads the current record from the read pointer while advancing the read pointer to the next record to be read

Write sets the write pointer, writing a record to the end of the file while pushing the write pointer

Allows n (integer) records to be jumped forward or backward on a read pointer

Direct access

Many applications require the ability to quickly read and write records directly in any order

For example, the airline booking system uses the flight number as a mark to store all the information of a specific flight in a physical block. When users book a flight, they can directly calculate the location of the flight

Indexed access

Index access method based on index file

For such files, the address of the information block can be translated by looking for the record key

In practical systems, multilevel indexes are used to speed up the record search process

Use of files

Users communicate with file systems through two types of interfaces

  1. Action commands related to files

    For example, in UNIX, cat, CD, cp, find, mv, rm, mkdir, rmdir, etc

  2. The second type is file-like system calls that are provided for use by user programs

    The basic file class system calls are: create, open, read/write, Locate, close, undo

Set up files

Create a file

Required parameters: file name, device class (number), file properties, and access control information

Process: Create a file directory entry on the corresponding device, allocate the first physical block for the file, apply for an entry in the active file table, register the directory information, and return a file handle

Cancel the file

Delete a file

Required parameters: filename and device class (number)

Processing flow: If the file is not closed, close the file first; If the file is shared, joint access processing is performed. Delete the corresponding directory entry in the directory file; Release file storage space occupied by files

If the file is shared, it may not be physically deleted, but only for the user

Open the file

Used to establish usage connections between files and user processes

Required parameters: file name, device class (number), and opening mode

Processing flow: apply for an item in the main memory active file table, return a file handle; Follow the file name to find directory files, copy the directory information to the active file table corresponding column; Check access validity according to access control instructions; If you open a shared file, you should handle it accordingly

Close the file

End reading or writing to a file

Required parameters: file handle

Processing flow: Reduce “Current user number” of the file in the active file table by 1; If this value is 0, the active file table is recalled; Complete “Write later”; If the contents of the active file table have been modified, the contents of the table should be written back to the corresponding table on the file storage to keep the file directory up to date

Read/write files

Required Parameters Parameters: file handle, user data area address, number of read and write records or bytes

Processing flow: according to the file handle from the active file table to find the file directory information; Converts related logical records into physical blocks based on the logical and physical organization of the file indicated by the directory entry

Locate the file

Used to adjust the read/write pointer position of an open file

Required parameters: file handle, location pointer

Use of auxiliary storage space

Large auxiliary storage space such as disks is shared by the OS and many users. Files are often created and deleted during the running of user processes. The OS should be able to automatically manage and control the auxiliary storage space

As user files are created and destroyed, file storage space can become fragmented. OS’s solution to ‘fragmentation’ is to sort out ‘fragmentation’; In the collation process, files are often reorganized and placed in contiguous storage

allocation

Continuous allocation: Storage in contiguous storage of auxiliary space (contiguous physical block numbers)

  • The sequential access is fast and easy to manage. However, in order to obtain a large contiguous storage area, periodic defragmentation is required

Discontinuous allocation: Dynamic allocation to a number of sectors or clusters (several consecutive sectors) without requiring continuity

  • The advantage is that the auxiliary storage space management efficiency is high, convenient for the dynamic growth and contraction of files

Management of free blocks

Shown in figure

A table consisting of several bytes, in which each bit corresponds to a physical block, and the order of the bits corresponds to the relative order of the blocks. If the byte bit is 1, the corresponding block is occupied. If the byte bit is 0, the block is idle

Its main advantage is that the bitmap can be stored in main memory, and then combined with the bit-operation instructions of modern computers, high-speed physical block allocation and allocation can be realized

Free block group join method

Hard disks are divided into blocks for management. The blocks are divided into two types: occupied (storing data) blocks, which are called dedicated blocks, and free blocks, which are not occupied. Free blocks form groups and are connected together, as shown in the figure

The top line of each free block group shows the number of free blocks

Each free block group contains a maximum of 100 free blocks, numbered from 0 to 99. Free blocks 0 are used as Pointers, and free blocks 1 to 99 are used as actual free blocks.

Whenever free blocks need to be allocated:

if(this. Number of free blocks =1) {if(thisThe first unit ==0){waiting for free blocks to be allocated; }else{x = the free block group to which a cell points; Allocate free blocks of X; X. Number of free blocks --; }}else if(this. Number of free blocks >1{distributionthis. Free block;this.allocate free block --; }Copy the code

Whenever a free block is returned:

if(this. Number of free blocks <100){return the free block;this. Number of free blocks ++; }else if(this. Number of free blocks ==100){free block group x =newFree block group (); X. Number of free blocks =1; X. First unit =this; Add free blocks to x; X. Number of free blocks ++; }Copy the code

The implementation hierarchy of the file system

Concurrent programming

concept

Sequential programming

Each program executes in strict order on the processor

The general habit of programming is sequential programming: the design of the solution of a specific problem into a program or, if strictly sequential, a sequence of programs, which is called the external sequentiality of program execution

Characteristics of the
  • Sequential execution: The execution of program instructions is strictly sequential
  • Closure of computing environments: Programs run as if they are exclusive resources protected by the operating system
  • Certainty of calculation results: program execution results have nothing to do with execution speed and execution time
  • Repeatability of the computation process: the execution trajectory of the program on the same data set is determined

Concurrent execution of processes

Multiprogramming allows multiple programs to enter memory at the same time to compete for the processor to run. The OS allows a computer system to have more than one running process at a time, that is, multiple processes to execute concurrently. The OS guarantees that programs written in a sequential programming approach will not be affected by concurrent execution, as if they were monopolizing the computer. These sequential programming processes execute unrelated concurrent processes in the OS.

While (1) {input, process, output} while(1) {input, process, output}

Processor utilization: 52/(78+52+20)≈35%

Instead, design three programs that run independently and let them run concurrently in a multi-program system

  1. While (1) {input, send}
  2. While (1) {receive, process, send}
  3. While (1) {receive, output}

Processor utilization: (52N) /(78N +52+20)≈67%

Concurrent programming

A method of designing the solution of a specific problem into a number of simultaneously executable program modules

features
  • Parallelism: Multiple processes execute concurrently in a multiprogram system or in a multiprocessor system, improving computational efficiency
  • Shareability: Multiple processes share software resources
  • Interactivity: Constraints exist when multiple processes execute concurrently, which increases the difficulty of programming

Constraints between concurrent processes

Independent concurrent processes: A group of concurrent processes run on different sets of variables, and the execution of one process is independent of the progress of other concurrent processes

Interacting concurrent processes: A group of concurrent processes share certain variables, and the execution of one process may affect the results of other concurrent processes

Time-related errors

For a group of interacting concurrent processes, the relative speed of execution cannot be mutually controlled. If the program is not designed properly, all kinds of “time-related” errors can occur.

  • The result error

    The two processes almost read and write data at the same time, causing data read and write errors

    If the data in x is 1, A and B both read and add 1 to write back, maybe they both read x is 1, when A writes back, B writes back, the final result is that B’s result overwrites A’s result, and if they execute separately, it is +2, and only +1 in the end

  • Wait forever

    For example, when process A applies for resources and finds that there are no resources, process A waits. Process B returns the resources later, but Process A does not check whether resources exist

Process mutual exclusion synchronizes with process

Interactive concurrent processes must be restricted in execution to ensure reasonable results

Process exclusion: Competing constraints between concurrent processes that compete for exclusive resources

Process synchronization: Cooperative constraints between concurrent processes that coordinate execution priorities based on certain conditions to complete a common task

A critical region

Critical resources: Resources represented by mutually exclusive shared variables that can only be used by one process at a time

A critical section is a segment of a concurrent process that is associated with mutually exclusive shared variables

When multiple concurrent processes access critical resources, there are competition constraints. If two processes stay in related critical regions at the same time, a time-dependent error occurs.

Description of critical region

Determine the critical resource shared

Region

do < statement_list>

The critical sections of both processes have the same critical resource, that is, the related critical section, which must be mutually exclusive

Three requirements for critical area management

  1. Allow at most one process at a time to remain within the relevant critical region
  2. A process cannot remain in a critical region indefinitely
  3. A process cannot wait indefinitely to enter a critical section

Nested use of critical sections

It is not necessarily possible for nested use of critical sections to generate deadlocks, as long as the order in which critical resources are applied is determined, there will be no deadlocks

Critical area management

Box (test lock, establish lock) two instructions, the execution process can not be interrupted, if interrupted may simultaneously enter the critical area or deadlock.

Test and set up instructions
TS(x) {
    if (x==false) { 
        x=true; return true;
    }else return false;
}
Copy the code
Boolean lock; lock = false; Process Pi {// I = 1,2... ,n Boolean pi; repeat pi = TS(lock) until pi; // loop request lock critical section; The lock = false; / / unlock}Copy the code
Exchange instructions
swap (a,b) { temp=a; a=b; b=temp; }
Copy the code
Boolean lock; lock = false; Process Pi {// I = 1,2... ,n Boolean pi; pi = true; repeat swap(lock, pi) until ! PI; // loop request lock critical section; The lock = false; / / unlock}Copy the code

TS and swap instructions are busy waiting, which is inefficient

interrupt

The simple solution is to break the switch when entering or leaving a critical section, so that the critical section execution does not break and the execution has atomicity

The interrupt; The critical area; Open the interrupt

Operating system primitives take this approach

However, the critical section instruction length should be short and concise, so as to ensure the efficiency of the system, do not recommend the use of user programs, abuse is terrible!

PV operation

TS or SWAP instruction management critical area, using busy polling, low efficiency

Turn off the interrupt management critical section, which is inconvenient for user programs to use

Reference: The operating system uses request-wait-Interrupt recovery to access hardware resources

Recording semaphore

A soft resource with a numerical value

typedef struct semaphore {
    int value; // Signal value
    struct pcb *list; // The semaphore waits for the process queue pointer
}
Copy the code
  • Each semaphore creates a queue list of waiting processes

  • Each semaphore is associated with an integer value

    • A positive value indicates the number of times a resource can be reused
    • A value of 0 indicates that there are no resources and no process is waiting
    • A negative value indicates the number of processes in the waiting queue

PV operation primitive

P Operation primitives (applying for resources)
procedure P(semaphore:s) {s = s --1; // Semaphore minus 1
    if (s < 0) W(s); // If the semaphore is less than 0, the calling process is set to wait for the semaphore s
}
Copy the code
V Operation primitives (free resources)
procedure V(semaphore:s) {
    s = s + 1; // The semaphore increases by 1
    if (s <= 0) R(s); // If the semaphore is less than or equal to 0, release a process waiting for the semaphore s
}
Copy the code

PV operation to solve the process mutually exclusive framework

semaphore s; s = 1; Cobegin process Pi {...... P(s); The critical area; V(s); ... } coend;Copy the code

PV operation solves process synchronization

Concurrent processes coordinate execution priorities based on certain conditions in order to complete a common task

The execution of one process waits for messages from other processes

1 Producer 1 Consumer 1 buffer problem

Producers and consumers share buffers

When the buffer is empty, the producer can put the product in, otherwise wait

Buffer has products when consumers take out products, otherwise wait

Synchronous relation 1: consumers are waiting for the arrival of the product at the beginning, and consider setting a semaphore (waiting for the product); We start with no product, we start with 0

Synchronization relation 2: the consumer has a vacancy in the wait buffer and can also set a semaphore (wait buffer); The buffer starts out empty, with an initial value of 1

Int B; // Share the bufferSemaphore sput;// Number of empty buffers that can be used
Semaphore sget; // The number of products that can be used in the buffer
sput = 1; // One item is allowed in the buffer
sget = 0; // There are no products in the buffer

process producer {
	while(1){produce a product; P(sput);//
        B = product;
        V(sget);//
	}
}

process consumer {
    while(1){
        P(sget);//The Product = B; V(sput);//Consume a product; }}Copy the code
1 Producer 1 consumer N Buffer problem
Int B[k]; // Share buffer queueSemaphore sput;// Number of empty buffers that can be used
Semaphore sget; // The number of products that can be used in the buffer
sput = k; // Allow k items in the buffer
sget = 0; // There are no products in the buffer
Int putptr, getptr; // Loop the queue pointer
putptr = 0; getptr = 0;

process producer{
    while(1){produce a product; P(sput);//B [putptr] = product; putptr =(putptr+1) mod k;
        V(sget);//
    }
}

process consumer{
    while(1){
        P(sget);//
        product= B[getptr];
        getptr=(getptr+1) mod k;
        V(sput);//consume a product; }}Copy the code
N producer n consumer n buffer
Int B [k]; Semaphore sput;/* Number of empty buffers available */
Semaphore sget; /* The number of products available in the buffer */
sput = k; /* The buffer allows k items */
sget = 0; /* There are no products in the buffer */
Int putptr, getptr; putptr = 0; getptr = 0;
Semaphore s1,s2; /* Mutually exclusive use putptr,getptr */
s1 = 1; s2 =1;

process producer_i {
    while(1){produce a product; P(sput);//
        P(s1);/ / *B [putptr] = product; putptr =(putptr+1) mod k;
        V(s1);/ / *
        V(sget); //
    }
}

process consumer_j {
    while(1){
        P(sget);//
        P(s2);/ / *The Product = B [getptr]; getptr=(getptr+1) mod k;
        V(s2);/ / *
        V(sput);//Consume a product; }}Copy the code
Apples and oranges
Int plate; Semaphore sp;/* You can put some fruit on the plate */
Semaphore sg1; There are oranges on the plate */
Semaphore sg2; /* There are apples on the plate */
sp = 1; /* One fruit is allowed on the plate */
sg1 = 0; There are no oranges on the plate */
sg2 = 0; There is no apple on the plate

process father {
    while(1){peel an apple; P(sp); Put the apple on the plate; V(sg2); } } process mother {while(1){peel an orange; P(sp); Put the orange on the plate; V(sg1); } } process son {while(1){ P(sg1); Take oranges from the plate; V(sp); Eat oranges; } } process daughter {while(1){ P(sg2); Take the apple from the plate; V(sp); Eat an apple; }}Copy the code

Process of communication

Process communication is the transfer of information between processes

The interaction process realizes the mutual exclusion and synchronization through semaphore operation, which is a low-level communication mode

Sometimes processes also need to exchange more information (such as sending data to another process). Advanced communication method — process communication mechanism can be introduced to realize the exchange of information by letters between processes

Direct process communication

The process that sends or receives a letter indicates to whom the letter is sent or received from

  • Send (P, letter) : Sends a letter to process P
  • Receive (Q, letters) : Receives letters from process Q

Process indirect communication

Mail is sent or received through a mailbox that has a unique identifier and is shared by multiple processes

  • Send (A, letter) : to send A letter to A mailbox
  • Receive (A, letter) : To receive A letter from mailbox A
Mailbox for indirect communication

A mailbox is a storage area for letters. Each mailbox can be divided into two parts: a mailbox feature and a letter box

  • Mailbox features indicate mailbox capacity, message format, Pointers, and so on
  • A letter box is used to store letters. The letter box is divided into sections, each containing a letter
Process of sending letter primitives
If the specified mailbox is not full

Then send the letter to the position indicated by the pointer in the mailbox, and release the waiting person waiting for the letter in the mailbox

If the specified mailbox is full

The sender of the letter is put in a mailbox waiting state

Process of receiving letter primitives
If there are letters in the specified mailbox

Take out a letter and release the person waiting for the mailbox

If there is no letter in the specified mailbox

The recipient is placed in a state of waiting for a letter in the mailbox

Advanced process communication mechanism

Stream-based process communication

Multiple processes use a shared message buffer (called pipe, multiplexer, socket)

Some processes write character streams (send/write) to message buffers

Some processes read character streams from message buffers (receive/read)

The information exchange unit is based on a character stream of any length

Rpc-based process communication

Client/server computing mode is adopted

The server process provides a set of procedures/services that the client process invokes

A client process obtains a service by invoking a procedure/service provided by the server process

Given the heterogeneous nature of the hardware of the client and server computers, external data representation XDR was introduced to convert each computer’s special data format to a standard data format

The external data representation XDR is an intermediate format that the client process and the server process use to transmit data in a different format

A deadlock

When multiple processes are allowed to concurrently execute shared system resources, the system must provide synchronization mechanisms and process communication mechanisms. However, if this mechanism is not used properly, the process can be blocked forever

The factors that cause deadlocks are not only related to the amount of resources the system has, but also related to resource allocation policies, resource usage requirements of processes, and the order in which concurrent processes advance

Deadlocks can be resolved in three ways:

  • Deadlock prevention
  • Deadlock avoiding
  • Deadlock detection and recovery

Deadlock prevention

The necessary conditions for a deadlock to occur

  • Mutually exclusive conditions: Processes must be mutually exclusive of resources. A resource can be exclusively used by only one process at any time
  • Possession and wait conditions: a process does not release occupied resources while waiting for an unfulfilled resource request
  • Non-deprivation condition: no process can steal resources from another process
  • Circular wait condition: There is a circular wait chain where each process waits separately for resources held by the process before it

Deadlock can be prevented by breaking one of the four necessary conditions

Break the first condition by transforming an exclusive resource into a shared resource, so that resources can be accessed simultaneously instead of being used mutually exclusively. This is a simple solution, but often impossible for many resources

The third condition can be broken by using the deprivative scheduling method, but the deprivative scheduling method currently applies only to the allocation of main memory and processor resources, not to all resources

Static allocation (preallocation)

Static allocation means that a process must request all the resources it wants before it executes and does not start executing until all the resources it wants are satisfied

The total number of resources required by all concurrently executing processes does not exceed the number of resources available to the system

With static allocation, the process does not claim resources during execution, so it does not occupy some resources and wait for others, which breaks the second condition

Level distribution

This allocation strategy prevents the fourth condition from appearing under a hierarchical allocation strategy, where resources are divided into multiple levels

Once a process has acquired a resource at one level, it can only apply for resources at a higher level

When a process wants to release a resource at a certain level, it must first release the resource at a higher level

When a process obtains a resource at a layer and wants to apply for another resource at that layer, it must first release the occupied resource at that layer

Deadlock avoidance

While deadlocks cannot be prevented, it is still possible to prevent deadlocks by knowing the resource requests associated with each process in a concurrent process

Simply test the system state before allocating resources to the applicant. If allocating resources to the applicant would cause a deadlock, then refuse to allocate resources. Otherwise, accept the application and allocate resources to it

Banker’s algorithm

The system first checks the applicant’s maximum demand for resources, and if the existing resources can meet its maximum demand, the current application is satisfied

That is, the system acts like A bank and evaluates whether or not you can return resources. For example, if you allocate x resources to process A, process A can release all resources, and the number of resources in the system can increase. If there are not x resources left in the system, then allocating resources to process A may not be able to obtain X resources at last, so it will not allocate resources to process A

Eg: Suppose the system has three processes P, Q and R, and the system has only one class of resources, 10 in total. The current allocation is as follows:

process Have accounted for resources Still need application number
P 4 4
Q 2 2
R 2 7

Now 4+2+2=8 resources have been occupied, leaving 10-8=2 resources

For P: 4>2, resources cannot be allocated

For Q: 2<=2, use allocated resources

For R: 7>2, resources cannot be allocated

Deadlock detection

There is no restriction on the allocation of resources, but the system periodically runs a “deadlock detection” program to determine whether the system has deadlocks, and if deadlocks are detected, try to remove them

One way to detect this: You can set up two tables to record resource usage by a process

  • The waiting resource table records the resources that each blocked process is waiting for
  • The occupied Resource table records the resources occupied by each process

When a process applies for a resource, check whether the resource is occupied by another process. If the resource is free, the resource will be allocated to the applicant and logged into the occupied resource table; Otherwise, the login process waits for the resource table

detection

The deadlock detection program periodically detects these two tables. If process Pi waits for resource Rk and RK is occupied by process Pj, then Pi and Pj are said to have “waiting occupation relationship”, denoted as W(Pi, Pj).

The deadlock detection program checks these two tables repeatedly to list all the “wait occupancy relationships.”

If W(Pi, Pj), W(Pj, Pk),… , W(Pm,Pn), W(Pn, Pi), obviously, there is a group of processes waiting for resources in the system: Pi, Pj, Pk… Pm Pn, which means there’s a deadlock

process P1 P2 Pn
P1 b11 b12 bn2
P2 b21 b22 bn2
Pn bn1 bn2 bn2

B I j = {1, 0 when P I and waiting for resources occupied by P j, B_ {ij} = \begin{cases} 1,&\text{when}P_i\text{and wait for resources occupied by}P_j\text{}\\[2ex] 0 ,&\text{when}P_i\text{and}P_j\text{there is no wait occupancy relationship} \end{cases} bij= op ⎨⎧1,0, when Pi is waiting for the resource occupied by Pj there is no wait occupancy relationship between Pi and Pj

Then such a table can be regarded as a directed graph represented by an adjacency matrix, and then determine whether the graph has a loop, if so, there is a deadlock

To deal with

Restarting process execution can be used, and the recovery effort should involve restarting one or all of the processes and from what point

All involved in deadlock start from scratch, but this cost is quite large, in the process of execution in the periodic set checkpoint, from the checkpoint restart

Aborts a process involved in a deadlock and reexecutes it later