Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, فصل 5 : زمانبندی پردازنده (CPU Scheduling)

Similar presentations


Presentation on theme: "Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, فصل 5 : زمانبندی پردازنده (CPU Scheduling)"— Presentation transcript:

1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, فصل 5 : زمانبندی پردازنده (CPU Scheduling)

2 5.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition فصل 5 : زمانبندی پردازنده (CPU Scheduling) Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation

3 5.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition اهداف آشنایی با زمانبندی پردازنده ها به عنوان پایه اصلی سیستم عامل های چندبرنامگی (multiprogrammed) توصیف الگوریتم های مختلف زمانبندی CPU بحث در مورد انتخاب یک الگوریتم زمانبندی CPU مناسب برای یک سیستم خاص

4 5.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition مفاهیم پایه ای حداکثر بهره وری (utilization) از CPU به کمک چندبرنامگی CPU–I/O Burst Cycle – اجرای یک پردازه شامل یک دور از اجراهای CPU و انتظار برای I/O است در حقیقت ما می خواهیم CPU burst را توزیع و مدیریت کنیم

5 5.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition هیستوگرام از زمان هر اجرا برای CPU Histogram of CPU-burst Times

6 5.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Alternating Sequence of CPU And I/O Bursts

7 5.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition زمانبند (Scheduler) CPU از میان پردازه های موجود در سیستم یکی را انتخاب می کند و CPU را در اختیار آن قرار دهد تصمیمات زمانبند CPU در یکی از حالات زیر اعمال می گردد. وقتی که یک پردازه : 1. از حالت اجرا به حالت انتظار سوئیچ کند (درخواست I/O، دستور wait و...) 2. از حالت اجرا به حالت آماده سوئیچ کند (وقوع وقفه) 3. از حالت انتظار به حالت آماده سوئیچ کند (اتمام I/O) 4. به اتمام برسد به حالتی که زمانبندی در شرایط 1 و 4 اعمال گردد حالت بدون غیر قابل پس گرفتنی (nonpreemptive) می گویند به حالت دیگر پس گرفتنی (preemptive) می گویند

8 5.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition اعزام کننده (Dispatcher) پیمانه اعزام کننده (Dispatcher module) کنترل CPU را به پردازه ای می دهد که توسط زمانبند کوتاه مدت (short-term scheduler) انتخاب شده است. شامل: سوئیچ زمینه (switching context) سوئیچ به مد کاربر (switching to user mode) مقدار دهی Program Counter به مکان مناسب برای ازسرگیری اجرای برنامه تاخیر اعزام (Dispatch latency) – مدت زمانی که یک پردازه متوقف و پردازه بعدی توسط اعزام کننده شروع به اجرا می کند

9 5.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition مقوله زمانبندی (Scheduling Criteria) بهره وری پردازنده ( CPU utilization ) – پردازه را تا حد امکان مشغول نگه داریم بازده ( Throughput ) – تعداد پردازه هایی که در یک واحد زمانی اجرای آنها به اتمام می رسد زمان گردش ( Turnaround time ) – مدت زمان مورد نیاز برای اجرای کامل یک پردازه زمان انتظار ( Waiting time ) – مدت زمانی که یک پردازه در مدت اجرای کامل خود در صف آماده منتظر تخصیص CPU می ماند زمان پاسخ ( Response time ) – مدت زمان که طول می کشد تا از زمان ارسال یک درخواست اولین پاسخ دریافت گردد. (برای سیستم های اشتراک زمانی منظور یک خروجی معین نیست)

10 5.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

11 5.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition First-Come, First-Served (FCFS) Scheduling ProcessBurst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 P1P1 P2P2 P3P3 2427300

12 5.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition FCFS Scheduling (Cont) Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: 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 Convoy effect short process behind long process P1P1 P3P3 P2P2 63300

13 5.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Shortest-Job-First (SJF) Scheduling برای هر پردازه یک عدد به عنوان زمان اجرای ( CPU Burst ) بعدی مورد نیاز در نظر گرفته می شود. از این عدد برای زمانبندی پردازه ها استفاده می شود. ابتدا پردازه های با CPU Burst کمتر اجرا خواهند شد. SJF بهینه است – یعنی برای یک مجموعه از پردازه ها کمترین متوسط زمان انتظار را خواهیم داشت اما یک مشکل بزرگ داریم: زمان مورد نیاز برای CPU در درخواست بعدی را نمی دانیم

14 5.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of SJF ProcessArrival TimeBurst Time P 1 0.06 P 2 2.08 P 3 4.07 P 4 5.03 SJF scheduling chart Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 P4P4 P3P3 P1P1 3 16 0 9 P2P2 24

15 5.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition مشخص نمودن اندازه CPU Burst بعدی می توانیم تنها طول اجرا را تخمین بزنیم می توانیم با دانستن زمان CPU Burst قبلی و به کمک میانگیری نمایی (exponential averaging) تخمینی از زمان بعدی داشت

16 5.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition پیش بینی اندازه CPU Burst بعدی

17 5.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Examples of Exponential Averaging  =0  n+1 =  n فقط همان تخمین اولیه مهم است و زمان آخر واقعی هیچ ارزشی ندارد  =1  n+1 =  t n همان زمان آخر واقعی اجرا برای مرحله بعد در نظر گرفته می شود اگر فرمول را بست دهیم به عبارت زیر می رسیم:  n+1 =  t n +(1 -  )  t n -1 + … +(1 -  ) j  t n -j + … +(1 -  ) n +1  0

18 5.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition زمانبندی اولویتی (Priority Scheduling) یک عدد اولویت برای هر پردازه در نظر گرفته می شود CPU ابتدا در اختیار پردازه ای قرار می گیرد که اولویت بیشتری (عدد اولویت کمتری) دارد پس گرفتنی (Preemptive) غیر قابل پس گرفتنی (nonpreemptive) SJF یک حالتی از زمانبندی اولویتی که در آن عدد اولویت معادل زمان اجرای بعدی مورد نیاز است مشکل روش اولویتی: قحطی (Starvation): CPU هیچگاه در اختیار پردازه های با اولویت کمتر قرار نمی گیرد. راه حل: سالخوردگی (Aging): با گذشت زمان اولویت را افزایش می دهیم.

19 5.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Round Robin (RR) هر پردازه یک واحد کوچک زمانی (time quantum) CPU را در اختیار می گیرد. این زمان معمولا بین بین 10 الی 100 میلی ثانیه است. بعد از اینکه این زمان سپری شد ، CPU از پردازه گرفته شده و پردازه به انتهای صف آماده اضافه می گردد. وقتی ما n پردازه در صف آماده داریم و کوانتوم زمانی را q در نظر بگیریم. در این حالت در یک دوره زمانی کامل از اجرای صف به هر پردازه در 1/n زمان cpu را در اختیار خواهد داشت. و هیچ پردازه ای بیشتر از (n-1)q منتظر نمی ماند. کارآیی (Performance) q (بزرگ )  FIFO q (کوچک )  q > context switch در حالتی که q کوچک باشد حتما باید این مقدار از زمان context switch بزرگتر باشد. اگر نباشد سربارهای بی مورد cpu بالا می رود

20 5.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of RR with Time Quantum = 4 ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: Typically, higher average turnaround than SJF, but better response P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P1 0 47 101418222630

21 5.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Time Quantum and Context Switch Time

22 5.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Turnaround Time Varies With The Time Quantum

23 5.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition صف چندسطحی (Multilevel Queue) صف اماده به دو صف مجزا تبدیل می شود پیش زمینه - foreground (interactive) پس زمینه - background (batch) هر صف الگوریتم زمانبندی خود را دارد foreground – RR background – FCFS باید یک زمانبندی بین صف ها نیز داشته باشیم 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

24 5.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multilevel Queue Scheduling

25 5.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition صف بازخورد چندسطحی (Multilevel Feedback Queue) یک پردازه می تواند بین صف های مختلف جابجا گردد (می توان از ایده سالخوردگی استفاده کرد) زمانبند صف بازخورد چندسطحی با پارامترهای زیر تعریف شده است: تعداد صف ها الگوریتم های زمانبندی ها برای هر صف روش مشخص نمودن زمان ارتقاع پردازه در صف ها روش مشخص نمودن زمان پایین آوردن پردازه در صف ها روش انتخاب صف اولیه برای یک پردازه در زمان ورود آن به حافظه

26 5.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example of Multilevel Feedback Queue 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.

27 5.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multilevel Feedback Queues

28 5.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Thread Scheduling Distinction between user-level and kernel-level threads 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 Kernel thread scheduled onto available CPU is system-contention scope (SCS) – competition among all threads in system

29 5.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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.

30 5.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Pthread Scheduling API #include #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[NUM THREADS]; pthread attr t attr; /* get the default attributes */ pthread attr init(&attr); /* set the scheduling algorithm to PROCESS or SYSTEM */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); /* set the scheduling policy - FIFO, RT, or OTHER */ pthread attr setschedpolicy(&attr, SCHED OTHER); /* create the threads */ for (i = 0; i < NUM THREADS; i++) pthread create(&tid[i],&attr,runner,NULL);

31 5.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Pthread Scheduling API /* now join on each thread */ for (i = 0; i < NUM THREADS; i++) pthread join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { printf("I am a thread\n"); pthread exit(0); }

32 5.32 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multiple-Processor Scheduling CPU scheduling more complex when multiple CPUs are available Homogeneous processors within a multiprocessor Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common ready queue, or each has its own private queue of ready processes Processor affinity – process has affinity for processor on which it is currently running soft affinity hard affinity

33 5.33 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition NUMA and CPU Scheduling

34 5.34 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multicore Processors Recent trend to place multiple processor cores on same physical chip Faster and consume less power Multiple threads per core also growing Takes advantage of memory stall to make progress on another thread while memory retrieve happens

35 5.35 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multithreaded Multicore System

36 5.36 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Operating System Examples Solaris scheduling Windows XP scheduling Linux scheduling

37 5.37 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Solaris Dispatch Table

38 5.38 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Solaris Scheduling

39 5.39 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Windows XP Priorities

40 5.40 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Linux Scheduling Constant order O(1) scheduling time Two priority ranges: time-sharing and real-time Real-time range from 0 to 99 and nice value from 100 to 140 (figure 5.15)

41 5.41 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Priorities and Time-slice length

42 5.42 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition List of Tasks Indexed According to Priorities

43 5.43 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Algorithm Evaluation Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload Queueing models Implementation (simulation)

44 5.44 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Evaluation of CPU schedulers by Simulation

45 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, پایان فصل 5

46 5.46 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 5.08

47 5.47 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition In-5.7

48 5.48 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition In-5.8

49 5.49 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition In-5.9

50 5.50 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Dispatch Latency

51 5.51 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Java Thread Scheduling JVM Uses a Preemptive, Priority-Based Scheduling Algorithm FIFO Queue is Used if There Are Multiple Threads With the Same Priority

52 5.52 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Java Thread Scheduling (cont) JVM Schedules a Thread to Run When: 1. The Currently Running Thread Exits the Runnable State 2. A Higher Priority Thread Enters the Runnable State * Note – the JVM Does Not Specify Whether Threads are Time-Sliced or Not

53 5.53 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Time-Slicing Since the JVM Doesn’t Ensure Time-Slicing, the yield() Method May Be Used: while (true) { // perform CPU-intensive task... Thread.yield(); } This Yields Control to Another Thread of Equal Priority

54 5.54 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Thread Priorities PriorityComment Thread.MIN_PRIORITYMinimum Thread Priority Thread.MAX_PRIORITY Maximum Thread Priority Thread.NORM_PRIORITY Default Thread Priority Priorities May Be Set Using setPriority() method: setPriority(Thread.NORM_PRIORITY + 2);

55 5.55 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Solaris 2 Scheduling


Download ppt "Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, فصل 5 : زمانبندی پردازنده (CPU Scheduling)"

Similar presentations


Ads by Google