CPU Scheduling Materi Basic Concepts Scheduling Criteria Scheduling
CPU Scheduling
Materi • • Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Realtime CPU Scheduling Operating Systems Examples Algorithm Evaluation
Basic Concept
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 (P 1) dieksekusi sampai masuk state waiting (misal karena I/O request) – CPU dalam kondisi free – OS membawa proses lain (P 2) untuk diolah CPU
CPU vs I/O burst • Siklus pemrosesan ada dua : – Siklus eksekusi CPU disebut CPU burst – Siklus I/O wait disebut I/O burst
CPU Scheduler • Tugas scheduler : memilih process di ready queue untuk dieksekusi di CPU • Keputusan untuk scheduling diambil ketika sebuah proses : 1. Pindah state dari running ke waiting 2. Pindah state dari running ke ready. e. x. interrupt 3. Pindah state dari waiting ke ready. e. x. I/O completion 4. 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
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
Scheduling Criteria
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)
Scheduling dikatakan Optimal Jika. . . • • • Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time
Scheduling Algorithm
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
First-Come, First-Served (FCFS) Scheduling • Process yang masuk pertama, dieksekusi pertama Process P 1 P 2 P 3 Burst Time 24 3 3 • Misal ada process dengan urutan masuk: P 1 , P 2 , P 3 Gantt Chart-nya: P 1 0 P 2 24 P 3 27 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 30
Contoh FCFS (2) Bagaimana jika urutan masuk processnya beda? Misal P 2 , P 3 , P 1 • Gantt chart: • • P 2 0 • • • P 3 3 P 1 6 Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 Average waiting time: (6 + 0 + 3)/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 30
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
Non-Preemptive SJF Scheduling • Algoritma 1. Jalankan process dengan burst time terkecil sampai masuk state waiting 2. Pilih dan jalankan process dengan burst time terkecil berikutnya • Contoh Process. Arriva P 1 P 2 P 3 P 4 • SJF scheduling chart P 4 0 l Time Burst Time 0. 0 6 2. 0 8 4. 0 7 5. 0 3 P 1 3 9 P 2 16 24 • Average waiting time = T 1 + T 2 + T 3 + T 4 = (3 + 16 + 9 + 0) / 4 = 7
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 Process. A P 1 P 2 P 3 P 4 • Preemptive SJF Gantt Chart 1 P 4 P 2 P 1 0 arri Waktu Masuk. T 0 1 2 3 5 10 Burst Time 8 4 9 5 P 3 17 26 • Avg. waiting time = T 1+T 2+T 3+T 4 = [(10 -1)+(17 -2)+5 -3)]/4 = 26/4 = 6. 5 msec
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 dimana – – τn+1 = αtn + (1 -α) τn τ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
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
Priority Scheduling (2) Process. A P 1 P 2 P 3 P 4 P 5 • Priority scheduling Gantt Chart 0 1 Priority 3 1 4 5 2 P 1 P 5 P 2 arri Burst Time. T 10 1 2 1 5 6 • Average waiting time = 8. 2 msec P 3 16 P 4 18 19
Round Robin • Setip proses mendapat sebuah alokasi unit waktu CPU (quantum waktu q) biasanya sekitar 10 -100 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
Example of RR with Time Quantum = 4 Process P 1 P 2 P 3 Burst Time 24 3 3 • The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 P 1 14 P 1 18 P 1 22 P 1 26 • Typically, higher average turnaround than SJF, but better response • q should be large compared to context switch time • q usually 10 ms to 100 ms, context switch < 10 usec 30
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
Multilevel Queue Scheduling
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
Contoh 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
Thread Scheduling
Thread Scheduling • Thread dapat dibedakan menjadi : user-level thread dan kernel-level thread • Pada OS yang mendukung threading, yang dischedule adalah kernel-level thread, bukan process. • User-level thread harus dipetakan ke kernellevel thread agar dapat dieksekusi CPU
Thread Scheduling (2) • 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
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
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 threads */ for (i = 0; i < NUM THREADS; i++) pthread create(&tid[i], &attr, runner, NULL);
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 threadn"); pthread exit(0); }
Multiprocessor Scheduling
Multiple-Processor Scheduling • Scheduling pada multiprocessor lebih rumit daripada 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 processor punya queue sendiri
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
Processor Afinity • Processor affinity – process has affinity for processor on which it is currently running – soft affinity – hard affinity – Variations including processor sets
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
Multithreaded Multicore System
Realtime CPU Scheduling • Baca textbook section 6. 6 chapter 6
Operating System Examples
Operating System Examples • Solaris scheduling • Windows XP scheduling • Linux scheduling
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 42
Solaris Dispatch Table
Solaris Scheduling 44
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
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 higherpriority 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
Windows Priority Classes • Win 32 API identifies several priority classes to which a process can belong – REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, ABOVE_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, 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 3 x priority boost
Windows XP Priorities
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 49
Linux Scheduling (Cont. ) • Real-time scheduling according to POSIX. 1 b – 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 50
Priorities and Time-slice length
List of Tasks Indexed According to Priorities 52
Algorithm Evaluation • Baca textbook section 6. 9 chapter 6
Terima Kasih
- Slides: 54