Скачать презентацию Chapter 6 Operating Systems 1 Learning outcomes Скачать презентацию Chapter 6 Operating Systems 1 Learning outcomes

5aa740926db356786b901561eb075b50.ppt

  • Количество слайдов: 78

Chapter 6 Operating Systems 1 Chapter 6 Operating Systems 1

Learning outcomes l l l l Describe the functions of the Operating System and Learning outcomes l l l l Describe the functions of the Operating System and its components. Explain where the operating system is usually stored and how it gets started. Explain the concept of process • Process control block? • Partitioning, swapping, paging, demand paging • Programmed I/O, Interrupt-driven I/O, DMA • Describe how interrupts are caused. Explain how the long-term, short-term and medium-term schedulers operate. Explain how the operating system manages memory Explain how the operating system manages I/O devices Explain the concept of Interrupt 2

Additional Reading l Essential Reading l Further Reading • Stalling (2003): Chapter 7 and Additional Reading l Essential Reading l Further Reading • Stalling (2003): Chapter 7 and 8 • • • Brookshear (2003): Chapter 3. 1 – 3. 4 Flynn and Mc. Hoes (2001): Chapters 1, 2, 3, 4, 5 and 7. Silberschatz et al. (2002): Chapters 1, 4, 6, 9, 10, and 13 3

What is an operating system? l l A program that runs on the hardware What is an operating system? l l A program that runs on the hardware and supports • • Abstracts and standardises the interface to the user across different type of hardware • l Resource Abstraction Resource Sharing Virtual machine hides the messy details witch must be performed. Manages the hardware resources • • Each program gets time with the resource Each program gets space on the resource 4

Introduction (1) l The aims of an operating system are: • User convenience • Introduction (1) l The aims of an operating system are: • User convenience • System performance • Number of requests serviced per unit time, etc 5

Introduction (2) l Fundamental tasks of an operation system • • • Management of Introduction (2) l Fundamental tasks of an operation system • • • Management of Programs • • Organize their execution by sharing the CPU Ensure good user service and efficient use Management of Resources • Efficient allocation/de-allocation without constraining user programs Security and Protection • Ensure absence of interference with programs and resources by entities within and outside the operating system 6

Operating Systems l l Application programs needs to access the devices connected to a Operating Systems l l Application programs needs to access the devices connected to a computer. Operation System: (system program – slide-19, chapter 2). • • is a software layer between the hardware and the user. provides a consistent application program interface (API). first program that runs when the computer boots up. is a program that is always running when the machine is on. 7

Main functions of an operating system 1. 2. User/computer interface: • Provides an interface Main functions of an operating system 1. 2. User/computer interface: • Provides an interface between the user and the computer Resource manager: • manages all computer’s resources. • • Process manager Memory manager Device manager File manager, etc. 8

A model of an operation System User command interface Operation System Process Manager Memory A model of an operation System User command interface Operation System Process Manager Memory manager Resource management Device Manager File manager Network manager 9

Operating system as a user/computer interface l l l A user command such as Operating system as a user/computer interface l l l A user command such as open, save or print would correspond a sequence of machinecode instructions. The user does not need to provide these sequences of instructions. Operating system translates these commands to a machine-code instructions. 10

Operating system as a resource manager Resource management Process Manager: • Next program to Operating system as a resource manager Resource management Process Manager: • Next program to be executed? • Time to be given to each program? Memory manager: • Best use of the memory to run as many programs as possible I/O Device (e. g. printer) Manager: • Which program should use a particular I/O device? Network manager: • which computer should execute a particular program? 11

Type of operating systems l Multi-programming • Operating system once. l can handle several Type of operating systems l Multi-programming • Operating system once. l can handle several programs at Time-sharing • Operating system allows many user to share the same computer and interact with it. • Or, in case of a single-user computer (e. g. PC), the user can work on several programs at the same time. 12

How the operating system get started? l Main memory has a small section of How the operating system get started? l Main memory has a small section of permanent read only memory (ROM) l ROM contains a program, bootstrap. l At the start the CPU runs bootstrap. Which directs the CPU to load the operation system from disk and transfer control to it. 13

Main memory R O M R A M Bootstrap program Disk storage Operating System Main memory R O M R A M Bootstrap program Disk storage Operating System Main memory Bootstrap Program Operating System R O M R A M 14

Operating system as a process manager l Coordinates the occupation of the main memory Operating system as a process manager l Coordinates the occupation of the main memory by different processes and their data. l At any time the operation system may be dealing with many processes. l e. g. a process may be executed or allowed to wait in main memory, or swapped out of the main memory. 15

Processes l l Definition of a process Process Scheduling Operations on Processes Cooperating Processes Processes l l Definition of a process Process Scheduling Operations on Processes Cooperating Processes 16

What is a process l l Process – a program in execution; process execution What is a process l l Process – a program in execution; process execution must progress in sequential fashion. A process includes: • • program counter stack data section heap 17

The Stack and the Heap An important thing to understand about variable storage is The Stack and the Heap An important thing to understand about variable storage is the difference between the program stack and the heap. Memory in the stack is of a fixed size. Each function has its own set of variables that reside in the stack storage, which are pushed on and popped off the program stack as the program moves in and out of functions. When you declare a simple variable, you are making room for it on the program stack. int my. Integer = 10; int my. Result = my. Integer * 100; This is possible because we know the exact amount of memory each of these variables require. (These variables can also reside in registers, so simple arithmetic operations can be executed very quickly. ) 18

The Stack and the Heap Memory in the heap must be allocated, that is, The Stack and the Heap Memory in the heap must be allocated, that is, it must be requested and assigned by the operating system. This is a slower operation because it typically requires more operations than simply writing or reading a single value from a memory location. The amount allocated can be determined at run time. For example, creating a variable that holds a string, the size of which we can’t know before hand, requires an allocation from the heap: string s = Console. Read. Line(); Instead of storing the entire string on the stack, a reference to the string is stored on the stack, and the string itself is stored on the heap. 19

Process State l As a process executes, it changes state • • • new: Process State l As a process executes, it changes state • • • new: The process is being created. running: Instructions are being executed. waiting: The process is waiting for some event to occur. ready: The process is waiting to be assigned to a process. terminated: The process has finished execution. 20

Process Control Block (PCB) Information associated with each process. l Identifier l Process state Process Control Block (PCB) Information associated with each process. l Identifier l Process state l Program counter l CPU registers l CPU scheduling information l Memory-management information l Accounting information l I/O status information 21

CPU Switch From Process to Process The PCB is saved when a process is CPU Switch From Process to Process The PCB is saved when a process is removed from the CPU and another process takes its place (context switch). 22

Process Scheduling Queues l l Job queue – set of all processes in the Process Scheduling Queues l l Job queue – set of all processes in the system. Ready queue – set of all processes residing in main memory, ready and waiting to execute. Device queues – set of processes waiting for an I/O device. Process migration between the various queues. 23

Schedulers l Long-term scheduler (or job scheduler) – selects which processes should be brought Schedulers l Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue. l Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU. 24

Medium Term Scheduling l l l Time sharing Operating systems may introduce a medium Medium Term Scheduling l l l Time sharing Operating systems may introduce a medium term scheduler Removes processes from memory (and thus CPU contention) to reduce the degree of multiprogramming – swapping Swapping may be needed to improve the process mix or to free up memory if it has become overcommitted 25

Intermediate queue Job Process queue request Ready queue CPU I/O I/O End I/O 26 Intermediate queue Job Process queue request Ready queue CPU I/O I/O End I/O 26

Scheduling Criteria l l l CPU utilization – keep the CPU as busy as Scheduling Criteria l l l CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process • l l waiting to get into memory + waiting in the ready queue + executing on the CPU + I/O Waiting time – amount of time a process has been waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response is produced, 27

Optimization Criteria l l l Max CPU utilization Max throughput Min turnaround time Min Optimization Criteria l l l Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time In most cases we optimize the average measure 28

Scheduling Algorithms First-Come, First-Served (FCFS) l Process Burst Time P 1 24 P 2 Scheduling Algorithms First-Come, First-Served (FCFS) l Process Burst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: P 1 0 l l P 2 24 P 3 27 30 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait. 29

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order P 2 FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order P 2 , P 3 , P 1 l The Gantt chart for the schedule is: P 2 0 l l P 3 3 P 1 6 30 Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case. Average waiting time is generally not minimal and may vary substantially if the process CPU-burst times vary greatly 30

FCFS Scheduling (Cont. ) l l l FCFS is non-preemptive Not good for time FCFS Scheduling (Cont. ) l l l FCFS is non-preemptive Not good for time sharing systems where each user needs to get a share of the CPU at regular intervals Short process(I/O bound) wait for one long CPU-bound process to complete a CPU burst before they get a turn • lowers CPU and device utilization 1. I/O bound processes complete their burst and enter ready queue – I/O devices idle and I/O bound processes waiting 2. CPU bound process completes CPU burst and moves to I/O device 3. I/O bound processes all quickly complete their CPU bursts and enter I/O queue – now CPU is idle 31

Shortest-Job-First (SJR) Scheduling l Associate with each process the length of its next CPU Shortest-Job-First (SJR) Scheduling l Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time (on a tie use FCFS) l Two schemes: • • • l nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst. preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is known as the shortest-Remaining-Time-First (SRTF). SJF is optimal – gives minimum average waiting time for a given set of processes. 32

Example of Non-Preemptive SJF l Process Arrival Time P 1 0. 0 7 P Example of Non-Preemptive SJF l Process Arrival Time P 1 0. 0 7 P 2 2. 0 4 P 3 4. 0 1 P 4 5. 0 4 SJF (non-preemptive) P 1 0 l 3 P 3 7 P 2 8 Burst Time P 4 12 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 33

Example of Preemptive SJF Process P 1 P 2 P 3 P 4 l Example of Preemptive SJF Process P 1 P 2 P 3 P 4 l Arrival Time 7 4 1 4 0. 0 2. 0 4. 0 5. 0 SJF (preemptive) P 1 0 l Burst Time P 2 2 P 3 4 P 2 5 P 4 7 P 1 11 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 34

Priority Scheduling l l l A priority number (integer) is associated with each process Priority Scheduling l l l A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority). • Can be preemptive (compares priority of process that has arrived at the ready queue with priority of currently running process) or non-preemptive (put at the head of the ready queue) SJF is a priority scheduling where priority is the predicted next CPU burst time. Problem Starvation – low priority processes may never execute. Solution Aging – as time progresses increase the priority of the process. 35

Round Robin (RR) l Each process gets a small unit of CPU time (time Round Robin (RR) l Each process gets a small unit of CPU time (time quantum), usually 10 -100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. l If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. 36

Example of RR with Time Quantum = 20 Process P 1 53 P 2 Example of RR with Time Quantum = 20 Process P 1 53 P 2 17 P 3 68 P 4 24 l The Gantt chart is: P 1 0 l Burst Time P 2 20 37 P 3 P 4 57 P 1 77 P 3 P 4 P 1 P 3 97 117 121 134 154 162 Typically, higher average turnaround than SJF, but better response. 37

Memory Management l When a process is executed it has to be in main Memory Management l When a process is executed it has to be in main memory as the main memory can be accessed quicker. l An efficient use of the main memory is an important task of the operation system. l Different memory management techniques are used for this purpose. 38

Memory partition l l l How processes are arranged in the main memory before Memory partition l l l How processes are arranged in the main memory before been executed? Fixed-sized partitions Variable-sized partitions 39

Fixed-sized partitions OS 8 M 8 M 8 M 40 Fixed-sized partitions OS 8 M 8 M 8 M 40

Variable-sized partitions OS 8 M 2 M 4 M 8 M 18 M 41 Variable-sized partitions OS 8 M 2 M 4 M 8 M 18 M 41

Swapping l l l I/O operations are slow If a running process requires an Swapping l l l I/O operations are slow If a running process requires an I/O operation. The CPU will move to another process in the main memory. Suppose the main memory is full of processes waiting on I/O. CPU becomes idle To solve this problem Swapping technique is used. 42

disk Main memory No Swapping Long-term queue Operation System Completed processes Main memory Long-term disk Main memory No Swapping Long-term queue Operation System Completed processes Main memory Long-term queue With Swapping Medium-term Operation System Completed processes 43

os os os P 1 P 1 p 2 a os p 2 p os os os P 1 P 1 p 2 a os p 2 p 3 c d os os os b os P 1 p 2 P 4 P 3 e P 4 P 3 p 3 f g h 44

Fragmentation l l l Memory is divided into partitions Each partition has a different Fragmentation l l l Memory is divided into partitions Each partition has a different size Processes are allocated space and later freed After a while memory will be full of small holes! • No free space large enough for a new process even though there is enough free memory in total If we allow free space within a partition we have internal fragmentation Fragmentation: • • External fragmentation = unused space between partitions Internal fragmentation = unused space within partitions 45

Problems with swapping l l Swapped process are I/O output processes. I/O processes are Problems with swapping l l Swapped process are I/O output processes. I/O processes are slower. The swapping process is slow as well. Solution: • Reduce the amount of codes that needs to be swapped. • Paging 46

Paging l A program is divided into small fixed-sized chunks(pages). l Main memory is Paging l A program is divided into small fixed-sized chunks(pages). l Main memory is divided into small fixed-sized chunks (frames). l A page is stored in one frame. l A program is stored in a set of frames. These frames do not need to be continuous. 47

disk Process A page 0 page 1 page 2 page 3 disk 13 14 disk Process A page 0 page 1 page 2 page 3 disk 13 14 15 16 In use 17 In use 18 19 In use 20 Process A page 0 page 1 page 2 page 3 13 14 page 0 of A page 1 of A page 2 of A 15 In use 16 In use 17 page 3 A- page table 18 of A 13 In use 19 14 15 20 18 48

Logical and physical address disk Process A page 0 page 1 page 2 page Logical and physical address disk Process A page 0 page 1 page 2 page 3 13 14 page 0 of A page 1 of A page 2 of A 15 16 In use 17 In use page 3 A- page table 18 of A 13 19 In use 14 20 15 18 Page 1 I. . . J(30) Logical address(J) 1: 30 Physical address(J) 14: 30 49

simple paging is not efficient l l l Better than fixed and variable-sized partitions. simple paging is not efficient l l l Better than fixed and variable-sized partitions. OS - loads all pages of a process in the main memory. However, not all pages of a process need to be in the main memory in order to be executed. OS - can still execute a process if only some of the pages are loaded Demand paging. 50

Demand paging l Operating system – loads a page only when it is required Demand paging l Operating system – loads a page only when it is required l No swapping in or out of unused pages is needed. l Better use of memory. l CPU can access only a number of pages of a process at one time. l Then asks for more pages to be loaded. 51

Virtual memory l l l Demand paging gives rise the concept of virtual memory. Virtual memory l l l Demand paging gives rise the concept of virtual memory. Only a small part of a process needs to be in main memory at one time. Programs which require bigger memory that main memory can still be executed. Impression of a bigger computer memory. This concept of the main memory is called virtual memory. Demand paging and virtual memory are widely used in today’s operation systems (wind-2000, XP). 52

Interrupts Definition of ‘Interrupt’ Event that disrupts the normal execution of a program and Interrupts Definition of ‘Interrupt’ Event that disrupts the normal execution of a program and causes the execution of special instructions 53

Interrupts Interrupt Program time t 54 Interrupts Interrupt Program time t 54

Interrupts Program time t 55 Interrupts Program time t 55

Interrupts Interrupt Program Interrupt Service Routine time t 56 Interrupts Interrupt Program Interrupt Service Routine time t 56

Interrupts fahrenheit = (cent * 9/5 )+ 32 Interrupt Program mov R 1, cent Interrupts fahrenheit = (cent * 9/5 )+ 32 Interrupt Program mov R 1, cent mul R 1, 9 div R 1, 5 add R 1, 32 mov fahr, R 1 time t 57

Interrupts Interrupt Program mov R 1, cent mul R 1, 9 Interrupt Service Routine Interrupts Interrupt Program mov R 1, cent mul R 1, 9 Interrupt Service Routine time t 58

Interrupts Interrupt Program mov R 1, cent Save Context Program Interrupt Service Routine Restore Interrupts Interrupt Program mov R 1, cent Save Context Program Interrupt Service Routine Restore Context mul R 1, 9 time t 59

Interrupts Interrupt Program mov R 1, cent Save Context eg push R 1 Program Interrupts Interrupt Program mov R 1, cent Save Context eg push R 1 Program Interrupt Service Routine Restore Context mul R 1, 9 eg pop R 1 time t 60

CPU Switch From Process to Process The PCB is saved when a process is CPU Switch From Process to Process The PCB is saved when a process is removed from the CPU and another process takes its place (context switch). 61

I/O devices l l Called peripherals: • • Keyboard Mouse Speakers Monitor scanner Printer I/O devices l l Called peripherals: • • Keyboard Mouse Speakers Monitor scanner Printer Disk drive CD-drive. OS – manages all I/O operations and devices 62

OS - I/O management l There are four main I/O operations. • Control: • OS - I/O management l There are four main I/O operations. • Control: • • • tell the system to perform some action (e. g. rewind tape). Test: check the status of the device Read: read data from the device Write write data to the device. 63

I/O modules System bus CPU Main memory I/O module I/O device 64 I/O modules System bus CPU Main memory I/O module I/O device 64

Advantages of I/O modules l l l They allow the CPU to view a Advantages of I/O modules l l l They allow the CPU to view a wide range of devices in a simple-minded format CPU does not need to know details of timing, format, or electronic mechanics. CPU only needs to function in terms of a simple read and write commands. They help the CPU to work more efficiently They are 3 ways in which I/O modules can work • • • Programmed I/O Interrupt-driven I/O Direct memory access. 65

Programmed I/O l The CPU controls I/O device directly Via the I/O modules. l Programmed I/O l The CPU controls I/O device directly Via the I/O modules. l The CPU sends an I/O command to the I/O module. l And waits until the I/O operation is completed before sending another I/O command. l The performance is poor as the CPU spends too much time waiting the I/O device. 66

Programmed I/O Issue Read to I/O module Check status Ready Read word from I/O Programmed I/O Issue Read to I/O module Check status Ready Read word from I/O module Write word To memory NO done Next instruction yes 67

Interrupt-driven I/O l The CPU issues a command to the I/O module and then Interrupt-driven I/O l The CPU issues a command to the I/O module and then gets on with executing other instructions. l The I/O module interrupts the CPU when it is ready to exchange data with the CPU. l The CPU then executes the data transfer. l Most computer have interrupt lines to detect and record the arrival of an interrupt request. 68

Interrupt-driven I/O Issue Read to I/O module CPU goes to do Other things Check Interrupt-driven I/O Issue Read to I/O module CPU goes to do Other things Check status When the status Is ready the I/O module sends An interrupt-signal Ready Read word from I/O module Write word To memory NO done Next instruction yes 69

How does I/O module send an interrupt to the CPU? l I/O module is How does I/O module send an interrupt to the CPU? l I/O module is linked to the control bus. l I/O module reads a word from the I/O device. l Puts the word in the data register which is linked to data bus. l Sends a interrupt signal to the CPU via control bus. 70

How does CPU know Interruptsignal? l l l l The CPU executes an instruction How does CPU know Interruptsignal? l l l l The CPU executes an instruction cycle. An interrupt stage is added at the end of the cycle. At the end of an instruction cycle the CPU checks for interrupts. The CPU hardware has a wire, interrupt-request line that the CPU can sense. If no interrupt the CPU carries on executing next instruction. Otherwise, it updates the process control block, save it. Then process the interrupt. Resume the execution of the interrupted process. 71

How does CPU process interrupts? l l l Interrupt detection. CPU executes Interrupt-handler program How does CPU process interrupts? l l l Interrupt detection. CPU executes Interrupt-handler program makes use of the process control block save earlier. Interrupt-handler decides what to do with interrupt. Then asks the CPU to resume the execution interrupted. 72

Disadvantages of Interruptdriven I/O l l CPU is responsible for managing I/O data transfer. Disadvantages of Interruptdriven I/O l l CPU is responsible for managing I/O data transfer. Every transferred word must go through the CPU. Devices with large transfer, e. g. disk drive, the CPU wastes time dealing with data transfer. Solution: Direct-memory-access(DMA). 73

Direct-memory-access - DMA l l l Special-purpose processor. Handles data transfer. CPU issues to Direct-memory-access - DMA l l l Special-purpose processor. Handles data transfer. CPU issues to the DMA: • starting address in main memory to read/write to. • Starting address in the I/O device to read/write to. • The number of words to be transferred. DMA transfers data without intervention from the CPU. DMA sends interrupt to the CPU when transfer is completed. 74

DMA/CPU - bus system l l l DMA take care data transfer. CPU free DMA/CPU - bus system l l l DMA take care data transfer. CPU free to do other jobs. However, they can not use the bus at the same time. DMA can use the bus only when the CPU is not using it. Some times it has to force to CPU to free the bus, cycles stealing. 75

DMA/CPU System bus DMA CPU Main memory I/O module I/O device 76 DMA/CPU System bus DMA CPU Main memory I/O module I/O device 76

Summery l OS- memory manager • Fixed-sized partition: waist of memory. • Variable-sized partition: Summery l OS- memory manager • Fixed-sized partition: waist of memory. • Variable-sized partition: fragmentation. • Swapping. Time wasted in swapping the whole process • Simple paging: process divided into pages and loaded • l into main memory(divided into frames). Demand paging: only the required pages are loaded to main memory. OS- I/O manager • Programmed I/O: CPU waste waiting for I/O operation. • Interrupt-driven I/O: CPU responsible for data transfer. • DMA: takes care of data transfer instead the CPU. 77

fin Thank you and good luck 78 fin Thank you and good luck 78