Professional Documents
Culture Documents
6
“Process
Synchronization”
Antonius Rachmat C, S.Kom, M.Cs
Paralel Processing
• Paralel processing is a situation in which
two/more processor operate in unison.
– Executing instruction simultaneously
• Benefits: increase reliability & faster
processing
• Evolution:
– Job level: each job has its own processor and
all processes and threads are run by the same
processor
– Process level: unrelated process, are
assigned to any available processor
– Thread level: threads are assigned to
avaliable processor
Mengapa Sinkronisasi
• Sinkronisasi diperlukan untuk menghindari
terjadinya ketidak konsistenan data
akibat adanya akses data secara
konkuren
• Diperlukan adanya suatu mekanisme
untuk memastikan urutan / giliran
pengaksesan suatu data yang saling
bekerjasama sehingga terjadi
sinkronisasi
• If we don’t make process synchronization:
– Race Condition
Producer and Consumer
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Producer
while (true) {
counter++;
counter--;
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
• Processor synchronous
– Dengan men-disable interupsi (interrupt)
– Dalam lingkungan multiprocessor:
• Hanya satu processor bisa didisable interruptnya
• Memory synchronous
– Instruksi Test-And-Set dan Semaphore
– Dalam lingkungan multiprocessor:
• Bisa dilakukan
• Semua processor tidak dapat memakai resource karena proteksi
dilakukan di memory
– Instruksi harus bersifat atomik
TestAndSet
Test AndSet dengan Java
X X X
writer reader writer reader
reader
writer reader
reader
reader
writer
reader reader
Reader & Writers
• Diketahui dua macam proses:
– Pembaca (reader)
– Penulis (writer)
• Kedua jenis proses berbagi sumber daya
penyimpanan yang sama, Misal: Basis data
• Tujuan: data tidak korup dan inkonsisten
• Kondisi:
– Proses-proses pembaca dapat membaca sumber daya
secara simultan
– Hanya boleh ada satu penulis menulis pada setiap saat
– Bila ada yang menulis, tidak boleh ada yang membaca
Shared Data
• Data set
• Semaphore mutex initialized to 1,
tanda mutual exclusion
• Semaphore wrt initialized to 1, tanda
untuk menulis
• Integer readcount initialized to 0,
tanda untuk membaca
Readers--Writers
Readers
• The structure of a writer process
while (true) {
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
Readers--Writers
Readers
• The structure of a reader process
while (true) {
wait (mutex) ;
readcount ++ ;
if (readcount == 1) wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0) signal (wrt) ;
signal (mutex) ;
}
Demo Reader & Writer
Dining Philosopher
• Diketahui:
– Mie (Data)
– Sebuah meja bundar
– N filsuf duduk melingkar di meja
bundar
– Antara dua filsuf terdapat
sebuah sumpit
– Didepan setiap filsuf terdapat
semangkuk mie
• Setiap filsuf hanya dapat
berada pada salah satu
kondisi berikut:
– Berpikir
– Lapar
– Makan
Dining Philosopher
• Shared data
–Bowl of rice (data set)
– Semaphore chopstick [5] initialized to 1
• Dua hal yang harus diperhatikan:
– Deadlock: Semua filsuf ingin makan dan
telah memegang sumpit
– Starvation: Ada filsuf yang kelaparan
dalam waktu yang lama
The Structure of Philosopher i
Philosopher I
While (true) {
wait ( chopstick[i] ); //kanan
wait ( chopStick[ (i + 1) % 5] ); //kiri
// eat
} A deadlock occurs!
Demo Dining Philosopher
Kelemahan Semaphore
• Termasuk Low Level
• Kesulitan dalam pemeliharaannya, karena
tersebar dalam seluruh program.
• Menghapus wait => dapat terjadi non-
mutual exclusion.
• Menghapus signal => dapat terjadi
deadlock
• Error yang terjadi sulit untuk dideteksi
System Model
• Assures that operations happen as a single
logical unit of work, in its entirety, or not
at all
• Challenge is assuring atomicity despite
computer system failures
• Transaction - collection of instructions or
operations that performs single logical
function
– Here we are concerned with changes to stable
storage – disk
– Transaction is series of read and write operations
– Terminated by commit (transaction successful) or
abort (transaction failed) operation
– Aborted transaction must be rolled back to undo
any changes it performed
Types of Storage Media
• Volatile storage – information stored
here does not survive system crashes
– Example: main memory, cache
• Nonvolatile storage – information
usually survives crashes
– Example: disk and tape
• Stable storage – information never lost
– Not actually possible, so approximated via
replication or RAID to devices with
independent failure modes
Log--Based Recovery
Log
• Record to stable storage information
about all modifications by a transaction
• Most common is write-ahead logging
– Log on stable storage, each log record
describes single transaction write operation,
including
• Transaction name
• Data item name
• Old value
• New value
– <Ti starts> written to log when transaction Ti
starts
– <Ti commits> written when Ti commits
Log--Based Recovery
Log
Algorithm
• Using the log, system can handle any volatile
memory errors
– Undo(Ti) restores value of all data updated by Ti
– Redo(Ti) sets values of all data in transaction Ti to new
values
• Undo(Ti) and redo(Ti) must be idempotent
– Multiple executions must have the same result as one
execution
• If system fails, restore state of all updated data
via log
– If log contains <Ti starts> without <Ti commits>,
undo(Ti)
– If log contains <Ti starts> and <Ti commits>, redo(Ti)
Checkpoints
• Log could become long, and recovery
could take long
• Checkpoints shorten log and recovery
time.
• Checkpoint scheme:
1. Output all log records currently in volatile
storage to stable storage
2. Output all modified data from volatile to
stable storage
3. Output a log record <checkpoint> to the
log on stable storage
NEXT
• Deadlock