Operating System Principle (2) – Operating Environment and Operating Mechanism


Processor state

  • Kernel state(Kernel Mode): Run operating system programs.
  • User State(User Mode): Run the user program.
  • User state Kernel state: interrupt / exception / trap mechanism.
  • Kernel State User State: Set the program status word PSW.
  • Privilege directivesInstructions that can only be used by the operating system and not by user programs.
  • Non-privileged directivesUser programs can use instructions.

A central processor

  • The processor is composed ofArithmetic unitControllerA series ofregisteras well asCachingComposition.
  • Two types of registers

    • User Visible RegisterHigh-level language compiler allocates and uses it through optimization algorithm to reduce the number of memory accesses.
    • Control and Status RegisterUsed to control the operation of the processor, usually by operating system code.

Control and Status Register

  • Used to control the operation of the processor.
  • It can be accessed and modified at a certain level of privilege.
  • Common Control and Status Registers

    • Program counter(PC: Program Counter): Record the address of the instruction to be taken out.
    • Instruction register(IR: Instruction Register): Record the instructions recently taken out.
    • Program status word(PSW: Program Status Word): Record the running state of the processor, such as conditional code, mode, control bit. Set up a special bit in PSW, according to the running program.Permission to use resources and instructionsSetting different CPU states.

Interruption and exception mechanism

It can be said that the operating system is made up of“Interrupt drive” Or“event driven” Yes.

Main role

  • Handle interruption requests from equipment in time.
  • It enables the operating system to capture service requests from user programs.
  • Prevent destructive activities during the execution of user programs.


  • CPU happens to the systemAn eventA response.
  • The CPU suspends executing programs.Preserve the sceneAutomatically switch back to executing the corresponding eventprocessing programWhen the processing is completed, the breakpoint is returned.Continue implementationInterrupted procedures.
  • interruptExternal events, which are not expected by the running program.
  • abnormalTriggered by an executing instruction.

Operating System Principle (2) - Operating Environment and Operating Mechanism

Working principle

  • Hardware work(Interrupt/exception response):captureInterrupt/exception requests from interrupt sources in a certain wayresponseTo control the processorHandSpecific handlers.
  • Work of software(Interrupt/exception handler):DistinguishInterrupt/exception type and complete the correspondingProcessing operations

Operating System Principle (2) - Operating Environment and Operating Mechanism

  • Interrupt vectorA memory unit for storageInterrupt Handler Entry AddressAnd what the program needs to runProcessor status word。 The execution process transfers control to the corresponding interrupt handler through the interrupt vector table according to the interrupt number/exception type.

    Operating System Principle (2) - Operating Environment and Operating Mechanism
    Operating System Principle (2) - Operating Environment and Operating Mechanism

Interrupt response diagram

Operating System Principle (2) - Operating Environment and Operating Mechanism

Interrupt Processing Procedure

  • When designing the operating system, the corresponding processing program is programmed for each kind of interrupt/exception event, and the interrupt vector table is set up.
  • If the system responds to interruption during operation, the interruption of hardware components will result in CPUcontrol powerTransfer to interrupt handler:

    • PreservationRelevant register information.
    • AnalysisSpecific reasons for interruption/anomaly.
    • implementThe corresponding processing function.
    • recoveryOn the spot, return to the procedure interrupted by the incident.

system call

  • Users can call when programmingOperating System Functions
  • The operating system isProcess running in user modeandHardware equipmentThe set of interfaces provided for interaction is set upBetween applications and hardware devicesAn interface layer. It can be said to be an interface left by the operating system to the user program.
  • Each operating system provides hundreds of system calls (process control, process communication, file usage, directory operation, device management, information maintenance, etc.).

The Relation and Difference between System Call and Library Function

  • What the system call provides to the user isDirect and pure high-level servicesIf we want to be more humane and have functions that are more suitable for specific situations, then we need to define them by our users themselves, so we derive library functions, which call part of the system.PackingGet up. For example, when we want to print a sentence in C language, if we don’t use the library function printf, then we need to call putc () and write () and other system functions.
  • System call is for the convenience of using the interface of operating system, while library function is for the convenience of programming.
  • In terms of portability, different operating systemssystem callGenerally different,Poor transplantabilityIn all ANSI C compiler versions, C library functions are the same.

Execution process

When the CPU executes a special trapped instruction:

  • Interrupt/exception mechanism

    • Hardware protection site
    • Transfer control to the user by looking up the interrupt vector tableSystem Call General Entry Program
  • System Call General Entry Program

    • Preservation site
    • Save parameters in the kernel stack
    • Transfer control to the corresponding system call processing routine or kernel function by looking up the system call table
  • Execute system call routines
  • Restore the scene and return to the user program

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]