Why do we need concurrency? To utilise the processor Software Architecture Bertrand Meyer 10 2 10 1 human tape 10 0 10-1 floppy ETH Zurich, March-July 2007 10-2 CD 10-3 10 -4 Lecture 9: Concurrency & SCOOP 10-5 10-6 10-7 memory 10-8 (with material by Piotr Nienaltowski & 10-9 processor Volkan Arslan) Chair of Softw are Engineering Concurrent Programming Why do we need concurrency? Definition (Ben- A r i, 1982): Multiprogramming on a single computer Distributed programming across networks Multiple activities in one tool (e.g. mail client, Web browser, Programming notations and techniques for IDE) expressing potential parallelism and solving the At the hardware level: multicore architectures resulting synchronization and communication problems Provides an abstract setting to study parallelism without getting into implementation details Terminology Parallelism between CPU and I/ O devices CPU I/O Device Dijkstra (1968) A concurrent program is a collection of autonomous sequential processes, executing (logically) in parallel Initiate I/O Operation Process I/O Each process has a single thread of control Request Implementation can multiplex process execution in three ways: Signal Completion � Multiprogramming: single processor Interrupt I/O � Multiprocessing: several processors with shared memory Routine � Distributed Processing: several processors not sharing memory I/O Finished Continue with Outstanding Requests 1
Processes and Objects Processes and threads All operating systems provide processes Active objects — undertake spontaneous actions Each process executes in its own virtual machine (VM) to avoid interference from others Reactive objects — only perform actions when invoked Also in modern OSes: several threads within one VM. Like lighter versions of processes, but: Resources — reactive but can control order of actions � Unrestricted access to respective VM � Language & programmer must avoid interference Passive — reactive, but no control over order Language may define concurrency or leave it to the OS: � Ada, Java and C#, Eiffel with SCOOP, provide Protected resource — passive resource controller concurrency � C, C++ do not Server — active resource controller Process states Process Representation Coroutines Non-existing Non-existing Fork and Join Created Cobegin Explicit Process Declaration Initializing Terminated Executable Coroutine Flow Control Concurrent Programming Constructs Concurrent programming language mechanisms support: Coroutine A Coroutine B Coroutine C � Expressing concurrent execution through the notion of process (or/and threads) 6 1 2 4 5 � Process synchronization 3 6 � Inter-process communication resume A resume B resume C Processes may be 13 12 8 � Independent 7 9 � Cooperating 14 resume A resume B � Competing resume B 10 15 11 12 resume C 2
Fork and join Tasks and Ada (See UNIX/POSIX and programming languages such as Mesa.) The unit of concurrency in Ada is called a task function F return is ...; procedure P; ... Tasks properties: C:= fork F; � Explicitly declared: no fork/join, COBEGIN etc. ... J:= join C; ... � Declared at any program level; created implicitly on end P; Fork: designated routine starts executing concurrently with invoker entry to declaration scope or via allocator Join: invoker waits for completion of invoked routine � Communicate and synchronise via various mechanisms: After fork, P and F will be executing concurrently. At join, P will wait rendezvous (synchronised message passing), until F has finished (if not already done protected units ( monitor/conditional critical region), shared variables Cobegin Robot Arm example type Dimension is (Xplane, Yplane, Zplane); cobegin (or parbegin or par) is a structured way of denoting concurrent task type Control(Dim : Dimension); execution of instructions: C1 : Control(Xplane); cobegin C2 : Control(Yplane); S1; C3 : Control(Zplane); S2; S3; task body Control is . Position : Integer; -- absolute position . Setting : Integer; -- relative movement Sn begin Position := 0; -- rest position coend loop Terminates when all have terminated New_Setting (Dim, Setting); Position := Position + Setting; Example languages: Edison, occam2. Move_Arm (Dim, Position); end loop ; end Control; Explicit process Declaration Java threads The structure of a program can be made clearer if routines � Dynamic thread creation state whether they will be executed concurrently � Constructors allow arbitrary data as arguments � No master or guardian concept; garbage collection Note that this does not say when they will execute cleans up no longer accessible objects � Main program terminates when all user threads have task body Process is terminated begin � One thread can wait for another to terminate . . . through join end ; � isAlive allows a thread to determine if another has terminated Languages that support explicit process declaration may have explicit or implicit process/task creation 3
Synchronization and Communication Shared resource communication The correct behaviour of a concurrent program depends on synchronisation and communication type Coordinates is type Coordinates is between its processes record record X : Integer; X : Integer; Y : Integer; Y : Integer; Synchronisation: the satisfaction of constraints on end record ; end record ; Shared_Cordinate: Coordinates; interleaving of process actions (e.g. action by a process Shared_Cordinate: Coordinates; should only occur after action by another) task body Helicopter is task body Police_Car is task body Helicopter is task body Police_Car is Also: bring two processes simultaneously into Next: Coordinates; Next: Coordinates; begin begin predefined states begin loop begin loop Plot(Shared_Cordinates); loop Plot(Shared_Cordinates); loop Plot(Shared_Cordinates); Compute_New_Cordinates(Next); Compute_New_Cordinates(Next); end loop ; end loop ; Communication: the passing of information from one Shared_Cordinates := Next; Shared_Cordinates := Next; end ; Shared_Cordinates := Next; end ; end loop process to another end loop end ; end ; Synchronization and Communication Villain's escape route X = 0 X = 4 X = 3 X = 3 X = 5 X = 2 X = 2 X = 4 X = 1 X = 1 Concepts are linked since communication requires Y = 1 Y = 1 Y = 2 Y = 0 Y = 2 Y = 3 Y = 0 Y = 4 Y = 4 Y = 3 synchronisation, and synchronisation can be considered as content-less communication. 3 1 4 2 5 4 4 2 11 4 5 3 3 Data communication is usually based upon either shared variables or message passing . 1,1 1,1 1,1 2,2 2,2 2,2 3,3 3,3 Villain 3,3 4,4 4,4 4,4 5,5 5,5 4,5 6,6 6,6 ... ... Escapes! Police Car’s Villain's Escape Pursuit Route Route (seen by helicopter) Avoiding interference Shared Variable Communication Examples: busy waiting, semaphores and monitors The parts of a process that access shared variables must be executed indivisibly (atomically) with respect to each Unrestricted use of shared variables is unreliable and other unsafe due to multiple update problems Consider two processes updating a shared variable, x, with the assignment: x := x + 1 These parts are called critical sections � load the value of x into some register � increment the value in the register by 1 and The required protection is called mutual exclusion � store the value in the register back to x As the three operations are not indivisible , two processes simultaneously updating the variable could follow an interleaving that would produce an incorrect result 4
Recommend
More recommend