• Tidak ada hasil yang ditemukan

Process Synchronization

N/A
N/A
Protected

Academic year: 2021

Membagikan "Process Synchronization"

Copied!
12
0
0

Teks penuh

(1)

Process Synchronization

SISTIM OPERASI

(Operating System)

IKI-20230

Johny Moningka

([email protected])

Fakultas Ilmu Komputer Universitas Indonesia Semester 2000/2001

Process Synchronization

n

Background

n

The Critical-Section Problem

n

Synchronization Hardware

n

Semaphores

n

Classical Problems of Synchronization

(2)

OS Processes JM-2000/v1.1/3

Synchronization

n

Sinkronisasi:

n Koordinasi akses ke shared data, misalkan hanya satu

proses yang dapat menggunakah shared var.

n Contoh operasi terhadap var. “counter” harus dijamin di-eksekusi dalam satu kesatuan (atomik) :

counter := counter + 1;counter := counter - 1;

n Sinkronisasi merupakan “issue” penting dalam

rancangan/implementasi OS (shared resources, data, dan multitasking).

OS Processes JM-2000/v1.1/4

The Critical-Section Problem

n n proses mencoba menggunakan shared data bersamaan

n Setiap proses mempunyai “code” yang mengakses/ manipulasi shared data tersebut => “critical section”

n Problem: Menjamin jika ada satu proses yang sedang “eksekusi” pada bagian “critical section” tidak ada proses lain yang diperbolehkan masuk ke “code” critical section dari proses tersebut. n Structure of process Pi repeat entry section critical section exit section reminder section until false;

(3)

OS Processes JM-2000/v1.1/5

n Ide:

n Mencakup pemakaian secara “exclusive” dari shared variable tersebut

n Menjamin proses lain dapat menggunakan shared variable tersebut

n Solusi “critical section problem” harus memenuhi:

1. Mutual Exclusion: Jika proses Pi sedang “eksekusi” pada

bagian “critical section” (dari proses Pi) maka tidak ada proses-proses lain dapat “eksekusi” pada bagian critical section dari proses-proses tersebut.

2. Progress: Jika tidak ada proses sedang eksekusi pada critical

section-nya dan jika terdapat lebih dari satu proses lain yang ingin masuk ke critical section, maka pemilihan siapa yang berhak masuk ke critical section tidak dapat ditunda tanpa terbatas.

Solution (pre-requsite)

Solutions (cont.)

3. Bounded Waiting: Terdapat batasan berapa lama

suatu proses harus menunggu giliran untuk

mengakses “critical section” – jika seandainya proses lain yang diberikan hak akses ke critical section.

• Menjamin proses dapat mengakses ke “critical section” (tidak mengalami starvation: proses se-olah berhenti menunggu request akses ke critical section diperbolehkan).

• Tidak ada asumsi mengenai kecepatan eksekusi proses-proses n tersebut.

(4)

OS Processes JM-2000/v1.1/7

Simple solution: case 2 process

n

Hanya 2 proses

n

Struktur umum dari program code Pi dan Pj:

repeat entry section critical section exit section reminder section until false;

n

Software solution: merancang algoritma program

untuk solusi critical section

n Proses dapat mengunakan “common var.” untuk

menyusun algoritma tsb. OS Processes JM-2000/v1.1/8

Algorithm 1

n Shared variables: n var turn: (0..1); initially turn = 0

n turn = i ⇒ Pi dapat masuk ke critical section⇒ n Process Pi

repeat

while turni do no-operation;

critical section

turn := j;

reminder section

until false;

(5)

OS Processes JM-2000/v1.1/9

Algorithm 2

n Shared variables

n var flag: array [0..1] of boolean;

initially flag [0] = flag [1] = false.

n flag [i] = true ⇒ P iready to enter its critical section

n Process Pi repeat

flag[i] := true;

while flag[j] do no-op;

critical section

flag [i] := false;

remainder section

until false;

n Satisfies mutual exclusion, but not progress requirement.

Algorithm 3

nCombined shared variables of algorithms 1 and 2.

nProcess Pi repeat

flag [i] := true; turn := j;

while (flag [j] and turn = j) do no-op;

critical section

flag [i] := false;

remainder section

until false;

nMeets all three requirements; solves the critical-section problem for two processes.

(6)

OS Processes JM-2000/v1.1/11

“Bakery Algorithm”

n

Critical Section for n processes

n Sebelum proses akan masuk ke dalam “critical

section”, maka proses harus mendapatkan “nomor” (tiket).

n Proses dengan nomor terkecil berhak masuk ke critical section.

If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj is served first.

n The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

OS Processes JM-2000/v1.1/12

Bakery Algorithm (Cont.)

n

Notasi <

lexicographical order (ticket #, process

id #) => pasti unik

n (a,b) < (c,d) if a < c or if a = c and b < d

note: (b dan d pasti tidak sama)

n max (a0,…, an-1) is a number, k, such that k ≥aifor i

-0, …, n – 1 n

Shared data

var choosing: array [0..n – 1] of boolean;

number: array [0..n – 1] of integer,

(7)

OS Processes JM-2000/v1.1/13

Bakery Algorithm (Cont.)

repeat

choosing[i] := true;

number[i] := max(number[0], number[1], …, number[n – 1])+1; choosing[i] := false;

for j := 0 to n – 1 do begin

while choosing[j] do no-op; while number[j] ≠ 0

and (number[j],j) < (number[i], i) do no-op; end; critical section number[i] := 0; remainder section until false;

Synchronization Hardware

n

Memerlukan dukungan hardware (prosesor)

n Dalam bentuk “instruction set” khusus: test-and-set

n Menjamin operasi atomik (satu kesatuan): test nilai dan ubah nilai tersebut.

n

Test-and-Set dapat dianalogikan dengan kode:

function Test-and-Set (var target:boolean): boolean;

begin

Test-and-Set := target; target := true;

(8)

OS Processes JM-2000/v1.1/15

Test-and-Set (mutual exclusion)

n

Mutual exclusion dapat diterapkan:

n Gunakan shared data,

variabel: lock: boolean (initially false)

n lock: menjaga critical section n

Process Pi:

repeat

while Test-and-Set (lock) do no-op; .. critical section .. lock := false; remainder section until false; OS Processes JM-2000/v1.1/16

Semaphore

n

Synchronization tool that does not require busy

waiting.

n

Semaphore S – integer variable

n Dapat dijamin akses ke var. S oleh dua operasi atomik:

• wait (S): while S ≤0 do no-op; S := S – 1;

(9)

OS Processes JM-2000/v1.1/17

Example: n processes

n

Shared variables

n var mutex : semaphore n initially mutex = 1 n

Process Pi

repeat wait(mutex); critical section signal(mutex); remainder section until false;

Semaphore Implementation

n

Define a semaphore as a record

type semaphore = record

value: integer

L: list of process;

end;

n

Assume two simple operations:

n block() : suspends the process that invokes it.

n wakeup(P): resumes the execution of a blocked process P.

(10)

OS Processes JM-2000/v1.1/19

Implementation (Cont.)

nSemaphore operations now defined as

n wait(S ): S.value := S.value – 1;

if S.value < 0 then begin

add this process to S.L;

block;

end;

n signal(S ): S.value := S.value + 1;

if S.value≤0

then begin

remove a process P from S.L;

wakeup(P );

end;

OS Processes JM-2000/v1.1/20

Semaphore: Synchronization Tool

n

Execute B in Pj only after A executed in Pi

n

Use semaphore flag initialized to 0

n Code:

Pi Pj

M M

A wait(flag)

(11)

OS Processes JM-2000/v1.1/21

Two Types of Semaphores

n

Counting semaphore

n Integer value can range over an unrestricted

domain.

n Efektif untuk pemakaian membatasi jumlah proses, counter (blok pada nilai tertentu) dll.

• Semaphore s = 10;

n

Binary semaphore

n Integer value can range only between 0 and 1;

n Digunakan untuk mutula exclusion: membatasi hanya ada satu proses pada critical section.

• Semaphore s = 1;

Semaphore: bounded buffer

Shared data (array of item): buffer Semaphore:

Binary Semaphore mutex; // mutual exclusion akses ke buffer Semaphore empty_slot; // slot buffer yang kosong :

Semaphore full_item; // item di buffer;

Inisialisasi: mutex = 1; empty_slot = n; full_item = 0; n

(12)

OS Processes JM-2000/v1.1/23

Semaphrore: bounded buffer

Producer:

do

.. produce item pada nextp; wait(empty_slot);

wait(mutex); …(critical section)

… add nextp to buffer;

signal(mutex); signal(full_item); while (true); Consumer: do wait(full_item); wait(mutex); …(critical section)

… remove nextp buffer;

signal(mutex); signal(empty_slot); .. consume item nextp;

while (true);

OS Processes JM-2000/v1.1/24

Deadlock and Starvation

nDeadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes.

nLet S and Q be two semaphores initialized to 1

P0 P1 wait(S); wait(Q); wait(Q); wait(S); M M signal(S); signal(Q); signal(Q) signal(S);

nStarvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is

Referensi

Dokumen terkait

ƒ Bahwa bis hanya bisa digerakkan bila kekuatan pendorong lebih besar dari beban yang ada. Perubahan hanya akan bisa mencapai tujuannya jika bisa mengatasi kekuatan anti

Berdasarkan hasil penelitian dapat disimpulkan bahwa respon siswa tentang active learning dengan model pembelajaran secara berkelompok pada mata pelajaran Aqidah Akhlak di

itu, putar sisi kanan rubik tadi (hijau) sampai Edge Pieces warna putihnya sejajar dengan Center Pieces-!.

Berdasarkan prioritas masalah dan diskusi lebih lanjut dengan kepala dan staf puskesmas, maka yang menjadi prioritas utama adalah “Rendahnya penemuan pasien baru BTA positif

Penelitian tugas karya akhir ini merupakan salah satu persyaratan dalam menyelesaikan Program Sarjana Pendidikan Dokter di Fakultas Kedokteran Universitas Sebelas

Penerapan sistem akuntansi keuangan daerah pada badan kepegawaian daerah kota padang telah sesuai dengan ketentuan yang. terdapat dalam Permendagri No.13 Tahun 2006 Syuriani

5 ADES AKASHA WIRA INTERNATIONAL Tbk RISR1 - RAYA SAHAM REGISTRA, PT 1000.. 6 ADHI ADHI KARYA (PERSERO) Tbk DAEN1 - DATINDO ENTRYCOM,

Expert systems are a part of the method sciences artificial intelligence to make an application program disease diagnosis teak computerized seek to replace and