5 Process Scheduling


Published on

Process Scheduling

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

5 Process Scheduling

  1. 1. Process SchedulingBasic ConceptsScheduling CriteriaScheduling AlgorithmsMultiple-Processor SchedulingThread Scheduling Loganathan R, CSE, HKBKCE 1
  2. 2. 1. Basic Concepts• Maximum CPU utilization obtained with multiprogramming1.1 CPU–I/O Burst Cycle• Process execution consists of a cycle of CPU execution and I/O wait• CPU burst distribution vary greatly from process to process and from computer to computer• This distribution is important in the selection of an CPU- scheduling algorithm Histogram of CPU-burst durations Alternating sequence of Loganathan R, CSE, HKBKCE CPU and I/O bursts 2
  3. 3. 1. Basic Concepts Contd..1.2 CPU Scheduler (Short-term scheduler)• Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them• Ready queue is not necessarily a first-in, first-out (FIFO) queue and it can be implemented as a FIFO queue, a priority queue, a tree, or an unordered linked list1.3 Preemptive Scheduling• 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 state 4. Terminates• Scheduling under 1 and 4 is non-preemptive or cooperative – Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. Example : Windows 3.x/95• Scheduling under 2 and 3 is preemptive -- Incurs cost in shared data access s and affects OS kernel design1.4 Dispatcher• Dispatcher module gives control of the CPU to the process selected by the short-term scheduler which 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 Loganathan R, CSE, HKBKCE 3
  4. 4. 2. Scheduling Criteria• Choosing a scheduling algorithms will be based on the properties of the algorithms• Many Criteria have been suggested for comparing CPU scheduling algorithms 1. CPU utilization – keep the CPU as busy as possible 2. Throughput – number of processes completed per time unit 3. Turnaround time – amount of time to execute a particular process 4. Waiting time – amount of time a process waiting in the ready queue 5. Response time - time from the submission of a request until the first response is produced, not output (for time-sharing environment)• Optimization Criteria – Maximize CPU utilization – Maximize throughput – Minimize turnaround time – Minimize waiting time – Minimize response time Loganathan R, CSE, HKBKCE 4
  5. 5. 3. Scheduling Algorithms3.1 First-Come, First-Served (FCFS) Scheduling• The process that requests the CPU first is allocated the CPU first• Managed with a FIFO queue -When a process enters the ready queue, its PCB is linked to the tail of the queue and when the CPU is free, it is allocated to the process at the head of the queue Process Burst Time P1 24 P2 3 P3 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 = 27• Average waiting time: (0 + 24 + 27)/3 = 17 P1 P2 P3 0 24 27 30Suppose 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 P2 P3 P1• Average waiting time: (6 + 0 + 3)/3 = 3 0 3 6 30• Convoy effect - all the other processes wait for the one big process to get off the CPU Loganathan R, CSE, HKBKCE 5
  6. 6. 3. Scheduling Algorithms Contd..3.2 Shortest-Job-First (SJF) Scheduling• Associate with each process the length of its next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst (shortest-next-CPU-burst algorithm).• Two schemes: – Non-preemptive – 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)• Example of Non-Preemptive SJF Process Burst Time P1 6 P4 P1 P3 P2 P2 8 P3 7 9 16 24 0 3 P4 3Waiting time for P1 = 3; P2 = 16; P3 = 9; P4 = 0Average waiting time: (3 + 16 + 9 + 0)/4 = 7 Loganathan R, CSE, HKBKCE 6
  7. 7. 3. Scheduling Algorithms Contd..3.2 Shortest-Job-First (SJF) Scheduling• Example of Preemptive SJF Process Arrival Time Burst Time P1 0 7 P2 2 4 P1 P2 P3 P2 P4 P1 P3 4 1 P4 5 4 0 2 4 5 7 11 16• Average waiting time = (9 + 1 + 0 +2)/4 = 3• Moving a short process before a long one decreases the waiting time of the short process more than it increases the waiting time of the long process• SJF is optimal – gives minimum average waiting time for a given set of processesDetermining Length of Next CPU Burst• Can be done by using the length of previous CPU bursts, using exponential averaging 1. t n  actual length of n th CPU burst 2.  n 1  predicted value for the next CPU burst 3.  , 0    1 4. Define :  n  1   t n  1    n .  Loganathan R, CSE, HKBKCE 7
  8. 8. 3. Scheduling Algorithms Contd..3.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)• Equal-priority processes are scheduled in FCFS order• Preemptive - Preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process.• SJF is a priority scheduling where priority is the predicted next CPU burst time• Problem  indefinite blocking or Starvation – low priority processes may never execute• Solution  Aging – as time progresses increase the priority of the process Process Burst Time Priority P1 10 3 P2 1 1 P3 2 4 P1 P4 P2 P5 P3 P4 1 5 P5 5 2 0 1 6 16 18 19 Loganathan R, CSE, HKBKCE 8
  9. 9. 3. Scheduling Algorithms Contd..3.4 Round Robin (RR) Scheduling• 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 is too large  FCFS – q is too small  context switch overhead is too high• Typically, higher average turnaround than SJF, but better response• Example of RR with Time Quantum = 20Process Burst Time P1 53 P2 17 P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 P3 68 0 20 37 57 77 97 117 121 134 154 162 P4 24 Loganathan R, CSE, HKBKCE 9
  10. 10. 3. Scheduling Algorithms Contd..3.4 Round Robin (RR) Scheduling• The time quantum to be large with respect to the context switch time(10% of the time quantum) Smaller time quantum increases context switches Turnaround timevaries time quantum Loganathan R, CSE, HKBKCE 10
  11. 11. 3. Scheduling Algorithms Contd..3.5 Multilevel Queue Scheduling• Ready queue is partitioned into separate queues and processes are permanently assigned to one queue, based on some of the process such as memory size, process priority, or process type. Example : foreground (interactive) & background (batch)• Each queue has its own scheduling algorithm ,foreground – RR and 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 and 20% to background in FCFS Loganathan R, CSE, HKBKCE 11
  12. 12. 3. Scheduling Algorithms Contd..3.6 Multilevel Feedback-Queue Scheduling• 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 it needs service• Example of Multilevel Feedback Queue with 3 queues: – Q0 – RR with time quantum 8 ms Q1 – RR time quantum 16 ms Q2 – FCFS• Scheduling – A new job enters queue Q0 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 Q1. – At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2. Loganathan R, CSE, HKBKCE 12
  13. 13. 4. Multiple-Processor Scheduling• Load sharing is possible but CPU scheduling is more complex when multiple CPUs are available4.1 Approaches (for Homogeneous processors in multiprocessor)• Asymmetric multiprocessing –one processor handles all scheduling, I/O processing and system activities, i.e. accesses the system data structures, reducing the need for data sharing• Symmetric multiprocessing(SMP) –each processor is self scheduling with common or private ready queue. If multiple processors trying to access and update a common data structure, the scheduler must be programmed carefully and ensure two processors do not choose the same process and that processes are not lost from the queue.4.2 Processor Affinity• SMP systems try to avoid migration of processes from one processor to another and instead attempt to keep a process running on the same processor due the high cost of invalidating and re-populating caches is known as Processor Affinity• Attempting to keep a process running on the same processor—but not guaranteeing it is known as soft affinity• A process to specify that it is not to migrate to other processors is known as hard affinity Loganathan R, CSE, HKBKCE 13
  14. 14. 4. Multiple-Processor Scheduling Contd…4.3 Load Balancing• Load balancing is to keep the workload evenly distributed across all processors• Load balancing is necessary where each processor has its own private queue• Approaches – Push migration, a specific task periodically checks the load on each processor and— if it finds an imbalance— evenly distributes the load by moving (or pushing) processes from overloaded to idle or less-busy – Pull migration, an idle processor pulls a waiting process from a busy processor• Load balancing often counteracts the benefits of processor affinity4.4 Symmetric Multithreading• To allow several threads to run concurrently, provide multiple logical— rather than physical— processors is known as symmetric multithreading or SMT also termed hyperthreading technology on Intel processors• Each logical processor has its own architecture state(own registers)• SMT is provided in hardware Loganathan R, CSE, HKBKCE 14
  15. 15. 5. Thread Scheduling• Scheduling issues involving user-level and kernel-level threads5.1 Contention Scope• Systems implementing the many-to-one and many-to-many models, the thread library schedules user-level threads to run on an available LWP, is known as process-contention scope (PCS), since competition for the CPU takes place among threads belonging to the same process• To decide which kernel thread to schedule onto a physical CPU, the kernel uses system-contention scope (SCS), since competition for the CPU takes place among threads in the system5.2 Pthread Scheduling• PTHREAD_SCOPE_PROCESS schedules threads using PCS scheduling.• PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling• Pthread IPC provides two functions for getting and setting the contention scope policy – pthread_attr_setscope(pthread_attr_t *attr, int scope) – pthread_attr_getscope(pthread_attr_t *attr, int *scope) Loganathan R, CSE, HKBKCE 15