Full Transcript

Chapter 5: CPU Scheduling Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Outline  Basic Concepts  Scheduling Criteria  Scheduling Algorithms  Thread Sch...

Chapter 5: CPU Scheduling Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Outline  Basic Concepts  Scheduling Criteria  Scheduling Algorithms  Thread Scheduling  Multi-Processor Scheduling  Real-Time CPU Scheduling  Operating Systems Examples  Algorithm Evaluation Operating System Concepts – 10th Edition 5.2 Silberschatz, Galvin and Gagne ©2018 Objectives  Describe various CPU scheduling algorithms  Assess CPU scheduling algorithms based on scheduling criteria  Explain the issues related to multiprocessor and multicore scheduling  Describe various real-time scheduling algorithms  Describe the scheduling algorithms used in the Windows, Linux, and Solaris operating systems  Apply modeling and simulations to evaluate CPU scheduling algorithms Operating System Concepts – 10th Edition 5.3 Silberschatz, Galvin and Gagne ©2018 Basic Concepts  Maximum CPU utilization obtained with multiprogramming  CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait  CPU burst followed by I/O burst  CPU burst distribution is of main concern Operating System Concepts – 10th Edition 5.4 Silberschatz, Galvin and Gagne ©2018 Histogram of CPU-burst Times Large number of short bursts Small number of longer bursts Operating System Concepts – 10th Edition 5.5 Silberschatz, Galvin and Gagne ©2018 CPU Scheduler  The CPU scheduler selects from among the processes in ready queue, and allocates a CPU core to one of them Queue may be ordered in various ways  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  For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready queue) must be selected for execution.  For situations 2 and 3, however, there is a choice. Operating System Concepts – 10th Edition 5.6 Silberschatz, Galvin and Gagne ©2018 Preemptive and Nonpreemptive Scheduling  When scheduling takes place only under circumstances 1 and 4, the scheduling scheme is nonpreemptive.  Otherwise, it is preemptive.  Under Nonpreemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases it either by terminating or by switching to the waiting state.  Virtually all modern operating systems including Windows, MacOS, Linux, and UNIX use preemptive scheduling algorithms. Operating System Concepts – 10th Edition 5.7 Silberschatz, Galvin and Gagne ©2018 Preemptive Scheduling and Race Conditions  Preemptive scheduling can result in race conditions when data are shared among several processes.  Consider the case of two processes that share data. While one process is updating the data, it is preempted so that the second process can run. The second process then tries to read the data, which are in an inconsistent state.  This issue will be explored in detail in Chapter 6. Operating System Concepts – 10th Edition 5.8 Silberschatz, Galvin and Gagne ©2018 Dispatcher  Dispatcher module gives control of the CPU to the process selected by the CPU 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 Operating System Concepts – 10th Edition 5.9 Silberschatz, Galvin and Gagne ©2018 Scheduling Criteria  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  Response time ‫ –زمنا الستجابة‬amount of time it takes from when a request was submitted until the first response is produced. Operating System Concepts – 10th Edition 5.10 Silberschatz, Galvin and Gagne ©2018 Scheduling Algorithm Optimization Criteria  Max CPU utilization  Max throughput  Min turnaround time  Min waiting time  Min response time Operating System Concepts – 10th Edition 5.11 Silberschatz, Galvin and Gagne ©2018 algorithms Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 The algorithms: (1) FIRST- COME, FIRST-SERVED (FCFS) SCHEDULING Operating System Concepts – 10th Edition 5.13 Silberschatz, Galvin and Gagne ©2018 First- Come, First-Served (FCFS) Scheduling Process Burst Time P1 24 P2 3 P3 3  Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P 1 P 2 P3 0 24 27 30  Waiting time for P1 = 0; P2 = 24; P3 = 27  Average waiting time: (0 + 24 + 27)/3 = 17 Operating System Concepts – 10th Edition 5.14 Silberschatz, Galvin and Gagne ©2018 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order: P2 , P3 , P1  The Gantt chart for the schedule is: P 2 P 3 P1 0 3 6 30  Waiting time for P1 = 6; P2 = 0; P3 = 3  Average waiting time: (6 + 0 + 3)/3 = 3  Much better than previous case  Convoy effect - short process behind long process Consider one CPU-bound and many I/O-bound processes Operating System Concepts – 10th Edition 5.15 Silberschatz, Galvin and Gagne ©2018 (2) Shortest-Job-First (SJF) Scheduling Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Shortest-Job-First (SJF) Scheduling  Associate with each process the length of its next CPU burst Use these lengths to schedule the process with the shortest time  SJF is optimal – gives minimum average waiting time for a given set of processes The difficulty is knowing the length of the next CPU request Could ask the user Operating System Concepts – 10th Edition 5.17 Silberschatz, Galvin and Gagne ©2018 Shortest-Job-First (SJF) Scheduling  Associate with each process the length of its next CPU burst Use these lengths to schedule the process with the shortest time  SJF is optimal – gives minimum average waiting time for a given set of processes  Preemptive version called shortest-remaining-time-first  How do we determine the length of the next CPU burst? Could ask the user Estimate Operating System Concepts – 10th Edition 5.18 Silberschatz, Galvin and Gagne ©2018 Example of SJF ProcessArriva l Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3  SJF scheduling chart P 4 P 1 P3 P2 0 3 9 16 24  Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 Operating System Concepts – 10th Edition 5.19 Silberschatz, Galvin and Gagne ©2018 Determining Length of Next CPU Burst  Can only estimate the length – should be similar to the previous one Then pick process with shortest predicted next CPU burst  Can be done by using the length of previous CPU bursts, using exponential averaging  Commonly, α set to ½ Operating System Concepts – 10th Edition 5.20 Silberschatz, Galvin and Gagne ©2018 Prediction of the Length of the Next CPU Burst Operating System Concepts – 10th Edition 5.21 Silberschatz, Galvin and Gagne ©2018 Examples of Exponential Averaging  =0 n+1 = n Recent history does not count  =1  n+1 =  tn Only the actual last CPU burst counts  If we expand the formula, we get:  n+1 = tn+(1 -  )tn -1 + … +(1 - )j tn -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 Operating System Concepts – 10th Edition 5.22 Silberschatz, Galvin and Gagne ©2018 Example of Shortest-remaining-time- first  Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT Burst Time wait time P1 0 8 9 (10-1) P2 1 4 0 (1-1) P3 2 9 15 (17-2) P4 3 5 2 (5-3)  Preemptive SJF Gantt Chart P 1 P 2 P4 P1 P3 0 1 5 10 17 26  Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 Operating System Concepts – 10th Edition 5.23 Silberschatz, Galvin and Gagne ©2018 Lecture Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Round Robin (RR)  Each process gets a small unit of CPU time (time quantum q), 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.  Timer interrupts every quantum to schedule next process  Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high Operating System Concepts – 10th Edition 5.25 Silberschatz, Galvin and Gagne ©2018 Example of RR with Time Quantum = 4 Process Burst Time waiting time response time turnaround time P1 24 6 (26-20) 0 30 P2 3 4 4 7 P3 3 7 7 10 Average 17/3=5.6 11/3=3.6 47/3=10.6  The Gantt chart is: P 1 P 2 P 3 P 1 P 1 P 1 P 1 P1 0 4 7 10 14 18 22 26 30  Typically, higher average turnaround than SJF, but better response No process waits more than (n-1)q time units  q should be large compared to context switch time q usually 10 milliseconds to 100 milliseconds, Context switch < 10 microseconds Operating System Concepts – 10th Edition 5.26 Silberschatz, Galvin and Gagne ©2018 Time Quantum and Context Switch Time Operating System Concepts – 10th Edition 5.27 Silberschatz, Galvin and Gagne ©2018 Turnaround Time Varies With The Time Quantum 80% of CPU bursts should be shorter than q For q=7 average turnaround =(6+9+10+17)/4=42/4=10.5 Operating System Concepts – 10th Edition 5.28 Silberschatz, Galvin and Gagne ©2018 Turnaround Time Varies With The Time Quantum 1. As we can see in above figure, the average turnaround time of a set of processes does not necessarily improved as the time-quantum size increase. 2. The average turnaround time can be improved if most processes finish their next CPU burst in single time quantum. o Example, if we have 3 processes with 10-time unit, and quantum=1 then average turnaround =29. But if time quantum=10 the average turnaround time drops to 20. 3. If time quantum is too large, RR scheduling degenerates to FCFS policy,. 80% of CPU bursts should be shorter than q Operating System Concepts – 10th Edition 5.29 Silberschatz, Galvin and Gagne ©2018 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) Preemptive Nonpreemptive  SJF is priority scheduling where priority is the inverse of predicted next CPU burst time  Problem Starvation – low priority processes may never execute  Solution Aging – as time progresses increase the priority of the process Operating System Concepts – 10th Edition 5.30 Silberschatz, Galvin and Gagne ©2018 Example of Priority Scheduling ProcessA arri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2  Priority scheduling Gantt Chart  Average waiting time = 8.2 =(6+0+16+18+1)/5=41/5 Operating System Concepts – 10th Edition 5.31 Silberschatz, Galvin and Gagne ©2018 Priority Scheduling w/ Round-Robin ProcessA arri Burst TimeT Priority P1 4 3 P2 5 2 P3 8 2 P4 7 1 P5 3 3  Run the process with the highest priority. Processes with the same priority run round-robin  Gantt Chart with time quantum = 2 Operating System Concepts – 10th Edition 5.32 Silberschatz, Galvin and Gagne ©2018 END LECTURE Operating System Concepts – 10th Edition 5.33 Silberschatz, Galvin and Gagne ©2018 Multilevel Queue  With priority scheduling, have separate queues for each priority.  Schedule the process in the highest-priority queue! Operating System Concepts – 10th Edition 5.34 Silberschatz, Galvin and Gagne ©2018 Multilevel Queue  Prioritization based upon process type Operating System Concepts – 10th Edition 5.35 Silberschatz, Galvin and Gagne ©2018 Multilevel Queue 1.Each queue has absolute priority over lower-priority queues.  No process in the batch queue, for example, could run unless the queues for real-time processes, system processes, and interactive processes were all empty. If an interactive process entered the ready queue while a batch process was running, the batch process would be preempted. 2. Another possibility is to time-slice among the queues. Here, each queue gets a certain portion of the CPU time, which it can then schedule among its various processes.  For instance, in the foreground –background queue example, the foreground queue can be given 80 percent of the CPU time for RR scheduling among its processes, while the background queue receives 20 percent of the CPU to give to its processes on an FCFS basis. Operating System Concepts – 10th Edition 5.36 Silberschatz, Galvin and Gagne ©2018 Multilevel Feedback Queue  A process can move between the various queues.  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  Aging can be implemented using multilevel feedback queue Operating System Concepts – 10th Edition 5.37 Silberschatz, Galvin and Gagne ©2018 Example of Multilevel Feedback Queue  Three queues: Q0 – RR with time quantum 8 milliseconds Q1 – RR time quantum 16 milliseconds Q2 – FCFS  Scheduling A new process enters queue Q0 which is served in RR  When it gains CPU, the process receives 8 milliseconds  If it does not finish in 8 milliseconds, the process is moved to queue Q1 At Q1 job is again served in RR and receives 16 additional milliseconds  If it still does not complete, it is preempted and moved to queue Q2 Operating System Concepts – 10th Edition 5.38 Silberschatz, Galvin and Gagne ©2018 Thread Scheduling  Distinction between user-level and kernel-level threads  When threads supported, threads scheduled, not processes  Many-to-one and many-to-many models, thread library schedules user- level threads to run on LWP Known as process-contention scope (PCS) since scheduling competition is within the process Typically done via priority set by programmer  Kernel thread scheduled onto available CPU is system-contention scope (SCS) – competition among all threads in system Operating System Concepts – 10th Edition 5.39 Silberschatz, Galvin and Gagne ©2018 Pthread Scheduling  API allows specifying either PCS or SCS during thread creation PTHREAD_SCOPE_PROCESS schedules threads using PCS scheduling PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling  Can be limited by OS – Linux and macOS only allow PTHREAD_SCOPE_SYSTEM Operating System Concepts – 10th Edition 5.40 Silberschatz, Galvin and Gagne ©2018 Pthread Scheduling API #include #include #define NUM_THREADS 5 int main(int argc, char *argv[]) { int i, scope; pthread_t tid[NUM THREADS]; pthread_attr_t attr; pthread_attr_init(&attr); if (pthread_attr_getscope(&attr, &scope) != 0) fprintf(stderr, "Unable to get scheduling scope\n"); else { if (scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS"); else if (scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM"); else fprintf(stderr, "Illegal scope value.\n"); } Operating System Concepts – 10th Edition 5.41 Silberschatz, Galvin and Gagne ©2018 Pthread Scheduling API pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } void *runner(void *param) { pthread_exit(0); } Operating System Concepts – 10th Edition 5.42 Silberschatz, Galvin and Gagne ©2018 5.5 Multiple-Processor Scheduling Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 5.5.Multiple-Processor Scheduling  CPU scheduling more complex when multiple CPUs are available  If multiple CPUs are available, load sharing, where multiple threads may run in parallel, becomes possible, however scheduling issues become correspondingly more complex.  Multiprocess may be any one of the following architectures: 1. Multicore CPUs 2. Multithreaded cores 3. Non-uniform memory access (NUMA) systems 4. Heterogeneous multiprocessing ‫ا لمع ا لجة ا لمتع ددة غير‬ ‫ا لمتجانسة‬ Operating System Concepts – 10th Edition 5.44 Silberschatz, Galvin and Gagne ©2018 5.5.1 Approaches to Multiple-Processor Scheduling  1. Asymmetric multiprocessing (AMP) – Only one processor (master) accesses the system data structures (scheduling decisions, i/o processing, etc), reducing the need for data sharing, others are slaves Problems: high load on master – bottle-neck problem 2. Symmetric multiprocessing (SMP) – 1. All threads may be in a common ready queue (a) 2. Each processor may have its own private queue of threads (b) Operating System Concepts – 10th Edition 5.45 Silberschatz, Galvin and Gagne ©2018 5.5.2 Multicore Processors(+)  Recent trend to place multiple processor cores on same physical chip  Each core maintains its architectural state and thus appears to the operating system to be a separate logical CPU.  Faster and consumes less power  They may complicate scheduling issues. Memory stall :when a processor accesses memory, it spends a significant amount of time waiting for the data to become available. Because modern processors operate at much faster speeds than memory. memory stall can also occur because of a cache miss  Figure :the processor can spend up to 50 percent on a memory stall Operating System Concepts – 10th Edition 5.46 Silberschatz, Galvin and Gagne ©2018 Multithreaded Multicore System(+)  Multiple threads per core also growing Takes advantage of memory stall to make progress on another thread while memory retrieve happens  Each core has > 1 hardware threads.  If one thread has a memory stall, switch to another thread!  Figure: illustrates a dual-threaded processing core on which the execution of thread 0 and the execution of thread 1 are interleaved. Operating System Concepts – 10th Edition 5.47 Silberschatz, Galvin and Gagne ©2018 Multithreaded Multicore System Chip- multithreading  Some system in which, each hardware thread maintains its architectural state, such as instruction pointer and register set, and thus appears as a logical CPU that is available to run a software thread.  This is knowing as Chip-multithreading (CMT) assigns each core multiple hardware threads.  On a quad-core system‫ رباعي‬with 2 hardware threads per core, the operating system sees 8 logical processors.  Intel processors use the term hyper- threading (also known as simultaneous multithreading or SMT) to describe assigning multiple hardware threads to a single processing core. ex. i7—support two threads per core, with 8 cores it sees 16 logical CPUs. the Oracle Sparc M7 processor supports 8 threads per core, with 8 cores it sees 64 logical CPUs. Operating System Concepts – 10th Edition 5.48 Silberschatz, Galvin and Gagne ©2018 Multithreaded Multicore System Chip- multithreading  The resources of the physical core (such as caches and pipelines) must be shared among its hardware threads, and therefore a processing core can only execute one hardware thread at a time.  Thus, a multithreaded, multicore processor requires two different levels of scheduling figure explains a dual-threaded processing core.  Two levels of scheduling: 1.The operating system deciding which software thread to run on a logical CPU. 2.How each core decides which hardware thread to run on the physical core. Operating System Concepts – 10th Edition 5.49 Silberschatz, Galvin and Gagne ©2018 Multiple-Processor Scheduling – Load Balancing  If Symmetric multiprocessing (SMP), need to keep all CPUs loaded for efficiency  Load balancing attempts to keep workload evenly distributed by: Push migration – periodic task checks load on each processor, and if found pushes task from overloaded CPU to other CPUs Pull migration – idle processors pulls waiting task from busy processor Operating System Concepts – 10th Edition 5.50 Silberschatz, Galvin and Gagne ©2018 Multiple-Processor Scheduling – Processor Affinity  When a thread has been running on one processor, the cache contents of that processor stores the memory accesses by that thread.  We refer to this as a thread having affinity for a processor (i.e., “processor affinity”)  Load balancing may affect processor affinity as a thread may be moved from one processor to another to balance loads, yet that thread loses the contents of what it had in the cache of the processor it was moved off, types: Soft affinity – the operating system attempts to keep a thread running on the same processor, but no guarantees. Hard affinity – allows a process to specify a set of processors it may run on. Operating System Concepts – 10th Edition 5.51 Silberschatz, Galvin and Gagne ©2018 Multiple-Processor Scheduling vs. Processor Affinity -NUMA and CPU Scheduling(+) If the operating system is NUMA-aware, it will assign memory closes to the CPU the thread is running on. In this type, load balancing often counteracts ‫يقاوم‬the benefits of processor affinity  the benefit of keeping a thread running on the same processor is that the thread can take advantage of its data being in that processor’s cache memory.  Balancing loads by moving a thread from one processor to another removes this benefit. Similarly, migrating a thread between processors may incur‫ تتحمل‬a penalty on NUMA systems, where a thread may be moved to a processor that requires longer memory access times. In other words, there is a natural tension between load balancing and minimizing memory access times.‫حمل وتقليل أوقات‬ ‫ هناك شدعكسي بين موازنة ال‬، ‫بمعنى آخر‬ ‫ذاكرة‬ ‫ى ال‬‫وصول إل‬ ‫ال‬. Operating System Concepts – 10th Edition 5.52 Silberschatz, Galvin and Gagne ©2018 lecture Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 5.6.Real-Time CPU Scheduling  In this section, we explore several issues related to process scheduling in both soft and hard real-time operating systems Can present obvious challenges  Soft real-time systems – Critical real-time tasks have the highest priority, but no guarantee as to when tasks will be scheduled  Hard real-time systems – task must be serviced by its deadline Operating System Concepts – 10th Edition 5.54 Silberschatz, Galvin and Gagne ©2018 5.6.Real-Time CPU Scheduling - ISSUES Operating System Concepts – 10th Edition 5.55 Silberschatz, Galvin and Gagne ©2018 ‫ت قليلزمن‬ 5.6.1 Minimizing Latency ‫ا لوصول‬  Event latency – the amount of time that elapses from when an event occurs to when it is serviced.  Two types of latencies affect performance 1. Interrupt latency –time from arrival of interrupt to start of routine that services interrupt ‫وقت د ا لمقاطعه‬‫ب‬ 2. Dispatch latency - time for schedule to take current process off CPU and switch to another‫زمنا لتبديل‬ Operating System Concepts – 10th Edition 5.56 Silberschatz, Galvin and Gagne ©2018 1.Interrupt Latency Operating System Concepts – 10th Edition 5.57 Silberschatz, Galvin and Gagne ©2018 2.Dispatch Latency  the real-time operating systems minimize this latency as well  Conflict phase of dispatch latency: 1. Preemption of any process running in kernel mode 2. Release by low- priority process of resources needed by high-priority processes  Following the conflict phase, the dispatch phase schedules the high-priority process onto Operating anConcepts System available CPU. – 10 Edition th 5.58 Silberschatz, Galvin and Gagne ©2018 5.6.2.Priority-based Scheduling  The most important feature of a real-time operating system is to respond immediately to a real-time process as soon as that process requires the ‫وقت لفعليهيا الستجابة ا لفورية ل عملية ف ي‬ CPU. ‫ا لميزة ا ألكثر أهمية ل نظام ا لتشغيلف يا ل ا‬ ‫وقت لفعليب مجرد أنت تطلبهذه ا لعملية وحدة ا لمع ا لجة ا لمركزية‬ ‫ا ل ا‬.  For real-time scheduling, scheduler must support preemptive, priority-based scheduling But only guarantees soft real-time  For hard real-time must also provide ability to meet deadlines  Processes have new characteristics: periodic ones require CPU at constant intervals ‫عدد‬ ‫ل لفتره ا لمحدده‬ ‫مراتحاجة ا لعمليه ل لنتفذ خال ا‬ Has processing time t, deadline d, period p 0 ≤ t ≤ d ≤ p;. Rate of periodic task is 1/p Operating System Concepts – 10th Edition 5.59 Silberschatz, Galvin and Gagne ©2018 5.6.3.Rate Monotonic Scheduling  The rate-monotonic scheduling algorithm schedules periodic tasks using a static priority policy with preemption. ‫ذات لمع دلب جدولة ا لمهام ا لدورية‬ ‫ا‬ ‫ت قوم خوارزمية ا لجدولة ا لرتيبة‬ ‫ب استخدام س ياسة أولوية ث ابتة م ع ا الستباقية‬. Idea:If a lower-priority process is running and a higher-priority process becomes available to run, it will preempt the lower-priority process. Upon entering the system, each periodic task is assigned a priority inversely based on its period.‫عند ا لدخولإ لى‬ ‫ ي تم ت عيينأولوية ل كلم همة دورية عكسيا ب ناء علىف ترتها‬، ‫ا لنظام‬. As the shorter the period‫مرحلة زمنية‬, the higher the priority; the longer the period, the lower the priority T1 of p1=20  So,A priority is assigned based on the inverse of its period T2 of p2=35 Shorter periods = higher priority; Longer periods = lower priority P1 is assigned a higher priority than P2. where, p1 = 50 and p2 = 100. Operating System Concepts – 10th Edition 5.60 Silberschatz, Galvin and Gagne ©2018  Rate-monotonic scheduling – GeeksforGeeks  reference Operating System Concepts – 10th Edition 5.61 Silberschatz, Galvin and Gagne ©2018 Rate Monotonic Scheduling  Example : We have two processes, P1 and P2.  The periods for P1 and P2 as, p1 = 50 and p2 = 100.  The processing times are t1 = 20 for P1 and t2 = 35 for P2.  The deadline for each process requires that it complete its CPU burst by the start of its next period.‫ل نفيذ وحدة ا لمع ا لجة ا لمركزية ا لخاصة ب ها ب حلول‬ ‫تطلب لموعد ا لنهائيل كلعملية إكما ت‬ ‫ا‬ ‫ي‬ ‫ب داية ا لفترة ا لتا لية‬.  We must first ask ourselves whether it is possible to schedule these tasks so that each meets its deadlines.  If we measure the CPU utilization of a process Pi as the ratio of its burst to its period—ti∕pi —the CPU utilization of P1 is 20∕50 = 0.40 and that of P2 is 35∕100 = 0.35, for a total CPU utilization of 75 percent. So, it seems we can schedule these tasks in such a way that both meet their deadlines and still leave the CPU with available cycles. Operating System Concepts – 10th Edition 5.62 Silberschatz, Galvin and Gagne ©2018 Rate Monotonic Scheduling  Suppose we assign P2 a higher priority than P1.  The execution of P1 and P2 in this situation is shown in Figure 5.21. As we can see, P2 starts execution first and completes at time 35. At this point, P1 starts; it completes its CPU burst at time 55.  However, the first deadline for P1 was at time 50, so the scheduler has caused P1 to miss its deadline. The periods for P1 and P2 as, p1 = 50 and p2 = 100. The processing times are t1 = 20 for P1 and t2 = 35 for P2. Operating System Concepts – 10th Edition 5.63 Silberschatz, Galvin and Gagne ©2018 Rate Monotonic Scheduling. Explaining example  Now suppose we use rate-monotonic scheduling, in which we assign P1 a higher priority than P2. as in figure;  P1 starts first and completes its CPU burst at time 20, thereby meeting its first deadline.  P2 starts running at this point and runs until time 50. At this time, it is preempted by P1, although it still has 5 milliseconds remaining in its CPU burst.  P1 completes its CPU burst at time 70, at which point the scheduler resumes P2.  P2 completes its CPU burst at time 75, also meeting its first deadline. The system is idle until time 100, when P1 is scheduled again. The periods for P1 and P2 as, p1 = 50 and p2 = 100. The processing times are t1 = 20 for P1 and t2 = 35 for P2. Operating System Concepts – 10th Edition 5.64 Silberschatz, Galvin and Gagne ©2018 Rate Monotonic Scheduling  Rate-monotonic scheduling is considered optimal if a set of processes cannot be scheduled by this algorithm, it cannot be scheduled by any other algorithm that assigns static priorities.  that cannot be scheduled using Let’s next examine a set of processes the rate-monotonic algorithm. Assume that process P1 has a period of p1 = 50 and a CPU burst of t1 = 25. For P2, the corresponding values are p2 = 80 and t2 = 35.  Rate-monotonic scheduling would assign process P1 a higher priority, as it has the shorter period. The total CPU utilization of the two processes is (25∕50) + (35∕80) = 0.94, and still leave the CPU with 6 percent available time.  Figure 5.23 shows the scheduling of processes P1 and P2. Initially, P1 runs until it completes its CPU burst at time 25. Process P2 then begins running and runs until time 50, when it is preempted by P1. At this point, P2 still has 10 milliseconds remaining in its CPU burst. Process P1 runs until time 75; consequently, P2 finishes its burst at time 85, after the deadline for completion of its CPU burst at time 80. Operating System Concepts – 10th Edition 5.65 Silberschatz, Galvin and Gagne ©2018  The worst-case CPU utilization for scheduling N processes is: N(21∕N − 1).  For two processes N(21∕N − 1)= about 83 percent  For the two processes scheduled in Figure 5.23, combined CPU utilization is approximately 94 percent; therefore, rate-monotonic scheduling cannot guarantee that they can be scheduled so that they meet their deadlines. Operating System Concepts – 10th Edition 5.66 Silberschatz, Galvin and Gagne ©2018 Example Rate Monotonic Scheduling – period  T1 3 20 L.C.M=20  T2 2 5 priority T2>T3>T1  T3 2 10 Operating System Concepts – 10th Edition 5.67 Silberschatz, Galvin and Gagne ©2018 5.6.4.Earliest Deadline First Scheduling (EDF)  Earliest deadline first (EDF) is a dynamic priority scheduling algorithm that is used to place processes in a priority queue. Whenever a scheduling event occurs (e.g., thread finishes, grabs a mutex) the queue will search for the processes closest to its deadline, and that process will be the next to be scheduled for execution. Operating System Concepts – 10th Edition 5.68 Silberschatz, Galvin and Gagne ©2018 5.6.4.Earliest Deadline First Scheduling (EDF)XX  Priorities are assigned according to deadlines: The earlier the deadline, the higher the priority The later the deadline, the lower the priority  However, whereas rate-monotonic scheduling allows P1 to preempt P2 at the beginning of its next period at time 50, EDF scheduling allows process P2 to continue running. Recall that P1 has values of p1 = 50 and t1 = 25 and that P2 has values of p2 = 80 and t2 = 35. Operating System Concepts – 10th Edition 5.69 Silberschatz, Galvin and Gagne ©2018 5.6.4.Earliest Deadline First Scheduling (EDF)  P2 now has a higher priority than P1 because its next deadline (at time 80) is earlier than that of P1 (at time 100).  Thus, both P1 and P2 meet their first deadlines. Process P1 again begins running at time 60 and completes its second CPU burst at time 85, also meeting its second deadline at time 100.  P2 begins running at this point, only to be preempted by P1 at the start of its next period at time 100.  P2 is preempted because P1 has an earlier deadline (time 150) than P2 (time 160). At time 125, P1 completes its CPU burst and P2 resumes execution, finishing at time 145 and meeting its deadline as well. The system is idle until time 150, when P1 is scheduled to run once again. Operating System Concepts – 10th Edition 5.70 Silberschatz, Galvin and Gagne ©2018 5.6.5.Proportional Share Scheduling  Proportional Share Scheduling is a type of scheduling that preallocates certain amount of CPU time to each of the processes.  In a proportional share algorithm every job has a weight, and jobs receive a share of the available resources proportional to the weight of every job.  Proportional share schedulers operate by allocating T shares among all applications.  An application can receive N shares of time, thus ensuring that the  application will have N∕T of the total processor time.  As an example, assume that a total of T = 100 shares is to be divided among three processes, A, B, and C. A is assigned 50 shares, B is assigned 15 shares, and C is assigned 20 shares.  This scheme ensures that A will have 50 percent of total processor time, B will have 15 percent, and C will have 20 percent. Operating System Concepts – 10th Edition 5.71 Silberschatz, Galvin and Gagne ©2018  Proportional share schedulers must work in conjunction with an admission- control policy to guarantee that an application receives its allocated shares of time. An admission-control policy will admit a client requesting a particular number of shares only if sufficient shares are available. ‫ يجب أن يعمل مجدولو األسهم التناسبية جنبا إلى جنب مع سياسة مراقبة‬ ‫ لن تسمح‬.‫القبول لضمان حصول الطلب على حصصه المخصصة من الوقت‬ ‫سياسة مراقبة القبول للعميل بطلب عدد معين من األسهم إال في حالة توفر‬.‫أسهم كافية‬  In our current example, we have allocated 50 + 15 + 20 = 85 shares of the total of 100 shares. If a new process D requested 30 shares, the admission controller would deny D entry into the system. 100 ‫ سهما من إجمالي‬85 = 20 + 15 + 50 ‫ خصصنا‬، ‫في مثالنا الحالي‬  ‫ فإن مراقب القبول سيرفض‬،‫ سهما‬30 ‫ وإذا طلبت عملية جديدة دال‬.‫سهم‬.‫دخول دال إلى النظام‬ Operating System Concepts – 10th Edition 5.72 Silberschatz, Galvin and Gagne ©2018 lecture Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Proportional Share Scheduling  T shares are allocated among all processes in the system  An application receives N shares where N < T  This ensures each application will receive N / T of the total processor time Operating System Concepts – 10th Edition 5.74 Silberschatz, Galvin and Gagne ©2018 POSIX Real-Time Scheduling  The POSIX.1b standard  API provides functions for managing real-time threads  Defines two scheduling classes for real-time threads: 1. SCHED_FIFO - threads are scheduled using a FCFS strategy with a FIFO queue. There is no time-slicing for threads of equal priority 2. SCHED_RR - similar to SCHED_FIFO except time-slicing occurs for threads of equal priority  Defines two functions for getting and setting scheduling policy: 1. pthread_attr_getsched_policy(pthread_attr_t *attr, int *policy) 2. pthread_attr_setsched_policy(pthread_attr_t *attr, int policy) Operating System Concepts – 10th Edition 5.75 Silberschatz, Galvin and Gagne ©2018 POSIX Real-Time Scheduling API #include #include #define NUM_THREADS 5 int main(int argc, char *argv[]) { int i, policy; pthread_t_tid[NUM_THREADS]; pthread_attr_t attr; pthread_attr_init(&attr); if (pthread_attr_getschedpolicy(&attr, &policy) != 0) fprintf(stderr, "Unable to get policy.\n"); else { if (policy == SCHED_OTHER) printf("SCHED_OTHER\n"); else if (policy == SCHED_RR) printf("SCHED_RR\n"); else if (policy == SCHED_FIFO) printf("SCHED_FIFO\n"); } Operating System Concepts – 10th Edition 5.76 Silberschatz, Galvin and Gagne ©2018 POSIX Real-Time Scheduling API (Cont.) if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) fprintf(stderr, "Unable to set policy.\n"); for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } void *runner(void *param) { pthread_exit(0); } Operating System Concepts – 10th Edition 5.77 Silberschatz, Galvin and Gagne ©2018 END LUCTURE Operating System Concepts – 10th Edition 5.78 Silberschatz, Galvin and Gagne ©2018

Use Quizgecko on...
Browser
Browser