process synchronization. whenever processes share things, there is a chance for screwing up things ...

Post on 05-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Process Synchronization

Whenever processes share things, there is a chance for screwing up things

For example◦ Two processes sharing one printer◦ Two processes updating one database record◦ Two processes sharing a single counter

Two processes – one a producer of something, and one a consumer of something

The producer produces something and places the something into a buffer

The consumer consumes the something, taking the something out of the buffer

Once again two processes are sharing something – the buffer

repeat repeat : while counter = 0 do no-op;produce an item in nextp (buffer is empty) : nextc := buffer[out];while counter = n do no-op; out := out + 1 mod n; (buffer is full) counter := counter – 1;buffer[in] := nextp; :in := in + 1 mod n; consume the item in nextccounter := counter + 1; :

until false; until false;

PRODUCER CONSUMER

Note that both the producer and the consumer reference variable counter

In the producer: counter := counter + 1 which in machine language is:

◦ register1 := counter;◦ register1 := register1 + 1;◦ counter := register1;

In the consumer: counter := counter - 1 Which in machine language is:

◦ register2 := counter◦ register2 := register2 - 1◦ counter := register2

counter could be 4, 5 or 6 depending upon where the code is interrupted

For example:

counter is at 5 and producer starts: register1 := counter; register1 = 5 register1 := register1 + 1; register1 = 6 the producer is swapped out and the consumer

starts anew register2 := counter; register2 = 5 register2 := register2 - 1; register2 = 4 the consumer is swapped out and the producer

returns counter := register1; counter = 6 the producer is done, and the consumer returns counter := register2; counter = 4 counter should be 5!

Thus, the accessing of counter is a critical section

What happens above is called a race condition

Must ensure than ONLY one process at a time references counter

We need process synchronization

To solve critical section problem, three things are needed◦ Mutual exclusion – only one at a time◦ Progress – process moves through event◦ Bounded waiting – everyone has to get a turn

A simple, clean way to support process synchronization

Supported by many OSs and even some processors

A semaphore is nothing more than an integer.

But, this integer can only be accessed (except for initialization) through two atomic operations:

wait(s): if s = 0 then go to sleep;s := s – 1;

signal(s): s := s + 1;wakeup sleeping process;

To use semaphores, create a structure that looks like the following:

repeat:wait(semaphore)

:(critical section)

:signal(semaphore):

until false;

Database update in which you want to lock out all but one concurrent user

procedure UpdateDatabase;var s: semaphore (:=1);begin

:(prompt user for data)

:wait(s);Update(record);signal(s);

:end;

CD-ROM reader which allows 5 concurrent users

procedure ReadCD;var t: semaphore (:=5);begin

:(prompt user for data)

:wait(t);ReadCD(buffer);signal(t);

:end;

Let’s re-visit the producer/consumer problem and solve the problem using semaphores (on the whiteboard)

var s: semaphore (:=1); n: semaphore (:=0); e: semaphore (:=sizeofbuffer);

procedure producer;begin

repeatproduce item for buffer;wait (e);wait(s); append to buffer;signal(s);signal(n);

forever;end;

procedure consumer;begin

repeatwait(n);wait(s); take from buffer;signal(s);signal(e);consume item

forever;end;

When two or more processes have conflicting needs for resources, we have deadlock

Example: P1 holds the disk and wants the tape; P2 holds the tape and wants the disk

Example: P1 has 80K or memory and wants 60K more; P2 has 70K of memory and wants 80K more; there is only 150K total of memory

Three conditions must first exist for deadlock to occur:1. Mutual exclusion2. Hold and wait3. No preemption

Then the following must happen:4. Circular wait

How do you avoid deadlock?

Indirect methods – stop one of the conditions 1-3 from happening (not desirable)

Direct methods – stop occurrence of condition 4 from happening

For example: Order all resources from 0 to N. If you have resource m, you can only ask for a resource > m, where Disk=5; Tape=7; Printer=12

Really is prevention, but not as restrictive Avoidance allows the 3 conditions but

makes dynamic decisions whether a current resource allocation request will lead to deadlock, such as Bankers’ Algorithm

Use an algorithm that can detect cycles in directed graphs

Costly, as OS must maintain graphs and check for cycles

top related