Operating System Structures

Zhao Cong

Operating-System Services

For helping the user

  • User interface(UI):graphical user interface (GUI),touch-screen interface ,command-line interface (CLI)
  • Program execution:The system must be able to load a program into memory and to run that program. The program must be able to end its execution, either normally or abnormally (indicating error).
  • I/O operations:For efficiency and protection, users usually cannot control I/O devices directly.
  • File-system manipulation:Programs need to read and write files and directories. They also need to create and delete them by name, search for a given file, and list file information. Finally, some operating systems include permissions management.
  • Communications:Communications may be implemented via shared memory, in which two or more processes read and write to a shared section of memory, or message passing.
  • Error detection

For ensuring the efficient operation of the system itself

  • Resource allocation
  • Logging:We want to keep track of which programs use how much and what kinds of computer resources.
  • Protection and security

User and Operating-System Interface

Command Interpreters

  • Treat the command interpreter as a special program that is running when a process is initiated or when a user first logs on (on interactive systems).
  • On systems with multiple command interpreters to choose from, the interpreters are known as shells.ex:C shell, Bourne-Again shell(bash), Korn shell.
  • The main function of the command interpreter is to get and execute the next user-specified command.
  • Commands can be implemented in two general ways:
    • The command interpreter itself contains the code to execute the command.
    • Implements most commands through system programs.(add new commands to the system easily)

Graphical User Interface

  • Users employ a mouse-based window and-menu system characterized by a desktop metaphor.
  • The user moves the mouse to position its pointer on images, or icons, on the screen (the desktop).
  • A file or directory is known as a folder.

Touch-Screen Interface

  • Users interact by making gestures on the touch screen.
  • . Both the iPad and the iPhone use the Springboard touch-screen interface.

System Calls

System calls provide an interface to the services made available by an operating system. These calls are generally available as functions written in C and C++, although certain low-level tasks (for example, tasks where hardware must be accessed directly) may have to be written using assembly-language instructions.

Application Programming Interface

  • Typically, application developers design programs according to an application programming interface (API).
  • A programmer accesses an API via a library of code provided by the operating system. In the case of UNIX and Linux for programs written in the C language, the library is called libc.
  • Another important factor in handling system calls is the run-time environment (RTE)—the full suite of software needed to execute applications written in a given programming language, including its compilers or interpreters as well as other software, such as libraries and loaders.
  • The RTE provides a system-call interface that serves as the link to system calls made available by the operating system.
  • The caller need know nothing about how the system call is implemented or what it does during execution. Rather, the caller need only obey the API and understand what the operating system will do as a result of the execution of that system call.
  • The simplest approach to pass parameters to the operating system is to pass the parameters in registers.
  • However, there may be more parameters than registers. In these cases, the parameters are generally stored in a block, or table, in memory, and the address of the block is passed as a parameter in a register.
  • Parameters also can be placed, or pushed, onto a stack by the program and popped off the stack by the operating system.

Types of System Calls

System calls can be grouped roughly into six major categories: process control, file management, device management, information maintenance, communications, and protection.

Process Control

  • create process, terminate process
  • load, execute
  • get process attributes, set process attributes
  • wait event, signal event
  • allocate and free memory
  • acquire lock(),release lock()
    • To ensure the integrity of the data being shared, operating systems often provide system calls allowing a process to lock shared data.
  • Single-tasking system:Arduino
    • To write a program for the Arduino, we first write the program on a PC and then upload the compiled program (known as a sketch) from the PC to the Arduino’s flash memory via a USB connection
    • The standard Arduino platform does not provide an operating system; instead, a small piece of software known as a boot loader loads the sketch into a specific region in the Arduino’s memory.
  • Multitasking system:FreeBSD (derived from Berkeley UNIX)

File Management

  • create file, delete file
  • open, close
  • read, write, reposition
  • get file attributes, set file attributes

Device Management

  • physical devices or abstract or virtual devices
  • request device, release device
  • read, write, reposition
  • get device attributes, set device attributes
  • logically attach or detach devices
  • A set of system calls is used on both files and devices. Sometimes, I/O devices are identified by special file names, directory placement, or file attributes.

Information maintenance

  • get time or date, set time or date
  • get system data, set system data
  • get process, file, or device attributes
  • set process, file, or device attributes
  • Many system calls exist simply for the purpose of transferring information between the user program and the operating system.
  • Another set of system calls is helpful in debugging a program.
  • Many operating systems provide a time profile of a program to indicate the amount of time that the program executes at a particular location or set of locations.
  • In addition, the operating system keeps information about all its processes, and system calls are used to access this information.

Communications

  • create, delete communication connection
  • send, receive messages
  • transfer status information
  • attach or detach remote devices

There are two common models of interprocess communication: the message passing model and the shared-memory model.

message-passing model

  • Each computer in a network has a host name by which it is commonly known. A host also has a network identifier, such as an IP address. Similarly, each process has a process name.
  • The source of the communication, known as the client, and the receiving daemon, known as a server.
  • Message passing is useful for exchanging smaller amounts of data, because no conflicts need be avoided. It is also easier to implement than is shared memory for intercomputer communication.
  • Shared memory allows maximum speed and convenience of communication, since it can be done at memory transfer speeds when it takes place within a computer. Problems exist, however, in the areas of protection and synchronization between the processes sharing memory.

Protection

  • get file permissions
  • set file permissions

System Services

  • At the lowest level is hardware. Next is the operating system, then the system services, and finally the application programs.

  • System services, also known as system utilities, provide a convenient environment for program development and execution.

  • System Services can be divided into these categories:

    • File management
    • Status information
    • File modificatio
    • Programming-language support
    • Program loading and execution
    • Communications
    • Background services
      • Constantly running system-program processes are known as services, subsystems, or daemons.
  • The view of the operating system seen by most users is defined by the application and system programs, rather than by the actual system calls.

Linkers and Loaders

  • Source files are compiled into object files that are designed to be loaded into any physical memory location, a format known as an relocatable object fil . Next, the linker combines these relocatable object files into a single binary executable file.
  • A loader is used to load the binary executable file into memory, where it is eligible to run on a CPU core.
  • An activity associated with linking and loading is relocation
  • Dynamically linked libraries (DLLs):avoids linking and loading libraries that may end up not being used into an executable file.
  • For UNIX and Linux systems, this standard format is known as ELF (for Executable and Linkable Format).
  • Windows systems use the Portable Executable (PE) format, and macOS uses the Mach-O format

Why Applications Are Operating-System Specific

  • Fundamentally, applications compiled on one operating system are not executable on other operating systems.
  • An application can be made available to run on multiple operating systems in one of three ways:
    • The application can be written in an interpreted language (such as Python or Ruby).
    • The application can be written in a language that includes a virtual machine containing the running application.
    • The application developer can use a standard language or API in which the compiler generates binaries in a machine- and operating-system-specific language
  • Other challenges exist at lower levels in the system, including the following.
    • Each operating system has a binary format for applications
    • CPUs have varying instruction sets, and only applications containing the appropriate instructions can execute correctly.
    • System calls vary among operating systems.
  • At the architecture level, an application binary interface (ABI) is used to define how different components of binary code can interface for a given operating system on a given architecture.an ABI is the architecture-level equivalent of an API.
  • If a binary executable file has been compiled and linked according to a particular ABI, it should be able to run on different systems that support that ABI

Operating-System Design and Implementation

Design Goals

  • The first problem in designing a system is to define goals and specifications
  • user goals
    • The system should be convenient to use, easy to learn and to use, reliable, safe, and fast.
  • system goals
    • The system should be easy to design, implement, and maintain; and it should be flexible, reliable, error free, and efficient
  • Specifying and designing an operating system's general principles have been developed in the field of software engineering

Mechanisms and Policies

  • One important principle is the separation of policy from mechanism
  • Mechanisms determine how to do something; policies determine what will be done.
  • A change in policy would then require redefinition of only certain parameters of the system.
  • Microkernel-based operating systems take the separation of mechanism and policy to one extreme by implementing a basic set of primitive building blocks. These blocks are almost policy free.

Implementation

  • Early operating systems were written in assembly language. Now, most are written in higher-level languages such as C or C++
  • Most Android system libraries are written in C or C++, and its application frameworks—which provide the developer interface to the system—are written mostly in Java.
  • The advantages of using a higher-level language:
    • the code can be written faster, is more compact, and is easier to understand and debug
    • an operating system is far easier to port to other hardware if it is written in a higher-level language
  • The only possible disadvantages of implementing an operating system in a higher-level language are reduced speed and increased storage requirements.This, however, is not a major issue in today’s systems. Although an expert assembly-language programmer can produce efficient small routines.
  • As is true in other systems, major performance improvements in operating systems are more likely to be the result of better data structures and algorithms than of excellent assembly-language code.

Operating-System Structure

  • A common approach is to partition the task into small components, or modules, rather than have one single system.

Monolithic Structure

  • The simplest structure for organizing an operating system is no structure at all.
  • An example of such limited structuring is the original UNIX operating system, which consists of two separable parts: the kernel and the system programs.
  • Everything below the system-call interface and above the physical hardware is the kernel.
  • Taken in sum, that is an enormous amount of functionality to be combined into one single address space.
  • Despite the apparent simplicity of monolithic kernels, they are difficult to implement and extend
  • There is very little overhead in the system-call interface, and communication within the kernel is fast.

Layered Approach

  • The monolithic approach is often known as a tightly coupled system
  • Alternatively, we could design a loosely coupled system
  • The advantage of this modular approach is that changes in one component affect only that component, and no others, allowing system implementers more freedom in creating and changing the inner workings of the system.
  • A system can be made modular in many ways. One method is the layered approach, in which the operating system is broken into a number of layers (levels). The bottom layer (layer 0) is the hardware; the highest (layer N) is the user interface.
  • A set of functions that can be invoked by higher-level layers. Layer M, in turn, can invoke operations on lower-level layers.
  • The main advantage of the layered approach is simplicity of construction and debugging
  • Each layer is implemented only with operations provided by lower-level layersHence, each layer hides the existence of certain data structures, operations, and hardware from higher-level layers.
  • Layered systems have been successfully used in computer networks (such as TCP/IP) and web applications
  • the challenges of appropriately defining the functionality of each layer and the overall performance of such systems is poor due to the overhead of requiring a user program to traverse through multiple layers to obtain an operating-system service.

Microkernels

  • In the mid-1980s, researchers at Carnegie Mellon University developed an operating system called Mach that modularized the kernel using the microkernel approach
  • This method structures the operating system by removing all nonessential components from the kernel and implementing them as user-level programs that reside in separate address spaces.
  • The main function of the microkernel is to provide communication between the client program and the various services that are also running in user space.
  • If the client program wishes to access a file, it must interact with the file server. The client program and service never interact directly. Rather, they communicate indirectly by exchanging messages with the microkernel.
  • One benefit of the microkernel approach is that it makes extending the operating system easier,All new services are added to user space and consequently do not require modification of the kernel
  • The resulting operating system is easier to port from one hardware design to another. The microkernel also provides more security and reliability, since most services are running as user—rather than kernel—processes. If a service fails, the rest of the operating system remains untouched.
  • Perhaps the best-known illustration of a microkernel operating system is Darwin, the kernel component of the macOS and iOS operating systems.
  • Another example is QNX, a real-time operating system for embedded systems.
  • Unfortunately, the performance of microkernels can suffer due to increased system-function overhead.In addition, the operating system may have to switch from one process to the next to exchange the messages. The overhead involved in copying messages and switching between processes
  • Windows architecture had become more monolithic than microkernel

Modules

  • Perhaps the best current methodology for operating-system design involves using loadable kernel modules (LKMs).
  • The idea of the design is for the kernel to provide core services, while other services are implemented dynamically, as the kernel is running
  • it is more flexible than a layered system
  • The approach is also similar to the microkernel approach in that the primary module has only core functions.but it is more efficient, because modules do not need to invoke message passing in order to communicate.
  • LKMs can be “inserted” into the kernel as the system is started (or booted) or during run time.

Hybrid Systems

macOS and iOS

  • User experience layer:macOS uses the Aqua user interface, which is designed for a mouse or trackpad, whereas iOS uses the Springboard user interface, which is designed for touch devices.
  • Application frameworks layer:This layer includes the Cocoa and Cocoa Touch frameworks, which provide an API for the Objective-C and Swift programming languages.
  • Core frameworks. This layer defines frameworks that support graphics and media including, Quicktime and OpenGL.
  • Kernel environment. This environment, also known as Darwin, includes the Mach microkernel and the BSD UNIX kernel.
  • Darwin provides two system-call interfaces:
    • Mach system calls (known as traps) and
    • BSD system calls (which provide POSIX functionality).
  • Mach provides fundamental operating system services, including memory management, CPU scheduling, and interprocess communication (IPC) facilities such as message passing and remote procedure calls (RPCs).
  • In addition to Mach and BSD, the kernel environment provides an I/O kit for development of device drivers and dynamically loadable modules (which macOS refers to as kernel extensions, or kexts)

Android

  • Whereas many Java virtual machines perform just-in-time (JIT) compilation to improve application efficiency, ART(Android RunTime) performs ahead-of-time (AOT) compilation.
  • Android developers can also write Java programs that use the Java native interface—or JNI—which allows developers to bypass the virtual machine and instead write Java programs that can access specific hardware features
  • Because Android can run on an almost unlimited number of hardware devices, Google has chosen to abstract the physical hardware through the hardware abstraction layer, or HAL
  • The standard C library used by Linux systems is the GNU C library (glibc).Google instead developed the Bionic standard C library for Android
  • It has also made changes in memory management and allocation and has added a new form of IPC known as Binder

Building and Booting an Operating System

Operating-System Generation

  • generating (or building) an operating system
    • Write the operating system source code (or obtain previously written source code).
    • Configure the operating system for the system on which it will run.
    • Compile the operating system
    • Install the operating system.
    • Boot the computer and its new operating system.
  • Typically, parameters describing how the system is configured is stored in a configuration file of some type, and once this file is created, it can be used in several ways.
    • At one extreme, a system administrator can use it to modify a copy of the operating-system source code. Then the operating system is completely compiled (known as a system build)ex:laptop computers,mobile devices
    • At the other extreme, it is possible to construct a system that is completely modular.ex:embedded systems.

System Boot

  • The process of starting a computer by loading the kernel is known as booting the system.
  • the boot process proceeds as follows:
    • A small piece of code known as the bootstrap program or boot loader locates the kernel.
    • The kernel is loaded into memory and started
    • The kernel initializes hardware
    • The root file system is mounted
  • Multistage boot process:When the computer is first powered on, a small boot loader located in nonvolatile firmware known as BIOS is run. This initial boot loader usually does nothing more than load a second boot loader, which is located at a fixed disk location called the boot block.
  • Many recent computer systems have replaced the BIOS-based boot process with UEFI (Unified Extensible Firmware Interface),it is better support for 64-bit systems and larger disks
  • UEFI is a single, complete boot manager and therefore is faster than the multistage BIOS boot process
  • the bootstrap program can perform a variety of tasks. In addition to loading the file containing the kernel program into memory, it also runs diagnostics to determine the state of the machine.
  • Android boot loader is LK (for “little kernel”). Android systems use the same compressed kernel image as Linux, as well as an initial RAM file system. However, whereas Linux discards the initramfs once all necessary drivers have been loaded, Android maintains initramfs as the root file system for the device.

Operating-System Debugging

  • Broadly, debugging is the activity of finding and fixing errors in a system, both in hardware and in software
  • Performance problems are considered bugs, so debugging can also include performance tuning, which seeks to improve performance by removing processing bottlenecks.

Failure Analysis

  • If a process fails, most operating systems write the error information to a log fil to alert system administrators or users that the problem occurred. The operating system can also take a core dump—a capture of the memory of the process—and store it in a file for later analysis
  • A failure in the kernel is called a crash. When a crash occurs, error information is saved to a log file, and the memory state is saved to a crash dump.

Performance Monitoring and Tuning

  • The operating system must have some means of computing and displaying measures of system behavior. Tools may be characterized as providing either per-process or system-wide observations.To make these observations, tools may use one of two approaches—counters or tracing.

  • Counters:ex:Windows Task Manager

  • Tracing:tracing tools collect data for a specific event—such as the steps involved in a system-call invocation

BCC

  • BCC, a toolkit for dynamic kernel tracing in Linux
  • BCC have a minimal performance impact provides a dynamic, secure, low-impact debugging environment.
  • BCC (BPF Compiler Collection) is a rich toolkit that provides tracing features for Linux systems. BCC is a front-end interface to the eBPF (extended Berkeley Packet Filter) tool.
  • What makes BCC especially powerful is that its tools can be used on live production systems that are running critical applications without causing harm to the system