* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Operating System Architecture
Survey
Document related concepts
Transcript
計算機概論 作業系統 (Operating Systems) 楊舜仁 清大資工系 References • J. Glenn Brookshear, "Computer Science - An Overview", Addison-Wesley • Abraham Silberschatz, Peter B. Galvin, and Greg Gagne, "Operating System Concepts", Wiley Outline • • • • • The History of Operating Systems Operating System Architecture Coordinating the Machine’s Activities Handling Competition Among Processes Security What is an Operating System? An operating system is the software that controls the overall operation of a computer. It: – provides the means by which a user can store and retrieve files, – provides the interface by which a user can request the execution of programs, and – provides the environment necessary to execute the programs requested. Outline • • • • • The History of Operating Systems Operating System Architecture Coordinating the Machine’s Activities Handling Competition Among Processes Security Batch Processing (1/2) • The computers of the 1940s and 1950s were not very flexible or efficient. • Anyone wanting a program run was required to submit it, along with – any required data and – special directions about the program’s requirements to the computer and return later for the results. • The programs were collected in a single batch, and then executed without further interaction with the user. Batch Processing (2/2) The Drawback of Batch Processing • A major drawback of batch processing is that – the users have no interaction with their jobs once they are submitted to the computer. • This approach is acceptable for applications, such as payroll processing, – in which the data and all processing decisions are established in advance. • However, it is not acceptable when the user must interact with a program during its execution. – Examples include word processing systems and computer games. Interactive Processing (1/2) • To allow a program being executed to carry on a dialogue with the user—a feature known as interactive processing, – the actions of the computer should be sufficiently fast to coordinate with the needs of the user – rather than forcing the user to conform to the machine’s timetable • In a sense, the computer is forced to execute tasks under a deadline, – a process that became known as real-time processing • in which the actions performed are said to occur in real-time. Interactive Processing (2/2) Time-sharing/Multitasking (1/2) • When several users seek interactive service from a machine at the same time, real-time considerations presented obstacles. – If the operating system insisted on executing only one job at a time, • only one user would receive satisfactory real-time service. • The solution to this problem was – to design operating systems that provided service to multiple users at the same time: • a feature called time-sharing, applying the technique called multiprogramming. Time-sharing/Multitasking (2/2) • In multiprogramming: – time is divided into intervals and then the execution of each job is restricted to only one interval at a time; – at the end of each interval, the current job is temporarily set aside and another is allowed to execute during the next interval; – by rapidly shuffling the jobs back and forth in this manner, the illusion of several jobs executing simultaneously is created. • Today, multiprogramming techniques are used in single-user as well, called multitasking: – referring to one user executing numerous tasks simultaneously Multiprocessor Machines • The development of multiprocessor machines has led to operating systems that provide timesharing/multitasking capabilities by – assigning different tasks to different processors – sharing the time of each single processor • These operating systems must wrestle with such problems as – load balancing: dynamically allocating tasks to the various processors – scaling: breaking tasks into a number of subtasks compatible with the number of processors available Outline • • • • • The History of Operating Systems Operating System Architecture Coordinating the Machine’s Activities Handling Competition Among Processes Security Software Classification Types of Software • Application software – Performs specific tasks for users – Examples: spreadsheets, database systems, accounting systems, program development software, and games. • System software – Performs those tasks that are common to computer systems in general • Provides infrastructure for application software – Consists of the operating system and utility software (e.g., software for playing multimedia presentations and handling network communication) • Unfortunately, the distinction between software can be vague. Operating System Components • Shell or User Interface: Communicates with users – Text based – Graphical user interface (GUI) • Kernel: Performs basic required functions – File manager – Memory manager – Device drivers – Scheduler and dispatcher • Scheduler: determines which activities are to be considered for execution; • Dispatcher: controls the allocation of time to the activities The shell as an interface between users and the operating system File Manager • Its job is to coordinate the use of the machine’s mass storage facilities. It “records” – Where each file is located – Which users are allowed to access the various files – Which portions of mass storage are available for new files or extensions to existing files • The procedure of any access to a file by other software units: – requesting that the file manager grant access to the file through a procedure known as opening the file – if the file manager approves the requested access, it provides: • the information needed to find and to manipulate the file Memory Manager (1/2) • For simple batch processing: – the program for performing the current task is placed at a predetermined location in main memory, executed, – and then replaced by the program for performing the next task • However, for multiuser or multitasking – many programs and blocks of data must reside in main memory concurrently – the memory manager must • find and assign memory space for these needs and • ensure that the actions of each program are restricted to the program’s allotted space – the memory manager must also keep track of those memory areas no longer occupied Memory Manager (2/2) • The total main memory space required may exceed the space actually available in the computer. • In this case, the memory manager may create the illusion of a large virtual memory space by using a technique called paging: – the memory manager reserves some storage space on a magnetic disk – the data is divided into uniform sized units called pages, which are typically a few KB in size – then, the memory manager shuffles these pages back and forth between main memory and mass storage • so that the pages that are needed at any given time are actually present in the “real” main memory Device Drivers (1/2) • Device drivers are the software units that communicate with the controllers to carry out operations on the peripheral devices. • Each device driver is uniquely designed for its particular type of device (such as a printer, disk drive, or monitor): – translates generic requests into the more technical steps required by the device assigned to that driver – e.g., reading and decoding a printer’s status word as well as all the other handshaking details Communicating with Other Devices The Role of Controllers (1/2) • Communication between a computer and other devices is normally handled – through an intermediary apparatus known as a controller. • A controller may – consist of circuitry permanently mounted on the computer’s motherboard or, – it may take the form of a circuit board that plugs into a slot on the motherboard • The controller connects via cables – to peripheral devices within the computer case or – perhaps to a connector, called a port, on the back of the computer where external devices can be attached. The Role of Controllers (2/2) • These controllers are sometimes small computers themselves, – each with its own memory circuitry and simple CPU • A controller translates messages and data back and forth between forms compatible with – the internal characteristics of the computer and – those of the peripheral device to which it is attached • Each controller communicates with the computer itself – by means of connections to the same bus that connects the computer’s CPU and main memory Handshaking (1/2) • Handshaking: a constant two-way dialogue, in which – the computer and the peripheral device exchange information about the device’s status and coordinate their activities • Handshaking often involves a status word: – which is a bit pattern that is generated by the peripheral device and sent to the controller; – the bits reflect the conditions of the device Handshaking (2/2) • For example, in the case of a printer, – the least significant bit of the status word may indicate whether the printer is out of paper – while the next bit may indicate whether the printer is ready for additional data • The controller may respond to this status information itself or make it available to the CPU. Device Drivers (1/2) • Device drivers are the software units that communicate with the controllers to carry out operations on the peripheral devices. • Each device driver is uniquely designed for its particular type of device (such as a printer, disk drive, or monitor): – translates generic requests into the more technical steps required by the device assigned to that driver – e.g., reading and decoding a printer’s status word as well as all the other handshaking details Device Drivers (2/2) • The other software components can merely rely on the device drivers to take care of the details. – In this manner, the design of the other software units can be independent of the unique characteristics of particular devices. Getting it Started • How does the operating system get started? – through a procedure known as boot strapping (often shortened to booting) • that is performed by a computer each time it is turned on • To understand the boot strap process and the reason it is necessary, we begin by considering the machine’s CPU: – A CPU is designed so that its program counter starts with a particular predetermined address each time the CPU is turned on. The 1st Approach • How about storing the operating system at this predetermined memory address the program counter points to? • Unfortunately, for technical reasons, a computer’s main memory (RAM) is typically constructed from volatile technologies: – the memory loses the data stored in it when the computer is turned off – the contents of main memory must be replenished each time the computer is restarted The 2nd Approach • How about storing the operating system at nonvolatile memory cells – known as read-only memory (ROM) because its contents can be read but not altered • However, devoting large blocks of main memory to nonvolatile storage is not efficient with today’s technology. • Moreover, computer operating systems undergo frequent updates – for security and device drivers of the latest hardware. The 3rd Approach: Boot Strapping • A program called the boot loader is permanently stored in the machine’s ROM. – the program that is initially executed when the machine is turned on • The boot loader: 1. first directs the CPU to transfer the operating system from a predetermined location (e.g., disk storage) into the main memory, in particular, the RAM; 2. then, directs the CPU to execute a jump instruction to that area of memory to launch the operating system. • The overall process of executing the boot loader and thus starting the operating system is called booting the computer. The booting process Firmware • The boot loader is a type of firmware. • The term firmware was coined to describe the middle ground between hardware and software. – Firmware is the software stored in nonvolatile memory such as FlashROM. – not immutably etched into the silicon of the machine—the hardware – also not as readily changeable as the rest of the programs in mass storage—the software • BIOS (Basic Input/Output System) is also a long used firmware system – used by the boot loader to perform I/O activities before the operating system becomes functional Outline • • • • • The History of Operating Systems Operating System Architecture Coordinating the Machine’s Activities Handling Competition Among Processes Security The Concept of a Process (1/2) • Program vs. Process: – Program: a static set of directions – Process: the activity of executing a program • a dynamic activity whose properties change as time progresses – Analogy: a piece of sheet music vs. a musician performing that piece The Concept of a Process (2/2) • Process State: Current status of the activity – the value of the program counter • the current position in the program being executed – the values in the other CPU registers – the associated memory cells The process state is a snapshot of the machine at a particular time. Process Competition/Administration • Typical time-sharing/multitasking computers are running many processes – all competing for the computer’s resources • It is the task of the operating system (the scheduler and dispatcher) to manage these processes so that: – each process has the resources (peripheral devices, space in main memory, access to files, and access to a CPU) that it needs; – independent processes do not interfere with one another; – processes that need to exchange information are able to do so The Scheduler (1/2) • To keep track of all the processes: – the scheduler maintains a block of information in main memory called the process table. • Each time the execution of a program is requested – the scheduler creates a new entry for that process in the process table • This table entry contains such information as – the memory area assigned to the process (obtained from the memory manager), – the priority of the process, and – whether the process is ready or waiting. The Scheduler (2/2) • Ready vs. waiting – A process is ready if it is in a state in which its progress can continue. – A process is waiting if its progress is currently delayed until some external event occurs, e.g., • the completion of a mass storage operation, • the pressing of a key at the keyboard, or • the arrival of a message from another process. The Dispatcher • The dispatcher is the component that controls the allocation of time slices to the processes in the process table. • In a time-sharing/multitasking system, this task is accomplished by multiprogramming: 1. dividing time into short segments, each called a time slice 2. then switching the CPU’s attention among the processes as each is allowed to execute for one time slice The procedure of changing from one process to another is called a process switch (or a context switch). Time-sharing between process A and process B Interrupt Handling (1/2) • Each time the dispatcher awards a time slice to a process, – it initiates a timer circuit that will indicate the end of the slice by generating a signal called an interrupt. • An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. – Other applications of a computer’s interrupt system: • clicking a mouse • pressing a key on the keyboard • if the computer’s power is unexpectedly disrupted – All these cases generate interrupt signals that cause the CPU to set aside its current activity and address the cause of the interrupt. Interrupt Handling (2/2) • Interrupt handling: – When receiving an interrupt signal, the CPU 1. completes its current machine cycle 2. saves the current process’s state (for later context switch-back) 3. begins execution of a program, called an interrupt handler, which preempts the current process and transfers control back to the dispatcher – The dispatcher then 1. selects the highest-priority ready process from the process table 2. restarts the timer circuit and begin the next time slice Performance of Multiprogramming • The use of multiprogramming has been found to increase the overall efficiency of a machine – although the shuffling of processes required by multiprogramming introduces an overhead. • If a process executes an I/O request identified as “waiting” in the process table the dispatcher will cease to award time slices to that process progress on other tasks will be made while the I/O request is being performed the entire collection of tasks will be completed in less time Outline • • • • • The History of Operating Systems Operating System Architecture Coordinating the Machine’s Activities Handling Competition Among Processes Security Allocation of a Machine’s Resources • An important task of an operating system is the allocation of the machine’s resources to the processes in the system. – the file manager allocates access to files and allocates mass storage space for the construction of new files; – the memory manager allocates memory space; – the scheduler allocates space in the process table; – the dispatcher allocates time slices. Access to a Printer • If a process needs to print its results, it must request that the operating system give it access to the printer’s device driver. • The operating system must decide whether to grant this request: – depending on whether the printer is already being used by another process. – If it is not, the operating system should • grant the request and allow the process to continue; – otherwise, the operating system should • deny the request • classify the process as a waiting process until the printer becomes available A Simple Flag System (1/2) • To control access to the printer, – the operating system must keep track of whether the printer has been allocated. • One approach is to use a flag, a bit in memory whose states are referred to as set and clear: – a clear flag (value 0) indicates that • the printer is available – a set flag (value 1) indicates that • the printer is currently allocated A Simple Flag System (2/2) • The operating system merely checks the flag each time a request for printer access is made. – If it is clear, the request is granted and the operating system sets the flag. – If the flag is set, the operating system makes the requesting process wait. • Each time a process finishes with the printer, the operating system – either allocates the printer to a waiting process – or, if no process is waiting, merely clears the flag A Problem of this Simple Flag System (1/4) • The task of testing and possibly setting the flag may require several machine instructions. – The value of the flag must be • retrieved from main memory, • manipulated within the CPU, and • finally stored back in memory. • It is therefore possible for a task to be interrupted – after a clear flag has been detected but – before the flag has been set. A Problem of this Simple Flag System (2/4) • In particular, suppose the printer is currently available, and a process requests use of it. – The flag is retrieved from main memory and found to be clear. • However, at this point, – the process is interrupted and – another process begins its time slice, who too requests the use of the printer. – Again, the flag is retrieved from main memory and found still clear. A Problem of this Simple Flag System (3/4) • Consequently, the operating system allows the second process to begin using the printer. • Later, the original process resumes execution where it left off, – which is immediately after the operating system found the flag to be clear. • Thus the operating system continues by – setting the flag in main memory and – granting the original process access to the printer. => Two processes are now using the same printer! A Problem of this Simple Flag System (4/4) • The solution to this problem is to insist that – the task of testing and possibly setting the flag be completed without interruption. The 1st Approach: Interrupt Enable/Disable Instructions • Using the interrupt disable and interrupt enable instructions provided in most machine languages. – an interrupt disable instruction causes future interrupts to be blocked, – an interrupt enable instruction causes the CPU to resume responding to interrupt signals. • Thus, if the operating system 1. starts the flag-testing routine with a disable interrupt instruction and 2. ends it with an enable interrupt instruction no other activity can interrupt the routine once it starts. The 2nd Approach: test-and-set Instruction • Using the test-and-set instruction that is available in many machine languages. • This instruction directs the CPU to 1. retrieve the value of a flag, 2. note the value received, and then 3. set the flag all within a single machine instruction. • Because the CPU always completes an instruction before recognizing an interrupt, – the task of testing and setting the flag cannot be split when it is implemented as a single instruction. Semaphore • A properly implemented flag, as just described, is called a semaphore, – in reference to the railroad signals used to control access to sections of track. • Semaphores are used in software systems in much the same way as they are in railway systems: – In railway systems: the section of track that can contain only one train at a time – In software systems: a sequence of instructions that should be executed by only one process at a time. Critical Region and Mutual Exclusion (1/2) • In software systems, a sequence of instructions that – should be executed by only one process at a time is called a critical region. • The requirement that – only one process at a time be allowed to execute a critical region is known as mutual exclusion. • A common way of obtaining mutual exclusion to a critical region is to – guard the critical region with a semaphore. Critical Region and Mutual Exclusion (2/2) 1. To enter the critical region, a process must – find the semaphore clear and – then set the semaphore before entering the critical region; 2. Upon exiting the critical region, the process must clear the semaphore. 3. If the semaphore is found in its set state, the process trying to enter the critical region must – wait until the semaphore has been cleared. Deadlock • Deadlock: the condition in which two or more processes are blocked from progressing because – each is waiting for a resource that is allocated to another. • For example: in systems in which processes are allowed to create new processes to perform subtasks, if – the scheduler has no space left in the process table and – each process in the system must create an additional process before it can complete its task, then no process can continue. A deadlock resulting from competition for nonshareable railroad intersections Deadlock Conditions • Deadlock cannot occur unless all three of the following conditions are satisfied: 1. There is competition for nonshareable resources. 2. The resources are requested on a partial basis; that is, • having received some resources, a process will return later to request more. 3. Once a resource has been allocated, it cannot be forcibly retrieved. Deadlock Detection and Correction • Use of deadlock detection and correction schemes to attack the third condition – the occurrence of such deadlock is considered so remote – no effort is made to avoid the problem • The approach is to detect it should it occur and then correct it by forcibly retrieving some of the allocated resources – If deadlock should occur due to a full process table, administrator can use his/her powers as “super-user” to “kill” some of the processes. Deadlock Avoidance • Use of deadlock avoidance schemes to attack the first two conditions. – Condition 2: Requiring each process to request all its resources at one time – Condition 1: Converting nonshareable resources into shareable ones • e.g., the “spooling” technique of holding data for output at a later but more convenient time An Example of Spooling • Suppose the resource is a printer and a variety of processes require its use. • Instead of connecting the process to the printer’s device driver, – connect it to a device driver that stores the information to be printed in mass storage – the nonshareable resource appears shareable under the illusion of more than one printer • Later, when the printer is available, – the operating system could transfer the data from mass storage to the printer Outline • • • • • The History of Operating Systems Operating System Architecture Coordinating the Machine’s Activities Handling Competition Among Processes Security Security • Since the operating system oversees the activities in a computer, it is natural for it to play a vital role in maintaining security. • This responsibility manifests itself in multiple forms, one of which is reliability. – a flaw in the file manager causes the loss of part of a file – a defect in the dispatcher leads to a system failure causing the loss of an hour’s worth of typing Attacks from outside (2/2) • Access by unauthorized personnel – establishing “accounts” for the authorized users • an account: being essentially a record containing the user’s name, password, and privileges – login procedure to control access to the system • Auditing software: record and analyze the activities taking place within the system – attempts to login using incorrect passwords – activities within a user’s account that do not conform to that user’s past behavior – the presence of sniffing software Attacks from outside (2/2) – the presence of sniffing software • e.g., a program that simulates the operating system’s login procedure and trick authorized users into thinking they are communicating with the operating system • The carelessness of the users themselves – easy to guess passwords – share their passwords with friends – fail to change their passwords on a timely basis – import unapproved software – transfer mass storage devices between machines Attacks from within • If an intruder gains access to a general user’s account, – to trick the operating system into allowing the intruder to reach beyond the privileges granted to that user – allowing a process to access main memory cells outside its allotted area (memory manager) – retrieving files whose access should be denied (file manager) – etc. An Example: Memory Access Restriction (1/2) • Restrict a process to the area of main memory assigned to it by the memory manager – special-purpose registers that store the upper and lower limits of a process’s allotted memory area. • If the reference is found to be outside the process’s designated area, – the CPU automatically transfers control back to the operating system • by performing an interrupt sequence An Example: Memory Access Restriction (2/2) • Unfortunately, without further security features, a process could still gain access to memory cells outside of its designated area merely – by changing the special-purpose registers that contain its memory limits. Privilege Levels and Privileged Instructions (1/2) • To protect against such actions, CPUs are designed to operate in one of two privilege levels: – in privileged mode: • the CPU is able to execute all the instructions in its machine language – in nonprivileged mode: • the list of acceptable instructions is limited • The instructions that are available only in privileged mode are called privileged instructions. Examples include instructions that – change the contents of memory limit registers; – change the current privilege mode of the CPU; Privilege Levels and Privileged Instructions (2/2) • When first turned on, the CPU is in privileged mode. – Thus, when the operating system starts at the end of the boot process, all instructions are executable. • Each time the operating system allows a process to start a time slice, – it switches the CPU to nonprivileged mode by executing a “change privilege mode” instruction – the operating system will be notified if the process attempts to execute a privileged instruction