1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchell whose.

Slides:



Advertisements
Similar presentations
Symmetric Multiprocessors: Synchronization and Sequential Consistency.
Advertisements

Introduction to Concurrency
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CSCC69: Operating Systems
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency in Ada Programming Languages 1 Robert Dewar.
Concurrency in Ada What concurrency is all about Relation to operating systems Language facilities vs library packages POSIX threads Ada concurrency Real.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 Concurrency: Deadlock and Starvation Chapter 6.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Parallel Programming. Introduction Idea has been around since 1960’s –pseudo parallel systems on multiprogram-able computers True parallelism –Many processors.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Multithreading / Concurrency
Processes and threads.
Background on the need for Synchronization
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Concurrency: Mutual Exclusion and Synchronization
Operating Systems Lecture 6.
Multithreading.
Multithreading.
Shared Memory Programming
Process Description and Control
CS510 Operating System Foundations
Presentation transcript:

1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to John Mitchell whose slides this lecture is based on.

2 Concurrency, distributed computing, the Internet Traditional view: Let the OS deal with this => It is not a programming language issue! End of Lecture Wait-a-minute … Maybe “the traditional view” is getting out of date?

3 Languages with concurrency constructs Maybe the “traditional view” was always out of date? Simula Modula3 Occam Concurrent Pascal ADA Linda CML Facile Jo-Caml Java C# …

4 Possibilities for Concurrency Architecture: Uniprocessor with: -I/O channel - I/o processor - DMA Multiprogramming, multiple process system Programs Network of uniprocessors Distributed programming Multiple CPU’sParallel programming

5 The promise of concurrency Speed –If a task takes time t on one processor, shouldn’t it take time t/n on n processors? Availability –If one process is busy, another may be ready to help Distribution –Processors in different locations can collaborate to solve a problem or work together Humans do it so why can’t computers? –Vision, cognition appear to be highly parallel activities

6 Challenges Concurrent programs are harder to get right –Folklore: Need an order of magnitude speedup (or more) to be worth the effort Some problems are inherently sequential –Theory – circuit evaluation is P-complete –Practice – many problems need coordination and communication among sub-problems Specific issues –Communication – send or receive information –Synchronization – wait for another process to act –Atomicity – do not stop in the middle and leave a mess

7 Why is concurrent programming hard? Nondeterminism –Deterministic: two executions on the same input it always produce the same output –Nondeterministic: two executions on the same input may produce different output Why does this cause difficulty? –May be many possible executions of one system –Hard to think of all the possibilities –Hard to test program since some may occur infrequently

8 Concurrency and programming Languages Library view –All(most all) concurrency constructs are packaged as library functions/methods –FORTRAN, C, Lisp, CML, Java (except for a few keywords like synchronized) Language view –Syntactic manifestation in the language –Concurrent Pascal –Occam –ADA –Facile –Erlang –…

9 Library view in C System Calls - fork( ) - wait( ) - pipe( ) - write( ) - read( ) Examples

10 Process Creation Fork( ) NAME fork() – create a new process SYNOPSIS # include pid_t fork(void) RETURN VALUE success parent- child pid child- 0 failure

11 Fork() system call- example #include Main() { printf(“[%ld] parent process id: %ld\n”, getpid(), getppid()); fork(); printf(“\n[%ld] parent process id: %ld\n”, getpid(), getppid()); }

12 Fork() system call- example [17619] parent process id: [2372] parent process id: 17619

13 Fork()- program structure #include Main() { pid_t pid; if((pid = fork())>0){ /* parent */ } else if ((pid==0){ /*child*/ } else { /* cannot fork* } exit(0); }

14 Wait() system call Wait()- wait for the process whose pid reference is passed to finish executing SYNOPSIS #include pid_t wait(int *stat)loc) The unsigned decimal integer process ID for which to wait RETURN VALUE success- child pid failure- -1 and errno is set

15 Wait()- program structure #include #include #include #include Main(int argc, char* argv[]) { pid_t childPID; if((childPID = fork())==0){ /*child*/ } else { /* parent* wait(0); } exit(0); }

16 Pipe() system call Pipe()- to create a read-write pipe that may later be used to communicate with a process we’ll fork off. SYNOPSIS Int pipe(pfd) int pfd[2]; PARAMETER Pfd is an array of 2 integers, which that will be used to save the two file descriptors used to access the pipe RETURN VALUE: 0 – success; -1 – error.

17 Pipe() - structure /* first, define an array to store the two file descriptors*/ Int pipe[2]; /* now, create the pipe*/ int rc = pipe (pipes); if(rc = = -1) { /* pipe() failed*/ Perror(“pipe”); exit(1); } If the call to pipe() succeeded, a pipe will be created, pipes[0] will contain the number of its read file descriptor, and pipes[1] will contain the number of its write file descriptor.

18 Write() system call Write() – used to write data to a file or other object identified by a file descriptor. SYNOPSIS #include Size_t write(int fildes, const void * buf, size_t nbyte); PARAMETER fildes is the file descriptor, buf is the base address of area of memory that data is copied from, nbyte is the amount of data to copy RETURN VALUE The return value is the actual amount of data written, if this differs from nbyte then something has gone wrong

19 Read() system call Read() – read data from a file or other object identified by a file descriptor SYNOPSIS #include Size_t read(int fildes, void *buf, size_t nbyte); ARGUMENT fildes is the file descriptor, buf is the base address of the memory area into which the data is read, nbyte is the maximum amount of data to read. RETURN VALUE The actual amount of data read from the file. The pointer is incremented by the amount of data read.

20 Solaris 2 Synchronization Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing. Uses adaptive mutexes for efficiency when protecting data from short code segments. Uses condition variables and readers-writers locks when longer sections of code need access to data. Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock.

21 Windows 2000 Synchronization Uses interrupt masks to protect access to global resources on uniprocessor systems. Uses spinlocks on multiprocessor systems. Also provides dispatcher objects which may act as wither mutexes and semaphores. Dispatcher objects may also provide events. An event acts much like a condition variable.

22 Basic question Maybe the library approach is not such a good idea? How can programming languages make concurrent and distributed programming easier?

23 Language support for concurrency Make invariants and intentions more apparent (part of the interface) Good software engineering Allows the compiler much more freedom to choose different implementations Also helps other tools

24 What could languages provide? Abstract model of system –abstract machine => abstract system Example high-level constructs –Process as the value of an expression Pass processes to functions Create processes at the result of function call –Communication abstractions Synchronous communication Buffered asynchronous channels that preserve msg order –Mutual exclusion, atomicity primitives Most concurrent languages provide some form of locking Atomicity is more complicated, less commonly provided

25 Basic issue: conflict between processes Critical section –Two processes may access shared resource –Inconsistent behavior if two actions are interleaved –Allow only one process in critical section Deadlock –Process may hold some locks while awaiting others –Deadlock occurs when no process can proceed

26 Cobegin/coend Limited concurrency primitive Example x := 0; cobegin begin x := 1; x := x+1 end; begin x := 2; x := x+1 end; coend; print(x); execute sequential blocks in parallel x := 0 x := 2 x := 1 print(x) x := x+1 Atomicity at level of assignment statement

27 Mutual exclusion Sample action procedure sign_up(person) begin number := number + 1; list[number] := person; end; Problem with parallel execution cobegin sign_up(fred); sign_up(bill); end; bob fred bill fred

28 Locks and Waiting cobegin begin sign_up(fred); // critical section end; begin sign_up(bill); // critical section end; Need atomic operations to implement wait

29 Mutual exclusion primitives Atomic test-and-set –Instruction atomically reads and writes some location –Common hardware instruction –Combine with busy-waiting loop to implement mutex Semaphore –Avoid busy-waiting loop –Keep queue of waiting processes –Scheduler has access to semaphore; process sleeps –Disable interrupts during semaphore operations OK since operations are short

30 Monitor Brinch-Hansen, Dahl, Dijkstra, Hoare Synchronized access to private data. Combines: –private data –set of procedures (methods) –synchronization policy At most one process may execute a monitor procedure at a time; this process is said to be in the monitor. If one process is in the monitor, any other process that calls a monitor procedure will be delayed. Modern terminology: synchronized object

31 OCCAM Program consists of processes and channels Process is code containing channel operations Channel is a data object All synchronization is via channels Formal foundation based on CSP

32 Channel Operations in OCCAM Read data item D from channel C –D ? C Write data item Q to channel C –Q ! C If reader accesses channel first, wait for writer, and then both proceed after transfer. If writer accesses channel first, wait for reader, and both proceed after transfer.

33 Tasking in Ada Declare a task type The specification gives the entries –task type T is entry Put (data : in Integer); entry Get (result : out Integer); end T; The entries are used to access the task

34 Declaring Task Body Task body gives actual code of task task body T is x : integer; -- local per thread declaration begin … accept Put (M : Integer) do … end Put; … end T;

35 Creating an Instance of a Task Declare a single task X : T; or an array of tasks P : array (1.. 50) of T; or a dynamically allocated task type AT is access T; P : AT; … P := new T;

36 Task execution Each task executes independently, until –an accept call wait for someone to call entry, then proceed with rendezvous code, then both tasks go on their way –an entry call wait for addressed task to reach corresponding accept statement, then proceed with rendezvous, then both tasks go on their way.

37 More on the Rendezvous During the Rendezvous, only the called task executes, and data can be safely exchanged via the entry parameters If accept does a simple assignment, we have the equivalent of a simple CSP channel operation, but there is no restriction on what can be done within a rendezvous

38 Termination of Tasks A task terminates when it reaches the end of the begin- end code of its body. Tasks may either be very static (create at start of execution and never terminate) Or very dynamic, e.g. create a new task for each new radar trace in a radar system.

39 The Delay Statement Delay statements temporarily pause a task –Delay xyz where xyz is an expression of type duration causes execution of the thread to be delayed for (at least) the given amount of time –Delay until tim where tim is an expression of type time, causes execution of the thread to be delayed until (at the earliest) the given tim

40 Selective Accept Select statement allows a choice of actions select entry1 (…) do.. end; or when bla entry2 (…); or delay...ddd...; end select; –Take whichever open entry arrives first, or if none arrives by end of delay, do …ddd…stmts.

41 Timed Entry Call Timed Entry call allows timeout to be set select entry-call-statement or delay xxx; … end select –We try to do the entry call, but if the task won’t accept in xxx time, then do the delay stmts.

42 Conditional Entry Call Make a call only if it will be accepted select entry-call.. else statements end select; –If entry call is accepted immediately, fine, otherwise execute the else statements.

43 Task Abort Unconditionally terminate a task abort taskname; –task is immediately terminated –(it is allowed to do finalization actions) –but whatever it was doing remains incomplete –code that can be aborted must be careful to leave things in a coherent state if that is important!

44 Java Concurrency Threads –Create process by creating thread object Communication –shared variables –method calls Mutual exclusion and synchronization –Every object has a lock (inherited from class Object) synchronized methods and blocks –Synchronization operations (inherited from class Object) wait : pause current thread until another thread calls notify notify : wake up waiting threads

45 Java Threads Thread –Set of instructions to be executed one at a time, in a specified order Java thread objects –Object of class Thread –Methods inherited from Thread: start : method called to spawn a new thread of control; causes VM to call run method suspend : freeze execution interrupt : freeze execution and throw exception to thread stop : forcibly cause thread to halt

46 Example subclass of Thread class PrintMany extends Thread { private String msg; public PrintMany (String m) {msg = m;} public void run() { try { for (;;){ System.out.print(msg + “ “); sleep(10); } } catch (InterruptedException e) { return; } } (inherits start from Thread)

47 Interaction between threads Shared variables –Two threads may assign/read the same variable –Programmer responsibility Avoid race conditions by explicit synchronization!! Method calls –Two threads may call methods on the same object Synchronization primitives –Each object has internal lock, inherited from Object –Synchronization primitives based on object locking

48 Synchronization example Objects may have synchronized methods Can be used for mutual exclusion –Two threads may share an object. –If one calls a synchronized method, this locks object. –If the other calls a synchronized method on same object, this thread blocks until object is unlocked.

49 Synchronized methods Marked by keyword public synchronized void commitTransaction(…) {…} Provides mutual exclusion –At most one synchronized method can be active –Unsynchronized methods can still be called Programmer must be careful Not part of method signature –sync method equivalent to unsync method with body consisting of a synchronized block –subclass may replace a synchronized method with unsynchronized method

50 Join, another form of synchronization Wait for thread to terminate class Future extends Thread { private int result; public void run() { result = f(…); } public int getResult() { return result;} } … Future t = new future; t.start() // start new thread … t.join(); x = t.getResult(); // wait and get result

51 Aspects of Java Threads Portable since part of language –Easier to use in basic libraries than C system calls –Example: garbage collector is separate thread General difficulty combining serial/concur code –Serial to concurrent Code for serial execution may not work in concurrent sys –Concurrent to serial Code with synchronization may be inefficient in serial programs (10-20% unnecessary overhead) Abstract memory model –Shared variables can be problematic on some implementations

52 Concurrent garbage collector How much concurrency? –Need to stop thread while mark and sweep –Other GC: may not need to stop all program threads Problem –Program thread may change objects during collection Solution –Prevent read/write to memory area –Details are subtle; generational, copying GC Modern GC distinguishes short-lived from long-lived objects Copying allows read to old area if writes are blocked … Relatively efficient methods for read barrier, write barrier

53 Java memory model Main ideas –Threads have local memory (cache) –Threads fill/flush from main memory Interaction restricted by constraints on actions –Use/assign are local thread memory actions –Load/store fill or flush local memory –Read/write are main memory actions

54 Thread Memory Hierarchy ThreadCache Shared Memory Cache code use/assign load/store read/write

55 Example Program r.i = r.i+1 load r.i store r.i use r.i assign r.i write r.i read r.i The value of field i of object i is –read from main memory –loaded into the local cache of the thread –used in the addition r.i+1 Similar steps to place the value of r.i in shared memory

56 Java Memory Model [Java Lang Spec] Example constraints on use, assign, load, store: –use and assign actions by thread must occur in the order specified by the program –Thread is not permitted to lose its most recent assign –Thread is not permitted to write data from its working memory to main memory for no reason –New thread starts with an empty working memory –New variable created only in main memory, not thread working memory “Provided that all the constraints are obeyed, a load or store action may be issued at any time by any thread on any variable, at the whim of the implementation.”

57 Access to Main Memory Constraints on load, store, read,write –For every load, must be a preceding read action –For every store, must be a following write action –Actions on master copy of a variable are performed by the main memory in order requested by thread

58 Prescient stores Under certain conditions … –Store actions (from cache to shared memory) may occur earlier than you would otherwise expect –Purpose: Allow optimizations that make properly synchronized programs run faster These optimizations may allow out-of-order operations for programs that are not properly synchronized Details are complicated. Main point: there’s more to designing a good memory model than you might think!

59 Criticism [Pugh] Model is hard to interpret and poorly understood Constraints –prohibit common compiler optimizations –expensive to implement on existing hardware Not commonly followed –Java Programs Sun Java Development Kit not guaranteed valid by the existing Java memory model –Implementations not compliant Sun Classic Wintel JVM, Sun Hotspot Wintel JVM, IBM 1.1.7b Wintel JVM, Sun production Sparc Solaris JVM, Microsoft JVM

60 Thread States born ready running blocked dead sleeping waiting start() sleep() yield() wait() notify() timeout terminate I/O started I/O completed

61 What the Language Spec. Says From The Java Language Specification Vague enough for you? Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.

62 Multiple threads at same priority? Language gives implementer freedom Calling yield() suspends current thread to allow other at same priority to run … maybe Solaris implementation runs threads until they stop themselves (wait(), yield(), etc.) Windows implementation timeslices

63 Concurrent ML Threads –New type of entity Communication –Synchronous channels Synchronization –Channels –Events Atomicity –No specific language support

64 Threads Thread creation –spawn : (unit  unit)  thread_id Example code CIO.print "begin parent\n"; spawn (fn () => (CIO.print "child 1\n";)); spawn (fn () => (CIO.print "child 2\n";)); CIO.print "end parent\n“ Result end parent child 2 child 1 begin parent

65 Channels Channel creation –channel : unit  ‘a chan Communication –recv : ‘a chan  ‘a –send : ( ‘a chan * ‘a )  unit Example ch = channel(); spawn (fn()=> … … send(ch,0); … …); spawn (fn()=> … … recv ch; … …); Result send/recv

66 CML programming Functions –Can write functions : channels  threads –Build concurrent system by declaring channels and “wiring together” sets of threads Events –Delayed action that can be used for synchronization –Powerful concept for concurrent programming Sample Application –eXene – concurrent uniprocessor window system

67 CML from continuations Continuation primitives –callcc : ('a cont  'a)  'a Call function argument with current continuation –throw : 'a cont -> 'a -> 'b –Curried function to invoke continuation with arg Example fun f(x,k) = throw k(x+3); fun g(y,k) = f(y+2,k) + 10; fun h(z) = z + callcc(fn k => g(z+1,k)); h(1);

68 A CML implementation (simplified) Use queues with side-effecting functions datatype 'a queue = Q of {front: 'a list ref, rear: 'a list ref} fun queueIns (Q(…)) = (* insert into queue *) fun queueRem (Q(…)) = (* remove from queue *) And continuations val enqueue = queueIns rdyQ fun dispatch () = throw (queueRem rdyQ) () fun spawn f = callcc (fn parent_k => ( enqueue parent_k; f (); dispatch())) Source: Appel, Reppy

69 Facile Programming Language Integration of Multiple Paradigms –Functions –Types/complex data types –Concurrency –Distribution/soft real-time –Dynamic connectivity Implemented as extension to SML Syntax for concurrency similar to CML

70

71 Facile implementation Pre-emptive scheduler implemented at the lowest level –Exploiting CPS translation => state characterised by the set of registers Garbage collector used for linearizing data structures Lambda level code used as intermediate language when shipping data (including code) in heterogeneous networks Native representation is shipped when possible –i.e. same architecture and within same trust domain Possibility to mix between interpretation or JIT depending on usage

72 Conclusion Concurrency may be an order of magnitude more difficult to handle Programming language support for concurrency may help make the task easier Which concurrency constructs to add to the language is still a very active research area