Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Process Scheduling Chapter 5. 2 Introduction u Policy and implementation u Objectives: u Fast response time u High throughput (turnaround time) u Avoidance.

Similar presentations


Presentation on theme: "1 Process Scheduling Chapter 5. 2 Introduction u Policy and implementation u Objectives: u Fast response time u High throughput (turnaround time) u Avoidance."— Presentation transcript:

1 1 Process Scheduling Chapter 5

2 2 Introduction u Policy and implementation u Objectives: u Fast response time u High throughput (turnaround time) u Avoidance of process starvation u Context switching is expensive u Context is a snapshot of the values of the general-purpose, memory management, and other special registers.

3 3 Type of Scheduling u Long-term u Performed when new process is created. u The decision to add to the pool of processes to be executed. u Medium-term u Swapping u The decision to add to the number of processes that are partially or fully in main memory

4 4 Types of Scheduling u Short-term u Which ready process to execute next u The decision as to which available processes will be executed by the processor. u FCFS, Round-Robin, Shortest process next, Shortest remaining time u I/O u The decision as to which process’s pending I/O request shall be handled by available I/O device

5 5 Scheduling and Process State Transition ExitRunning Ready Blocked Blocked, suspend Ready, suspend New Long-term scheduling Medium-term scheduling Medium-term scheduling Long-term scheduling Short-term scheduling

6 6 Queuing Diagram for Scheduling Processor Medium-term scheduling Medium-term scheduling Batch jobs Long-term scheduling Time-out Interactive users Ready Queue Ready, Suspend Queue Blocked, Suspend Queue Blocked Queue Event Occurs Event Wait Release Short-term scheduling

7 7 5.2 Clock Interrupt Handling u Clock interrupt is the 2 nd to the power-failure interrupt. u Tasks: u Returns the hardware clock u Update CPU usage statistics u Performs scheduler-related functions u Sends a SIGXCPU signal to the current process u Updates the time-of-day and other related clocks. u Handles callouts u Wakes up system processes u Handles alarms

8 8 5.2.1 Callouts u Records a function that the kernel must invoke at a later time. u int to_ID = timeout(void(*fn), caddr_t arg, long delta) u void untimeout(int to_ID) u Tasks: u Retransmission of network packets u Certain scheduler and memory management functions u Monitoring devices to avoid losing interrupts u Polling devices that do not support interrupts

9 9 Callout in BSD UNIX

10 10 5.2.2 Alarms u Real-time: u relates to the actual elapsed time, and notifies the process via a SIGALRM signal. u Profiling: u Measures the amount of time the process has been executing and uses the SIGPROF signal for notification. u Virtual-time: u Monitors only the time spent by the process in user mode and sends the SIGVTALRM signal.

11 11 5.3 Scheduler Goals u The scheduler must ensure that the system delivers acceptable performance to each application. u Different applications: u Interactive: 50-150ms u Batch: scientific computation u Real-time: time-critical

12 12 5.4 Traditional UNIX Scheduling u To improve response times of interactive users, while ensuring that low-priority, background jobs do not starve. u Priority-based: u User-process is preempted u Kernel is strictly non-preempted

13 13 Priority u Kernel:0-49, user: 50-127 u proc fields: u p_pri: Current scheduling priority u p_usrpri: User mode priority u p_cpu: Measure of recent CPU usage u p_nice: User-controllable nice factor u Kernel: u Sleeping priority

14 14 User mode priority u Depends on two factors: u Nice: 0-39 u CPU usage u Time-sharing: equal opportunity u decay factor: for SVR3 it is 1/2, for 4.3BSD: u decay = ( 2*load_average)/(2*load_average+1) u p_cpu = p_cpu* decay u p_usrpri = PUSER + (p_cpu/4) +(2*p_nice)

15 15 Example : PUSER = 50 P1 P_usrpri= 110 P_cpu = 80 Nice = 20 P2 P_usrpri= 120 P_cpu = 80 Nice=25 T1 P1 P_usrpri= 115 P_cpu = 100 Nice = 20 P2 P_usrpri= 110 P_cpu = 40 Nice = 25 T2 Decay =1/2 P1 P_usrpri= 102 P_cpu = 50 Nice = 20 P2 P_usrpri= 115 P_cpu = 60 Nice = 25 T3 Decay =1/2

16 16 Scheduler Implementation u 32 run queues: doubly linked list of proc structures for runnable processes. u whichqs: bitmask for each queue, “1” means that there is a runnable process u swtch(): context switch by p_addr u Saving part of u area (pcb) u Loading the saved context. u VAX ffs & ffc : special instructions for context switch

17 17

18 18 Run Queue Manipulation u roundrobin(): for the processes with the same priority. u schedcpu(): recomputes the priority once per second u Removes the process from the run queue; u recomputes the priority u Puts it back

19 19 When to switch context u The current process blocks on a resource or exits. u The priority recomputation procedure results in the priority of another process becoming greater than that of the current one( flag runrun set). u The current process, or an interrupt handler, wake up a higher-priority process

20 20 Analysis u Not scale well u No way to let a specific process to occupy the CPU u No guarantee to real-time applications u Little control of priorities u Kernel is non-preemptive, high-priority runnable processes may have to wait for the kernel to relinquish the CPU

21 21 5.5 The SVR4 Scheduler u Support a diverse range of applications including those requiring real-time response u Separate the scheduling policy from the mechanisms that implement it u Provide applications with greater control over their priority and scheduling. u Define a scheduling framework with a well-defined interface to the kernel u Allow new scheduling policies to be added in a modular manner, including dynamic loading of scheduler implementations. u Limit the dispatch latency for time-critical applications.

22 22 The class-independent Layer u Responsible for context switching, run queue management, & preemption.

23 23 Preemption points u Places of code where the kernel data is in a steady state and is about to begin a long computation. u In the pathname parsing routine lookuppn() u In the open system call, before file creation u In the memory subsystem, before freeing the pages of a process. u Call PREEMPT() check kprunrun

24 24 Interface to the Scheduling Classes u 3 fields of proc u p_cid: class ID, an index into the global class table u p_clfuncs: pointer to the classfuncs vector for the class u p_clproc: pointer to a class-dependent private data structure u #define CL_SLEEP(procp, clprocp, …) (*(procp)-p->clfuncs->cl_sleep)(clprocp, …)

25 25 Interface cnt’d u Entry u CL_TICK: the clock interrupt handler u CL_FORK, CL_FORKRET: fork u CL_ENTERCLASS, CL_EXITCLASS: enter, exit u CL_SLEEP: sleep() u CL_WAKEUP: wakeprocs() u Priorities: u 0-59: time-sharing class u 60-99: system priority u 100-159: real-time class

26 26

27 27 The Time Sharing Class u The default class for a process. u Round-robin scheduling: u Event-driven scheduling u tsproc: u ts_timeleft: time remaining in the quantum u ts_cpupri : system part of the priority u ts_upri: user part of the priority(nice value) u ts_umpri: user mode priority (ts_cpupri+ ts_upri) u ts_dispwait: seconds since start the quantum u Dispatcher parameter table

28 28 Dispatcher parameter table New ts_cpupri to set when the quantum expires. New ts_cpupri to set when returning to user mode after sleeping Number of seconds to wait for quantum expiry before using ts_lwait. Use instead of ts_tqexp if process took longer than ts_maxwait to use up its quantum.

29 29 The Real-Time Class u 100-159: higher than any time-sharing process. u The real-time process must wait until the current process is about to return to user mode or until it reaches a kernel preemption point. u Real-time processes require bounded dispatch latency and bounded response time. u The response time = the time for interrupt handler + dispatch latency.

30 30

31 31 The priocntl System Call u Basic operations: u Changing the priority class of the process u Setting ts_upri for time-sharing processes u Resetting priority and quantum for real-time processes u Obtaining the current value of several scheduling parameters u priocntlset: perform the same operations on a set of processes - a system/ a process group/ session/ a scheduling class/ a particular user/ having the same parent.

32 32 Adding a scheduling class u Provide an implementation of each class- dependent scheduling function u Initialize a classfuncs vector to point to these functions u Provide an initialization function to perform setup tasks such as allocating internal data structures u Add an entry for this class in the class table u Rebuild the kernel

33 33 Analysis u Provides flexible approach that allows the addition of scheduling classes to a system. u Event-driven scheduling favors I/O-bound & interactive jobs over CPU-bounded ones. u No good way for a time-sharing class process to switch to a different one. priocntl is only used by the superuser. u It is difficult to tune the system properly for a mixed set of applications. u Solaris2.x improved SVR4

34 34 5.6 Solaris 2.x Enhancements u Multithreaded, symmetric- multiprocessing OS u Preemptive Kernel u Fully preemptive u Implement interrupts by special kernel threads u Interrupt threads always run at the highest priority in the system.

35 35 Multiprocessor Support u Processors can communicate by cross- processor interrupt u Per-processor data structure u Cpu_thread: currently running thread u Cpu_dispthread: last selected to run u Cpu_idle: idle thread u Cpu_runrun: preemption flag used for time- sharing threads u Cpu_kprunrun: preemption flag set by real-time threads u Cpu_chosen_level: priority of thread that is going to preempt the current thread

36 36 Multiprocessor scheduling T6 becomes runnable - preempts T3

37 37

38 38 Hidden Scheduling u The kernel schedules the work without considering the priority of the thread for which it is doing the work. u E.G. STREAMS services. u Moving STREAMS processing into kernel threads. u Callouts handled by a special callout thread (has max system priority)

39 39 Priority Inversion u A situation where a lower-priority thread holds a resource needed by a higher priority process, thereby blocking that higher-priority process.

40 40 Solution u Solved by priority inheritance or priority lending.

41 41 Priority inheritance must be transitive.

42 42 Implementation of Priority inheritance u An extra state to implement priority inheritance u A global priority & inherited priority for each thread u pi_willto(): traverses the synchronization chain and passes on the inherited priority of the calling thread. u pi_waive(): surrenders its inherited priority.

43 43

44 44

45 45 Limitations of Priority Inheritance u Can be implemented only when it is known which thread is going to free the resource, i.e. when the resource is held by a single, known thread. u For mutexes the owner is always known, so pr. Inh. can be used, u For semaphores, and conditions variables the owner is usually indeterminate, so pr. inh. is not used, u When a reader/writer lock is used for writing there is a single, known owner; It may be held however by multiple readers, so then there is no single owner.

46 46 Limitations of Priority Inheritance u Solaris defines an owner-of record, which is the first thread that obtained the read clock. If a higher priority writer blocks on this object, the owner-of record thread will inherit its priority. If there are other readers – they cannot inherit the writer’s priority, so the solution is limited. u While reducing the time a high-priority process must block, in the worst case however this time is still much greater than what is acceptable for many real-time applications. u Alternative solutions – ceiling protocol – it requires however a priori knowledge of all processes in the system and their resource requirements – possible in embedded applications.

47 47 Turnstiles u Restrict the sleep queue to threads blocked on a particular resource – limiting the time taken to process the queue u Threads are queued in order of their priority; u To unlock turnstile: signal – for single highest priority thread, broadcast – for all blocked threads.

48 48

49 49 Solaris scheduling evaluation u Suitable for multithreaded and many real-time applications for uni- and multiprocessors; u Still missing other desirable real-time features such as gang scheduling and deadline-driven scheduling

50 50 Linux Scheduling u Scheduling classes u SCHED_FIFO: First-in-first-out real-time threads u SCHED_RR: Round-robin real-time threads u SCHED_OTHER: Other, non-real-time threads u Within each class multiple priorities may be used

51 51

52 52 Non-Real-Time Scheduling u Linux 2.6 uses a new scheduler - the O(1) scheduler u Time to select the appropriate process and assign it to a processor is constant regardless of the load on the system or number of processors u Separate queue for each priority. u Higher priority assigned lower number

53 53 Non-Real-Time Scheduling u Two queue structure – for active queues and for expired queues u All scheduling is done from the active queue structure; u when it becomes empty a switch is made with the expired queue structure and the scheduling continues

54 54

55 55 Calculating priorities u For non-real time priority is changed dynamically as a function of the task’s static priority and its execution behavior. u For real-time tasks priority is fixed u SCHED_FIFO tasks do not have assigned time-slices u SCHED_RR tasks have assigned time slices but they are never moved to the expired queue structure


Download ppt "1 Process Scheduling Chapter 5. 2 Introduction u Policy and implementation u Objectives: u Fast response time u High throughput (turnaround time) u Avoidance."

Similar presentations


Ads by Google