chapter 3: concurrent processes and programmingnemo/cop5615/chow/ch3.pdf · chapter 3: concurrent...

21
CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline Thread implementations Process models The client/server model Time services Language constructs for synchronization Concurrent programming systems 1

Upload: vubao

Post on 27-Feb-2018

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

CHAPTER 3: CONCURRENT PROCESSES

AND PROGRAMMING

Chapter outline

• Thread implementations

• Process models

• The client/server model

• Time services

• Language constructs for synchronization

• Concurrent programming systems

1

Page 2: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Processes and threads

• Processes: separate logical address space

• Threads: common logical address space

Major Issues

• Process/thread creation

• Light weight context switching

• Blocking and scheduling

2

Page 3: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Two-level concurrency of processes and threads

PCB

TCB TCB TCB

thre

ad

thre

ad

thre

ad

PCB

TCB TCB TCB

thre

ad

thre

ad

thre

ad

PCB

thre

ad

process

Native Computer System

Single threadMultiple thread processes

Thread run-time library support

Native operating system

Thread applications

thread

thread

thread

buff

er

mainthread

threadwri

te

read

requests

dispatcher asynchronous requests

(b) File server(a) Terminal server (c) Client

Identical static threads Dynamic threads with Concurrent and

3

Page 4: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Thread implementations

• User space: simple but non-preemptable

• Kernel space: efficient but not portable

Solaris thread implementation

multiple processor system

LWP LWP LWP LWP

Heavy-weight process Heavy-weight process

User space threads

Light-weight processes

Kernel space threads

4

Page 5: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Process models

Synchronous Process, Asynchronous Communication, Time-Space

Graph representations

precedence

relations

communication

channels

one - way

client / server

peer

communication scenariosSynchronous process graph

Asynchronous process graph and

5

Page 6: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Time-space model

Processes

P1

P2

P3

P4

: communication

: events

Spac

e

Time

Client/server model

client server

kernel kernelnetwork

request

reply

actual communication

logical communication

6

Page 7: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Time services

• time and timer

• physical and logical clocks

Physical clock

A distributed time service architecture

TS

TSTS TC

TC

Distributed Time Service

Client

Time Clerks

T�ime

Servers

External UTC

Sources

Time Discrepancies

UTC 5

discarded new UTC

UTC 4

UTC 3

UTC 2

UTC 1

7

Page 8: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Lamport Logical Clock

The happens-before relationship: →

1. If a → b within a same process then C(a) < C(b).

2. If a is the sending event of Pi andb is the corresponding receiving event of Pj, then a → b andCi(a) < Cj(b).

For it to be possible for a to have an influence on b, then a → b must be true.

Implementation:

C(b) = C(a) + d andCj(b) = max(TSa + d, Cj(b)),where TSa is the timestamp of the sending event and d is a positive number.

42

43

55

56 80

81

d = 1

57

5840 45 60

20 50 60

50 75

a, c,b,

d, e, f,

g, h,

So, a → b =⇒ C(a) < C(b), but C(a) < C(b) =⇒/ a → b.

8

Page 9: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Vector Logical Clock

Used so that if Ci(a) < Cj(b) then a → b.

Define V Ci = [TS1, TS2, ..., Ci, ..., TSn],where n is the number of cooperating processes.

On message receipt, use pair-wise maximum.

V Cj [j] = V Cj[j] + dV Cj [k] = max(V Cj [k], TSi[k]) : l = 1..n

TS = VCi

j

Pj’s Lamport Clock

elementwise maxmerge

VCj

k

Pj’s knowledge of Pk’s Lamport Clock

V Cj [j] is Pj’s count of events that have occured at Pj ,V Cj [k] is Pj’s knowledge of events that have occured at Pk.

200

220

242

250

450

244

274

100a,

010d, 230 260

001 243

e, f,

g, h,

300b, 550c,

240

9

Page 10: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Matrix Logical Clock

MCi representsPi’s knowledge of its local events (MCi[i, i]),its knowledge of the events that Pj knows about (MCi[i, j]), andits knowledge of Pj’s knowledge of events at Pk (MCi[j, k]).

MCi[i, i] = MCi[i, i] + d – Pi updates local event counter on send

When Pj receives a message from Pi with timestamp TS, MCj [j, l] =max(MCj [j, l], TSi[i, l]) : l = 1..n update vector clock, and

MCj [k, l] = max(MCj [k, l], TSi[k, l]) : k = 1..n, l = 1..n update Pk’sknowledge of Pl’s counter

i

i

i,i

j j

Pi’s Vector Clock

Pj’s Vector Clock

Pi’s knowledge of Pk’s Vector Clock Pj’s knowledge of Pk’s Vector Clockk k

i

MCjTS = MCi

Pj’s knowledge of Pi’s Vector Clock

Pi’s knowledge of Pj’s knowledge of Pi’s Lamport Clock

elementwise maxmerge

10

Page 11: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Concurrent languages

• Specification of concurrent activities

• Synchronization of processes

• Interprocess communication

• Nonderterministic execution of processes

Language constructs

• Program structure

• Data structure

• Control structure

• Procedure and system call

• Input and output

• Assignment

11

Page 12: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Synchronization mechanisms and language facilities

Synchronization Methods Language Facilities

Shared-Variable Synchronization

semaphore shared variable and system callmonitor data type abstractionconditional critical region control structureserializer data type and control structurepath expression data type and program structure

Message Passing Synchronization

communicating sequential processes input and outputremote procedure call procedure callrendezvous procedure call and communication

Shared-variable synchronization

• Semaphore and conditional critical region

• Monitor and serializer

• Path expression

Classic Problems

• Critical Section

• Dining Philosophers

• Readers/Writers

• Producer-Consumer12

Page 13: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Example: the Reader/Writer Problemssynchronization + concurrency

Basics

• if DB empty, allow anyone in

• if reader in DB, writer not allowed in

• if writer in DB, nobody allowed in

Write Lock

Lock Held

Write LockRead Lock

Read Lock

RequestedLock

Variations

• reader preference

Allow a reader in if other readers are in

• strong reader preference

Allow readers in when writer leaves

• weak reader preference

When writer leaves, select a process at random

• weaker reader preference

Allow a writer in when writer leaves

• writer preference

Do not allow readers in if writer is waiting

13

Page 14: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Semaphore solution to the weak reader preference problem

var mutex=1, db=1: semaphore; rc=0: integer

reader processes writer processes

do (forever) do (forever)BEGIN BEGIN

otherStuff() otherStuff()

P(mutex)rc := rc + 1if rc = 1 then P(db) P(db)V(mutex)

read database write database

P(mutex)rc := rc -1if rc = 0 then V(db) V(db)V(mutex)

END END

14

Page 15: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Monitor solution

rw : monitorvar rc : integer; busy : boolean; toread, towrite : condition;

procedure startread procedure endreadbegin beginif busy then toread.wait;rc := rc + 1; rc := rc - 1;toread.signal; if rc = 0 then towrite.signal;end end

procedure startwrite procedure endwritebegin beginif busy or rc 6= 0then towrite.wait; busy := false;busy := true; toread.signal or towrite.signal;end end

begin rc := 0; busy := false end—————————————————————-reader processes writer processes

do (forever) BEGIN do (forever) BEGIN

otherStuff() otherStuff()

rw.startread rw.startwriteread database write databaserw.endread rw.endwrite

END END

15

Page 16: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

CCR solution

var db: shared; rc: integer;

reader processes writer processes

region db begin rc := rc + 1 end; region db when rc = 0read database begin write dat abase endregion db begin rc := rc - 1 end;

Serializer solution

rw : serializervar readq, writeq: queue; rcrowd, wcrowd: crowd;

procedure readbeginenqueue(readq) until empty(wcrowd);joincrowd(rcrowd) then begin read database end;end

procedure writebeginenqueue(writeq) until (empty(wcrowd) and empty(rcrowd));joincrowd(wcrowd) then begin write database end;end

Path Expression solution

path 1:([read],write) end

16

Page 17: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Message Passing Synchronization

• Asynchronous: non-blocking send, blocking receive

• Synchronous: blocking send, blocking receive

Mutual exclusion using asyn. msg. passing

process Pi channel server process Pj

begin begin beginreceive(channel) create channel receive(channel)critical section send(channel) critical sectionsend(channel) manage channel send(channel)end end end

Mutual exclusion using syn. msg. passing

process Pi semaphore server process Pj

begin loop beginsend(sem,msg) receive(pid,msg) send(sem,msg)critical section send(pid,msg) critical sectionreceive(sem,msg) end receive(sem,msg)end end

17

Page 18: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Communicating Sequential Processes (CSP)

P : Q!exp, Q: P ?var, and guarded commandsProcess P executes Q!(x + y),. then expression x + y is evaluated and sent to process Q.Process Q executes P ?z,. then process Q sets variable z to the value received from process P

ADA rendezvous

task rw isentry startread;entry endread;entry startwrite;entry endwrite;

end

task body rw isrc: integer := 0;busy: boolean := false;

beginloop

selectwhen busy = false →

accept startread do rc := rc + 1 end;or

accept endread do rc := rc - 1 end;or

when rc = 0 and busy = false →

accept startwrite do busy = true end;or

accept endwrite do busy = false end;end loopend;

18

Page 19: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Concurrent Programming Languages

A taxonomy

synchronization and communication mechanisms

shared variable message passing

type

abstraction

control

structure

program

structure

monitor CCR Path

asynchronous

direct

process

naming

procedure

naming

indirect

port/mailbox

naming

synchronous

request / reply

(passive)

rendezvous

(active)

process

naming

procedure

naming

process

naming

procedure

naming

RPC

asynchronous

RPC

rendezvous

form of RPC

Concurrent

Pascal

Path

Pascal

PLITS Gypsy CSP ADA

Argus DP DP

Concurrent C

SR

SR

X-11

window

sockets

embedded

in many OS

noneDP

asymmetric symmetric

19

Page 20: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Coordination languages

• OCCAM: based on CSP process model, use PAR, ALT, and SEQ con-structors, use explict global links for communication.

• SR: based on resource (object) model, use synchronous CALL and asyn-chronous SEND and rendezvous IN, use capability for channel naming.

• LINDA: based on distributed data structure model, use tuples to repre-sent both process and object, use blocking IN and RD and non-blockingOUT for communication.

concurrent programming

language

concurrent programming

language

concurrent programming

paradigm

OCCAM

SR

LINDA

processes

resources

distributed

data structures

static

global channels

dynamic

capabilities

associative

tags

System Object model Channel naming

20

Page 21: CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMINGnemo/cop5615/chow/ch3.pdf · CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline • Thread implementations ... Q!exp, Q:

Distributed and Network Programming

Programming languages for loosely coupled systems:

ORCA

fork process-name(parameters) [on (processor-number)];

operation op(parameters)guard condition do statements;guard condition do statements;

invoke(object, operation, parameters)

t[1] = 6, A, 8t[6] = 0, B, 0t[8] = 0, C, 0

JAVA

• Well-defined standard interfaces for integrating software modules

• Capability of running software modules on any machine

• Infrastructure for coordinating and transporting software modules

Applet and system security

21