Operating System Introduction

Zhao Cong

What Operating Systems Do

  1. A computer system can be divided roughly into four components: the hardware, the operating system, the application programs, and a user
  2. An operating system is similar to a government. Like a government, it performs no useful function by itself. It simply provides an environment within which other programs can do useful work.
  3. In this context, we can view an operating system as a resource allocator
  4. An operating system is a control program. A control program manages the execution of user programs to prevent errors and improper use of the computer.
  5. The common functions of controlling and allocating resources are then brought together into one piece of software: the operating system.
  6. The operating system is the one program running at all times on the computer—usually called the kernel.
  7. Along with the kernel, there are two other types of programs: system programs and application programs
  8. Mobile operating systems often include not only a core kernel but also middleware—a set of software frameworks that provide additional services to application developers.
  9. the operating system includes the always running kernel, middleware frameworks that ease application development and provide features, and system programs that aid in managing the system while it is running.

Computer-System Organization

  1. A modern general-purpose computer system consists of one or more CPUs and a number of device controllers connected through a common bus that provides access between components and shared memory.
  2. A device controller maintains some local buffer storage and a set of special-purpose registers. The device controller is responsible for moving the data between the peripheral devices that it controls and its local buffer storage
  3. Typically, operating systems have a device driver for each device controller. This device driver understands the device controller and provides the rest of the operating system with a uniform interface to the device.

Interrupts

  1. To start an I/O operation
    1. the device driver loads the appropriate registers in the device controller.
    2. The device controller, in turn, examines the contents of these registers to determine what action to take (such as “read a character from the keyboard”).
    3. The controller starts the transfer of data from the device to its local buffer.
    4. Once the transfer of data is complete, the device controller informs the device driver that it has finished its operation.
    5. The device driver then gives control to other parts of the operating system, possibly returning the data or a pointer to the data if the operation was a read. the controller is accomplished via an interrupt to inform the device driver that it has finished its operation.
  2. Hardware may trigger an interrupt at any time by sending a signal to the CPU, usually by way of the system bus
  3. When the CPU is interrupted, it stops what it is doing and immediately transfers execution to a fixed location
  4. The fixed location usually contains the starting address where the service routine for the interrupt is located.
  5. The interrupt must transfer control to the appropriate interrupt service routine,interrupt-specific handler examine the interrupt information.
  6. . A table of pointers to interrupt routines can be used instead to provide the necessary speed
  7. the addresses of the interrupt service routines for the various devices, called interrupt vector,to provide the address of the interrupt service routine for the interrupting device.
  8. The interrupt architecture must also save the state information of whatever was interrupted, so that it can restore this information after servicing the interrupt
  9. After the interrupt is serviced, the saved return address is loaded into the program counter, and the interrupted computation resumes as though the interrupt had not occurred.
  10. The CPU hardware has a wire called the interrupt-request line that the CPU senses after executing every instruction. When the CPU detects that a controller has asserted a signal on the interrupt-request line, it reads the interrupt number and jumps to the interrupt-handler routine by using that interrupt number as an index into the interrupt vector.
  11. We say that the device controller raises an interrupt by asserting a signal on the interrupt request line, the CPU catches the interrupt and dispatches it to the interrupt handler, and the handler clears the interrupt by servicing the device.
  12. The basic interrupt mechanism just described enables the CPU to respond to an asynchronous event, as when a device controller becomes ready for service.In a modern operating system, however, we need more sophisticated interrupt handling features:
    1. We need the ability to defer interrupt handling during critical processing
    2. We need an efficient way to dispatch to the proper interrupt handler for a device
    3. We need multilevel interrupts, so that the operating system can distinguish between high- and low-priority interrupts and can respond with the appropriate degree of urgency In modern computer hardware, these three features are provided by the CPU and the interrupt-controller hardware.
  13. Most CPUs have two interrupt request lines. One is the nonmaskable interrupt, which is reserved for events such as unrecoverable memory errors.The second interrupt line is maskable: it can be turned off by the CPU before the execution of critical instruction sequences that must not be interrupted. The maskable interrupt is used by device controllers to request service(feature 1)
  14. interrupt chaining, in which each element in the interrupt vector points to the head of a list of interrupt handlers.When an interrupt is raised, the handlers on the corresponding list are called one by one, until one is found that can service the request. This structure is a compromise between the overhead of a huge interrupt table and the inefficiency of dispatching to a single interrupt handler.(feature 2)
  15. The interrupt mechanism also implements a system of interrupt priority levels(feature 3).
  16. In summary, interrupts are used throughout modern operating systems to handle asynchronous events.

Storage Structure

  1. General-purpose computers run most of their programs from rewritable memory, called main memory (also called random-access memory, or RAM).Main memory commonly is implemented in a semiconductor technology called dynamic random-access memory (DRAM)
  2. the first program to run on computer power-on is a bootstrap program, which then loads the operating system
  3. RAM is volatile—loses its content when power is turned off or otherwise lost.
  4. To hold the bootstrap program,the computer uses electrically erasable programmable read-only memory (EEPROM) and other forms of firmwar —storage that is infrequently written to and is nonvolatile.EEPROM can be changed but cannot be changed frequently
  5. Interaction is achieved through a sequence of load or store instructions to specific memory addresses.
  6. instruction–execution cycle:
    1. fetches an instruction from memory and stores that instruction in the instruction register.
    2. The instruction is then decoded and may cause operands to be fetched from memory and stored in some internal register.
    3. After the instruction on the operands has been executed, the result may be stored back in memory
  7. The programs and data cannot reside in main memory permanently for two reasons:
    1. Main memory is usually too small to store all needed programs and data permanently.
    2. Main memory, as mentioned, is volatile—it loses its contents when power is turned off or otherwise lost.
  8. Most computer systems provide secondary storage as an extension of main memory.
  9. The most common secondary-storage devices are hard-disk drives (HDDs) and nonvolatile memory (NVM) devices.
  10. tertiary storage:optical disk, magnetic tapes
  11. The main differences among the various storage systems lie in speed, size, and volatility.
  12. As a general rule, there is a trade-off between size and speed,with smaller and faster memory closer to the CPU
  13. The top four levels of memory in the figure(registers, cache, main memory,NVM) are constructed using semiconductor memory.
  14. The most common form of NVM device is flash memory, which is popular in mobile devices such as smartphones and tablets.
  15. Volatile storage will be referred to simply as memory.
  16. Nonvolatile storage will be referred to as NVS,The vast majority of the time we spend on NVS will be on secondary storage,This type of storage can be classified into two distinct types:
    1. Mechanical:HDDs, optical disks, holographic storage, and magnetic tape
    2. Electrical:flash memory, FRAM, NRAM, and SSD.

I/O Structure

  1. The form of interrupt-driven I/O is fine for moving small amounts of data but can produce high overhead when used for bulk data movement such as NVS I/O. To solve this problem, direct memory access (DMA) is used.
  2. After setting up buffers, pointers, and counters for the I/O device, the device controller transfers an entire block of data directly to or from the device and main memory, with no intervention by the CPU.
  3. Some high-end systems use switch rather than bus architecture.In this case, DMA is even more effective.

Computer-System Architecture

We can categorize computer system roughly according to the number of general-purpose processors used

Single-Processor Systems

  1. The core is the component that executes instructions and registers for storing data locally
  2. The use of special-purpose microprocessors is common and does not turn a single-processor system into a multiprocessor.
  3. If there is only one general-purpose CPU with a single processing core, then the system is a single-processor system.

Multiprocessor Systems

  1. Multiprocessor Systems have two (or more) processors, each with a single-core CPU.
  2. The primary advantage of multiprocessor systems is increased throughput
  3. The speed-up ratio with N processors is not N, however; it is less than N.a certain amount of overhead is incurred in keeping all the parts working correctly.
  4. The most common multiprocessor systems use symmetric multiprocessing (SMP), in which each peer CPU processor performs all tasks, including operating-system functions and user processes.
  5. each CPU processor has its own set of registers, as well as a private—or local—cache. However, all processors share physical memory over the system bus.
  6. The benefit of this model is that many processes can run simultaneously
  7. These inefficiencies(one may be sitting idle while another is overloaded) can be avoided if the processors share certain data structures.
  8. Multicore systems can be more efficient than multiple chips with single cores because on-chip communication is faster than between-chip communication.
  9. In a dual-core design, each core has its own register set, as well as its own local cache, often known as a level 1, or L1, cache.that a level 2 (L2) cache is local to the chip but is shared by the two processing cores
  10. once we add too many CPUs, contention for the system bus becomes a bottleneck and performance begins to degrade. An alternative approach is instead to provide each CPU (or group of CPUs) with its own local memory that is accessed via a small, fast local bus.
  11. The CPUs are connected by a shared system interconnect, so that all CPUs share one physical address space. This approach—known as non-uniform memory access, or NUMA.The advantage is that, when a CPU accesses its local memory, not only is it fast, but there is also no contention over the system interconnect
  12. A potential drawback with a NUMA system is increased latency when a CPU must access remote memory across the system interconnect, creating a possible performance penalty.Operating systems can minimize this NUMA penalty through careful CPU scheduling and memory management.
  13. blade servers are systems in which multiple processor boards, I/O boards, and networking boards are placed in the same chassis. The difference between these and traditional multiprocessor systems is that each blade processor board boots independently and runs its own operating system.In essence, these servers consist of multiple independent multiprocessor systems.

Clustered Systems

  1. Another type of multiprocessor system is a clustered system.Clustered systems differ from the multiprocessor systems in that they are composed of two or more individual systems—or nodes—joined together.each node is typically a multicore system. Such systems are considered loosely coupled
  2. Clustered computers share storage and are closely linked via a local-area network LAN or a faster interconnect.
  3. Clustering is usually used to provide high-availability service—that is, service that will continue even if one or more systems in the cluster fail.
  4. We obtain high availability by adding a level of redundancy in the system.
  5. A layer of cluster software runs on the cluster nodes. Each node can monitor one or more of the others (over the network). If the monitored machine fails, the monitoring machine can take ownership of its storage and restart the applications that were running on the failed machine. The users and clients of the applications see only a brief interruption of service.
  6. The ability to continue providing service proportional to the level of surviving hardware is called graceful degradation,Some systems go beyond graceful degradation and are called fault tolerant
  7. Clustering can be structured asymmetrically or symmetrically.
    1. In asymmetric clustering, one machine is in hot-standby mode while the other is running the applications. The hot-standby host machine does nothing but monitor the active server.If that server fails, the hot-standby host becomes the active
    2. In symmetric clustering, two or more hosts are running applications and are monitoring each other.This structure is obviously more efficient, as it uses all of the available hardware. However, it does require that more than one application be available to run.
  8. Clusters can also be used to provide high-performance computing environments,This involves a technique known as parallelization
  9. To provide this shared access(access the same data on shared storage), the system must also supply access control and locking to ensure that no conflicting operations occur. This function, commonly known as a distributed lock manager (DLM)
  10. Storage-area networks (SANs) allows many systems to attach to a pool of storage.

Operating-System Operations

  1. Another form of interrupt is a trap (or an exception), which is a software-generated interrupt caused either by an error (for example, division by zero or invalid memory access) or by a specific request from a user program that an operating-system service be performed by executing a special operation called a system call.

Multiprogramming and Multitasking

  1. Multiprogramming increases CPU utilization, as well as keeping users satisfied, by organizing programs so that the CPU always has one to execute. In a multiprogrammed system, a program in execution is termed a process.
  2. In a non-multiprogrammed system, the CPU would sit idle. In a multiprogrammed system, the operating system simply switches to, and executes, another process,As long as at least one process needs to execute, the CPU is never idle.
  3. Multitasking is a logical extension of multiprogramming. In multitasking systems, the CPU executes multiple processes by switching among them, but the switches occur frequently, providing the user with a fast response time.
  4. Choose which process will run next is CPU scheduling
  5. Virtual memory, a technique that allows the execution of a process that is not completely in memory. The main advantage of this scheme is that it enables users to run programs that are larger than actual physical memory..Further, it abstracts main memory into a large, uniform array of storage, separating logical memory as viewed by the user from physical memory.

Dual-Mode and Multimode Operation

  1. .In order to ensure the proper execution of the system, we must be able to distinguish between the execution of operating-system code and user-defined code. The approach taken by most computer systems is to provide hardware support that allows differentiation among various modes of execution.
  2. At the very least, we need two separate modes of operation: user mode and kernel mode (also called supervisor mode, system mode, or privileged mode)
  3. A bit, called the mode bit, is added to the hardware of the computer to indicate the current mode: kernel (0) or user (1).
  4. When a user application requests a service from the operating system (via a system call), the system must transition from user to kernel mode to fulfill the request.
  5. At system boot time, the hardware starts in kernel mode. The operating system is then loaded and starts user applications in user mode. Whenever a trap or interrupt occurs, the hardware switches from user mode to kernel mode (that is, changes the state of the mode bit to 0). Thus, whenever the operating system gains control of the computer, it is in kernel mode. The system always switches to user mode (by setting the mode bit to 1) before passing control to a user program
  6. We accomplish this protection by designating some of the machine instructions that may cause harm as privileged instructions. The hardware allows privileged instructions to be executed only in kernel mode

Timer

  1. We must ensure that the operating system maintains control over the CPU,We cannot allow a user program to get stuck in an infinite loop or to fail to call system services and never return control to the operating system. To accomplish this goal, we can use a timer.
  2. A timer can be set to interrupt the computer after a specified period. The period may be fixedb or variable.

Resource Management

Process Management

  1. We emphasize that a program by itself is not a process. A program is a passive entity, like the contents of a file stored on disk, whereas a process is an active entity.
  2. A single-threaded process has one program counter specifying the next instruction to execute. The execution of such a process must be sequential.
  3. Although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences.
  4. A multithreaded process has multiple program counters, each pointing to the next instruction to execute for a given thread.
  5. A process is the unit of work in a system. A system consists of a collection of processes, some of which are operating-system processes (those that execute system code) and the rest of which are user processes (those that execute user code).
  6. The operating system is responsible for the following activities in connection with process management:
    1. Creating and deleting both user and system processes
    2. Scheduling processes and threads on the CPUs
    3. Suspending and resuming processes
    4. Providing mechanisms for process synchronization
    5. Providing mechanisms for process communication

Memory Management

  1. the main memory is generally the only large storage device that the CPU is able to address and access directly
  2. For a program to be executed, it must be mapped to absolute addresses and loaded into memory.
  3. The operating system is responsible for the following activities in connection with memory management
    1. Keeping track of which parts of memory are currently being used and which process is using them
    2. Allocating and deallocating memory space as needed
    3. Deciding which processes (or parts of processes) and data to move into and out of memory

File-System Management

  1. The operating system is responsible for the following activities in connection with file management:
    1. Creating and deleting files
    2. Creating and deleting directories to organize files
    3. Supporting primitives for manipulating files and directories
    4. Mapping files onto mass storage
    5. Backing up files on stable (nonvolatile) storage media

Mass-Storage Management

  1. The operating system is responsible for the following activities in connection with secondary storage management
    1. Mounting and unmounting
    2. Free-space management
    3. Storage allocation
    4. Disk scheduling
    5. Partitioning
    6. Protection

Cache Management

  1. Because caches have limited size, cache management is an important design problem. Careful selection of the cache size and of a replacement policy can result in greatly increased performance
  2. The movement of information between levels of a storage hierarchy may be either explicit or implicit.data transfer from cache to CPU and registers is usually a hardware function,with no operating-system intervention. In contrast, transfer of data from disk to memory is usually controlled by the operating system
  3. In a hierarchical storage structure, the same data may appear in different levels of the storage system. ## I/O System Management
  4. One of the purposes of an operating system is to hide the peculiarities of specific hardware devices from the user.
  5. The I/O subsystem consists of several components:
    1. A memory-management component that includes buffering, caching, and spooling
    2. A general device-driver interface
    3. Drivers for specific hardware devices