Operating System Introduction

Computer System Architecture

主要由Memory,CPU,I/O Devices构成

  • Memory
  • CPU
  • I/O Devices
    • disks
    • mouse
    • keyboard
    • printer
    • monitor
    • ...

ComputerSystemArchitecture


I/O Device In Computer

ComputerSystemArchitecture


CPU and Memory in Computer

CPU与Memory间的关系 可以分成三种模型

CPU


Operating Systems

Abstract
  • 操作系统作为用户和计算机硬件的中介
  • 它主要包括kernel,以及系统程序,应用程序,中间件(软件框架,如数据库,多媒体,...)
  • 操作系统是中断驱动的
    • 内核中的设备驱动器管理支配设备
    • 设备控制器通过中断告知CPU,设备已经完成操作
    • 内核的代码只有当中断发生时才执行
General goals of operation systems
  • Execute user programs and make solving user problems easier 更简单的执行 User Programs
  • Make the computer system convenient to use 更方便的使用Computer System
  • Use the computer hardware in an efficient manner 更高效的使用Computer Hardware

What is Operating System ?

OS Include:

  • Kernel Part of a operating system, resides in memory at all times on the computer
    KernelInMemory
  • Others
    • System programs -all programs associated with the operating system, but not in kernel
    • Application programs - all programs not associated with the operating system
    • Middleware中间件– a set of software frameworks that provide additional services to application developers such as: databases, multimedia, graphics
Operating System Is Interrupt-Driven
  • Device driver inside the kernel knows how to talk and manage the device
    在kernel中的设备驱动知道如何管理设备以及和设备交流
  • Device driver provides uniform interface between controller and kernel
    设备驱动器为控制器和内核提供统一接口
  • Each device controller informs CPU that it has finished its operation by causing an interrupt
    每个设备控制器通过中断告知CPU,它已经完成了它的操作
    The code of the kernel is in memory all the time but the code of the kernel is only executed when there is an interrupt, on demand!
    内核的代码一直在内存中,但内核的代码只有在出现中断时才会按需执行!
    Device Driver In Kernel

Interrupts 中断

Abstract
  • 当软硬件发生需要OS立刻处理的事件时中断发生
    • 在软件层面:由Error或者System call引起中断
    • 在硬件层面:由Device 引发
  • 由ISR决定对于不同中断应该采取什么行动
Interrupt

An interrupt is a signal emitted by hardware or software when a process or an event needs immediate attention

  • Software: A trap or exception is a software-generated interrupt caused either by an error or a user request (system call)
  • Hardware: Device
    [!abstract] Interrupt Service Routines (ISR)
    ISR inside the kernel determine what action should be taken for each type of interrupt

Interrupt Handling Steps

When there is an interrupt, the operating system

Preserves the state of the CPU

  • by storing registers and the program counter(PC) for the software that was just interrupted (so that the same software can be restarted later).

Determine type of interrupt

  • check Interrupt vector to get the address of corresponding ISR for this interrupt (used on all modern computers)

Runs ISR in kernel to handle interrupt


DMA

Direct Memory Access (DMA)

DMA

  1. Device controller transfers blocks of data from buffer directly to main memory without CPU intervention, In the meantime the CPU can work on something else.
  • DMA允许设备控制器直接将数据块从缓冲区传输到主内存,而不需要CPU干预。
  • 这样做的好处是CPU可以同时进行其他计算任务,而不需要管理这些数据传输。
  1. Only one interrupt is generated per block
  2. High transfer speed: not byte by byte, but block by block

Interrupt-Driven I/O Cycle: Timeline View

Timeline View

CPU

  • CPU有两种状态,一种是执行用户程序,另一种是处理I/O中断。

I/O Device

  • I/O设备的状态可以是空闲的或正在传输数据。

I/O Request

  • 当I/O设备准备好数据交换时,它会发出I/O请求。这通常发生在读取或写入操作开始时。

Interrupt

  • 当I/O设备完成数据传输时,它会发出中断信号给CPU。在图表中,这些中断用垂直的绿线表示。

Timeline

Interrupt Dealing

  • CPU在执行用户程序的过程中会接收到中断信号,然后切换到处理I/O中断的状态。这个过程中,CPU会停止当前的用户程序执行,保存必要的状态,并开始执行与中断处理相关的指令。

Transfer Done

  • 一旦中断处理完成,I/O设备会标记为传输完成,并准备接收下一个I/O请求。

User Program Done

  • 在I/O设备忙于传输数据(绿线下降部分)和CPU处理中断之间的时间,CPU会继续执行用户程序。

Red Circle

  • 图表中的红色圆圈可能代表一个特定事件或异常,例如一个处理延迟或一个需要特殊注意的中断。
    [!abstract]+
    总的来说,这个时间线展示了中断驱动I/O如何使CPU在等待I/O操作完成时可以继续其他任务,从而提高了系统的效率。
    当设备完成数据传输时,它通过中断通知CPU,CPU随后会处理该中断,并在处理完毕后恢复用户程序的执行。这种机制减少了CPU等待I/O操作的空闲时间。
    WorkflowView

Interrupt-Driven I/O Cycle: Two Methods

TwoIOMethods
这张图展示了中断驱动I/O周期的两种方法。
在两种情况下,I/O操作都是由中断控制的,但是用户程序对I/O完成的处理方式不同。

  1. Synchronous Method
    • 在这种方法中,用户程序启动I/O操作后,会一直等待直到I/O操作完成后才继续执行。这是一个同步I/O操作,因为用户程序在请求的I/O操作完成之前不会进行任何其他工作。
    • 用户程序进入等待状态,操作系统的设备驱动程序启动硬件操作,数据传输开始。
    • 一旦数据传输完成,中断处理程序(interrupt handler)会处理相应的中断,并控制权返回到用户程序,用户程序继续执行。
  1. Asynchronous Method
    • 在这种方法中,用户程序在启动I/O操作后不会等待I/O完成,而是立即继续执行其他工作。这是一个异步I/O操作,用户程序可以在I/O操作正在进行时做其他事情。
    • 用户程序发起I/O请求后,控制权立即返回,无需等待硬件操作完成。
    • 数据传输和中断处理会在后台进行,而用户程序在这个过程中可以继续执行其他任务。

这两种方法的选择取决于程序设计的需求。

  • 同步I/O操作简单但可能效率较低,因为它会阻塞程序执行;
  • 异步I/O操作更复杂,但允许多任务同时进行,提高了效率。在多线程环境中,异步I/O特别有用,因为它允许程序在等待I/O完成的同时,继续在其他线程上工作。

Operating System Activities

  • Operating system does a lot of management work
    • Caching
    • Process management
    • Memory management
    • File management
    • Secondary storage management
    • I/O
    • Protection and security

Caching

Cache

  • Caching: Copy information from slower into faster storage system
    TransferDirection

  • Data access procedure: faster storage (cache) checked first

    • Data exist, information is used directly from the cache (fast)
    • Data not exist , data are copied from slower device to the faster.
  • Cache design consideration

    • size
    • content replacement policy
  • Cache coherency

    • The coordination of the contents of caches such that an update to a value stored in one cache is immediately reflected in all other caches that hold that value

CharacteristicsofVariousTypesofStorage


Process Management

Program vs Process

A process is a program in execution

  • Program is a passive entity, on storage
  • Process is an active entity, in memory
Process
  • Creation, execution needs resources
    • CPU, memory, I/O, files
    • Initialization data
  • Termination requires reclaim of any reusable resources
  • Process can be single-threaded, or multi-threaded
  • Each process has a program counter to specify location of next instruction to execute

Multiprogramming VS Multitasking

  • Multiprogramming : A subset of total jobs are kept in memory at the same time

    • Job scheduling: choose which jobs to load into memory.
      • When job has to wait (for I/O for example), OS switches to another job
  • Multitasking: The CPU switches jobs so frequently to increase interactions with users

    • CPU scheduling: choose which job will run next If several jobs are ready to run at the same time
  • Scheduling


Dual Mode

Dual Mode

  • User Mode
  • Kernel Mode

Dual-mode operation allows OS to protect itself and other system components


Process management activities include:
  • Creating and deleting both user and system processes
  • Suspending and resuming processes
  • Process synchronization
  • Process communication
  • Deadlock handling

Memory Management

  • To execute a program
    • All (or part) of the instructions of the program must be in memory
    • All (or part) of the data needed by the program must be in memory
Memory management activities include
  1. Keeping track of which parts of memory are currently being used and by whom

  2. Deciding which processes and data (or part of them) to move into and out of memory

  3. Allocating and de-allocating memory space as needed


File System Management

  • Abstracts physical properties to logical storage unit - file
  • Files are usually organized into directories
  • Access control determines who can access what
File system activities include
  1. Creating and deleting files and directories

  2. Mapping files onto secondary storage


Secondary Storage Management

Secondary storage management activities
  1. Mounting and unmounting
  2. Free-space management
  3. Storage allocation
  4. Disk scheduling
  5. Partitioning
  6. Protection

Disks usually are used to store data that do not fit in main memory or data that must be kept for a “long” period of time

  • Proper management is of central importance
  • Entire speed of computer operation hinges on disk subsystem and its algorithms

I/O Subsystem

I/O subsystem responsible for
  • Memory management of I/O including buffering (storing data temporarily while it is being transferred), caching (storing parts of data in faster storage for performance), spooling (the overlapping of output of one job with input of other jobs)
  • General device-driver interface
  • Drivers for specific hardware devices

One purpose of OS is to hide peculiarities of hardware devices from the user


Protection and Security

Protection

  • Any mechanism for controlling access of processes or users to resources defined by the OS
    Security
  • defense of the system against internal and external attacks 4
    • Huge range, including denial-of-service, worms, viruses, identity theft, theft of service
  • Systems generally first distinguish among users, to determine who can do what
    • User identities (user IDs, security IDs) include name and associated number, one per user
    • Group identifier (group ID) allows set of users to be defined