操作系统课程摘要第二章:操作系统结构

1. Chapter 2: Operating-SystemStructures

This Chapter’s Objectives:
• To describe the services an operating system provides to users, processes, and other systems.

• To discuss the various methodologies of structuring an operating system.

• To explain how operating systems are installed and customized and how they boot.

1.1. Operating System Services

Operating systems provide an environment for execution of programs and services to programs and users

One set of operating-system services provides functions that are helpful to the user:

  1. User interface - Almost all operating systems have a user interface (UI).

    • Varies between Command-Line (CLI), Graphics User Interface (GUI), Batch or combinations of those !
  1. Program execution - The system must be able to load a program into memory and to run that program, end execution, either normally or abnormally (indicating error)
  1. I/O operations - A running program may require I/O, which may involve a file or an I/O device

  2. File-system manipulation - The file system is of particular interest. Programs need to read and write files and directories, create and delete them, search them, list file Information, permission management.

  3. Communications – Processes may exchange information, on the same computer or between computers over a network

    • Communications may be via shared memory or through message passing (packets moved by the OS)”
  4. Error detection – OS needs to be constantly aware of possible errors

    • May occur in the CPU and memory hardware, in I/O devices, in user program

    • For each type of error, OS should take the appropriate action to ensure correct and consistent computing

    • Debugging facilities can greatly enhance the user’s and programmer’s abilities to efficiently use the system

Another set of OS functions exists for ensuring the efficient operation of the system itself via resource sharing

  1. Resource allocation - When multiple users or multiple jobs running
    concurrently, resources must be allocated to each of them

    • Many types of resources and strategies - Some (e.g., CPU cycles, main memory, and file storage) may have special allocation code, others (e.g., I/O devices) may have general request and release code
  2. Accounting - To keep track of which users use how much and what kinds of computer resources fpr billing or statistics to tune the parameters

  3. Protection and security - The owners of information stored in a multiuser or networked computer system may want to control use of that information, concurrent processes should not interfere with each other

    • Protection involves ensuring that all access to system resources is controlled

    • Security of the system from outsiders requires user authentication, extends to defending external I/O devices from invalid access attempts

A View of Operating System Services
1

1.2. User Operating System Interface

1.2.1. Command Interpreter

  1. Some operating systems include the command interpreter in the kernel. Others, such as Windows XP and UNIX, treat the command interpreter as a special program

  2. Sometimes multiple flavors implemented – shells : such as Bourne-Again Shell ( bash ) that always on

  3. Primarily fetches a command from user and executes it

    • Sometimes commands built-in
    • sometimes just names of programs
      If the latter, adding new features does not require shell modification

1.2.2. Graphical User Interfaces

  • User-friendly desktop metaphor interface

    • Usually mouse, keyboard, and monitor

    • Icons represent files, programs, actions, etc.

    • Various mouse buttons over objects in the interface cause various actions (provide information, options, execute function, open directory (known as a folder))

    • Invented at Xerox PARC ( 1973 ), popularized by Apple’s Macintosh Operating System

1.2.3. Now CLI & GUI

  • Many systems now include both CLI and GUI interfaces

    • Microsoft Windows is GUI with CLI “command” shell

    • Apple Mac OS X is “Aqua” GUI interface with UNIX kernel underneath and shells available

    • Unix and Linux have CLI with optional GUI interfaces (CDE, KDE( open source ), GNOME( open source by GUN project ))

1.2.4. Touchscreen Interfaces

  • Touchscreen devices require new interfaces

    • Mouse not possible or not desired

    • Actions and selection based on gestures

    • Virtual keyboard for text entry

1.3. System Calls

1.3.1. System Calls

  • System calls provide an interface to the services made available by an operating system. These calls are generally available as routines written in C and C++

1.3.2. example of System Calls

System call sequence to copy the contents of one file to another file, systems execute thousands of system calls per second.
——–>pic<——

1.3.3. Application Programming Interface (API)

  • Mostly accessed by programs via a high-level Application Program Interface (API) rather than direct system call use

    • Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM)

1.3.4. API rather than System Calls ?

The relationship System Call with Api: API is in higher level
2

Benefit :

  • make program portability

  • more easier by API than Syatem Calls

1.3.5. System Call Implementation

1.3.5.1. System-Call Interface

  1. API —> System-Call Interface —> System Call
  • The run-time support system (a set of functions built into libraries included with a compiler) for most programming languages provides a system-call interface that serves as the link to system calls made available by the operating system.

  • The system-call interface intercepts function calls in the API and invokes the necessary system call within the operating system.

  • Typically, a number is associated with each system call, and the system-call interface maintains a table indexed according to these numbers.

  • The system call interface then invokes the intended system call in the operating system kernel and returns the status of the system call and any return values.

  1. The caller need know nothing about how the system call is implemented
  • Just needs to obey API and understand what OS will do as a result call

  • Most details of OS interface hidden from programmer by API

    • Managed by run-time support library (set of functions built into libraries included with compiler)

1.3.5.2. System Call Parameter Passing

For system calls, more information is required than simply the identity of the desired system call. So we should submit information by passing parameters to the operating system.

  1. Simplest: pass the parameters in registers

    • In some cases, may be more parameters than registers ——>2, 3
  2. Parameters stored in a block, or table, in memory, and address of block passed as a parameter in a register

    • This approach taken by Linux and Solaris
  3. Parameters placed, or pushed, onto the stack by the program and popped off the stack by the operating system

Block and stack methods do not limit the number or length of parameters being passed

1.4. Types of System Calls

1.4.1. Process control

  • end, abort
  • load, execute
  • create process, terminate process
  • get process attributes, set process attributes
  • wait for time
  • wait event, signal event
  • allocate and free memory

  • Dump memory if error

  • Debugger for determining bugs, single step execution
  • Locks for managing access to shared data between processes

1.4.2. File management

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

1.4.3. Device management

Once the device has been requested (and allocated to us), we can read, write, and (possibly) reposition the device, just as we can with files.

  • request device, release device
  • read, write, reposition
  • get device attributes, set device attributes
  • logically attach or detach devices

1.4.4. 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

1.4.5. Communication

  • create, delete communication connection
  • send, receive messages
  • send, receive messages if message passing model to host name or process name!
    • From (source) client to (receiving deamon) server!
  • Shared-memory model create and gain access to memory regions
  • transfer status information
  • attach or detach remote devices

1.4.6. Protection

  • Control access to resources
  • Get and set permissions
  • Allow and deny user access to certain resources

1.5. System Programs

  1. System programs provide a convenient environment for program development and execution. They can be divided into:

    • File manipulation
    • Status information sometimes stored in a File modification
    • Programming language support
    • Program loading and execution
    • Communications
    • Background services
    • Application programs
  2. Most users’ view of the operation system is defined by system programs and application programs, not the actual system calls

  3. Provide a convenient environment for program development and execution

    • Some of them are simply user interfaces to system calls; others are considerably more complex

1.5.1. File management

  • Create, delete, copy, rename, print, dump, list, and generally manipulate files and directories

1.5.2. Status information!

  • Some ask the system for info - date, time, amount of available memory, disk space, number of users
  • Others provide detailed performance, logging, and debugging information
  • Typically, these programs format and print the output to the terminal or other output devices
  • Some systems implement a registry - used to store and retrieve configuration information

1.5.3. File modification!

  • Text editors to create and modify files
  • Special commands to search contents of files or perform transformations of the text

1.5.4. Programming-language support

  • Compilers, assemblers, debuggers and interpreters sometimes provided

1.5.5. Program loading and execution

  • Absolute loaders, relocatable loaders, linkage editors, and overlay-loaders, debugging systems for higher-level and machine language

1.5.6. Communications

  • Provide the mechanism for creating virtual connections among processes, users, and computer systems
  • Allow users to send messages to one anotherʼs screens, browse web pages, send electronic-mail messages, log in remotely, transfer files from one machine to another

1.5.7. Background Services!

  • Launch at boot time
    • Some for system startup, then terminate
    • Some from system boot to shutdown
  • Provide facilities like disk checking, process scheduling, errorlogging, printing
  • Run in user context not kernel context
  • Known as services, subsystems, daemons !

1.5.8. Application programs!

  • Do not pertain to system
  • Run by users
  • Not typically considered part of OS
  • Launched by command line, mouse click, finger poke

1.6. Operating System Design and Implementation

Design and Implementation of OS not “solvable”, but some approaches have proven successful

  1. Internal structure of different Operating Systems can vary widely

  2. Start by defining goals and specifications

  3. Affected by choice of hardware, type of system

  4. User goals and System goals

    • User goals – operating system should be convenient to use, easy to learn, reliable, safe, and fast
    • System goals – operating system should be easy to design, implement, and maintain, as well as flexible, reliable, error-free,and efficient.
  1. Important principle to separate

    • Policy: What will be done?
    • Mechanism: How to do it?
    • The separation of policy from mechanism is a very important principle, it allows maximum flexibility if policy decisions are to be changed later
  2. Specifying and designing OS is highly creative task of software engineering

  1. Much variation

    • Early OSes in assembly language
    • Then system programming languages like Algol, PL/1
    • Now C, C++
  2. Actually usually a mix of languages

    • Lowest levels in assembly
    • Main body in C
    • Systems programs in C, C++, scripting languages like PERL, Python, shell scripts
  3. More high-level language easier to port to other hardware. But slower

  4. Emulation can allow an OS to run on non-native hardware”

1.7. Operating System Structure

General-purpose OS is very large program, Various ways to structure one as follows

1.7.1. Simple Structure

MS-DOS:
msdos

UNIX:
unix

1.7.2. Layered Approach

layered

1.7.3. Microkernel System Structure

micro

1.7.4. Modules

mod

1.7.5. Hybrid Systems

1.7.6. Mac OS X

1.7.7. iOS

1.7.8. Android

android

1.8. Operating System Debugging

Debugging is finding and fixing errors, or bugs!

  1. OSes generate log files containing error information
  2. Failure of an application can generate core dump file capturingmemory of the process
  3. Operating system failure can generate crash dump file containingkernel memory
  4. Beyond crashes, performance tuning can optimize system performance
  • Sometimes using trace listings of activities, recorded for analysis
  • Profiling is periodic sampling of instruction pointer to look forstatistical trends

Kernighan’s Law: “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

1.9. Virtual Machines

1.10. Operating System Generation

Operating systems are designed to run on any of a class of machines; the system must be configured for each specific computer site

  1. SYSGEN program obtains (automatically or via queries to the sysadmin) information concerning the specific configuration of the hardware system
  • Used to build system-specific compiled kernel or system-tuned
  • Can generate more efficient code than one general kernel
  1. OS compiled for the specific machine (faster), or tables linked with the identified specificities of a machine (intermediate), or every possible option is there, and linked when requested (slower but more general)

  2. Does the OS need to be recompiled at every change, or adapts itself as changes occur?

1.11. System Boot

When power initialized on system, execution starts at a fixed memory location

  • Firmware ROM used to hold initial boot code”
  1. Operating system must be made available to hardware so hardware can start it

    • Small piece of code – bootstrap loader, stored in ROM or EEPROM locates the kernel, loads it into memory, and starts it
    • Sometimes two-step process where boot block at fixed location loaded by ROM code, which loads bootstrap loader from disk
  2. Common bootstrap loader, GRUB, allows selection of kernel from
    multiple disks, versions, kernel options

  3. Kernel loads and system is then running!