Monitor SISTIM OPERASI (IKI-20230) Process Synchronization – Ch. 6 Johny Moningka (
[email protected]) Fakultas Ilmu Komputer Universitas Indonesia Semester 2000/2001
Process Synchronization n n n n n n n
Background The Critical-Section Problem Synchronization Hardware Semaphores Monitors Synchronization in Solaris 2 Atomic Transactions
OS Processes JM -2000/v1.1/2
1
P-C: Semaphore
OS Processes JM -2000/v1.1/3
High Level Solution n
Motif: n
n
n
n
Operasi wait(S) dan signal(S) tersebar pada code program => manipulasi langsung struktur data semaphore Bagaimana jika terdapat bantuan dari lingkungan HLL (programming) untuk sinkronisasi ? Pemrograman tingkat tinggi disediakan sintaks-sintaks khusus untuk menjamin sinkronisasi antar proses, thread.
Misalnya: n n
Monitor & Condition Conditional Critical Region OS Processes JM -2000/v1.1/4
2
Monitor n
Monitor mensinkronisasi sejumlah proses: n
n
Bagian dari bahasa program (mis. Java). n
n
suatu saat hanya satu yang aktif dalam monitor dan yang lain menunggu Tugas compiler menjamin hal tersebut terjadi dengan menerjemahkan ke “low level synchronization” (semphore, instruction set dll)
Cukup dengan statement (deklarasi) suatu section/fungsi adalah monitor => mengharuskan hanya ada satu proses yang berada dalam monitor (section) tsb OS Processes JM -2000/v1.1/5
Monitor (example) n
Contoh: Monitor
type monitor-name = monitor variable declarations procedure entry P1 :(…); begin … end; procedure entry P2(…); begin … end; M procedure entry Pn (…); begin…end; begin OS Processes JM -2000/v1.1/6
3
Related: semaphore n
High-level: monitor (implementasi compiler) n
n
n
n
Menambahkan Semaphore mutex = 1; pada (class, atau section procedure monitor), Menambahkan call mutex.down() pada setiap procedure (method), dan Menambahkan mutex.up() jika kembali (return) dari procedure (method). Proses lain yang akan mengakses monitor akan menunggu di luar monitor (mutex.down()). Dan akan masuk ke monitor saat proses lain exit dari monitor (mutex.up()). OS Processes JM -2000/v1.1/7
Schematic monitor
OS Processes JM -2000/v1.1/8
4
Condition variable n Proses-proses
harus disinkronisasikan di dalam
monitor: n n
n
Memenuhi solusi critical section. Proses dapat menunggu di dalam monitor. Mekanisme: terdapat variabel (condition) dimana proses dapat menguji/menunggu sebelum mengakses “critical section”
var x, y: condition OS Processes JM -2000/v1.1/9
Condition n
Condition: memudahkan programmer untuk menulis code pada monitor. Misalkan : var x: condition ;
n
Variabel condition hanya dapat dimanipulasi dengan operasi: wait() dan signal() n
n
x.wait() jika dipanggil oleh suatu proses maka proses tsb. akan suspend sampai ada proses lain yang memanggil: x. signal() x.signal() hanya akan menjalankan (resume) 1 proses saja yang sedang menunggu (suspend) (tidak ada proses lain yang wait maka tidak berdampak apapun) OS Processes JM -2000/v1.1/10
5
Monitor: shared var.
OS Processes JM -2000/v1.1/11
Example: monitor BoundedBuffer { private Buffer b = new Buffer(10); private int count = 0; private Condition nonfull, nonempty; public void insert(Object item) { if (count == 10) nonfull.wait(); b.addElement(item); nonempty.signal(); } public Object remove() { if (count == 0) nonempty.wait(); item result = b.removeElement(); nonfull.signal(); return result; } } OS Processes JM -2000/v1.1/12
6
Wait() n
wait() : untuk suatu condition variabel memberikan efek: n
n
n
Monitor mutex: mutex.up => memberikan kesempatan proses lain masuk ke monitor. Blok proses yang memanggil wait() => pada condition variabel tersebut.
Jadi jika c adalah condition variable: c.wait() sama dengan: n
n
mutex.up(); c.down() [c.down() menyebabkan proses wait] Operasi ini dijalankan sebagai single atomic action. OS Processes JM -2000/v1.1/13
Java Implementation n
n
Tidak menerapkan “monitor” pada seluruh “class”. => tapi pada methods. Hanya menambahkan modifier (methods): synchronized => pada setiap method yang perlu disinkronisasikan. n
n
Setiap object mempunyai potensi menjadi monitor.
Tidak terdpat condition variables secara eksplisit: n n
Setiap monitor (methods) mempunyai sebuah anonymous condition variable. Sehingga tidak dikenal: c.wait() atau c.signal(), tapi menggunakan wait() atau notify(). OS Processes JM -2000/v1.1/14
7
Condition synchronization in Java Java menyediakan wait queue untuk thread per monitor (per object) dengan methods: public final void notify() Wakes up a single thread that is waiting on this object's queue. (sama seperti signal) public final void notifyAll() Wakes up all threads that are waiting on this object's queue. public final void wait() throws InterruptedException Waits to be notified by another thread. The waiting thread releases the synchronization lock associated with the monitor. When notified, the thread must wait to reacquire the monitor before resuming execution. OS Processes JM -2000/v1.1/15
Condition synchronization Thread masuk (entering) sebuah monitor saat mendapatkan (acquire) mutual exclusion lock yang dikaitkan dengan monitor, dan keluar (exiting) monitor saat melepaskan (release) lock. Wait() - causes the thread to exit the monitor, permitting other threads to enter the monitor. Monitor Thread A
Thread B
data wait() notify()
OS Processes JM -2000/v1.1/16
8
Wait: FSP:
when cond act -> NEWSTAT
Java:
public synchronized void act() throws InterruptedException { while (!cond) wait(); // modify monitor data notifyAll() }
The while loop is necessary to retest the condition cond to ensure that cond is indeed satisfied when it re-enters the monitor. notifyall() is necessary to awaken other thread(s) that may be waiting to enter the monitor now that the monitor data has been changed. OS Processes JM -2000/v1.1/17
P-C: monitor public interface Buffer {…} class BufferImpl implements Buffer { …
public synchronized void put(Object o) throws InterruptedException { while (count==size) wait(); buf[in] = o; ++count; in=(in+1)%size; notify(); } public synchronized Object get() throws InterruptedException { while (count==0) wait(); Object o =buf[out]; buf[out]=null; --count; out=(out+1)%size; notify(); return (o); } } OS Processes JM -2000/v1.1/18
9
Producer process class Producer implements Runnable { Buffer buf; String alphabet= "abcdefghijklmnopqrstuvwxyz"; Producer(Buffer b) {buf = b;} Sama untuk public void run() { try { “Consumer” tapi: int ai = 0; buf.get(). while(true) { ThreadPanel.rotate(12); buf.put(new Character(alphabet.charAt(ai))); ai=(ai+1) % alphabet.length(); ThreadPanel.rotate(348); } } catch (InterruptedException e){} } } OS Processes JM -2000/v1.1/19
10