Process Synchronization SISTIM OPERASI (Operating System) IKI-20230 Johny Moningka (moningka@cs.ui.ac.id) Fakultas Ilmu Komputer Universitas Indonesia Semester 2000/2001 Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors OS Processes JM-2000/v1.1/2 1
Synchronization Sinkronisasi: Koordinasi akses ke shared data, misalkan hanya satu proses yang dapat menggunakah shared var. Contoh operasi terhadap var. counter harus dijamin di-eksekusi dalam satu kesatuan (atomik) : counter := counter + 1; counter := counter - 1; Sinkronisasi merupakan issue penting dalam rancangan/implementasi OS (shared resources, data, dan multitasking). OS Processes JM-2000/v1.1/3 The Critical-Section Problem n proses mencoba menggunakan shared data bersamaan Setiap proses mempunyai code yang mengakses/ manipulasi shared data tersebut => Problem: Menjamin jika ada satu proses yang sedang eksekusi pada bagian tidak ada proses lain yang diperbolehkan masuk ke code dari proses tersebut. Structure of process P i entry section exit section reminder section OS Processes JM-2000/v1.1/4 2
Solution (pre-requsite) Ide: Mencakup pemakaian secara exclusive dari shared variable tersebut Menjamin proses lain dapat menggunakan shared variable tersebut Solusi problem harus memenuhi: 1. Mutual Exclusion: Jika proses Pi sedang eksekusi pada bagian (dari proses Pi) maka tidak ada prosesproses lain dapat eksekusi pada bagian dari proses-proses tersebut. 2. Progress: Jika tidak ada proses sedang eksekusi pada critical section-nya dan jika terdapat lebih dari satu proses lain yang ingin masuk ke, maka pemilihan siapa yang berhak masuk ke tidak dapat ditunda tanpa terbatas. OS Processes JM-2000/v1.1/5 Solutions (cont.) 3. Bounded Waiting: Terdapat batasan berapa lama suatu proses harus menunggu giliran untuk mengakses jika seandainya proses lain yang diberikan hak akses ke. Menjamin proses dapat mengakses ke (tidak mengalami starvation: proses se-olah berhenti menunggu request akses ke diperbolehkan). Tidak ada asumsi mengenai kecepatan eksekusi prosesproses n tersebut. OS Processes JM-2000/v1.1/6 3
Simple solution: case 2 process Hanya 2 proses Struktur umum dari program code Pi dan Pj: entry section exit section reminder section Software solution: merancang algoritma program untuk solusi Proses dapat mengunakan common var. untuk menyusun algoritma tsb. OS Processes JM-2000/v1.1/7 Algorithm 1 Shared variables: var turn: (0..1); initially turn = 0 turn = i Pi dapat masuk ke Process P i while turn i do no-operation; turn := j; reminder section Satisfies mutual exclusion, but not progress OS Processes JM-2000/v1.1/8 4
Algorithm 2 Shared variables var flag: array [0..1] of boolean; initially flag [0] = flag [1] = false. flag [i] = true P i ready to enter its Process P i flag[i] := true; while flag[j] do no-op; flag [i] := false; remainder section Satisfies mutual exclusion, but not progress requirement. OS Processes JM-2000/v1.1/9 Algorithm 3 Combined shared variables of algorithms 1 and 2. Process P i flag [i] := true; turn := j; while (flag [j] and turn = j) do no-op; flag [i] := false; remainder section Meets all three requirements; solves the critical-section problem for two processes. OS Processes JM-2000/v1.1/10 5
Bakery Algorithm Critical Section for n processes Sebelum proses akan masuk ke dalam critical section, maka proses harus mendapatkan nomor (tiket). Proses dengan nomor terkecil berhak masuk ke critical section. If processes P i and P j receive the same number, if i < j, then P i is served first; else P j is served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... OS Processes JM-2000/v1.1/11 Bakery Algorithm (Cont.) Notasi < lexicographical order (ticket #, process id #) => pasti unik (a,b) < (c,d) if a < c or if a = c and b < d note: (b dan d pasti tidak sama) max (a 0,, a n-1 ) is a number, k, such that k a i for i - 0,, n 1 Shared data var choosing: array [0..n 1] of boolean; number: array [0..n 1] of integer, Initialized: choosing =: false ; number => 0 OS Processes JM-2000/v1.1/12 6
Bakery Algorithm (Cont.) choosing[i] := true; number[i] := max(number[0], number[1],, number[n 1])+1; choosing[i] := false; for j := 0 to n 1 do begin while choosing[j] do no-op; while number[j] 0 and (number[j],j) < (number[i], i) do no-op; end; number[i] := 0; remainder section OS Processes JM-2000/v1.1/13 Synchronization Hardware Memerlukan dukungan hardware (prosesor) Dalam bentuk instruction set khusus: test-and-set Menjamin operasi atomik (satu kesatuan): test nilai dan ubah nilai tersebut. Test-and-Set dapat dianalogikan dengan kode: function Test-and-Set (var target:boolean): boolean; begin end; Test-and-Set := target; target := true; OS Processes JM-2000/v1.1/14 7
Test-and-Set (mutual exclusion) Mutual exclusion dapat diterapkan: Gunakan shared data, variabel: lock: boolean (initially false) lock: menjaga Process Pi: while Test-and-Set (lock) do no-op;.... lock := false; remainder section OS Processes JM-2000/v1.1/15 Semaphore Synchronization tool that does not require busy waiting. Semaphore S integer variable Dapat dijamin akses ke var. S oleh dua operasi atomik: wait (S): while S 0 do no-op; S := S 1; signal (S): S := S + 1; OS Processes JM-2000/v1.1/16 8
Example: n processes Shared variables var mutex : semaphore initially mutex = 1 Process Pi wait(mutex); signal(mutex); remainder section OS Processes JM-2000/v1.1/17 Semaphore Implementation Define a semaphore as a record type semaphore = record value: integer L: list of process; end; Assume two simple operations: block() : suspends the process that invokes it. wakeup(p) : resumes the execution of a blocked process P. OS Processes JM-2000/v1.1/18 9
Implementation (Cont.) Semaphore operations now defined as wait(s): S.value := S.value 1; if S.value < 0 then begin end; signal(s): S.value := S.value + 1; if S.value 0 then begin end; add this process to S.L; block; remove a process P from S.L; wakeup(p); OS Processes JM-2000/v1.1/19 Semaphore: Synchronization Tool Execute B in Pj only after A executed in Pi Use semaphore flag initialized to 0 Code: Pi Pj M M A wait(flag) signal(flag) B OS Processes JM-2000/v1.1/20 10
Two Types of Semaphores Counting semaphore Integer value can range over an unrestricted domain. Efektif untuk pemakaian membatasi jumlah proses, counter (blok pada nilai tertentu) dll. Semaphore s = 10; Binary semaphore Integer value can range only between 0 and 1; Digunakan untuk mutula exclusion: membatasi hanya ada satu proses pada. Semaphore s = 1; OS Processes JM-2000/v1.1/21 Semaphore: bounded buffer P C Shared data (array of item): buffer Semaphore: Binary Semaphore mutex; // mutual exclusion akses ke buffer Semaphore empty_slot; // slot buffer yang kosong : n Semaphore full_item; // item di buffer; Inisialisasi: mutex = 1; empty_slot = n; full_item = 0; OS Processes JM-2000/v1.1/22 11
Semaphrore: bounded buffer Producer: do.. produce item pada nextp; wait(empty_slot); wait(mutex); () add nextp to buffer; signal(mutex); signal(full_item); while (true); Consumer: do wait(full_item); wait(mutex); () remove nextp buffer; signal(mutex); signal(empty_slot);.. consume item nextp; while (true); OS Processes JM-2000/v1.1/23 Deadlock and Starvation Deadlock two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(s); wait(q); wait(q); wait(s); M M signal(s); signal(q); signal(q) signal(s); Starvation indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. OS Processes JM-2000/v1.1/24 12