Processes Scheduling and Switching
Total Page:16
File Type:pdf, Size:1020Kb
Operating Systems Process Scheduling and Switching Introduction • An important aspect of multiprogramming is scheduling. The resources that are scheduled are IO and processors. • The goal is to achieve – High processor utilization – High throughput • number of processes completed per unit time – Low response time • time elapse from the submission of a request to the beginning of the response Processor Scheduling • Maximize CPU use, quickly switch processes onto CPU for time sharing. • Process scheduler selects among available processes for next execution on CPU. • Maintains scheduling queues of processes: – 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. • Processes migrate among the various queues. Scheduling Criteria • CPU utilisation: During heavy loads, the CPU is busy almost 90% and in the lighter loads it is only active around 40% • Throughput: the total number of processes that gets completed in unit of time is called throughput. • Turnaround time: the time span from submission of a process to the system until is completed • Waiting Time: the time spent by a process in a different queues • Response Time: the time taken by a process in producing its first response after submission. Requirements of scheduling • An ideal scheduling discipline – is easy to implement – is fair and protective – provides performance bounds • Each scheduling discipline makes a different trade-off among these requirements Ease of implementation • Scheduling discipline has to make a decision once every few microseconds! • Should be implementable in a few instructions or hardware – for hardware: critical constraint is VLSI space – Complexity of enqueue + dequeue processes • Work per packet should scale less than linearly with number of active connections Types of Scheduling • Preemptive • Non-Preemptive Preemptive Scheduling • When the CPU switches from one process to another before its completion, then is called preemptive scheduling • Reasons why CPU leaves one process: – Some higher priority process arrives in the system – An interrupt occurs in a process – A child process comes into a parent process Non-preemptive Scheduling • The CPU executes the process until it is terminated or until any input/output need arise. Self-Quiz – Define CPU Scheduling – Define turnarounds time Types of Schedulers 1. Long-term scheduler (jobs scheduler) – selects which programs/processes should be brought into the ready queue. 2. Medium-term scheduler (emergency scheduler) – selects which job/process should be swapped out if system is loaded. 3. Short-term scheduler (CPU scheduler) – selects which process should be executed next and allocates CPU. Long-Term Scheduling • Determines which programs are admitted to the system for processing. • Controls the degree of multiprogramming. • If more processes are admitted: – less likely that all processes will be blocked – better CPU usage. – each process has less fraction of the CPU. • Long-term scheduler strives for good process mix. Short-Term Scheduling • Determines which process is going to execute next (also called CPU scheduling). • The short term scheduler is also known as the dispatcher (which is part of it). • Is invoked on a event that may lead to choose another process for execution: – clock interrupts – I/O interrupts – operating system calls and traps – signals Long/Short-Term Scheduling Short- term Long- term Dispatcher (short-term scheduler) • Is an OS program that moves the processor from one process to another. • It prevents a single process from monopolizing processor time. • It decides who goes next according to a scheduling algorithm. • The CPU will always execute instructions from the dispatcher while switching from process A to process B. Dispatcher • Is a module of a OS that provides control of the CPU to the process which is selected by the short time scheduler. • The dispatcher should be as fast as possible. • The time consumed by the dispatcher is known as dispatch latency Degree of multiprogramming. • The degree of multiprogramming describes the maximum number of processes that a single- processor system can accommodate efficiently. • The primary factor affecting the degree of multiprogramming is the amount of memory available to be allocated to executing processes. Aspects of Schedulers • Long-term scheduler is invoked very infrequently (seconds, minutes) ⇒ (may be slow). • The long-term scheduler controls the degree of multiprogramming. • Short-term scheduler is invoked very frequently (milliseconds) ⇒ (must be fast). • Processes can be described as either: – I/O-bound process – spends more time doing I/O than computations, many short CPU bursts. – CPU-bound process – spends more time doing computations; few very long CPU bursts. Medium-Term Scheduling • So far, all processes have to be (at least partly) in main memory. • Even with virtual memory, keeping too many processes in main memory will deteriorate the system’s performance. • The OS may need to swap out some processes to disk, and then later swap them back in. • Swapping decisions based on the need to manage multiprogramming. Addition of Medium Term Scheduling Medium- term Short- term Long- term SWAPPING Schematic View of Swapping Dynamics of Swapping • A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued execution • Backing store – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images. • Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priority process is swapped out so higher-priority process can be loaded and executed. • Major part of swap time is transfer time; total transfer time is directly proportional to the amount of memory swapped. • Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and Windows). • System maintains a ready queue of ready-to-run processes which have memory images on disk Swapping Example Support for Swapping • The OS may need to suspend some processes, i.e., to swap them out to disk and then swap them back in. • We add 2 new states: – Blocked Suspend: blocked processes which have been swapped out to disk. – Ready Suspend: ready processes which have been swapped out to disk. STATE TRANSITIONS New state transitions • Blocked –> Blocked Suspend – When all processes are blocked, the OS will make room to bring a ready process in memory. • Blocked Suspend –> Ready Suspend – When the event for which it has been waiting occurs (state info is available to OS). • Ready Suspend –> Ready – when no more ready processes in main memory. • Ready –> Ready Suspend (unlikely) – When there are no blocked processes and must free memory for adequate performance. Another view of the 3 levels of scheduling Classification of Scheduling Activity A Seven-state Process Model QUEUING Queuing Diagram for Scheduling Process Scheduling Queues • Process queue – set of all processes in the system. • Ready queue – set of processes residing in main memory, ready and waiting to execute. • Device queues – set of processes waiting for an I/O device. • Processes migrate among the various queues. A Queuing Discipline • When event n occurs, the corresponding process is moved into the ready queue PROCESS CONTROL BLOCK Ready Queue and various I/O Device Queues Process state. The state may be new, ready, running, waiting, halted, and so on. Program counter. The counter indicates the address of the next instruction to be executed for this process CPU registers. They include accumulators, index registers,stack pointers, and general-purpose registers, plus any condition-code information. CPU-scheduling information. This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. Memory-management information. This information may include such information as the value of the base and registers, the page tables, or the segment tables, depending on the memory system used by the operating system. Accounting information. This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, I/O status information. This information includes the list of I/O devices allocated to the process, a list of open files, and so on. SWITCHING The CPU-I/O Cycle • We observe that processes require alternate use of processor and I/O in a repetitive fashion • Each cycle consist of a CPU burst (typically of 5 ms) followed by a (usually longer) I/O burst • A process terminates on a CPU burst • CPU-bound processes have longer CPU bursts than I/O-bound processes The CPU-I/O Cycle • CPU bursts vary from process to process, and from program to program, but an extensive study shows frequency patterns similar to that The CPU-I/O Cycle • Almost all processes alternate between two states in a continuing cycle : – A CPU burst of performing calculations, and – An I/O burst, waiting for data transfer in or out of the system. CPU/IO Bursts • Bursts of CPU usage alternate with periods of I/O wait – a CPU-bound process – an I/O bound process When to Switch a Process? • A process switch may occur whenever the OS has gained control of CPU. i.e., when: – Supervisor Call • explicit request by the program (example: file open) – the process will probably be blocked. – Trap • an error resulted from the last instruction – it may cause the process to be moved to terminated state. – Interrupt • the cause is external to the execution of the current instruction – control is transferred to Interrupt Handler. Reasons for Process Switch Context Switch • When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process. • This is called context switch. • Context of a process represented in the PCB. • The time it takes is dependent on hardware support. • Context-switch time is overhead; the system does no useful work while switching. Process Switch Context switch between processes (1) A.