Monitor SISTIM OPERASI (IKI-20230) ProcessSynchronization Ch. 6 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 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 Motif: 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: Monitor & Condition Conditional Critical Region OS Processes JM-2000/v1.1/4 2
Monitor Monitor mensinkronisasi sejumlah proses: suatu saat hanya satu yang aktif dalam monitor dan yang lain menunggu Bagian dari bahasa program (mis. Java). 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) 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 High-level: monitor (implementasi compiler) 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 Proses-proses harus disinkronisasikan di dalam monitor: 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 Condition: memudahkan programmer untuk menulis code pada monitor. Misalkan : var x: condition ; Variabel condition hanya dapat dimanipulasi dengan operasi: wait() dan signal() 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() wait() : untuk suatu condition variabel memberikan efek: 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: 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 Tidak menerapkan monitor pada seluruh class. => tapi pada methods. Hanya menambahkan modifier (methods): synchronized => pada setiap method yang perlu disinkronisasikan. Setiap object mempunyai potensi menjadi monitor. Tidak terdpat condition variables secara eksplisit: 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
Conditionsynchronizationin 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. Thread A notify() Monitor data wait() Thread B 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; public void run() { Sama untuk 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