2 Learning outcomesDescribe 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 processProcess control block?Explain how the long-term, short-term and medium-term schedulers operate.Explain how the operating system manages memoryPartitioning, swapping, paging, demand pagingExplain how the operating system manages I/O devicesProgrammed I/O, Interrupt-driven I/O, DMAExplain the concept of InterruptDescribe how interrupts are caused.
3 Additional Reading Essential Reading Further Reading Stalling (2003): Chapter 7 and 8Further ReadingBrookshear (2003): Chapter 3.1 – 3.4Flynn and McHoes (2001): Chapters 1,2,3,4,5 and 7.Silberschatz et al. (2002): Chapters 1,4,6,9,10, and 13
4 What is an operating system? A program that runs on the hardware and supportsResource AbstractionResource SharingAbstracts and standardises the interface to the user across different type of hardwareVirtual machine hides the messy details witch must be performed.Manages the hardware resourcesEach program gets time with the resourceEach program gets space on the resource
5 Introduction (1) The aims of an operating system are: User convenience System performanceNumber of requests serviced per unit time, etc
6 Introduction (2) Fundamental tasks of an operation system Management of ProgramsOrganize their execution by sharing the CPUEnsure good user service and efficient useManagement of ResourcesEfficient allocation/de-allocation without constraining user programsSecurity and ProtectionEnsure absence of interference with programs and resources by entities within and outside the operating system
7 Operating SystemsApplication 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.
8 Main functions of an operating system User/computer interface:Provides an interface between the user and the computerResource manager:manages all computer’s resources.Process managerMemory managerDevice managerFile manager, etc.
9 A model of an operation System User commandinterfaceProcess ManagerMemory managerDevice ManagerFile managerNetwork managerOperation SystemResourcemanagement
10 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.
11 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 asmany programs as possibleI/O Device (e.g.printer) Manager:Which program should use a particular I/O device?Network manager:which computer should execute a particular program?Resourcemanagementprocess management: which program should be executed next? and how much time should be given to each program?memory management: how to make the best use of the available memory to run as many program as possible?I/O(e.g. a printer) management: which program should use a particular I/O device.
12 Type of operating systems Multi-programmingOperating system can handle several programs at once.Time-sharingOperating 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.Multiprogramming OSAll the programs to be run are loaded in the main memory.The operation system picks one program and execute it.Once it is finished, the operation system picks another one and execute it.A program might involve I/O operation, which is usually a slow operation. If that happens, instead of waiting the I/O to operation the complete, the operation system starts executing another program.Time sharing OSOperation system allows many user to share the same computer and interact with it.Many users can use one computer through terminals.The OS allocates a very short time to each user-program, and switches rapidly from one user-program to another.Each user has the impression that the entire computer is dedicated to his/her.Time sharing can also be used on single-user computer(e.g. PC), in which case the user can work on several program at the same time (e.g. printing, , editing, …). In this case the CPU switches rapidly between the programsA time sharing operation system is an extension the multiprogramming operation system.
13 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.When the computer is turned on, the program counter starts with a particular predetermined address.At this address, a program in the ROM called ‘Bootstrap’ is stored.this program is executed, it directs the CPU to load the operation system into the main memory from mass storage.The operation system is now executed by the CPU. The operation system takes over and begins controlling the computer’s activities.
14 Main memoryMain memoryDisk storageROMROMBootstrapprogramBootstrapProgramOperatingSystemOperatingSystemRAMRAM
15 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.
16 Processes Definition of a process Process Scheduling Operations on ProcessesCooperating Processes
17 What is a processProcess – a program in execution; process execution must progress in sequential fashion.A process includes:program counterstackdata sectionheap
18 The Stack and the HeapAn 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.)
19 The Stack and the HeapMemory 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.
20 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.
21 Process Control Block (PCB) Information associated with each process.IdentifierProcess stateProgram counterCPU registersCPU scheduling informationMemory-management informationAccounting informationI/O status information
22 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).
23 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.
24 SchedulersLong-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.
25 Medium Term Scheduling Time sharing Operating systems may introduce a medium term schedulerRemoves processes from memory (and thus CPU contention) to reduce the degree of multiprogramming – swappingSwapping may be needed to improve the process mix or to free up memory if it has become overcommitted
26 Intermediate queue Job queue Ready queue CPU End Process request I/O This figure illustrates these queues and the interaction between them. A process is first added to the job queue. If the resources are available, it is put into main memory and it joins the ready queue. It is then executed by the CPU. If it requires an I/O operation, it is put in the I/O queue on the relevant I/O device. A process requiring I/O may be temporary kicked out of the main memory on to the hard disk (joining the intermediate queue), in order to to free some some space in main memory for other operation to come in. When a process is completely executed it is removed from all the queues.I/OI/OI/O
27 Scheduling Criteria CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution per time unitTurnaround time – amount of time to execute a particular processwaiting to get into memory + waiting in the ready queue + executing on the CPU + I/OWaiting time – amount of time a process has been waiting in the ready queueResponse time – amount of time it takes from when a request was submitted until the first response is produced,
28 Optimization Criteria Max CPU utilizationMax throughputMin turnaround timeMin waiting timeMin response timeIn most cases we optimize the average measure
29 Scheduling Algorithms First-Come, First-Served (FCFS) Process Burst TimeP1 24P2 3P3 3Suppose 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 = 27Average waiting time: ( )/3 = 17P1P2P3242730CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.
30 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P2 , P3 , P1The Gantt chart for the schedule is:Waiting time for P1 = 6; P2 = 0; P3 = 3Average waiting time: ( )/3 = 3Much better than previous case.Average waiting time is generally not minimal and may vary substantially if the process CPU-burst times vary greatlyP1P3P26330
31 FCFS Scheduling (Cont.) FCFS is non-preemptiveNot good for time sharing systems where each user needs to get a share of the CPU at regular intervalsShort process(I/O bound) wait for one long CPU-bound process to complete a CPU burst before they get a turnlowers CPU and device utilizationI/O bound processes complete their burst and enter ready queue – I/O devices idle and I/O bound processes waitingCPU bound process completes CPU burst and moves to I/O deviceI/O bound processes all quickly complete their CPU bursts and enter I/O queue – now CPU is idle
32 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.
33 Example of Non-Preemptive SJF Process Arrival Time Burst TimePPPPSJF (non-preemptive)Average waiting time = ( )/4 = 4P1P3P27316P4812
34 Example of Preemptive SJF Process Arrival Time Burst TimePPPPSJF (preemptive)Average waiting time = ( )/4 = 3P1P3P24211P45716
35 Priority SchedulingA priority number (integer) is associated with each processThe 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.
36 Round Robin (RR)Each process gets a small unit of CPU time (time quantum), usually 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.
37 Example of RR with Time Quantum = 20 Process Burst TimeP1 53P2 17P3 68P4 24The Gantt chart is:Typically, higher average turnaround than SJF, but better response.P1P2P3P42037577797117121134154162
38 Memory ManagementWhen 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.The operation system manages the use of the main memory, to make the best and efficient of it use.
39 Memory partitionHow processes are arranged in the main memory before been executed?Fixed-sized partitionsVariable-sized partitions
40 Fixed-sized partitions OS8M8M8MThe main memory is divided into equal-sized chunks.The size of the chunks are predetermined, fixed before the process is loaded.Fixed-sized partitioning is not an efficient way of managing main memory.Only a few processes will require exactly the same amount of memory as provided by the partition.A process of 3M will occupy one chunk and therefore the 5M is wasted.8M8M
41 Variable-sized partitions OS8M2M4M8MA better way of partitioning the main memory is variable-sized.Each process is allocated exactly the amount of memory it requires.18M
42 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 idleTo solve this problem Swapping technique is used.If the CPU becomes idle, Some processes are moved temporarily out the main memory to the disk,to free a space for new process to come to be executed by the CPU.
43 disk Main memory No Swapping Completed processes Main memory With OperationSystemLong-termqueueNoSwappingCompletedprocessesMain memoryLong-termqueueOperationSystemWithSwappingCompletedprocessesMedium-term
44 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 Process 1,2,3 (b,c,d) are loaded and each one of them is given the exact amount of memory they require.In (e-f) P4 needs to be loaded. And there is not enough space left to accommodate it.P4 < P2 and P2 requires an I/O operation.There P2 is swapped out of the memory and P4 is loaded.In (g-h) P2 becomes ready again after completing the I/O operation.P1 which now require an I/O operation is swapped out of the memory and P2 is swapped back in.Variable-sized partitioning was good initially, but gets worst quickly.The main memory will become more and more fragmented.To solve this problem we use paging technique.P4P4P3P3p3p3egfh
45 Fragmentation Memory is divided into partitions Each partition has a different sizeProcesses are allocated space and later freedAfter 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 totalIf we allow free space within a partition we have internal fragmentationFragmentation:External fragmentation = unused space between partitionsInternal fragmentation = unused space within partitions
46 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
47 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.
48 disk disk 13 13 14 14 15 15 16 In use In use 16 17 In use In use 17 18 page 0of AProcess AProcess A1314page 1of Apage 0page 1page 2page 3page 0page 1page 2page 314page 2of A151516In useIn use1617In useIn use1718A- page tablepage 3of A1819In useIn use131914202015When a program is loaded in main memory it is stored in frames.The wasted space is a fraction of the last page.If a process is divided the to 100 the first 99page are full. And therefore occupy 99 frames. The last page is the only one that might not be full. But still occupy the whole frame.As it is shown before the process A is loaded some frames in the main memory are already used.The frames representing a process A are not necessarily continuous.The operation system must know where the pages of a process are loaded in which frames in order to find the pages in main memory for the CPU to execute.The operation system maintain a page table for each process.18
49 Logical and physical address diskpage 0of APage 1Process A1314page 1of AI.J(30)page 0page 1page 2page 3Logical address(J)page 2of A151:3016In use17In useThe operation system must also know where each instruction of a process is? The instruction in a process has a logical address.Logical address:Its relative position in the program:In case of paging, it is relative position in the page containing the instruction.e.g. 1:30 ~ position 30 in page 1Physical address:When an instruction is loaded into the main memory, it is stored in a frame or a set of frames. Thus, each instruction has a physical address.The page table helps to know the physical address of each instruction in the process.e.g. physical address of the instruction (J) is 14:30 (frame 14 position 30).A- page table18page 3of A1319In use1420Physical address(J)151814:30
50 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 loadedDemand paging.
51 Demand paging Operating system – loads a page only when it is required No swapping in or out of unused pages is needed.Better use of memory.CPU can access only a number of pages of a process at one time.Then asks for more pages to be loaded.
52 Virtual memory 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).
53 Definition of ‘Interrupt’ InterruptsDefinition of ‘Interrupt’Event that disrupts the normal execution of a program and causes the execution of special instructions
58 Interrupt Service Routine ProgramProgramInterrupt Service Routinemul R1, 9mov R1, centtime t
59 Interrupt Service Routine ProgramProgramSave ContextRestore ContextInterrupt Service Routinemul R1, 9mov R1, centtime t
60 Interrupt Service Routine ProgramProgramSave ContextRestore ContextInterrupt Service Routinemul R1, 9mov R1, centeg push R1eg pop R1time t
61 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).
62 I/O devices Called peripherals: KeyboardMouseSpeakersMonitorscannerPrinterDisk driveCD-drive.OS – manages all I/O operations and devices
63 OS - I/O management 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 deviceRead: read data from the deviceWrite write data to the device.
64 I/O modules System bus I/O module I/O module CPU Main memory I/O CPU and Main memory are linked to I./O devices through the system bus.I/O module are just interface between the CPU and main memory on one side and the an I/O device on the other side.Data transfer rate in I/O devices is much lower than the CPU and the main memory.Different I/O devices use different data formats and word lengths.I/O modules facilitate the communication between the CPU and an I/O device.I/OdeviceI/Odevice
65 Advantages of I/O modules They allow the CPU to view a wide range of devices in a simple-minded formatCPU 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 efficientlyThey are 3 ways in which I/O modules can workProgrammed I/OInterrupt-driven I/ODirect memory access.
66 Programmed I/OThe CPU controls I/O device directly Via the I/O modules.The CPU sends an I/O command to the I/O module.And waits until the I/O operation is completed before sending another I/O command.The performance is poor as the CPU spends too much time waiting the I/O device.
67 Programmed I/O Ready NO yes Issue Read to I/O module Check status Read word fromI/O moduleFor example the use of programmed I/O device to read in a block of data, which consists of many words.Suppose that each instruction is to read a word and write it to the main memory.Reading a block of data consists of large number instructions.The CPU first issues a read command to the I/O module. The I/O module then tries to obtain a word from the I/O device.This takes some times and when this is done the word is put in I/O register in the I/O module which is linked the data bus.This procedure continues until all the instructions are executed – the whole block of data is transferred to the main memory.A great deal of the CPU time is wasted when a large block of data is transferred.Write wordTo memoryNOdoneyesNext instruction
68 Interrupt-driven I/OThe CPU issues a command to the I/O module and then gets on with executing other instructions.The I/O module interrupts the CPU when it is ready to exchange data with the CPU.The CPU then executes the data transfer.Most computer have interrupt lines to detect and record the arrival of an interrupt request.
69 Interrupt-driven I/O Ready NO yes CPU goes to do Other things Issue Read toI/O moduleCPU goes to doOther thingsCheck statusWhen the statusIs ready theI/O module sendsAn interrupt-signalReadyRead word fromI/O moduleWhen the CPU wants to read a word the I/O device.It issues a read signal to the CPU. The CPU then goes on doing other things.When the I/O module finishes reading the word from the device, it sends an interrupt signal to the CPU. The CPU then comes back to get the word from the I/O module and write it the main memory.In this way the CPU is doing many jobs while the I/O operation is taking place.This approach is more efficient than Programmed I/O.Write wordTo memoryNOdoneyesNext instruction
70 How does I/O module send an interrupt to the CPU? I/O module is linked to the control bus.I/O module reads a word from the I/O device.Puts the word in the data register which is linked to data bus.Sends a interrupt signal to the CPU via control bus.
71 How does CPU know Interrupt-signal? 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.
72 How does CPU process interrupts? Interrupt detection.CPU executes Interrupt-handler program.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.
73 Disadvantages of Interrupt-driven I/O 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).
74 Direct-memory-access - DMA 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.
75 DMA/CPU - bus system 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.
77 Summery OS- memory manager OS- I/O manager Fixed-sized partition: waist of memory.Variable-sized partition: fragmentation.Swapping. Time wasted in swapping the whole processSimple paging: process divided into pages and loaded into main memory(divided into frames).Demand paging: only the required pages are loaded to main memory.OS- I/O managerProgrammed 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.