ILUSTRASI KLASIK: BOUNDED BUFFER Oleh kelompok 54.4: Reza Lesmana (1203000978) Suryamita Harindrari (1203001087) Fitria Rahma Sari (1203007034) Kritik dapat dikirimkan melalui rezalesmana@mhs.cs.ui.ac.id 54.4 Ilustrasi Klasik: Bounded Buffer 2004 1
Komunikasi proses dalam sistem Pertukaran pesan memerlukan antrian sementara (buffer), ada tiga: Kapasitas nol Kapasitas terbatas Kapasitas tidak terbatas 54.4 Ilustrasi Klasik: Bounded Buffer 2004 2
Bounded Buffer? Suatu struktur data yang mampu untuk menyimpan beberapa nilai dan mengeluarkannya kembali ketika diperlukan 54.4 Ilustrasi Klasik: Bounded Buffer 2004 3
Implementasi Producer Consumer Problem Dua proses berbagi buffer yang sama dengan ukuran tertentu. Producer menaruh informasi ke dalam buffer, dan consumer mengambilnya. 54.4 Ilustrasi Klasik: Bounded Buffer 2004 4
Hal yang harus diperhatikan Bounded buffer memiliki batas banyaknya data yang dimasukkan Barang yang dikonsumsi oleh konsumer terbatas Jika bounded buffer telah penuh produser tidak mampu menaruh lagi dan akan menunggu sampai ada tempat yang kosong Jika bounded buffer kosong maka konsumer harus menunggu sampai ada barang yang ditaruh oleh produser 54.4 Ilustrasi Klasik: Bounded Buffer 2004 5
4 Masalah dalam Bounded Buffer Busy wait Race Condition Deadlock Livelock 54.4 Ilustrasi Klasik: Bounded Buffer 2004 6
UnsynchronizedBoundedBuffer public class UnsynchronizedBoundedBuffer { protected final Object[] goods; protected int putptr = 0; protected int takeptr = 0; protected int usedslots = 0; public UnsynchronizedBoundedBuffer( int capacity ) throws IllegalArgumentException { if( capacity <= 0 ) throw new IllegalArgumentException(); goods = new Object[capacity]; public void put( Object x) throws InterruptedException { while( usedslots == goods.length ) Thread.yield(); usedslots++; goods[putptr] = x; putptr = ( putptr + 1) % goods.length; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 7
Cont. public Object take() throws InterruptedException { while( usedslots == 0) Thread.yield(); Object x = goods[takeptr]; goods[takeptr] = null; takeptr = ( takeptr + 1) % goods.length; usedslots--; return x; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 8
yield() Thread tetap dalam runnable state tetapi mengizinkan JVM untuk mengambil runnable thread lainnya untuk dijalankan. 54.4 Ilustrasi Klasik: Bounded Buffer 2004 9
SynchronizedBoundedBuffer public class SynchronizedBoundedBuffer{ protected final Object[] goods; protected int putptr = 0; protected int takeptr = 0; protected int usedslots = 0; public SynchronizedBoundedBuffer( int capacity ) throws IllegalArgumentException { if( capacity <= 0 ) throw new IllegalArgumentException(); goods = new Object[capacity]; public synchronized void put( Object x) throws InterruptedException { while( usedslots == goods.length ) wait(); goods[putptr] = x; putptr = ( putptr + 1) % goods.length; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 10
Cont. if( usedslots++ == 0 ) notifyall(); public synchronized Object take() throws InterruptedException { while( usedslots == 0) wait(); Object x = goods[takeptr]; goods[takeptr] = null; takeptr = ( takeptr + 1) % goods.length; if( usedslots-- == goods.length ) notifyall(); return x; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 11
Wait() 1. Thread melepas lock terhadap objek. 2. Status thread diubah menjadi blocked. 3. Thread diletakkan dalam wait set untuk objek tersebut. 54.4 Ilustrasi Klasik: Bounded Buffer 2004 12
notify() & notifyall() 1. Mengambil thread dari daftar tunggu dari objek yang telah dilepas lock-nya 2. Memindahkan T dari wait set ke entry set 3. Mengubah status T dari blocked ke runnable 54.4 Ilustrasi Klasik: Bounded Buffer 2004 13
BufferArray public class BufferArray { protected final Object[] array; protected final putptr = 0; protected final takeptr = 0; public BufferArray( int n ){ array = new Object[n]; synchronized void insert( Object x ){ array[putptr] = x; putptr = ( putptr + 1 )%array.length; synchronized Object extract(){ Object x = array[ takeptr ]; array[ takeptr ] = null; takeptr = ( takeptr + 1)%array.length; return x; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 14
Semaphore : Counting Version Counting Semaphore bisa digunakan untuk mengontrol akses ke resource dalam jumlah terbatas yang diberikan. 54.4 Ilustrasi Klasik: Bounded Buffer 2004 15
Ide dasar Bounded Buffer dengan Semaphore 1. Untuk buffer berukuran n,ada sebanyak n put permits dan 0 take permits. 2. Operasi take harus acquire sebuah takepermits dan kemudian me-release sebuah put-permits. 3. Operasi put harus acquire sebuah putpermits dan kemudian me-release sebuah take-permits. 54.4 Ilustrasi Klasik: Bounded Buffer 2004 16
BoundedBufferWithSemaphore public class BoundedBufferWithSemaphore { protected final BufferArray buffer; protected final Semaphore putpermits; protected final Semaphore takepermits; public BoundedBufferWithSemaphore(int capacity ){ if( capacity <= 0 ) throw new IllegalArgumentException(); buffer = new BufferArray ( capacity ); putpermits = new Semaphore ( capacity ); takepermits = new Semaphore (0); public void put (Object x) throws InterruptedException{ putpermits.acquire(); buffer.insert( x ); takepermits.release(); 54.4 Ilustrasi Klasik: Bounded Buffer 2004 17
Cont. public Object take() throws InterruptedException{ takepermits.acquire(); Object x = buffer.extract(); putpermits.release(); return x; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 18
Semaphore public class Semaphore implements Sync{ protected long permits; public Semaphore( long initialpermits ){ permits = initialpermits; public synchronized void release(){ ++permits; notify(); public void acquire() throws InterruptedException{ if( Thread.interrupted() ) throw new InterruptedException(); synchronized( this ){ try{ while( permits <= 0 ){ wait(); 54.4 Ilustrasi Klasik: Bounded Buffer 2004 19
Cont. --permits; catch( InterruptedException ie ){ notify(); throw ie; // Sync interface public interface Sync{ void acquire() throws InterruptedException; void release(); 54.4 Ilustrasi Klasik: Bounded Buffer 2004 20
Livelock Keadaan dimana suatu proses terus berusaha melakukan sesuatu namun selalu gagal, dikarenakan kondisi yang diperlukan agar proses tersebut bisa terus berjalan tidak terpenuhi. 54.4 Ilustrasi Klasik: Bounded Buffer 2004 21
Copyright Silahkan menampilkan, menyebarkan, atau mendistribusikan slide ini secara bebas, selama tidak mengubah isinya, dan tidak diperbolehkan digunakan untuk tujuan komersial. (educational only ). Kelompok 54.4 Semester gasal 2004; 54.4 Ilustrasi Klasik: Bounded Buffer 2004 22