Docsity

Concurrency: Executing Code Simultaneously - Architecture, Synchronization, and Examples, Study notes of Operating Systems

Toggle sidebar
bg1




Concurrency
Execute two or more pieces of code "at the same time“
Why ?
No choice:
Geographically distributed data
Interoperability of different machines
A piece of code must "serve" many other client processes
To achieve reliability
By choice:
To achieve speedup
Sometimes makes programming easier (e.g., UNIX pipes)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Concurrency: Executing Code Simultaneously - Architecture, Synchronization, and Examples and more Study notes Operating Systems in PDF only on Docsity!

 Concurrency

 Execute two or more pieces of code "at the same time“

 Why?

 No choice:

 Geographically distributed data

 Interoperability of different machines

 A piece of code must "serve" many other client processes

 To achieve reliability

 By choice:

 To achieve speedup

 Sometimes makes programming easier (e.g., UNIX pipes)

  

Architecture: Program Style:

Uniprocessor with: Multiprogramming,

  • I/O channel multiple process system
  • I/O processor programs
  • DMA

Multiprocessor Parallel programming

Network of processors Distributed Programs

!"#

$

Example 1: Unix pipes

Motivations:

  • fast to write code
  • fast to execute

Example 2: Buffering

Motivation:

  • required when two asynchronous processes must

communicate

Example 3: Client/Server model

Motivation:

  • geographically distributed computing

'  

 Concurrent process execution can be:

 interleaved, or

 physically simultaneous

 Interleaved

 Multi-programming on uniprocessor

 Physically simultaneous

 Uni- or multi-programming on multiprocessor

'  &

 Process, thread, or task

 Scheduleable unit of computation

 Granularity

 Process "size" or computation to

 Communication ratio

 Too small: excessive overhead

 Too large: less concurrency

(

Consider writing a program as a set of tasks.

Precedence graph:

specifies execution ordering among tasks

S1 : A := X + Y

S2 : B := Z + 1

S3 : C := A - B

S4 : W := C + 1

S1 S

S

S

Parallelizing compilers for computers with vector processors build

dependency graphs.

(

Precedence Graphs must

be ACYCLIC

S

S

S

What does the following graph represent?

S2 must be performed before S3 begins

AND

S3 must be performed before S2 begins

) * 

The following conditions must hold for two statements S1 and S2 to

execute concurrently with valid results:

1) R ( S1 ) INTERSECT W ( S2 ) = { }

2) W ( S1 ) INTERSECT R ( S2 ) = { }

3) W ( S1 ) INTERSECT W ( S2 ) = { }

These are called the Bernstein Conditions.



Sequential execution splits off into several concurrent

sequences

Parallel computations merge

PARBEGIN

PAREND

PARBEGIN

Statement 1;

Statement 2;

Statement N;

PAREND;

PARBEGIN

Q := C mod 25;

Begin

N := N - 1;

T := N / 5;

End;

Proc1 ( X, Y );

PAREND;

PARBEGIN / PAREND

 + !"#

Begin

PARBEGIN

A := X + Y;

B := Z + 1;

PAREND;

C := A - B;

W := C + 1;

End;

Begin

S1;

PARBEGIN

S3;

BEGIN

S2;

S4;

PARBEGIN

S5;

S6;

PAREND;

End;

PAREND;

S7;

End;

 + !"#

 . 

 Within the monitor, Condition Variables are declared

 A queue is associated with each condition variable

 Only two operations are allowed on a condition variable:

The procedure performing the wait is put on the

queue associated with x

If queue is non-empty: resume some process at

the point it was made to wait

X.wait

X.signal

  • Note: V operations on a semaphore are "remembered," but if

there are no waiting processes, the signal has no effect

  • OS scheduler decides which of several waiting monitor calls to

unlock upon signal

  • &

 Queue to enter monitor via calls to procedures

 Queues within the monitors via condition variables

 ADTs and condition variables only accessible via monitor

procedure calls

  • &

Monitors contain procedures that control access to a < CS >, but

not the < CS > code itself.

Program

Begin

Request;

< CS >

Release;

End;

Monitor

condition i;

Request

Release

end monitor

4     5

  •  

Monitor NCS {

OK: condition

Busy: boolean <-- FALSE

Request() {

if (Busy) OK.wait;

Busy = TRUE;

Release() {

Busy = FALSE;

OK.signal;

Procedure P {

NCS.Request();

;

NCS.Release();

main() {

parbegin P;P;P;P; parend }

6 78 #

0  

monitor reader_writer_2{ int numberOfReaders = 0; boolean busy = false ; condition okToRead, okToWrite; public : startRead(){ if (busy || okToWrite.queue) okToRead.wait; numberOfReaders = numberOfReaders+1; okToRead.signal; } finishRead() { numberOfReaders = numberOfReaders-1; if (numberOfReaders =0) okToWrite.signal; } startWrite(){ if (busy || numberOfReaders != 0) okToWrite.wait; busy = true ; } finishWrite() { busy = false ; if (okToWrite.queue) okToWrite.signal; else okToRead.signal; } }

'   1 #

while(TRUE) {

think();

eat();

'   1 #

0  

enum status {eating, hungry, thinking}; monitor diningPhilosophers{ status state[N]; condition self[N]; int j; // This procedure can only be called from within the monitor test( int i) { if((state[i-1 MOD N] != eating) && (state[i] == hungry) && (state[i+1 MOD N] != eating) ) { state[i] = eating; self[i].signal; } public : pickUpForks(){ state[i] = hungry; test(i); if (state[i] != eating) self[i].wait; } putDownForks(){ state[i] = thinking; test(i-1 MOD N); test(i+1 MOD N); } diningPhilosophers() { // Monitor initialization code for ( int i=0; i<N; i++) state[i] = thinking; } }

  •  ## 

   

monitor ringBufferMonitor;

var ringBuffer: array [0..slots-1] of stuff;

slotInUse: 0..slots; nextSlotToFill: 0..slots-1; nextSlotToEmpty: 0..slots-1;

ringBufferHasData, ringBufferHasSpace: condition ;

procedure fillASlot(slotData: stuff);

begin

if (slotInUse = slots) then wait(ringBufferHasSpace); ringBuffer[nextSlotToFill] := slotData; slotInUse := slotInUse + 1; nextSlotToFill := (nextSlotToFill+1) MOD slots;

signal(ringBufferHasData);

end ;