Docsity

Process Synchronization - Notes for Module 6 | CSE 536, Exams of Operating Systems

Partial preview of the text

Download Process Synchronization - Notes for Module 6 | CSE 536 and more Exams Operating Systems in PDF only on Docsity!

Module 6: Process Synchronization

Background

The Critical-Section Problem

Synchronization Hardware

Semaphores

Classical Problems of Synchronization

Critical Regions

Monitors

Synchronization in Solaris 2

Atomic Transactions

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Background

inconsistency.Concurrent access to shared data may result in data

the orderly execution of cooperating processes.Maintaining data consistency requires mechanisms to ensure

  1. allows at mostShared-memory solution to bounded-buffer problem (Chapter

n

1 items in buffer at the same time. A

solution, were all

N

buffers are used is not simple.

adding a variableSuppose that we modify the producer-consumer code by

counter

, initialized to 0 and incremented

each time a new item is added to the buffer.

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Bounded-Buffer (Cont.)

Consumer process

repeat

while

counter

do

no-op

nextc

buffer

[

out

];

out

out

mod

n

;

counter

counter

consume the item in

nextc

until

false

The statements:

-

counter

counter

counter

counter

must be executed

atomically

Operating System Concepts

Silberschatz and Galvin

c

©

1998

The Critical-Section Problem

n

processes all competing to use some shared data

Each process has a code segment, called

critical section

, in

which the shared data is accessed.

critical section.critical section, no other process is allowed to execute in itsProblem – ensure that when one process is executing in its

Structure of process

P

i

repeat

entry section

critical section

exit section

remainder section

until

false

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Initial Attempts to Solve Problem

Only 2 processes,

P

0

and

P

1

General structure of process

P

i^

(other process

P

j (^) )

repeat

entry section

critical section

exit section

remainder section

until

false

their actions.Processes may share some common variables to synchronize

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Algorithm 1

Shared variables:

- var

turn

initially

turn

turn

i

⇒

P

i

can enter its critical section

Process

P

i

repeat

while

turn

i

do

no-op

critical section

turn

j ;

remainder section

until

false

Satisfies mutual exclusion, but not progress.

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Algorithm 3

Combined shared variables of algorithms 1 and 2.

Process

P

i

repeat

flag

[

i ] :=

true

turn

j ;

while

flag

[

j ]

and

turn

j )

do

no-op

critical section

flag

[

i ] :=

false

remainder section

until

false

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

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Bakery Algorithm

Critical section for

n

processes

Holder of the smallest number enters the critical section.Before entering its critical section, process receives a number.

If processes

P

i

and

P

j

receive the same number, if

i

j , then

P

i

is served first; else

P

j

is served first.

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

Operating System Concepts

Silberschatz and Galvin

c

©

1998

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

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

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Synchronization Hardware

Test and modify the content of a word atomically.

function

Test-and-Set

var

target

boolean

boolean

begin

Test-and-Set

target

target

true

end

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Semaphore

Synchronization tool that does not require busy waiting.

Semaphore

S

  • integer variable

can only be accessed via two indivisible (atomic) operations

wait

S

while

S

do

no-op

S

S

signal

S

S

S

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Example: Critical Section for

n

Processes

Shared variables

- var

mutex

semaphore

initially

mutex

Process

P

i

repeat

wait

mutex

critical section

signal

mutex

remainder section

until

false

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Implementation (Cont.)

Semaphore operations now defined as

wait

S

S

value

S

value

if

S

value

then begin

add this process to

S.L

block

end

signal

S

S

value

S

value

if

S

value

then begin

remove a process

P

from

S.L

wakeup

P

end

Operating System Concepts

Silberschatz and Galvin

c

©

1998

Semaphore as General Synchronization Tool

Execute

B

in

P

j

only after

A

executed in

P

i

Use semaphore

flag

initialized to 0

Code:

P

i

P

j

A

wait

flag

signal

flag

B

Operating System Concepts

Silberschatz and Galvin

c

©

1998