780 likes | 1.12k Views
Chapter 6. Operating Systems. Learning outcomes. 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?
E N D
Chapter 6 Operating Systems
Learning outcomes • 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? • Explain how the long-term, short-term and medium-term schedulers operate. • Explain how the operating system manages memory • Partitioning, swapping, paging, demand paging • Explain how the operating system manages I/O devices • Programmed I/O, Interrupt-driven I/O, DMA • Explain the concept of Interrupt • Describe how interrupts are caused.
Additional Reading • Essential Reading • Stalling (2003): Chapter 7 and 8 • Further Reading • Brookshear (2003): Chapter 3.1 – 3.4 • Flynn and McHoes (2001): Chapters 1,2,3,4,5 and 7. • Silberschatz et al. (2002): Chapters 1,4,6,9,10, and 13
What is an operating system? • A program that runs on the hardware and supports • Resource Abstraction • Resource Sharing • Abstracts and standardises the interface to the user across different type of hardware • 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
Introduction (1) • The aims of an operating system are: • User convenience • System performance • Number of requests serviced per unit time, etc
Introduction (2) • 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
Operating Systems • 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.
Main functions of an operating system • 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.
A model of an operation System User command interface Process Manager Memory manager Device Manager File manager Network manager Operation System Resource management
Operating system as a user/computer interface • A user command such as open, save or print would correspond a sequence of machine-code instructions. • The user does not need to provide these sequences of instructions. • Operating system translates these commands to a machine-code instructions.
Operating system as a resource manager • 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? Resource management
Type of operating systems • Multi-programming • Operating system can handle several programs at once. • 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.
How the operating system get started? • Main memory has a small section of permanent read only memory (ROM) • ROM contains a program, bootstrap. • At the start the CPU runs bootstrap. Which directs the CPU to load the operation system from disk and transfer control to it.
Main memory Main memory Disk storage R O M R O M Bootstrap program Bootstrap Program Operating System Operating System R A M R A M
Operating system as a process manager • Coordinates the occupation of the main memory by different processes and their data. • At any time the operation system may be dealing with many processes. • e.g. a process may be executed or allowed to wait in main memory, or swapped out of the main memory.
Processes • Definition of a process • Process Scheduling • Operations on Processes • Cooperating Processes
What is a process • Process – a program in execution; process execution must progress in sequential fashion. • A process includes: • program counter • stack • data section • heap
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 myInteger = 10; int myResult = myInteger * 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.)
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.ReadLine(); 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.
Process State • 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.
Process Control Block (PCB) Information associated with each process. • Identifier • Process state • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information
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).
Process Scheduling Queues • 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.
Schedulers • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue. • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.
Medium Term Scheduling • 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
Intermediate queue Job queue Ready queue CPU End Process request I/O I/O I/O I/O
Scheduling Criteria • 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 • 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,
Optimization Criteria • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time • In most cases we optimize the average measure
P1 P2 P3 0 24 27 30 Scheduling AlgorithmsFirst-Come, First-Served (FCFS) ProcessBurst Time P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3 = 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.
P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P2 , P3 , P1 • The Gantt chart for the schedule is: • Waiting time for P1 = 6;P2 = 0; P3 = 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
FCFS Scheduling (Cont.) • 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 • I/O bound processes complete their burst and enter ready queue – I/O devices idle and I/O bound processes waiting • CPU bound process completes CPU burst and moves to I/O device • I/O bound processes all quickly complete their CPU bursts and enter I/O queue – now CPU is idle
Shortest-Job-First (SJR) Scheduling • 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) • Two schemes: • 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.
P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (non-preemptive) • Average waiting time = (0 + 6 + 3 + 7)/4 = 4
P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 Example of Preemptive SJF Process Arrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (preemptive) • Average waiting time = (9 + 1 + 0 +2)/4 = 3
Priority Scheduling • 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.
Round Robin (RR) • 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. • 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.
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example of RR with Time Quantum = 20 ProcessBurst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is: • Typically, higher average turnaround than SJF, but better response.
Memory Management • When a process is executed it has to be in main memory as the main memory can be accessed quicker. • An efficient use of the main memory is an important task of the operation system. • Different memory management techniques are used for this purpose.
Memory partition • How processes are arranged in the main memory before been executed? • Fixed-sized partitions • Variable-sized partitions
Fixed-sized partitions OS 8M 8M 8M 8M 8M
Variable-sized partitions OS 8M 2M 4M 8M 18M
Swapping • 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.
disk Main memory Operation System Long-term queue No Swapping Completed processes Main memory Long-term queue Operation System With Swapping Completed processes Medium-term
os os os os P1 P1 P1 p2 p2 p3 a c b d os os os os P1 P1 p2 P4 P4 P4 P3 P3 p3 p3 e g f h
Fragmentation • 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
Problems with swapping • 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
Paging • A program is divided into small fixed-sized chunks(pages). • Main memory is divided into small fixed-sized chunks (frames). • A page is stored in one frame. • A program is stored in a set of frames. These frames do not need to be continuous.
disk disk 13 page 0 of A 13 Process A Process A 14 page 1 of A page 0 page 1 page 2 page 3 page 0 page 1 page 2 page 3 14 page 2 of A 15 15 16 In use In use 16 17 In use In use 17 18 A- page table page 3 of A 18 19 In use In use 13 19 14 20 20 15 18
Logical and physical address disk page 0 of A Page 1 13 Process A page 1 of A 14 I . . . J(30) page 0 page 1 page 2 page 3 Logical address(J) page 2 of A 15 1:30 In use 16 In use 17 A- page table page 3 of A 18 In use 19 13 14 20 Physical address(J) 15 18 14:30
simple paging is not efficient • 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.