Intended Schedule III. Process Scheduling Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment 2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment 3 11.05. Process Scheduling 3. Assignment 2. Assignment 4 18.05. Process Synchronization 4. Assignment 3. Assignment 5 25.05. Inter Process Communication 5. Assignment 4. Assignment 6 01.06. Pfingstmontag 6. Assignment 5. Assignment 7 08.06. Input / Output 7. Assignment 6. Assignment 8 15.06. Memory Management 8. Assignment 7. Assignment 9 22.06. 9. Assignment 8. Assignment Filesystems 10 29.06. 10. Assignment 9. Assignment 11 06.07. Special subject: Transactional Memory 10. Assignment 12 13.07. Special subject: XQuery your Filesystem 13 20.07. Wrap up session 27.07. First examination date 12.10. Second examination date 1 2 Processes An operating system executes a variety of programs: Batch system jobs Time-shared systems user programs or tasks Textbooks use the terms job and process almost interchangeably Process a program in execution; process execution must progress in sequential fashion For the OS, processes are the unit of all resource allocation A process includes: program counter stack data section Process in memory 3 4
Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information 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 Accounting information I/O status information 5 6 At any time, processes can be described as either: Single Process I/O-bound spends more time doing I/O than computations, many short CPU bursts CPU-bound spends more time doing computations; few very long CPU bursts typical processes follow a CPU I/O burst cycle Process execution consists of a cycle of CPU execution and I/O wait Process Switching Operating system tries to maximize CPU utilization: multiprogramming run multiple processes and try to interleave their CPU-I/O burst cycles Scheduling 7 8
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 Context-switch time is overhead; the system does no useful work while switching Time dependent on hardware support Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue controls the degree of multiprogramming invoked very infrequently (sec, min) may be slow Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU invoked very frequently (msec) must be fast Medium-term scheduler swaps running processes out 9 10 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 11 12
Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program Dispatch latency time it takes for the dispatcher to stop one process and start another running 13 14 Scheduling 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 time amount of time a process has been waiting in the ready queue Optimization Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time Scheduling Strategies Response time amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) Deadlines esp. for real-time scheduling 15 16
Process Burst Time (duration) P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: P 2 P 3 P 1 P 1 P 2 P 3 0 24 27 30 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 0 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 3 6 30 Convoy effect (or phenomenon) short processes behind long process 17 18 Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time 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 know as the Shortest-Remaining-Time-First (SRTF) Process Arrival Time Burst 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 P 3 P 2 P 4 SJF is optimal gives minimum average waiting time for a given set of processes 0 3 7 8 12 16 Average waiting time = (0 + 6 + 3 + 7)/4 = 4 19 20
Process Arrival Time Burst Time P 1 0.0 7 P 2 2.0 4 P 3 4.0 1 P 4 5.0 4 SJF (preemptive) Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging P 1 P 2 P 3 P 4 P 2 P 1 0 2 4 5 7 11 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 21 22 = 0 n+1 = n Recent history does not count = 1/2 = 1 0 = 10 n+1 = t n Only the actual last CPU burst counts If we expand the formula, we get: n+1 = t n +(1 - ) t n -1 + +(1 - ) j t n -j + +(1 - ) n +1 0 Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor 23 24
A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive nonpreemptive 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 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. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high rule of thumb: 80% of CPU bursts should be smaller than time quantum 25 26 Process Burst Time P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: P 1 P 2 P 3 P 4 P 1 P 3 P 4 P 1 P 3 P 3 0 20 37 57 77 97 117 121 134 154 162 Typically, higher average turnaround than SJF, but better response 27 28
Ready queue is partitioned into separate queues, e.g., foreground (interactive) background (batch) Each queue has its own scheduling algorithm foreground RR background FCFS Scheduling must be done between the queues Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. Time slice each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR 20% to background in FCFS 29 30 A process can move between the various queues; aging can be implemented this way Multilevel-feedback-queue scheduler defined by the following parameters: number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service 31 32
Three queues: Q 0 RR with time quantum 8 milliseconds Q 1 RR time quantum 16 milliseconds Q 2 FCFS Scheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. 33 34 Multiple Processors CPU scheduling more complex when multiple CPUs are available Homogeneous processors within a multiprocessor Load sharing Asymmetric multiprocessing only one processor accesses the system data structures, alleviating the need for data sharing Real-Time Scheduling Hard real-time systems required to complete a critical task within a guaranteed amount of time Soft real-time computing requires that critical processes receive priority over less fortunate ones Threads 35 36
Threads...? Multiple streams of program execution within a single process Light-weight processes Switching from one thread to another requires less effort Threads share (some) context... can be implemented on top, e.g., in a library (userlevel threads) or inside the kernel (kernel-level threads) 37 38 Responsiveness Resource Sharing Economy Utilization of MP Architectures Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X 39 40
Many-to-One: Many user-level threads mapped to single kernel thread Solaris Green Threads GNU Portable Threads One-to-One: Each user-level thread maps to kernel thread Windows NT/XP/2000 Linux Solaris 9 and later Many-to-Many Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package 41 42 43 44
Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier Semantics of fork() and exec() system calls Does fork() duplicate only the calling thread or all threads? Thread cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled Signal handling Thread pools Thread specific data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Scheduler activations 45 46 Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads Examples 47 48
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) 49 50 Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface 51 52
Intended Schedule Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment 2 04.05. Systems Programming using C (Process Control) 2. Assignment 1. Assignment 3 11.05. Process Scheduling 3. Assignment 2. Assignment 4 18.05. Process Synchronization 4. Assignment 3. Assignment 5 25.05. Inter Process Communication 5. Assignment 4. Assignment 6 01.06. Pfingstmontag 6. Assignment 5. Assignment 7 08.06. Input / Output 7. Assignment 6. Assignment 8 15.06. Memory Management 8. Assignment 7. Assignment 9 22.06. 9. Assignment 8. Assignment Filesystems 10 29.06. 10. Assignment 9. Assignment 11 06.07. Special subject: Transactional Memory 10. Assignment 12 13.07. Special subject: XQuery your Filesystem 13 20.07. Wrap up session 27.07. First examination date 12.10. Second examination date IV. Process Synchronization 53 54