MODUL PERKULIAHAN Sinkronisasi Proses Sistem Operasi Fakultas Program Studi Ilmu Komputer Teknik Informatika Abstract Tatap eMuka 06 Kode MK Disusun Oleh Misbahul Fajri, ST., MTI Kompetensi Basic Review • Beberapa difinisi tentang managemen process dan Thread, yaitu : – Multiprogramming, eksekusi multiple processor pada uniprocessor / single processor. – Multiprocessing, eksekusi multiple process pada multiple processor. – Distributed Processing, eksekusi multiple process yang dieksekusi pada muliple / beberapa sistem komputer. Concurrency • Terdapat beberapa term, yang berkaitan dengan concurrency, seperti yang ditunjukan pada gambar disamping. • Concurrency merujuk kepada mekanisme scheduling yang termasuk komunikasi antar process, resource sharing (memory, I/O, file, etc). Latar Belakang • Misalkan rutin producer-consumer – ‘13 2 Producer Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id • Repeat – • – Until false Consumer • • Counter := counter + 1 Repeat – Counter := counter -1 – Until false Meskipun kedua rutin producer dan consumer tersebut benar secara terpisah, namun keduanya tidak akan berfungsi dengan benar pada saat dieksekusi secara simultan. • Race condition – Situasi dimana beberapa proses mengakses dan memanipulasi data yang sama secara simultan, dan hasil dari eksekusi tergantung pada suatu urutan tertentu Critical Section • Adalah suatu bagian yang berisi sejumlah variabel yang akan di-share proses yang lain • Syarat penyelesaian critical section: 1. Mutual exclusion 2. Progress 3. Bounded Waiting 1. Mutual Exclusion ‘13 3 – Jika suatu proses sedang mengerjakan critical section. – Maka tidak boleh ada proses lain yang masuk (mengerjakan) critical section tersebut Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id 2. Progress – Jika tidak ada suatu proses yang mengerjakan critical section, dan beberapa proses yang akan masuk ke critical section. – Maka hanya proses-proses yang sedang berada pada entry-section saja yang boleh berkompetisi untuk mengerjakan critical section. 3. Bounded Waiting – Besarnya waktu tunggu dari suatu proses yang akan memasuki critical section. – Sejak proses itu meminta ijin untuk mengerjakan critical section hingga permintaan itu dipenuhi. Tipe-Tipe Sinkronisasi • Sinkronisasi Software • – Algoritma-1 – Algoritma-2 – Algoritma-3 (Peterson) – Algoritma Dekker Sinkronisasi Hardware – Instruksi Test And Set Lock – Instruksi Exchange Semaphore • Adalah salah satu cara menangani critical section. • Semaphore S merupakan variabel bertipe integer yang diakses dengan 2 standar operasi atomic, yaitu wait dan signal. • Wait(S) : while S <= 0 do no_op S:=S-1 • ‘13 4 Signal(S) : S:=S+1; Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id • Misalkan ada 2 proses yang sedang berjalan konkuren, yaitu P1 dengan pernyataan S1 dan P2 dengan pernyataan S2 • Hal ini dapat dilakukan dengan menggunakan bantuan semaphore synch (dengan nilai awal=0) yang akan di-share ole kedua proses. – – Proses P1: » S1; » Signal(synch) » Wait(synch) » S2; » Karena nilai awal untuk synch adalah nol, maka P2 akan Proses P2 mengeksekusi S2 hanya setelah P1 mengerjakan signal(synch) setelah S1. Masalah Klasik Sinkronisasi • The Bounded-Buffer (Producer-Consumer) Problem • The Reader and Writers Problem • The Dining-Philosophers Problem • The Sleeping Barber Problem Synchronization Examples ‘13 Solaris Windows XP Linux Pthreads 5 Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id Solaris Synchronization Implements a variety of locks to support multitasking, multithreading (including realtime threads), and multiprocessing Uses adaptive mutexes for efficiency when protecting data from short code segments Uses condition variables and readers-writers locks when longer sections of code need access to data Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock Windows XP Synchronization Uses interrupt masks to protect access to global resources on uniprocessor systems Uses spinlocks on multiprocessor systems Also provides dispatcher objects which may act as either mutexes and semaphores Dispatcher objects may also provide events o An event acts much like a condition variable Linux Synchronization Linux: o Prior to kernel Version 2.6, disables interrupts to implement short critical sections o Version 2.6 and later, fully preemptive Linux provides: o semaphores o spin locks Pthreads Synchronization ‘13 Pthreads API is OS-independent It provides: 6 o mutex locks o condition variables Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id Non-portable extensions include: o read-write locks o spin locks Atomic Transactions System Model Log-based Recovery Checkpoints Concurrent Atomic Transactions System Model Assures that operations happen as a single logical unit of work, in its entirety, or not at all Related to field of database systems Challenge is assuring atomicity despite computer system failures Transaction - collection of instructions or operations that performs single logical function o Here we are concerned with changes to stable storage – disk o Transaction is series of read and write operations o Terminated by commit (transaction successful) or abort (transaction failed) operation o 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 o Nonvolatile storage – Information usually survives crashes o ‘13 Example: main memory, cache Example: disk and tape Stable storage – Information never lost 7 Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id Not actually possible, so approximated via replication or RAID to devices with independent failure modes Goal is to assure transaction atomicity where failures cause loss of information on volatile storage Log-Based Recovery Record to stable storage information about all modifications by a transaction Most common is write-ahead logging o Log on stable storage, each log record describes single transaction write operation, including Transaction name Data item name Old value New value o <Ti starts> written to log when transaction Ti starts o <Ti commits> written when Ti commits Log entry must reach stable storage before operation on data occurs Log-Based Recovery Algorithm Using the log, system can handle any volatile memory errors o Undo(Ti) restores value of all data updated by Ti o Redo(Ti) sets values of all data in transaction Ti to new values Undo(Ti) and redo(Ti) must be idempotent o ‘13 Multiple executions must have the same result as one execution If system fails, restore state of all updated data via log 8 o If log contains <Ti starts> without <Ti commits>, undo(Ti) o If log contains <Ti starts> and <Ti commits>, redo(Ti) Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id 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 Now recovery only includes Ti, such that Ti started executing before the most recent checkpoint, and all transactions after Ti All other transactions already on stable storage Concurrent Transactions Must be equivalent to serial execution – serializability Could perform all transactions in critical section o Inefficient, too restrictive Concurrency-control algorithms provide serializability Serializability Consider two data items A and B Consider Transactions T0 and T1 Execute T0, T1 atomically Execution sequence called schedule Atomically executed transaction order called serial schedule For N transactions, there are N! valid serial schedules Schedule 1: T0 then T1 ‘13 9 Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id Nonserial Schedule Nonserial schedule allows overlapped execute o Consider schedule S, operations Oi, Oj o 10 Then S’ with swapped order Oj Oi equivalent to S If S can become S’ via swapping nonconflicting operations o ‘13 Conflict if access same data item, with at least one write If Oi, Oj consecutive and operations of different transactions & Oi and Oj don’t conflict o Resulting execution not necessarily incorrect S is conflict serializable Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id Schedule 2: Concurrent Serializable Schedule Locking Protocol Ensure serializability by associating lock with each data item o Locks o Shared – Ti has shared-mode lock (S) on item Q, Ti can read Q but not write Q o Exclusive – Ti has exclusive-mode lock (X) on Q, Ti can read and write Q Require every transaction on item Q acquire appropriate lock If lock already held, new request may have to wait o ‘13 Follow locking protocol for access control 11 Similar to readers-writers algorithm Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id Daftar Pustaka • Operating System Concepts (6th or 7th Edition). Silberschatz, Galvin, Gagne, ISBN: 0-471-25060-0. Wiley ‘13 12 Manajemen Proyek Perangkat Lunak Misbahul Fajri, ST., MTI Pusat Bahan Ajar dan eLearning http://www.mercubuana.ac.id