Chapter 2: System Structures Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009 Chapter 2: System Structures Operating System Services User Operating System Interface System Calls Types of System Calls System Programs Operating System Design and Implementation Operating System Structure Virtual Machines Operating System Debugging Operating System Generation System Boot Operating System Concepts – 8th Edition 2.2 Silberschatz, Galvin and Gagne ©2009 Objectives To describe the services an operating system provides to users, processes, and other systems To discuss the various ways of structuring an operating system To explain how operating systems are installed and customized and how they boot Operating System Concepts – 8th Edition 2.3 Silberschatz, Galvin and Gagne ©2009 Operating System Services One set of operating-system services provides functions that are helpful to the user: User interface - Almost all operating systems have a user interface (UI) Varies between Command-Line (CLI), Graphics User Interface (GUI), Batch 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) I/O operations - A running program may require I/O, which may involve a file or an I/O device File-system manipulation - The file system is of particular interest. Obviously, programs need to read and write files and directories, create and delete them, search them, list file Information, permission management. Operating System Concepts – 8th Edition 2.4 Silberschatz, Galvin and Gagne ©2009 A View of Operating System Services Operating System Concepts – 8th Edition 2.5 Silberschatz, Galvin and Gagne ©2009 Operating System Services (Cont) One set of operating-system services provides functions that are helpful to the user (Cont): 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) 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 Operating System Concepts – 8th Edition 2.6 Silberschatz, Galvin and Gagne ©2009 Operating System Services (Cont) Another set of OS functions exists for ensuring the efficient operation of the system itself via resource sharing Resource allocation - When multiple users or multiple jobs running concurrently, resources must be allocated to each of them Many types of resources - Some (such as CPU cycles, main memory, and file storage) may have special allocation code, others (such as I/O devices) may have general request and release code Accounting - To keep track of which users use how much and what kinds of computer resources 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 If a system is to be protected and secure, precautions must be instituted throughout it. Operating System Concepts – 8th Edition 2.7 Silberschatz, Galvin and Gagne ©2009 User Operating System Interface - CLI Command Line Interface (CLI) or command interpreter allows direct command entry Sometimes implemented in kernel, sometimes by systems program Sometimes multiple flavors implemented – shells Primarily fetches a command from user and executes it, commands can be implemented in two ways: – Commands built-in, number of commands determines the size of command interpreter since each command has its own code. – Command interpreter does not understand command, it uses only the command to identify a file to be loaded into memory and executed. – Example rm file.txt search for file rm, load it into memory and execute it with the parameter file.txt » If the latter, adding new features doesn’t require shell modification. Command interpreter can be small. Operating System Concepts – 8th Edition 2.8 Silberschatz, Galvin and Gagne ©2009 User Operating System Interface - GUI 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 Many systems now include both CLI and GUI interfaces Microsoft Windows is GUI with CLI “command” shell Apple Mac OS X as “Aqua” GUI interface with UNIX kernel underneath and shells available Solaris is CLI with optional GUI interfaces (Java Desktop, KDE) Operating System Concepts – 8th Edition 2.9 Silberschatz, Galvin and Gagne ©2009 Bourne Shell Command Interpreter Operating System Concepts – 8th Edition 2.10 Silberschatz, Galvin and Gagne ©2009 The Mac OS X GUI Operating System Concepts – 8th Edition 2.11 Silberschatz, Galvin and Gagne ©2009 System Calls Programming interface to the services provided by the OS Typically written in a high-level language (C or C++) “routines” some low level tasks may need to be written in assembly language. (Note that the system-call names used throughout this text are generic) Operating System Concepts – 8th Edition 2.12 Silberschatz, Galvin and Gagne ©2009 System Calls Example of a system call: writing a program to read data from a file and copy them to another file: Need to know two files names, requires a sequence of system call A prompt message, read characters from keyboard. Define two files other option can have a mouse which requires another system calls Open the input file and create the output file (system calls) Possible errors: input file does not exist or has protection: Print a message on the console to user (system calls) then terminate abnormally (system call) Output file: exist may cause abort (a system call) and create new one (a system call) in interactive system: may ask to replace or abort the program (system calls) Operating System Concepts – 8th Edition 2.13 Silberschatz, Galvin and Gagne ©2009 System Calls Both files are ready: We enter a loop read from input file (system call) and write to output file (system call) After entire file is copied: – program may close both files (system call) – Write a message to the console or window (more system calls) – Terminate normally (final system calls) Usually system executes thousands of system calls per second see Figure (2.4) Operating System Concepts – 8th Edition 2.14 Silberschatz, Galvin and Gagne ©2009 System Calls 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) So functions (made from API) invoke the actual system calls on behalf of the application programmers Why use APIs rather than system calls? An application programmer designing a program using an API can expect her program to compile and run on any system that supports the same API. ( works on different systems) Actual system calls can often be more detailed and difficult to work with than the API available to an application programmer. Operating System Concepts – 8th Edition 2.15 Silberschatz, Galvin and Gagne ©2009 Example of System Calls System call sequence to copy the contents of one file to another file Operating System Concepts – 8th Edition 2.16 Silberschatz, Galvin and Gagne ©2009 Example of Standard API Consider the ReadFile() function in the Win32 API—a function for reading from a file A description of the parameters passed to ReadFile() HANDLE file—the file to be read LPVOID buffer—a buffer where the data will be read into and written from DWORD bytesToRead—the number of bytes to be read into the buffer LPDWORD bytesRead—the number of bytes read during the last read LPOVERLAPPED ovl—indicates if overlapped I/O is being used Operating System Concepts – 8th Edition 2.17 Silberschatz, Galvin and Gagne ©2009 System Call Implementation Typically, a number associated with each system call System-call interface maintains a table indexed according to these numbers The system call interface invokes intended system call in OS kernel and returns status of the system call and any return values 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) Operating System Concepts – 8th Edition 2.18 Silberschatz, Galvin and Gagne ©2009 API – System Call – OS Relationship Operating System Concepts – 8th Edition 2.19 Silberschatz, Galvin and Gagne ©2009 Standard C Library Example C program invoking printf() library call, which calls write() system call Operating System Concepts – 8th Edition 2.20 Silberschatz, Galvin and Gagne ©2009 System Call Parameter Passing Often, with system call, more information is required than simply identity of desired system call Exact type and amount of information vary according to OS and call For example to get input we may need: Specify the fie or the device as the source Address and length of memory buffer into in which the input should be read. Operating System Concepts – 8th Edition 2.21 Silberschatz, Galvin and Gagne ©2009 System Call Parameter Passing Three general methods used to pass parameters to the OS 1. Simplest: pass the parameters in registers In some cases, may be more parameters than registers 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 2. Operating System Concepts – 8th Edition 2.22 Silberschatz, Galvin and Gagne ©2009 Parameter Passing via Table Operating System Concepts – 8th Edition 2.23 Silberschatz, Galvin and Gagne ©2009 Types of System Calls Process control File management Device management Information maintenance Communications Protection Operating System Concepts – 8th Edition 2.24 Silberschatz, Galvin and Gagne ©2009 Examples of Windows and Unix System Calls Operating System Concepts – 8th Edition 2.25 Silberschatz, Galvin and Gagne ©2009 Process control end / abort A running program needs to be able to halt its execution either normally (end) or abnormally (abort) Load / execute A process or job executing one program may want to load and execute another program Create process / terminate a process Process may need to create a new process and terminate it. Get process attributes/ set process attributes If we create new job or process, we need to control the execution. This control requires the ability to determine and reset the attributes of a job or process, including the process’s priority, time execution and so on. Operating System Concepts – 8th Edition 2.26 Silberschatz, Galvin and Gagne ©2009 Process control wait for time/ wait event/ signal event Having created new processes, we may need to wait for them to finish their execution, we may want to wait for a certain amount of time to pass (wait time) We will want to wait for a specific event to occur (wait event) The process should then signal when the event has occurred (signal event) Lock / release lock When two or more process share data, OS provides system calls allow a process to lock shared data to prevent another process from accessing the data while its locked. Release lock, remove prevention from locked data. Such system calls lock/ release lock used in concurrent process execution Operating System Concepts – 8th Edition 2.27 Silberschatz, Galvin and Gagne ©2009 Process control Variations of process control: Single-tasking system MS-Dos Execute only one process Process cannot create another process Multi-tasking system FreeBSD (derived from Berkeley UNIX) It accept commands from user and return back to command line To run another process, shell execute fork() system call, then the selected program loaded into memory via exec() system call When process is done, it executes an exit() system call to terminate Operating System Concepts – 8th Edition 2.28 Silberschatz, Galvin and Gagne ©2009 MS-DOS execution (a) At system startup (b) running a program Operating System Concepts – 8th Edition 2.29 Silberschatz, Galvin and Gagne ©2009 FreeBSD Running Multiple Programs Operating System Concepts – 8th Edition 2.30 Silberschatz, Galvin and Gagne ©2009 File Management Create file/ delete file Need the files name and perhaps their attributes Open/ close/ read/ write/ reposition Once created, we need to open it and use it We may also need to read, write, reposition (rewinding or skipping to the end of the file) We need to close the file Same as the directories Get file attributes/ set file attributes Attributes such as file name, file type, protection code, accounting information and so on. Operating System Concepts – 8th Edition 2.31 Silberschatz, Galvin and Gagne ©2009 Device Management A process may need several resources to execute. Resources devices physical (disk drives) virtual devices (files) Request device/ release device When program needs to use some resources. Once it done, it release it Functions are similar to open and close system calls for files Read/ write/ reposition device Same as files Operating System Concepts – 8th Edition 2.32 Silberschatz, Galvin and Gagne ©2009 Information Maintenace Many system calls exist to transfer information between user program and OS Get time/ get date Other system may return other information such as number of users, OS version, free memory and so on OS keep information about all processes and system calls are used to access this information. Get (process/ file / device) attributes Set (process/ file / device) attributes Operating System Concepts – 8th Edition 2.33 Silberschatz, Galvin and Gagne ©2009 Communication Two common models of interprocesses communication Message-passing model Shared-memory model Message-passing model Use messages between processes to transfer information Messages exchanged through a common mail-box First: a connection must be opened, the name of other communicator muse be known Can be a process in the same system or other computer in the network Each computer has a host name, and a network identifier (IP address) Each process has a process name. Operating System Concepts – 8th Edition 2.34 Silberschatz, Galvin and Gagne ©2009 Communication Message-passing model –continued The get hostid and get processid system calls (from process name to process identifier) do this translation The identifiers then passed to: General-purpose open and close calls provided by file system or Specific open connection and close connection system calls depending on the system model of communication. Operating System Concepts – 8th Edition 2.35 Silberschatz, Galvin and Gagne ©2009 Communication Message-passing model –continued The recipient process must give its permission for communication to take place with an accept connection call. Most processes receiving connection are special-purpose daemons. They execute a wait for connection call and are awakened when the connection is made. Source process known as client and receiving daemon known as server. Then exchange message by using read message and write message system calls The close connection call terminates the communication Operating System Concepts – 8th Edition 2.36 Silberschatz, Galvin and Gagne ©2009 Communication Shared memory model Processes use shared memory create and shared memory attach system calls to create and gain access to regions of memory owned by other process. Once permission granted, processes can exchange information by reading and writing data in the shared memory. This form of data is controlled by the process not OS. Processes ensure that they are not using the same location of memory simultaneously Comparisons: Both models are used, the first one is useful for smaller amount of data, and easier to implement than shared memory model for inter-computer communication. Shared-memory model allow maximum speed since it is done at the memory transfer speed. Problems (protection –synchronization between processes sharing the memory) Operating System Concepts – 8th Edition 2.37 Silberschatz, Galvin and Gagne ©2009 Protection Protection provide a mechanism for controlling access to the resources of the system With the advent of networking and Internet, protection becomes more important. Typical system calls for protection include Set permission Get permission Which manipulate the permission settings of resources such as files and disks. Allow user and Deny user system calls decide what user has the right to access certain resources. Operating System Concepts – 8th Edition 2.38 Silberschatz, Galvin and Gagne ©2009 End of Chapter 2 Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009