Presentation is loading. Please wait.

Presentation is loading. Please wait.

فصل 5: زمان بندی CPU.

Similar presentations


Presentation on theme: "فصل 5: زمان بندی CPU."— Presentation transcript:

1 فصل 5: زمان بندی CPU

2 مفاهیم اساسی زمان بندی چند برنامگی/ چند نخی بودن باعث افزایش بهره وری از CPU می شود. اجرای فرآیند با انفجار CPU ( CPU burst ) شروع می شود . به دنبال آن ممکن است یک انفجار I/O ( I/O burst ) نیز قرار داشته باشد و به همین ترتیب انفجار دیگری از CPU و انفجار دیگری از I/O قرار داشته باشد و به همین ترتیب ادامه می یابد تا سرانجام آخرین انفجار CPU با درخواست سیستم برای خاتمه اجرا ، پایان می پذیرد.

3 Example: CPU cycle and I/O cycles
{ printf(“\nEnter the first integer: ”); scanf(“%d”, &a); I/O cycle printf(“\nEnter the second integer: ”); scanf(“%d”, &b); c = a+b d = (a*b)–c CPU cycle e = a–b f = d/e printf(“\n a+b= %d”, c); printf(“\n (a*b)-c = %d”, d); I/O cycle printf(“\n a-b = %d”, e); printf(“\n d/e = %d”, f); } I/O cycle CPU cycle I/O cycle

4 Alternating Sequence of CPU and I/O Bursts
Initially, the process is always started with CPU burst Eventually, the final CPU burst ends with a system request to terminate execution

5 Histogram of CPU-burst Times
تعداد زیادی از انفجار های CPU کوتاه و تعداد اندکی از انفجار های CPU بلند وجود دارد. برنامه مقید به I/O معمولاً دارای چند انفجار کوتاه CPU است. برنامه مقید به CPU معمولاً چند انفجار بلند CPU است.

6 Three-level Scheduling
LONG-TERM MID-TERM SHORT-TERM

7 زمان بند CPU ( زمان بند کوتاه مدت)
صف آماده ( ready queue ) ممکن است به روش های مختلفی چیدمان شده باشد. تصمیمات زمان بندی CPU ممکن است تحت چهار شرط زیر اتخاذ شود: وقتی که فرآیندی از حالت اجرا به حالت انتظار می رود. وقتی که فرآیندی از حالت اجرا به حالت آماده می رود. وقتی که فرآیندی از حالت انتظار به حالت آماده می رود. وقتی که فرایندی خاتمه می یابد. وقتی زمان بندی فقط تحت شرایط 1و 4 انجام می گیرد ، الگوی زمان بندی را غیرقابل پس گرفتن ( nonpreemptive ) و در غیر اینصورت ،آن را قابل پس گرفتن (preemptive ) می نامیم. در زمان بندی غیرقابل پس گرفتن ( nonpreemptive ) ، وقتی CPU به فرآیندی تخصیص یافت، آنقدر آن را نگه می دارد تا اینکه خاتمه یابد یا به حالت انتظار برود، که در این صورت CPU را آزاد می کند. سیستم عامل Mac OS X برای مکینتاش نیز از زمان بندی قابل پس گرفتن ( preemptive ) استفاده می کند.

8 زمان بندی preemptive وnompreemptive
حالتی را در نظر بگیرید که دو پردازه داده مشترکی دارند . یکی از آنها ممکن است در هنگام به هنگام سازی داده ها پس گرفته شود (cpu گرفته شود) و حال پردازه دوم در حال اجرا باشد. پردازه دوم ممکن است تلاش کند داده هایی را بخواند که در حالت ناسازگار قرار دارد (یعنی هنوز بطور کامل بروز رسانی نشده) . بنابراین نیاز به راهکار جدیدی است تا دسترسی به داده های مشترک را هماهنگ کند.(راهکار در فصل بعد) زمان بندی preemptive در طراحی هسته سیستم عامل موثر است.هنگام پردازش فراخوان سیستم ، هسته ممکن است مشغول انجام فعالیتی در رابطه با یک پردازه باشد. این فعالیت ها ممکن است داده های مهم هسته را تغییر دهند(مثل صف I/O ) . اگر پردازه ای که در حال انجام این تغییرات است ، پردازنده از آن پس گرفته شود و هسته نیاز به خواندن یا تغییر همان ساختار داشته باشد ، چه اتفاقی می افتد؟ وقفه ها در هر زمانی می توانند رخ بدهند ، و چون همیشه نمی توانند توسط هسته نادیده گرفته شوند ، بخش هایی از کد که تحت تاثیر وقفه قرار دارند ، باید از استفاده ی همزمان در امان باشند .

9 توزیع کننده یا اعزام کننده
توزیع کننده ( dispatcher ) ، پیمانه ای(ماژولی) است که کنترل را به پردازنده ای می دهد که توسط زمان بند کوتاه مدت انتخاب شده است. این عمل شامل موارد زیر است: تعویض متن تغییر به حالت کاربر پرش به محل مناسبی در برنامه کاربر و آغاز مجدد آن برنامه مدت زمانی که توزیع کننده صرف می کند تا یک وقفه را متوقف و فرآیند دیگری را شروع کند ، تاخیر توزیع ( dispatch latency ) نام دارد.

10 معیار های زمان بندی بهره وری CPU : می خواهیم تا آنجا که ممکن است CPU مشغول باشد. توان عملیاتی( throughput ) : تعداد فرآیند هایی است که در واحد زمان کامل می شوند. زمان برگشت ( turnaround time ): از نقطه نظر یک فرآیند خاص، مهم ترین معیار، مدت زمان اجرای یک فرآیند است.فاصله زمانی از زمان تحویل فرآیند تا زمان کامل شدن اجرای آن زمان برگشت نام دارد. زمان برگشت مجموع زمان انتظار برای قرار گرفتن در حافظه ، زمان انتظار برای قرار گرفتن در صف آماده ، اجرا در CPU و عمل I/O است. زمان انتظار (waiting time ): زمان انتظار برابر مجموع مدت های انتظار در صف آماده است. زمان پاسخ ( response time): مدت زمانی که از درخواست اولیه تا شروع پاسخ آن طول می کشد و نه مدت زمانی که طول می کشد تا آن پاسخ چاپ شود . مدت زمانی را که از درخواست اولیه یک پردازه تا اینکه پاسخ آن در دسترس قرار گیرد. این مفهوم اغلب در پردازه های همکار مشاهده می شود. مطلوب است که توان عملیاتی و بهره وری CPU به حداکثر برسد و زمان برگشت، زمان انتظار و زمان پاسخ کاهش یابد .

11 معیار های بهینه سازی الگوریتم های زمان بندی
ماکزیمم بهره وری CPU ماکزیمم توان عملیاتی می نیمم زمان برگشت می نیمم زمان انتظار می نیمم زمان پاسخ در اغلب موارد، معیار میانگین را بهینه می کنیم همچنین مهم است که واریانس را به حداقل برسانیم برسی ها نشان می دهد برای سیستم های محاوره ای، می نیمم کردن واریانس در زمان پاسخ ( response time ) ، مهم تر از مینیمم کردن میانگین زمان پاسخ است.سیستمی با زمان پاسخ معقول و قابل پیش بینی ، نسبت به سیستمی که در حالت میانگین سریع تر ولی به شدت متغیر باشد مطلوب تر است.

12 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 (all at time 0) The Gantt Chart for the schedule is: Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time= ( )/3 = 17 P1 P2 P3 24 27 30

13 FCFS Scheduling (Cont.)
Suppose 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 Average waiting time: ( )/3 = 3 Much better than previous case P1 P3 P2 6 3 30 The convoy effect is the slow down of traffic due to queuing caused by slow processing having knock on effects slowing the whole system down.

14 مشکلات FCFS غیرقابل پس گرفتن
میانگین زمان انتظار (AWT) را حداقل سازی نمی کند عدم توانایی در بهره وری موازی ( parallel ) ار منابع یک پردازه ی مقید به CPU و چندین پردازه ی مقید به I/O را در نظر بگیرید: نتیجه: اثر اسکورت (convoy effect) ، بهره وری پایین از CPU و دستگاه های ورودی اثر اسکورت (convoy effect): فرآیند های کوتاه پشت فرآیند های بزرگ

15 چرا convoy effect با FCFS؟
فرض کنید n-1 کار مقید به I/O و یک کار مقید به CPU باشد: کارهای مقید به I/O خیلی سریع از صف آماده عبور کرده و حالت معوق منتظر I/O می شوند. کار مقید به CPU در سر صف می رسد و اجرا می شود تا تکمیل شود. کارهای مقید به I/O دوباره به صف ملحق می شوند و منتظر کار مقید به CPU می شوند تا اتمام یابد. دستگاه های I/O تا زمانی که کار مقید به CPU تمام شود بیکار باقی می مانند. وقتی که کار مقید به CPU تکمیل می شود، پردازه های آماده ی مقید به I/O به سرعت به حالت در حال اجرا تبدیل وضعیت می یابند و رخداد (event ) های I/O دوباره به حالت بلوکه شده ( blocked ) در می آید.CPU بیکار می شود.

16 Shortest Job First (SJF)
در ابتدا به زمان بندی کاری که دارای کوتاه ترین زمان محاسباتی باشد می پردازد. اسامی مختلف: Shortest-Job-First Scheduling or shortest-next-CPU-burst algorithm دو نوع: بدون قبضه کردن با قابلیت پس گرفتن ( کوتاه ترین زمان باقی مانده (shortest-remaining-time-first)) الگوریتم زمان بندی SJF احتمالاً بهینه است، زیرا برای این مجموعه از فرآیند ها میانگین زمان انتظار آن می نیمم است.

17 Non-preemptive SJF: Example
Process Duration Order Arrival Time P1 1 4 P2 8 2 P3 7 3 P4 P4 (3) P1 (6) P3 (7) P2 (8) 3 9 16 24 P4 waiting time: 0 P1 waiting time: 3 P3 waiting time: 9 P2 waiting time: 16 The total time is: 24 The average waiting time (AWT): ( )/4 = 7

18 Comparing to FCFS Process Duration Order Arrival Time P1 6 1 P2 8 2 P3
P2 8 2 P3 7 3 P4 4 P1 (6) P2 (8) P3 (7) P4 (3) 14 21 24 6 P1 waiting time: 0 P2 waiting time: 6 P3 waiting time: 14 P4 waiting time: 21 The total time is the same. The average waiting time (AWT): ( )/4 = 10.25 (comparing to AWT(SJF) = 7)

19 Another example of SJF Process Arrival Time Burst Time P1 0 10 P2 2 8
SJF scheduling chart Average waiting time = ( ) / 4 = 5 P1 P3 P4 6 16 9 P2 24

20 Shortest-Job-First (SJF) Scheduling
مشکلات: مشکل عمده الگوریتم SJF این است که طول درخواست بعدی CPU باید مشخص باشد. می تواند از کاربر درخواست کند (Could ask the user) می تواند برای زمان بندی بلند مدت در سیستم های دسته ای (batch) استفاده شود. اگرچه الگوریتم SJF بهینه است، نمی تواند در سطح زمان بندی کوتاه مدت CPU به کار گرفته شود. مناسب برای زمان بندی در سیستم های دسته ای (batch) می باشد .

21 تعیین طول درخواست انفجار بعدی CPU
پیش بینی در SJF تعیین طول درخواست انفجار بعدی CPU Can only estimate the length – should be similar to the previous one Approximate next CPU-burst duration from the durations of the previous bursts The past can be a good predictor of the future No need to remember entire past history Use exponential average: tn duration of the nth CPU burst n+1 predicted duration of the (n+1)st CPU burst n+1 =  tn + (1- ) n where 0    1  determines the weight placed on past behavior Commonly, α set to ½ (recent history and past history are equally weighted)

22 Prediction of the Length of the Next CPU Burst

23 Examples of Exponential Averaging
 =0 n+1 = n Recent history does not count (current state is not stable)  =1 n+1 =  tn Only the actual last CPU burst counts If we expand the formula, we get: n+1 =  tn+(1 - ) tn (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

24 Example of Preemptive SJF or Shortest-remaining-time-first
Now we add the concepts of varying arrival times and preemption to the analysis ProcessA arri Arrival TimeT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec P1 P2 1 17 10 P3 26 5 P4

25 A Problem with Preemptive SJF
Starvation A job may keep getting preempted by shorter ones Example Process A with computation time of 1 hour arrives at time 0 But every 1 minute, a short process with computation time of 2 minutes arrives Result of SJF: A never gets to run

26 Priority Scheduling (Event Driven)
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

27 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 msec P2 P3 P5 1 18 16 P4 19 6 P1

28 Priority Scheduling: example
Pi Burst Time (τ) Priority P0 350 5 P1 125 2 P2 475 3 P3 250 1 P4 75 4 Highest priority corresponds to highest value Average turn around time: ATTRnd = ( )/5 = 795 Average wait time: AWT = ( )/5 = 540

29 Round-robin One of the oldest, simplest, most commonly used scheduling algorithm Select process/thread from ready queue in a round-robin fashion (take turns) Problems: Does not consider priority Context switch overhead Preemption after quantum expiration Each process gets a small unit of CPU time (time quantum q), usually milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.

30 Round Robin (RR) 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 behavior Poor initial waiting time q small  Too many context switches (overheads) Inefficient CPU utilization q must be large with respect to context switch, otherwise overhead is too high

31 Example of RR with Time Quantum = 4
Process Burst Time P1 24 P2 3 P3 3 The Gantt chart is: Typically, higher average turnaround than SJF, but better response Heuristic: 70-80% of jobs block within time-slice Typical time-slice: ms (depends on job priority) P1 P2 P3 4 7 10 14 18 22 26 30

32 Time Quantum and Context Switch Time

33 Turnaround Time Varies With The Time Quantum
80% of CPU bursts should be shorter than q

34 Multilevel Queue Ready queue is partitioned into separate queues, eg:
foreground (interactive) background (batch) Process permanently in a given queue 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 and 20% to background in FCFS

35 Multilevel Queue Scheduling

36 Practical example: BSD UNIX scheduling
MLQ with feedback approach – 32 run queues 0 through 7 for system processes 8 through 31 for processes executing in user space The dispatcher selects a process from the queue with highest priority RR is used within a queue therefore only processes in highest priority queue can execute the time slice is less than 100us

37 Multilevel Feedback Queue
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

38 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 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

39 Multilevel Feedback Queues

40 Example Suppose, there are two queues with time slices of 2 for queue1 (Q1) and time slice 4 for queue 2 (Q2). Each queue uses RR algorithm. Compute the AWT with MLFQ scheduling. Suppositions: Process P3 has an I/O burst of 1 unit after each CPU burst of 2 units. After each I/O burst P3 returns to queue 1. ProcessA arri Arrival TimeT Burst Time P P P Q1 1 2 3 Q2 I/O MLFQ waiting time: P1:30; P2:32; P3:4 AWT: ( )/3 = 22

41 Case study: CPU scheduling in Solaris
Priority-based scheduling Four classes: real time, system, time sharing, interactive (in order of priority) Different priorities and algorithm in different classes Default class: time sharing Policy in the time sharing class: Multilevel feedback queue with variable time slices

42 Windows XP scheduling A priority-based, preemptive scheduling
Highest priority thread will always run Also have multiple classes and priorities within classes Similar idea for user processes – Multilevel feedback queue Lower priority when quantum runs out Increase priority after a wait event Some twists to improve “user perceived” performance: Boost priority and quantum for foreground process (the window that is currently selected). Boost priority more for a wait on keyboard I/O (as compared to disk I/O)

43 Linux scheduling A priority-based, preemptive with global round-robin scheduling Each process have a priority Processes with a larger priority also have a larger time slices Before the time slices is used up, processes are scheduled based on priority. After the time slice of a process is used up, the process must wait until all ready processes to use up their time slice (or be blocked) – a round-robin approach. No starvation problem. For a user process, its priority may + or – 5 depending whether the process is I/O- bound or CPU-bound. Giving I/O bound process higher priority.

44 Thread Scheduling Threads: Distinction between user-level and kernel-level threads When threads supported by OS, it schedules threads, 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

45 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 Mac OS X only allow PTHREAD_SCOPE_SYSTEM

46 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 (diminish) 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 Currently, SMP is most common Processor affinity – process has affinity for processor on which it is currently running soft affinity hard affinity Variations including processor sets

47 NUMA and CPU Scheduling
NUMA: Non-Uniform Memory Access a CPU has faster access to some parts of main memory than to other parts. Note that memory-placement algorithms can also consider affinity

48 Load balancing Load balancing Push migration Pull migration
On SMP systems, it is important to keep the workload balanced among all processors to fully utilize the benefits of having more than one processor. attempts to keep the workload evenly distributed across all processors in an SMP system. On systems with a common run queue, load balancing is often unnecessary Load balancing Push migration a specific task periodically checks the load on each processor It moves (or pushes) processes from overloaded to idle or less-busy processors Pull migration Pull migration occurs when an idle processor pulls a waiting task from a busy processor load balancing often counteracts (negate) the benefits of processor affinity Solution: processes are moved only if the imbalance exceeds a certain threshold

49 Multicore Processors Recent trend to place multiple processor cores on same physical chip Faster and consumes less power Multiple threads per core also growing Takes advantage of memory stall to make progress on another thread while memory retrieve happens memory stall : may occur for various reasons, such as a cache miss (accessing data that is not in cache memory)

50 Multithreaded Multicore System

51 How to multithread a processor
there are two ways to multithread a processor: coarse-grained and fine-grained multithreading. coarse-grained: a thread executes on a processor until a long-latency event such as a memory stall occurs. However, the cost of switching between threads is high Fine-grained (or interleaved): switching between threads at a much finer level of granularity—typically at the boundary of an instruction cycle. However, the architectural design of fine-grained systems includes logic for thread switching. As a result, the cost of switching between threads is small.

52 Virtualization and Scheduling
Virtualization software schedules multiple guests onto CPU(s) Each guest doing its own scheduling Not knowing it doesn’t own the CPUs Can result in poor response time Can effect time-of-day clocks in guests Virtualization can undo good scheduling algorithm efforts of guests

53 End of Chapter 5


Download ppt "فصل 5: زمان بندی CPU."

Similar presentations


Ads by Google