Top Banner
Sistem Operasi Sistem Operasi 6 “Process “Process SynchronizationSynchronizationSynchronization Synchronization Antonius Rachmat C, S.Kom, M.Cs Antonius Rachmat C, S.Kom, M.Cs
52

Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Aug 25, 2018

Download

Documents

vuongthu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Sistem Operasi Sistem Operasi 66

“Process “Process Synchronization”Synchronization”SynchronizationSynchronization

Antonius Rachmat C, S.Kom, M.CsAntonius Rachmat C, S.Kom, M.Cs

Page 2: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

P l l P iP l l P iParalel ProcessingParalel ProcessingPa alel p ocessing is a sit ation in hich • Paralel processing is a situation in which two/more processor operate in unison. – Executing instruction simultaneouslyExecuting 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 all processes and threads are run by the same processor

– Process level: unrelated process, are d l blassigned to any available processor

– Thread level: threads are assigned to avaliable processoravaliable processor

Page 3: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

M Si k i iM Si k i iMengapa SinkronisasiMengapa SinkronisasiSink oni i dipe l k n nt k menghind i • Sinkronisasi diperlukan untuk menghindari terjadinya ketidak konsistenan dataakibat adanya akses data secara akibat adanya akses data secara konkuren

• Diperlukan adanya suatu mekanisme p yuntuk memastikan urutan / giliranpengaksesan suatu data yang saling bekerjasama sehingga terjadi bekerjasama sehingga terjadi sinkronisasi

• If we don’t make process synchronization:If we don t make process synchronization:– Race Condition

Page 4: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Review: Producer and Review: Producer and CCConsumerConsumer

#define BUFFER_SIZE 10typedef struct {. . .

} item;item buffer[BUFFER_SIZE];int in = 0;int out = 0;int counter = 0;

Page 5: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

P d P d Producer Producer while (true) {

/* produce an item and put in nextProduced */while (count == BUFFER SIZE){( _ ){} // do nothingbuffer [in] = nextProduced;in = (in + 1) % BUFFER SIZE;in = (in + 1) % BUFFER_SIZE;count++;

}

Page 6: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

CCConsumerConsumerwhile (true) {

while (count == 0){while (count == 0){} // do nothingnextConsumed = buffer[out];nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;count ;count--;/* consume the item in nextConsumed

}}

Page 7: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

A i PA i PAtomic ProcessAtomic ProcessTh t t t• The statements

counter++;counter++;counter--;

must be performed atomically.

• Atomic operation means an operation that completes in its entirety without completes in its entirety without interruption.

Page 8: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

BoundedBounded BufferBufferBoundedBounded--BufferBuffer• Perintah “count++” diimplementasikan pada bahasa mesin:

i t 1 t– register1 = counter– register1 = register1 + 1– counter = register1

Perintah “count ” diimplementasikan pada bahasa mesin:• Perintah “count--” diimplementasikan pada bahasa mesin:– register2 = counter– register2 = register2 – 1

counter register2– counter = register2• Jika kedua perintah tersebut berusaha mengakses nilai

counter secara konkuren, maka dapat terjadi kesalahan pada nilai counter karena sifat bahasa mesin yang pada nilai counter karena sifat bahasa mesin yang menggunakan register untuk mengupdate nilai counter

• Kesalahan nilai akhir counter dapat terjadi, tergantungdari penjadwalan yang dilakukan terhadap proses yang p j y g p p y gdilakukan oleh produsen dan konsumen.

• Dengan kata lain, masalah tersebut belum tentu terjadi, tapi dapat terjadi

Page 9: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Mi lMi lMisalnyaMisalnya• Consider this execution interleaving with

“count = 5” initially:

t0: producer execute register1 = count {register1 = 5}t1: producer execute register1 = register1 + 1 {register1 = 6} t1: producer execute register1 = register1 + 1 {register1 = 6} t2: consumer execute register2 = count {register2 = 5} t3: consumer execute register2 = register2 - 1 {register2 = 4} t4: producer execute count = register1 {count = 6 } t5: consumer execute count = register2 {count = 4}

Page 10: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

R C di iR C di iRace ConditionRace Condition• Race condition: situasi dimana beberapa

proses mengakses dan memanipulasi suatu data secara konkuren. – Nilai akhir dari data tersebut tergantung dari

proses mana yang terakhir mengubah data• Untuk menghindari terjadinya situasi

tersebut, semua proses yang dapat mengakses suatu data tertentu harus disinkronisasi

Page 11: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

C i i l S iC i i l S iCritical SectionCritical SectionLebih da i sat p o e be lomb lomb • Lebih dari satu proses berlomba-lomba pada saat yang sama untuk menggunakan data yang sama.data yang sama.

• Setiap proses memiliki segmen kode yang digunakan untuk mengakses data yang g g y gdigunakan secara bersama-sama.– Segmen kode tersebut disebut critical

section section. • Masalahnya: menjamin bahwa jika suatu

proses sedang menjalankan critical p g jsection, maka proses lain tidak boleh masuk ke dalam critical section tersebut.

Page 12: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Solusi masalah critical Solusi masalah critical iisectionsection

Mutual Exclusion• Mutual Exclusion– Tidak ada dua proses yang berada di critical section

pada saat yang bersamaan.T j di k j (P )• Terjadi kemajuan (Progress)– Jika tidak ada proses yang sedang berada di critical

section, maka proses lain yang ingin menjalankan iti l ti d t k k d l iti l ti critical section dapat masuk ke dalam critical section

tersebut.• Ada batas waktu tunggu (Bounded Waiting)

– Tidak ada proses yang menunggu selama-lamanyauntuk masuk ke dalam critical section

– Assume that each process executes at a nonzero dspeed

– Tidak ada asumsi lain mengenai kecepatan relatif setiap proses ataupun jumlah CPU.

Page 13: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

I l i l iI l i l iImplementasi solusiImplementasi solusi• Solusi perangkat lunak

– Dengan menggunakan algoritma-algoritmayang nilai kebenarannya tidak tergantung pada asumsi-asumsi lain, selain bahwa setiap proses berjalan pada kecepatan yang bukan nolberjalan pada kecepatan yang bukan nol

• Solusi perangkat kerasT t d b b i t k i i– Tergantung pada beberapa instruksi mesintertentu, misalnya dengan me-non-aktifkan interuppt atau dengan mengunci (lock) suatu interuppt atau dengan mengunci (lock) suatu variabel tertentu

Page 14: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Implementasi software dan Implementasi software dan iiasumsinyaasumsinya

Mi l h d d it P0 d P1• Misal hanya ada dua proses, yaitu P0 dan P1.• Struktur umum dari proses Pi (proses yang lain: Pj)

• Proses-proses tersebut boleh berbagi beberapa • Proses proses tersebut boleh berbagi beberapa variabel yang sama untuk mensinkronisasikan apa yang akan dilakukan oleh setiap proses t b ttersebut.

Page 15: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Algoritma 1Algoritma 1gg• Variabel yang digunakan bersama:

– int turn; //pada awalnya turn = 0– turn = i; //Pi dapat masuk ke critical section

• Untuk proses Pi

• If P0 ingin akses critical section turn diset ke 0, if P1 juga akses, turn diset ke 1

Mutual exclusion but not progress nor bounded bufferIf P0 selesai menggunakan critical section, turn diset ke 1, tapi P1 tidak ingin masuk ke critical section jadi turn tidak akan diset ke tidak ingin masuk ke critical section, jadi turn tidak akan diset ke 0.

Page 16: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Algoritma 2Algoritma 2Algoritma 2Algoritma 2• Variabel yang digunakan bersama:

b l fl [2] – boolean flag[2]; • pada awalnya flag [0] = flag [1] = false

– flag [i] = true; //Pi dapat k k iti l timasuk ke critical section

If P0 mengakses critical section P0 mengeset flag[0] ke True. Sementara P1 masih menggunakan critical section, P0 akan menunggu If P0 finish P0 akan set flag[0] ke falsemenunggu. If P0 finish, P0 akan set flag[0] ke false.

Mutual exclusion but not progress nor bounded bufferTapi jika P0 & P1 ingin akses ke c itical section seca a konk en Tapi jika P0 & P1 ingin akses ke critical section secara konkuren, keduanya akan set flag[0] & flag[1] ke true, dan semua proses menunggu terus…

Page 17: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

P ’ Al i hP ’ Al i hPeterson’s AlgorithmPeterson’s AlgorithmTh h • The two processes share two variables:– int turn; –Boolean flag[2];g[ ];

• The variable turn indicates whose turn it is to enter the critical section turn it is to enter the critical section.

• The flag array is used to indicate if a process is ready to enter the critical process is ready to enter the critical section. flag[i] = true implies that process P is ready!process Pi is ready!

Page 18: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Algorithm for Process Algorithm for Process PPAlgorithm for Process Algorithm for Process PPiiwhile (true) {

flag[i] = TRUE;turn = j;while ( flag[j] && turn == j);

CRITICAL SECTION

flag[i] = FALSE;

REMAINDER SECTION

}

Mutual exclusion progress and bounded buffer!Mutual exclusion, progress, and bounded buffer!If P0 want to access critical section, P0 will set flag[0] to true and turn to P1.

Page 19: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

B k Al i hB k Al i hBakery AlgorithmBakery AlgorithmC iti l ti f Critical section for: n processes• Sebelum memasuki critical section, setiap proses

menerima sebuah nomor menerima sebuah nomor. • Yang memegang ID terkecil yang dilayani dahulu.• Skema penomoran selalu naik secara berurut, Skema penomoran selalu naik secara berurut,

contoh: 1, 2, 2, 2, 3, 3, 4, 5…• Diperkenalkan pertama kali oleh Leslie

Lamport.• Data yang digunakan bersama

boolean choosing [n];– boolean choosing [n];– int number [n];

• Struktur data diinisialisi awal ke false dan 0.• (a,b) < (c,d) jika a < c atau jika a = c dan b < d

Page 20: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

B k Al i hB k Al i hBakery AlgorithmBakery Algorithm

Page 21: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Si k i iSi k i iSinkronisasiSinkronisasi• Metode dalam sinkronisasi hardware• Metode dalam sinkronisasi hardware

– Processor Synchronous ( Disable Interrupt )– Memory Synchronous ( Instruksi Test-And-Set )

• Interrupt used in Round Robin Algorithm with quantum • Interrupt used in Round Robin Algorithm, with quantum time

• Processor synchronous– Dengan men-disable interupsi (interrupt)Dengan men disable interupsi (interrupt)– Dalam lingkungan multiprocessor:

• Hanya satu processor bisa didisable interruptnya• Memory synchronousMemory synchronous

– Instruksi Test-And-Set, Wait-Signal, dan Semaphore– Dalam lingkungan multiprocessor:

• Bisa dilakukan• Semua processor tidak dapat memakai resource karena proteksi

dilakukan di memory– Instruksi harus bersifat atomik

Page 22: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

TT A dSA dS d Jd JTestTestAndSetAndSet dengan Javadengan Java

Kelemahan: bisa terjadi starvation & busy waiting

Page 23: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

M l E l i SM l E l i SMutual Exclusion: SwapMutual Exclusion: SwapHardwareData lock = new HardwareData(false); // aHardwareData lock = new HardwareData(false); // a shared lockHardwareData key = new HardwareData(true); // my key

1st Process 2nd Process

while (true) {key.set(true); // my key is now true

key keytrue true

2 Process

falsedo {

lock.swap(key);// my key got lock’s content. 1st swap 2nd swap

I got it!

y y g} while (key.get() == true); // this means lock

was true locked!criticalSection( ); // now in critical section codec t ca Sect o ( ); // o c t ca sect o codelock.set(false);nonCriticalSection( ); // out of critical section

Lockfalse true

}

Page 24: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Mutual Exclusion: Memory Mutual Exclusion: Memory hhsynchronoussynchronous

Kelebihan• Kelebihan:– Dapat diaplikasikan ke beberapa prosesor,

dengan sharing memorydengan sharing memory– Simpel dan mudah diverifikasi– Dapat digunakan untuk banyak critical section

• Kekurangan:– Busy-waiting memakan processor time yang

besarbesar– Mungkin terjadi starvation – Deadlock

• Jika low priority process mendapat critical region dan higher priority process butuh juga, higher priority process akan mendapat processor dan low priority

k process akan menunggu

Page 25: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

S hS hSemaphoreSemaphore• Invented by Djikstra (1960)• Invented by Djikstra (1960)• Semaphore digunakan untuk memberi

sinyaly• Non negative integer• Jika proses menunggu sinyal, maka dia

akan ditunda sampai sinyal yg ditunggu akan ditunda sampai sinyal yg ditunggu tersebut terkirim

• Operasi: wait dan signalp g• Wait dan signal operations tidak dapat

diinteruptQueue digunakan untuk menahan proses • Queue digunakan untuk menahan proses proses yang sedang menunggu semaphore

Page 26: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

S hS hSemaphoreSemaphore• Two standard operations modify S: wait() and signal()• Two standard operations modify S: wait() and signal()

– Originally called P() and V()• Can only be accessed via two indivisible (atomic)

operationsoperations– wait (S) {

while S <= 0 // ; // no-op

S--;}jika s < 0 akan menunggu, lalu menjalankan proses

lain– signal (S) {

S++;}memberikan kesempatan bagi para proses untuk p g p p

berkompetisi mendapatkan semafore

Page 27: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

S h W i S h W i S i l kS i l kSemaphore: Wait Semaphore: Wait -- SpinlockSpinlock

Page 28: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Semaphore: Wait Semaphore: Wait –– non non i l ki l kspinlockspinlock

Page 29: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

S h Si lS h Si lSemaphore: SignalSemaphore: Signal

Page 30: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

C h S hC h S hContoh SemaphoreContoh Semaphore

Test(s) = if mutex > 0 then mutex = mutex – 1Inc(s) mutex mutex + 1Inc(s) = mutex = mutex + 1

Page 31: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

I l i S hI l i S hImplementasi SemaphoreImplementasi SemaphoreWi d• Windows– Fungsi yg dipakai adalah CreateSemaphore

Bi di k t k b t i j l h – Biasanya digunakan untuk membatasi jumlah thread yang memakai suatu resource secara bersamaanbersamaan

• Java– Semafor di Java™ bersifat transparan oleh Semafor di Java bersifat transparan oleh

programmer• Java™ menyembunyikan Semafor dibalik konsep

monitormonitor• Reserved Word yang dipakai Java™ adalah

synchronized

Page 32: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Classical Problems of Classical Problems of S h i tiS h i tiSynchronizationSynchronization

• Bounded-Buffer Problem• Readers and Writers Problem• Readers and Writers Problem• Dining-Philosophers Problem

Page 33: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

B d d B ffB d d B ffBounded BufferBounded BufferPenge ti n temp t pen mp ng d t ng • Pengertian: tempat penampung data yang ukurannya terbatas

• Contoh: proses produsen dan • Contoh: proses produsen dan konsumen

• Masalah produsen-konsumenMasalah produsen konsumen– Produsen menaruh data pada buffer. Jika

buffer tersebut sudah terisi penuh, maka produsen tidak melakukan apa apa dan produsen tidak melakukan apa-apa dan menunggu sampai konsumen mengosongkan isi buffer. K bil d d i b ff Jik – Konsumen mengambil data dari buffer. Jika buffer tersebut kosong, maka konsumen tidak melakukan apa-apa dan menunggu sampai p p gg pbuffer tersebut diisi oleh produsen.

Page 34: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Penyelesaian dgn Penyelesaian dgn S hS hSemaphoreSemaphore

S f t• Semafor mutex– Menyediakan mutual exclusion untuk

mengakses buffermengakses buffer– Inisialisasi dengan nilai 1

• Semafor full• Semafor full– Menyatakan jumlah buffer yang sudah terisi– Inisialisasi dengan nilai 0– Inisialisasi dengan nilai 0

• Semafor empty– Menyatakan jumlah buffer yang kosong – Menyatakan jumlah buffer yang kosong – Inisialisasi dengan nilai n (jumlah buffer)

Page 35: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

B d d B ff P dB d d B ff P dBounded Buffer ProducerBounded Buffer Producer• Init => full = 0, empty = n, mutex = 1 , p y ,

Page 36: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

B d d B ff CB d d B ff CBounded Buffer ConsumerBounded Buffer Consumer

Page 37: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Contoh Producer & Contoh Producer & CCConsumerConsumer

Page 38: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

The ReadersThe Readers--Writers Writers P blP blProblemProblem

• Multiple readers or a single writer can p guse DB.

writer readerreaderwriter readerX XX

it

readerwriter

reader

reader

writer

reader reader

Page 39: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

R d & W iR d & W iReader & WritersReader & WritersDik t h i d • Diketahui dua macam proses:– Pembaca (reader)– Penulis (writer)Penulis (writer)

• Kedua jenis proses berbagi sumber daya penyimpanan yang sama, Misal: Basis data

• Tujuan: data tidak korup dan inkonsisten• Kondisi:

P b d b b d – 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

Page 40: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Sh d DSh d DShared DataShared Data• Data set• Semaphore mutex initialized to 1 • Semaphore mutex initialized to 1,

tanda mutual exclusionS h t i iti li d t 1 t d • Semaphore wrt initialized to 1, tanda untuk menulis

• Integer readcount initialized to 0, tanda untuk membacatanda untuk membaca

Page 41: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

ReadersReaders--WritersWritersReadersReaders WritersWriters• The structure of a writer process

while (true) {( )wait (wrt) ;

// iti i f d// writing is performed

signal (wrt) ;signal (wrt) ;}

Page 42: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

R dR d W iW iReadersReaders--WritersWriters• The structure of a reader process• The structure of a reader process

while (true) {wait (mutex) ;readcount ++ ;if (readcount == 1) wait (wrt) ;signal (mutex)

// reading is performed

wait (mutex) ;dreadcount - - ;

if (readcount == 0) signal (wrt) ;signal (mutex) ;

}}

Page 43: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Di i Phil hDi i Phil hDining PhilosopherDining PhilosopherDiketahui:• Diketahui:– Mie (Data)– Sebuah meja bundar– N filsuf duduk melingkar di meja

bundar– Antara dua filsuf terdapat

b h itsebuah sumpit– Didepan setiap filsuf terdapat

semangkuk mieS ti fil f h d t • Setiap filsuf hanya dapat berada pada salah satu kondisi berikut:– Berpikir– Lapar– MakanMakan

Page 44: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Di i Phil hDi i Phil hDining PhilosopherDining Philosopher• Shared data

–Bowl of rice (data set)–Bowl of rice (data set)–Semaphore chopstick [5] initialized to 1

• Dua hal yang harus diperhatikan:–Deadlock: Semua filsuf ingin makan dan Deadlock: Semua filsuf ingin makan dan

telah memegang sumpit–Starvation: Ada filsuf yang kelaparan –Starvation: Ada filsuf yang kelaparan

dalam waktu yang lama

Page 45: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

The Structure of Philosopher iThe Structure of Philosopher i

Philosopher I

While (true) { wait ( chopstick[i] ); //kananwait ( chopStick[ (i + 1) % 5] ); //kiri

// eat

signal ( chopstick[i] ); //kanansignal (chopstick[ (i + 1) % 5] ); //kiri

Picked upWaiting

// think

}

Picked upWaiting

A deadlock occurs!} A deadlock occurs!

Page 46: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

K l h S hK l h S hKelemahan SemaphoreKelemahan Semaphore• Termasuk Low Level • Kesulitan dalam pemeliharaannya, karena p y ,

tersebar dalam seluruh program.• Menghapus wait => dapat terjadi non-Menghapus wait > dapat terjadi non

mutual exclusion.• Menghapus signal => dapat terjadi • Menghapus signal => dapat terjadi

deadlockError yang terjadi sulit untuk dideteksi• Error yang terjadi sulit untuk dideteksi

Page 47: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

S M d lS M d lSystem ModelSystem 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 failuresTransaction collection of instructions or • 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 performedany changes it performed

Page 48: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

T f S M diT f S M diTypes of Storage MediaTypes of Storage Media• Volatile storage – information stored

here does not survive system crasheshere does not survive system crashes– Example: main memory, cache

• Nonvolatile storage – information • Nonvolatile storage – information usually survives crashes

Example: disk and tape– Example: disk and tape• Stable storage – information never lost

– Not actually possible, so approximated via replication or RAID to devices with independent failure modesindependent failure modes

Page 49: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

LL B d RB d RLogLog--Based RecoveryBased RecoveryRe o d to t ble to ge info m tion bo t • Record to stable storage information about all modifications by a transaction

• Most common is write-ahead logging• Most common is write-ahead logging– Log on stable storage, each log record

describes single transaction write operation, i l diincluding• Transaction name• Data item name• Old value• New value

– <T starts> written to log when transaction T<Ti starts> written to log when transaction Tistarts

– <Ti commits> written when Ti commits

Page 50: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

LogLog--Based Recovery Based Recovery Al i hAl i hAlgorithmAlgorithm

U i th l t h dl l til • Using the log, system can handle any volatile memory errors– Undo(Ti) restores value of all data updated by TiUndo(Ti) restores value of all data updated by Ti– Redo(Ti) sets values of all data in transaction Ti to new

valuesUndo(T ) and redo(T ) must be idempotent• 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 logIf log contains <T starts> without <T commits> – If log contains <Ti starts> without <Ti commits>, undo(Ti)

– If log contains <Ti starts> and <Ti commits>, redo(Ti)

Page 51: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

Ch k iCh k iCheckpointsCheckpointsL ld b l d • Log could become long, and recovery could take longCh k i t h t l d • Checkpoints shorten log and recovery time.Ch k i t h• Checkpoint scheme:1.Output all log records currently in volatile

storage to stable storagestorage to stable storage2.Output all modified data from volatile to

stable storageg3.Output a log record <checkpoint> to the

log on stable storage

Page 52: Sistem Operasi 6 - elearning.amikom.ac.idelearning.amikom.ac.id/index.php/download/materi/190302161-DT019...P l Pl iParalel Processing • Pa alel p ocessing Paralel processing is

NEXTNEXTNEXTNEXT• Deadlock