Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

CPU Scheduling.

Presentasi serupa


Presentasi berjudul: "CPU Scheduling."— Transcript presentasi:

1 CPU Scheduling

2 Materi Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Realtime CPU Scheduling Operating Systems Examples Algorithm Evaluation

3 Basic Concept

4 Process lain akan dikerjakan bergantian begitu CPU free
Latar Belakang Pada dasarnya, satu CPU hanya bisa mengerjakan satu process sekali waktu Process lain akan dikerjakan bergantian begitu CPU free Tujuan : memaksimalkan penggunaan CPU dengan multiprogramming Bagaimana? Sebuah process (P1) dieksekusi sampai masuk state waiting (misal karena I/O request) CPU dalam kondisi free OS membawa proses lain (P2) untuk diolah CPU analogi : manusia yang mengerjakan beberapa pekerjaan dalam sekali waktu e.x. baca, makan, bikin kopi

5 CPU vs I/O burst Siklus pemrosesan ada dua :
Siklus eksekusi CPU disebut CPU burst Siklus I/O wait disebut I/O burst dalam satu siklus eksekusi process ada dua jenis burst : I/O burst dan CPU burst

6 CPU Scheduler Tugas scheduler : memilih process di ready queue untuk dieksekusi di CPU Keputusan untuk scheduling diambil ketika sebuah proses : Pindah state dari running ke waiting Pindah state dari running ke ready. e.x. interrupt Pindah state dari waiting ke ready. e.x. I/O completion Terminate Scheduling pada 1 dan 4 disebut nonpreemptive scheduling (tidak bisa interrupt) Ekesekusi bisa berpindah tanpa harus menunggu proses yang sedang dieksekusi Terminate atau Waiting. Scheduling selainnya disebut preemptive scheduling kapan scheduling dilakukan? ada dua kondisi : preemptive dan non-preemptive

7 Dispatcher Modul dispatcher memberikan kontrol CPU ke proses yang dipilih oleh short-time scheduler meliputi: switching context switching ke user mode jumping to the proper location in the user program to restart that program Dispatch latency – waktu yang dipakai distapcher untuk menghentikan sebuah process dan menjalankan process lain

8 Scheduling Criteria

9 Scheduling Criteria Ada beberapa kriteria dalam scheduling
CPU utilization – buat CPU sesibuk mungkin. Jangan biarkan CPU idle Throughput – Jumlah proses yang selesai dieksekusi per unit waktu Turnaround time – jumlah waktu untuk mengeksekusi sebuah proses Waiting time – jumlah waktu sebuah proses harus menunggu di ready queue Response time – jumlah waktu yang dibutuhkan dari pertama request eksekusi dikirim sampai respon pertama muncul (bukan output akhir)

10 Scheduling dikatakan Optimal Jika...
Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

11 Scheduling Algorithm

12 Butuh algoritma agar pilihan tadi memberi hasil optimal sesuai krteria
Scheduling Algorithm Pada dasarnya, scheduling adalah kegiatan memilih sebuah process dalam ready queue untuk dieksekusi CPU Butuh algoritma agar pilihan tadi memberi hasil optimal sesuai krteria Beberapa algoritma scheduling First-Come, First-Served (FCFS) Scheduling Shortest-Job-First (SJF) Scheduling Priority Scheduling Round Robin (RR) Multilevel Queue

13 First-Come, First-Served (FCFS) Scheduling
Process yang masuk pertama, dieksekusi pertama Process Burst Time P1 24 P2 3 P3 3 Misal ada process dengan urutan masuk: P1 , P2 , P3 Gantt Chart-nya: Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: ( )/3 = 17 P1 P2 P3 24 27 30

14 Contoh FCFS (2) Bagaimana jika urutan masuk processnya beda? Misal
P2 , P3 , P1 Gantt chart: Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: ( )/3 = 3 Average waiting time-nya lebih baik FCFS tidak selalu memberi hasil optimal Convoy effect - short process behind long process Consider one CPU-bound and many I/O-bound processes P1 P3 P2 6 3 30

15 Shortest-Job-First (SJF) Scheduling
Process dengan CPU burst time lebih kecil akan dieksekusi lebih dulu SJF memberi hasil scheduling yang optimal Berdasarkan kapan keputusan scheduling dilakukan, SJF dibagi menjadi : Non-preemptive SJF scheduling Preemptive SJF scheduling

16 Non-Preemptive SJF Scheduling
Algoritma Jalankan process dengan burst time terkecil sampai masuk state waiting Pilih dan jalankan process dengan burst time terkecil berikutnya Contoh ProcessArriva l Time Burst Time P P P P SJF scheduling chart Average waiting time = T1 + T2 + T3 + T4 = ( ) / 4 = 7 P4 P3 P1 3 16 9 P2 24

17 Preemptive SJF Scheduling
Algoritma Jalankan process dengan burst time terkecil TANPA menunggu state berubah waiting, cek burst time dari process yang baru masuk (Tnew) Jika Tnew < sisa burst time process sekarang, pindah eksekusi ke process baru Contoh ProcessA arri Waktu MasukT Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart Avg. waiting time = T1+T2+T3+T4 = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec P1 P2 1 17 10 P3 26 5 P4

18 Masalah pada SJF Scheduling
Bagaimana cara mengetahui besaran burst pada waktu selanjutnya? Nilai burst selanjutnya tidak bisa dicari nilai pastinya Estimasi nilai burst diperlukan Pakai exponential averaging τn+1 = αtn + (1 -α) τn dimana τn+1 : estimasi burst pada waktu berikutnya (n+1) tn : nilai burst pada waktu sekaang (n) τn : estimasi burst pada waktu sekarang (n) 0≤α≤1 : derajat kepercayaan. Jika α = 1, gunakan informasi terbaru saja. Jika α = 0, gunakan estimasi lama saja

19 Priority Scheduling Sebuah priority number (integer) diberikan ke setiap proses CPU mengeksekusi dulu proses dengan prioritas tertinggi (proses dengan priority number terkecil = process dengan prioritas tertinggi) Preemptive Nonpreemptive SJF adalah bentuk priority scheduling dimana prioritasnya adalah inverse dari estimasi nilai burst berikutnya Problem = Starvation – process dengan prioritas rendah bisa jadi tidak pernah dieksekusi Solution = Aging – seiring berjalannya waktu, tingkatkan prioritas dari sebuah process

20 Priority Scheduling (2)
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

21 Round Robin Setip proses mendapat sebuah alokasi unit waktu CPU (quantum waktu q) biasanya sekitar ms. Pemrosesan dimulai dari process yang pertama masuk Setelah quantum waktu q berlalu, eksekusi berpindah ke process berikutnya sampai waktu quantum q nya berakhir, dan seterusnya. Process sebelumnya ditempatkan di akhir ready queue Jika waktu wuantum = q dengan n process di ready queue, maka setiap process mendapat menunggu paling laa (n-1)q unit waktu. Butuh timer untuk interrupt setiap quantum agar proses berikutnya bisa dijadwalkan Performance q besar = FIFO q kecil = q must be large with respect to context switch, otherwise overhead is too high

22 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 q should be large compared to context switch time q usually 10ms to 100ms, context switch < 10 usec P1 P2 P3 4 7 10 14 18 22 26 30

23 Multilevel Queue Ready queue dipartisi menjadi beberapa queues terpisah, contoh: foreground (interactive) background (batch) Suatu process berada secara permanen dalam suatu queue berdasarkan : Ukuran memori Prioritas process Tipe process Setiap queue memakai algoritma scheduling yang berbeda: foreground – RR background – FCFS Scheduling harus dilakukan antar queue. Beberapa metode : Fixed priority scheduling : Process dalam queue dengan prioritas tertinggi dieksekusi dulu. Contoh : eksekusi dari foreground queue lalu background) Time slice : setiap queue mendapatan sejumlah waktu CPU dimana proses didalamnya dapat di-sechedule; i.e., 80% to foreground in RR, 20% to background in FCFS

24 Multilevel Queue Scheduling

25 Multilevel Feedback Queue
Sebuah proses dapat berpindah antar queue Multilevel-feedback-queue scheduler didefinisikan melalui parameter berikut: jumlah queue algoritma scheduling pada tiap queue metode yang digunakan untuk menentukan kapan harus mengupgrade sebuah process metode yang digunakan untuk menentukan kapan harus menghancurkan sebuah proses metode yang digunakan untuk menentukan queue mana yang akan dimasuki ketika ada process baru akan masuk

26 Contoh 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

27 Thread Scheduling

28 Thread Scheduling Thread dapat dibedakan menjadi : user-level thread dan kernel-level thread Pada OS yang mendukung threading, yang di-schedule adalah kernel-level thread, bukan process. User-level thread harus dipetakan ke kernel-level thread agar dapat dieksekusi CPU

29 Thread Scheduling (2) System Contention Scope (SCS)
Process Contention Scope (PCS) Kompetisi antar beberapa thread dalam sebuah proses Pada model thread Many-to-one and many-to-many User-level thread dijadwalkan oleh thread library untuk dijalankan di LWP Biasanya dilakukan dengan pengaturan prioritas oleh programmer System Contention Scope (SCS) Kompetisi antar beberapa thread dalam sebuah sistem Kernel-level thread dijadwalkan untuk diproses pada CPU yang tersedia

30 API allows specifying either PCS or SCS during thread creation
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

31 Pthread Scheduling API
#include <pthread.h> #include <stdio.h> #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);

32 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);

33 Multiprocessor Scheduling

34 Multiple-Processor Scheduling
Scheduling pada multiprocessor lebih rumit daripada pada single processor Hanya membahas homogeneous processor : processor dengan fungsi yang sama Ada dua pendekatan : Asymmetric Multiprocessing Hanya ada satu processor master yang punya akses ke struktur data sistem Keputusan scheduling diambil oleh satu processor master Processor lain hanya mengeksekusi user code Symmetric Multiprocessing (SMP) Setiap processor menjadwalkan processnya sendiri (self scheduling) Semua process bisa jadi ada di satu queue yang sama atau masing-masing processor punya queue sendiri

35 Load Balancing Dalam SMP, beban kerja tiap processor harus seimbang
Butuh load balancing agar tidak ada processor menganggur sementara processor lain bekerja keras Ada dua pendekatan Push migration Sistem secara periodik mengecek load setiap processor Jika tidak seimbang, pindahkan process ke processor lain Pull migration Processor yang menganggur meminta process ke processor yang sibuk

36 Processor Afinity Processor affinity – process has affinity for processor on which it is currently running soft affinity hard affinity Variations including processor sets

37 Recent trend to place multiple processor cores on same physical chip
Multicore Processor 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

38 Multithreaded Multicore System

39 Realtime CPU Scheduling
Baca textbook section 6.6 chapter 6

40 Operating System Examples

41 Operating System Examples
Solaris scheduling Windows XP scheduling Linux scheduling

42 Solaris Priority-based scheduling Six classes available
Time sharing (default) Interactive Real time System Fair Share Fixed priority Given thread can be in one class at a time Each class has its own scheduling algorithm Time sharing is multi-level feedback queue Loadable table configurable by sysadmin

43 Solaris Dispatch Table

44 Solaris Scheduling

45 Solaris Scheduling (Cont.)
Scheduler converts class-specific priorities into a per-thread global priority Thread with highest priority runs next Runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority thread Multiple threads at same priority selected via RR

46 Windows Scheduling Windows uses priority-based preemptive scheduling
Highest-priority thread runs next Dispatcher is scheduler Thread runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority thread Real-time threads can preempt non-real-time 32-level priority scheme Variable class is 1-15, real-time class is 16-31 Priority 0 is memory-management thread Queue for each priority If no run-able thread, runs idle thread

47 Windows Priority Classes
Win32 API identifies several priority classes to which a process can belong REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS All are variable except REALTIME A thread within a given priority class has a relative priority TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL, LOWEST, IDLE Priority class and relative priority combine to give numeric priority Base priority is NORMAL within the class If quantum expires, priority lowered, but never below base If wait occurs, priority boosted depending on what was waited for Foreground window given 3x priority boost

48 Windows XP Priorities

49 Linux Scheduling Constant order O(1) scheduling time
Preemptive, priority based Two priority ranges: time-sharing and real-time Real-time range from 0 to 99 and nice value from 100 to 140 Map into global priority with numerically lower values indicating higher priority Higher priority gets larger q Task run-able as long as time left in time slice (active) If no time left (expired), not run-able until all other tasks use their slices All run-able tasks tracked in per-CPU runqueue data structure Two priority arrays (active, expired) Tasks indexed by priority When no more active, arrays are exchanged

50 Linux Scheduling (Cont.)
Real-time scheduling according to POSIX.1b Real-time tasks have static priorities All other tasks dynamic based on nice value plus or minus 5 Interactivity of task determines plus or minus More interactive -> more minus Priority recalculated when task expired This exchanging arrays implements adjusted priorities

51 Priorities and Time-slice length

52 List of Tasks Indexed According to Priorities

53 Algorithm Evaluation Baca textbook section 6.9 chapter 6

54 Terima Kasih


Download ppt "CPU Scheduling."

Presentasi serupa


Iklan oleh Google