JCSP Tutorial & Homework Hints

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

COMMUNICATING SEQUENTIAL PROCESSES C. A. R. Hoare The Queen’s University Belfast, North Ireland.
Programming in Occam-pi: A Tutorial By: Zain-ul-Abdin
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
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.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Concurrency CS 510: Programming Languages David Walker.
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.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Concurrency: Deadlock and Starvation Chapter 6.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research.
Real-Time Software Design Yonsei University 2 nd Semester, 2014 Sanghyun Park.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Distributed and Parallel Processing George Wells.
Segments Introduction: slides minutes
Jim Fawcett CSE 691 – Software Modeling and Analysis Fall 2000
Process Synchronization
Background on the need for Synchronization
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2002
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Concurrency: Mutual Exclusion and Synchronization
Multithreading Chapter 23.
Monitors Chapter 7.
Distributed Mutual Exclusion
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Outline Distributed Mutual Exclusion Introduction Performance measures
Semaphores Chapter 6.
Monitors Chapter 7.
Concurrency: Mutual Exclusion and Process Synchronization
Monitors Chapter 7.
Chapter 6 Synchronization Principles
Chapter 6 – Distributed Processing and File Systems
CMSC 202 Threads.
Presentation transcript:

JCSP Tutorial & Homework Hints COP 6614, Fall 2005

JCSP Tutorial The JCSP Library JCSP Processes Channels & Interfaces “Alting” Channels & Event Selection The Parallel Construct Putting It Together: A Simple Example

The JCSP Library Developed by Peter Welch and Paul Austin Download Here: http://www.cs.kent.ac.uk/projects/ofa/jcsp/ Include the JCSP library in your project with import jcsp.lang.*

JCSP Processes The process is the basic construct in JCSP Simply a class that implements the CSProcess interface. Ex: public class MyProcess implements CSProcess { public void run(); }

Channels & Interfaces (1) Processes are “wired” together using channels. ChannelInput interface Interface for reading from a channel Consists of single method called read If called on an object, it will block until an object is actually written on the channel by a process at the other end of the channel. ChannelOutput interface Interface for writing from a channel Consists of a single method called write(Object o). A process that calls this method on an object will block until the object is accepted by the channel.

Channels & Interfaces (2) Types of channel classes One2OneChannel  implements a "single-writer-single-reader" type channel One2AnyChannel implements a "single-writer-multiple-readers" object channel. (Note: This is not a broadcast mechanism in that the multiple readers actually compete with each other for reading from the channel; only one reader can use the channel along with the writer at any given time.) Any2OneChannel  implements a "multiple-writers-single-reader" object channel. As in the above case, writing processes compete with each other to use the channel. Only the reader and one of the multiple writers can actually use the channel at any given time Any2AnyChannel implements a "multiple-writers-multiple-readers" object channel. Reading processes compete with each other to use the channel, as do writing processes. Only one reader and one writer can actually use the channel at any one time.

Channels & Interfaces (3) Channels are non buffering by default, but JCSP supports buffering. Buffering is delegated to another class, which must implement an interface called ChannelDataStore. JCSP provides multiple built-in implementations for this interface, including the following: ZeroBuffer – default non buffering channel Buffer – provides a blocking FIFO buffered semantics for the channel with which it is associated. InfiniteBuffer – provides a FIFO semantics, except that only readers can be blocked if the buffer is empty. Writers are never blocked because the buffer capacity can be expanded indefinitely, or at least until the limit imposed by the underlying memory system is reached.

“Alting” Channels & Event Selection A channel guard in JCSP can be of the following types: AltingChannelInput / AltingChannelInputInt, which are ready whenever object/integer data (respectively) is pending in the corresponding channel The Alternative class provides methods for event-selection The method select() of the Alternative class corresponds to an arbitrary selection strategy. The method priSelect() corresponds to the highest priority strategy. The method fairSelect is fair in choosing between the more-than-one ready guards

The Parallel Construct Takes an array of individual CSProcess instances and runs them “in parallel”. A run of a Parallel process terminates when, and only when, all its component processes terminate. A mechanism for composing multiple individual processes together, using channels as "wires" to connect them. Example: new Parallel ( new CSProcess[] { new SendEvenIntsProcess (chan), new ReadEvenIntsProcess (chan) } ).run ();

Putting It All Together: A Simple Example (1) Consider a simple process that “produces” the integers from 1 to 100 and another process which “consumes” them. Need 3 Processes Producer Process Consumer Process Driver Process to instantiate and start the producer and consumer processes. Producer and Consumer are “wired” together using ChannelInput and ChannelOutput interfaces.

Putting It All Together: A Simple Example (2) Int Producer Process Int Consumer Process Channel Channel Input Channel Output

Putting It All Together: A Simple Example (3) Producer process to generate even integers between 1 and 100 import jcsp.lang.*; public class SendEvenIntsProcess implements CSProcess { private ChannelOutput out; public SendEvenIntsProcess(ChannelOutput out) this.out = out; } public void run() for (int i = 2; i <= 100; i = i + 2) out.write (new Integer (i));

Putting It All Together: A Simple Example (4) Consumer process to read even integers between 1 and 100 import jcsp.lang.*; public class ReadEvenIntsProcess implements CSProcess { private ChannelInput in; public ReadEvenIntsProcess(ChannelInput in) this.in = in; } public void run() while (true) Integer d = (Integer)in.read(); System.out.println("Read: " + d.intValue());

Putting It All Together: A Simple Example (5) The “Driver” Program import jcsp.lang.*; public class DriverProgram { public static void main(String[ ] args) One2OneChannel chan = new One2OneChannel(); new Parallel ( new CSProcess[ ] new SendEvenIntsProcess (chan), new ReadEvenIntsProcess (chan) } ).run ();

Homework Hints (problem 1) Producer / Consumer The Producer process takes in the number of items and the speed at which it will produce an item as parameters. The Buffer process implements a circular queue of size 20. Once the Buffer process detects that the queue has been emptied, it will send a signal to the two consumers to indicate that the buffer is empty. The two Consumers will run at different speeds as defined by a parameter passed to it . The two consumers should then terminate normally when the Buffer process informs that there are no more items to consume.

Homework Hints (problem 1) Producer / Consumer 4 Processes 1 driver process (i.e. “main”) Producer process Consumer process Buffer Process

Homework Hints (problem 1) Producer / Consumer Consumer1 receives items from the buffer One2OneChannelInt chanConToBuff1 One2OneChannelInt chanConFromBuff1 Define Channels NOTE: Buffer / Consumer channels are Bidirectional. Consumers send request To buffer when wanting ton consume. Buffer will send item to consumer if requesting & Items are available in the buffer. Producer sends items (ints) to the buffer: One2OneChannelInt chanProdBuff Consumer2 receives items from the buffer One2OneChannelInt chanConToBuff2 One2OneChannelInt chanConFromBuff2 Note: Buffer has potential to receive multiple events (receiving an item from a consumer and message that data is available from the producer. Need event selection (i.e. Alting Channel).

Homework Hints (problem 1) Producer / Consumer Producer Process public class Producer implements CSProcess public Producer(int numItems, int interval, One2OneChannelInt toBuffer) Create an int and send it to the buffer using toBuffer.write(i); Consumer Process public class Consumer implements CSProcess public Consumer(String str, int interval, ChannelOutputInt toBuffer, One2OneChannelInt fromBuffer) signal the buffer we are ready to consume using toBuffer.write(MORE); read the data from the channel int item = fromBuffer.read();

Homework Hints (problem 1) Producer / Consumer Driver Process Instantiate channels One2OneChannelInt ProdBuff = new One2OneChannelInt(); One2OneChannelInt ConBuff1 = new One2OneChannelInt(); One2OneChannelInt ConBuff2 = new One2OneChannelInt(); Instantiate producer, consumer, buffer processes Example: Consumer consumer1 = new Consumer(100, ConToBuff1,ConFromBuff1); Start processes running in parallel Example: new Parallel ( new CSProcess [ ] { buffer,producer,consumer1, consumer2 } ).run();

Homework Hints (problem 2) Optimal Algorithm Ricart-Agrawal Algorithm: Implement the Optimal Algorithm for Mutual Exclusion for Computer Networks as given by Ricart-Agrawal (the paper was presented in class). The link for the paper can be found in the reading list. The number of nodes in the network (N) will be passed as parameter. Note: Use message passing to resolve all synchronizations. (Do not use semaphores).

Homework Hints (problem 2) Optimal Algorithm Reviewing The Algorithm (1)

Homework Hints (problem 2) Optimal Algorithm Reviewing The Algorithm (1)

Homework Hints (problem 2) Optimal Algorithm Reviewing The Algorithm (3)

Homework Hints (problem 2) Optimal Algorithm Parallel Inside Of Parallel Architecture Node Processes consist of 3 Sub Processes Mutex Process – invokes mutual exclusion for the node Request Process – process that receives request messages Reply Process – process that receives reply messages Node contained a database of shared variables between the 3 sub-processes – must provide protected access to them via mutex.

Homework Hints (problem 2) Optimal Algorithm Channel Layout Receive Request Reply Invoke MutEx Receive Request Reply Invoke MutEx Node (shared database) Node (shared database)

Project 1 Due Date – Oct 12 ?