1 Chapter 8 Channels. 2 Concurrent Programming Constructs So far we have seen contructs based on shared memory concept (shared directly – buffer - or.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

COMMUNICATING SEQUENTIAL PROCESSES C. A. R. Hoare The Queen’s University Belfast, North Ireland.
Parallel Processing & Parallel Algorithm May 8, 2003 B4 Yuuki Horita.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
1 On the Duality of Operating System Structures by Hugh C. Lauer, Xerox Corporation and Roger M. Needham, Cambridge University Presented by Scott Fletcher.
VBA Modules, Functions, Variables, and Constants
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Concurrency in Ada What concurrency is all about Relation to operating systems Language facilities vs library packages POSIX threads Ada concurrency Real.
Program Design and Development
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Asynchronous Message Passing EE 524/CS 561 Wanliang Ma 03/08/2000.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 Chapter 9 Spaces with LINDA. 2 Linda Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
CS5204 – Operating Systems 1 Communicating Sequential Processes (CSP)
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 12 Concurrency can occur at four levels: 1. Machine instruction level 2. High-level language.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
1 Chapter 10 Distributed Algorithms. 2 Chapter Content This and the next two chapters present algorithms designed for loosely-connected distributed systems.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
ICS 313: Programming Language Theory Chapter 13: Concurrency.
CS212: OPERATING SYSTEM Lecture 2: Process 1. Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
Synchronization Methods in Message Passing Model.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12B - Synchronization and semaphores Programming Language.
Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
55:032 - Intro. to Digital DesignPage 1 VHDL and Processes Defining Sequential Circuit Behavior.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
1 Chapter 11 Global Properties (Distributed Termination)
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Channels. Models for Communications Synchronous communications – E.g. Telephone call Asynchronous communications – E.g. .
Semaphores Chapter 6. Semaphores are a simple, but successful and widely used, construct.
Interprocess Communication Race Conditions
Introduction To Flowcharting
Chapter 5: Process Synchronization (Con’t)
Monitors Chapter 7.
Operating System Concepts
Channels.
Semaphores Chapter 6.
Monitors Chapter 7.
Subject : T0152 – Programming Language Concept
Monitors Chapter 7.
Chapter 6 Synchronization Principles
Channels.
Channels.
Synchronization and semaphores
Presentation transcript:

1 Chapter 8 Channels

2 Concurrent Programming Constructs So far we have seen contructs based on shared memory concept (shared directly – buffer - or through a shared service of an OS – semaphores and monitors) So far we have seen contructs based on shared memory concept (shared directly – buffer - or through a shared service of an OS – semaphores and monitors) Now, we will see constructs based on communications (processes send and receive messages) which may be Now, we will see constructs based on communications (processes send and receive messages) which may be Synchronous in which the exchange of message is an atomic operation requiring participation of both the sender and the receiver (receiver is blocked is message is not send yet or sender is blocked if receiver is not ready yet Synchronous in which the exchange of message is an atomic operation requiring participation of both the sender and the receiver (receiver is blocked is message is not send yet or sender is blocked if receiver is not ready yet Asynchronous in which messages are saved somewhere and sender nor receiver is blocked Asynchronous in which messages are saved somewhere and sender nor receiver is blocked This chapter is on synchronous communication methods (channels and ADA rendezvous) This chapter is on synchronous communication methods (channels and ADA rendezvous)

3 Channels A channel connects a sending process with a receiving process A channel connects a sending process with a receiving process Channels are typed, meaning that they must be declared Channels are typed, meaning that they must be declared The exchange of messages is synchronous, that is sender and receiver have to wait for each other The exchange of messages is synchronous, that is sender and receiver have to wait for each other Channels in operating systems are called pipes Channels in operating systems are called pipes

4 Producer - Consumer Using a Channel At p2 and q1 both processes must wait for each other At p2 and q1 both processes must wait for each other

5 Conway’s Problem inC is the input channel for a sequence of characters send by some process inC is the input channel for a sequence of characters send by some process outC is the output channel for a sequence of transformed characters to be received by some other process outC is the output channel for a sequence of transformed characters to be received by some other process Transformation Transformation 2 ≤ n ≤ 9 occurrences of the same character is replaced by n and the character (compress process) 2 ≤ n ≤ 9 occurrences of the same character is replaced by n and the character (compress process) A newline character is appended following every K’th character (output process) A newline character is appended following every K’th character (output process) Pipe is the channel connecting compress to output Pipe is the channel connecting compress to output

6 Conway’s Problem

7 Transputer and OCCAM Language The transputer was a microprocessor developed by Inmos Corporation to support the OCCAM programming model. The transputer was a microprocessor developed by Inmos Corporation to support the OCCAM programming model. The IMS 800 transputer contained a fast 32-bit processor, a floating point processor, a small amount of on-chip memory (4K) and four bi-directional high-speed serial communication links. The IMS 800 transputer contained a fast 32-bit processor, a floating point processor, a small amount of on-chip memory (4K) and four bi-directional high-speed serial communication links. Each of these communication links implement a pair of occam channels (for example, north, east, south and west). Each of these communication links implement a pair of occam channels (for example, north, east, south and west). OCCAM was a parallel computation language designed for the transputer architectures OCCAM was a parallel computation language designed for the transputer architectures

8 IMS 800 Transputer Architecture x External Memory Interface

9 A Matrix Multiplication Example To multiply two matrices A * X, a process is allocated for each A[i,j]. To multiply two matrices A * X, a process is allocated for each A[i,j]. The elements of X are fed from the top (an input channel) The elements of X are fed from the top (an input channel) The partial sums are passed from right to left until they emerge from the array of processes with the final result The partial sums are passed from right to left until they emerge from the array of processes with the final result At the right, a set of processes feed in zeros to initialize the sums At the right, a set of processes feed in zeros to initialize the sums At the bottom, sink processes absorb the elements of X as they leave the bottom row. This is necessary since matrix element processes are identical and need not to know if they are at a particular edge of the array At the bottom, sink processes absorb the elements of X as they leave the bottom row. This is necessary since matrix element processes are identical and need not to know if they are at a particular edge of the array Matrices: Matrices: x =

10 Computation of One Element x 2 = 7x2+8x2+9x0 = = x 2 = 7x2+8x2+9x0 = = 30 0

11 Processor Array for Multiplication

12 Multiplier Processes (one for each A[i,j].. 9 in total)

13 Occam Syntax Every statement in occam is a process. It is up to the programmer to indicate whether the statements will be combined in sequence or parallel. For example, Every statement in occam is a process. It is up to the programmer to indicate whether the statements will be combined in sequence or parallel. For example,SEQstatement_1statement_2statement_3PARstatement_1statement_2statement_3 Indentation is used instead of punctuation (';') to show program structure. Input and output statements have the form Indentation is used instead of punctuation (';') to show program structure. Input and output statements have the form channel ? variable channel ! value respectively.

14 Multiplier Process in Occam Syntax WHILE TRUE SEQ north ? x south ! x east ? sum sum := sum + a * x west ! sum

15 Multiplier Process Made More Paralel north ? x -- read first x north ? x -- read first x WHILE TRUE SEQPAR south ! x east ? sum temp := a * x PAR west ! sum + temp north ? X After reading the x value, transferring value to south, reading the partial sum from east and multiplication of a*x can be done in parallel. Similarly, transferring new sum and reading next x value can be done in parallel after waiting for the first set of parallel computations. After reading the x value, transferring value to south, reading the partial sum from east and multiplication of a*x can be done in parallel. Similarly, transferring new sum and reading next x value can be done in parallel after waiting for the first set of parallel computations.

16 The Dining Philosophers with Channels A fork is a process which is connected by a channel to the philosopher on its left and right A fork is a process which is connected by a channel to the philosopher on its left and right Before eating a philosopher waits for an input from the left and right channels (value is not important – but both forks must be available) in p2 and p3 Before eating a philosopher waits for an input from the left and right channels (value is not important – but both forks must be available) in p2 and p3 A fork process sends a “true” value down the channel when the fork is free (q1) and then waits for the fork to become free again after eating (q2) A fork process sends a “true” value down the channel when the fork is free (q1) and then waits for the fork to become free again after eating (q2) The philosopher releases the forks after eating in steps p5 and p6 The philosopher releases the forks after eating in steps p5 and p6

17 Rendezvous in ADA Communication in ADA is synchronous and unbuffered Communication in ADA is synchronous and unbuffered Two processes (tasks in ADA) must meet in a rendezvous in order to communicate. This means one of the two processes must wait for the other to come to the rendezvous point Two processes (tasks in ADA) must meet in a rendezvous in order to communicate. This means one of the two processes must wait for the other to come to the rendezvous point One of the two processes is called the calling task and the other as an accepting task One of the two processes is called the calling task and the other as an accepting task The calling task must know the identity of the accepting task and the name of the rendezvous location called the entry The calling task must know the identity of the accepting task and the name of the rendezvous location called the entry The accepting task does not know the identity of the calling task The accepting task does not know the identity of the calling task

18 ADA Tasks An ADA task has two sections: An ADA task has two sections: A specification (declaration) section contains the declarations of the entries (their names) A specification (declaration) section contains the declarations of the entries (their names) The body (code) section contains the entry points and the code to be executed in the rendezvous The body (code) section contains the entry points and the code to be executed in the rendezvous

19 Comparison of Communication Methods ADA Way Other Methods Consumer Producer Buffer Process Consumer Producer Buffer Data Structure

20 Producer-Consumer With a Degenerate Bounded Buffer (Task Specification) task buffer is entry append(i: in integer); entrytake(i: out integer); end buffer; The above task specification declares two entry points (append, take) for the accepting task The above task specification declares two entry points (append, take) for the accepting task

21 Producer-Consumer With a Degenerate Bounded Buffer (Task Body) task body buffer is buf:array(0..n-1) of integer; in_ptr, out_ptr:integer:= 0; count:integer:= 0; beginloop accept append(i: in integer) do buf(in_ptr) := i; end append; count:= count+1; in_ptr := (in_ptr+1) mod N; accept take(i : out integer) do i:= buf(out_ptr); end take; count:= count-1; out_ptr:= (out_ptr+1) mod N; end loop; end buffer;

22 Producer and Consumer Processes procedure producer; var i:integer; beginrepeatproduce(i);buffer.append(i); until false end; procedure consumer; var i:integer; beginrepeatbuffer.take(i);consume(i); until false end;

23 Comments Producer and consumer processes interact with the buffer task using the entry points buffer.append(i) and buffer.take(i) Producer and consumer processes interact with the buffer task using the entry points buffer.append(i) and buffer.take(i) Producer, consumer and buffer tasks execute concurrently. Sometime later, one of the processes, producer or consumer, try to make a rendezvous with the buffer task by calling the appropriate entry point. The process then stops and waits until the rendezvous is accomplished Producer, consumer and buffer tasks execute concurrently. Sometime later, one of the processes, producer or consumer, try to make a rendezvous with the buffer task by calling the appropriate entry point. The process then stops and waits until the rendezvous is accomplished

24 Comments (Cont.) The buffer process executes until execution reaches one of the accept statements (append or take). Let us assume that it reaches the accept append. This entry point is for the producer to pass a value. The buffer process executes until execution reaches one of the accept statements (append or take). Let us assume that it reaches the accept append. This entry point is for the producer to pass a value. If the producer had called buffer.append(i) before, the buffer task executes its accept append body If the producer had called buffer.append(i) before, the buffer task executes its accept append body If the producer had not called buffer.append(i), the buffer task stops and waits for the rendezvous If the producer had not called buffer.append(i), the buffer task stops and waits for the rendezvous When the accept body execution is finished, the value from the producer is stored in the buffer task and both processes (consumer and buffer) start to execute concurrently When the accept body execution is finished, the value from the producer is stored in the buffer task and both processes (consumer and buffer) start to execute concurrently

25 Comments (Cont.) In the buffer process, the task makes two rendezvous. Note that, buffer process works in a synchronized fashion. Producer produces a value, puts it in buffer by calling buffer.append(i). The next append rendezvous happens after a take rendezvous. In the buffer process, the task makes two rendezvous. Note that, buffer process works in a synchronized fashion. Producer produces a value, puts it in buffer by calling buffer.append(i). The next append rendezvous happens after a take rendezvous. Other producer processes may call buffer.append(i) causing them to be queued in a FIFO manner. Similarly, consumer processes are queued up at take entries assuming that there are several producers and consumers active at the same time Other producer processes may call buffer.append(i) causing them to be queued in a FIFO manner. Similarly, consumer processes are queued up at take entries assuming that there are several producers and consumers active at the same time

26 The Select Statement The previous solution is a degenerate because it has no provisions for refusing a rendezvous with a full or empty buffer. The previous solution is a degenerate because it has no provisions for refusing a rendezvous with a full or empty buffer. Furthermore, there is a strict alternation between producing and consuming Furthermore, there is a strict alternation between producing and consuming The select statement allows a task to select an entry call amoung several alternatives (like a case statement in Pascal) and also conditionally accept an entry call The select statement allows a task to select an entry call amoung several alternatives (like a case statement in Pascal) and also conditionally accept an entry call

27 Task Body with a Select Statement task body buffer is buf:array(0..n-1) of integer; in_ptr, out_ptr:integer:= 0; count:integer:= 0; beginloop select select when count when count accept append(i: in integer) do buf(in_ptr) := i; buf(in_ptr) := i; end append; count:= count+1; in_ptr := (in_ptr+1) mod N; or when count > 0 => accept take(i : out integer) do i:= buf(out_ptr); i:= buf(out_ptr); end take; count:= count-1; out_ptr:= (out_ptr+1) mod N; end select; end select; end loop; end buffer;

28 Comments The boolean expressions prefixed to the accept statements are called quards. If the expression is true, the alternative is an open alternative permitting a rendezvous else it is a closed alternative denying a rendezvous The boolean expressions prefixed to the accept statements are called quards. If the expression is true, the alternative is an open alternative permitting a rendezvous else it is a closed alternative denying a rendezvous When the buffer is empty (count = 0), the take alternative is closed. When the buffer is full (count = N), only the take alternative is open When the buffer is empty (count = 0), the take alternative is closed. When the buffer is full (count = N), only the take alternative is open When “0 < count < N”, buffer task will wait for the first caller that calls an entry. If there are calling tasks waiting in both entry queues, the task is chosen according to an implementation algorithm (first open alternative for example). In the bounded buffer case, first open alternative is a good choice to fill an empty buffer or empty a full buffer When “0 < count < N”, buffer task will wait for the first caller that calls an entry. If there are calling tasks waiting in both entry queues, the task is chosen according to an implementation algorithm (first open alternative for example). In the bounded buffer case, first open alternative is a good choice to fill an empty buffer or empty a full buffer

29 General Form of a Select Statement Arbitrary number of quarded accept statements separated with or’s Arbitrary number of quarded accept statements separated with or’s The quard may be missing implying “when true =>” The quard may be missing implying “when true =>” The last alternative may be The last alternative may be Else followed by a sequence of statements Else followed by a sequence of statements Delay T followed by a sequence of statements Delay T followed by a sequence of statements Terminate Terminate

30 Semantics The quards are evaluated to determine open alternatives. There must be at least one open alternative else it is a fatal error. The quards are not re-evaluated during the execution of the select statement The quards are evaluated to determine open alternatives. There must be at least one open alternative else it is a fatal error. The quards are not re-evaluated during the execution of the select statement If all open alternative queues are empty, the accepting task is suspended until a caller asks for a rendezvous If all open alternative queues are empty, the accepting task is suspended until a caller asks for a rendezvous If open alternative queues are not empty, the first process from one of the alternative queue is chosen (depending on the implementation algorithm) If open alternative queues are not empty, the first process from one of the alternative queue is chosen (depending on the implementation algorithm)

31 Programming with Rendezvous A simple rendezvous is a remote procedure call. The calling task calls a procedure which belongs to another task, namely the accepting task A simple rendezvous is a remote procedure call. The calling task calls a procedure which belongs to another task, namely the accepting task Producer Buffer.append(i) Buffer Accept append End append; Producer Buffer.append(i)

32 Emulation of Binary Semaphores in ADA Null accept bodies may be used for synchronization of tasks. As an example, consider the following emulation of binary semaphores in ADA Null accept bodies may be used for synchronization of tasks. As an example, consider the following emulation of binary semaphores in ADA Task body semaphore is Beginloop accept wait; accept signal; end loop; End semaphore;

33 Semaphore Example Continued.. Task body producer is Beginloopproduce;semaphore.wait;put-in-buffer;semaphore.signal; end loop; End producer; Task body consumer is Beginloopsemaphore.wait;get-from-buffer;semaphore.signal;consume; end loop; End consumer;