CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
Advertisements

6.1 Synchronous Computations ITCS 4/5145 Cluster Computing, UNC-Charlotte, B. Wilkinson, 2006.
CS 149: Operating Systems February 3 Class Meeting
Synchronization and Deadlocks
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Go Language * Go - Routines * Channels. New Concepts Do not communicate by sharing memory; instead, share memory by communicating. creating shared memory.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Concurrency CS 510: Programming Languages David Walker.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Computer Science Lecture 12, page 1 CS677: Distributed OS Last Class Vector timestamps Global state –Distributed Snapshot Election algorithms.
Fundamentals of Python: From First Programs Through Data Structures
CS Introduction to Operating Systems
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Advanced Operating Systems CIS 720 Lecture 1. Instructor Dr. Gurdip Singh – 234 Nichols Hall –
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
111 © 2002, Cisco Systems, Inc. All rights reserved.
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Global States Steve Ko Computer Sciences and Engineering University at Buffalo.
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
CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 149: Operating Systems March 5 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS 235: User Interface Design March 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS 152: Programming Language Paradigms April 28 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Pitfalls: Time Dependent Behaviors CS433 Spring 2001 Laxmikant Kale.
Outline Other synchronization primitives
Other Important Synchronization Primitives
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Transactional Memory Semaphores, monitors, and conditional critical regions all suffer from limitations based on lock semantics Naïve synchronization may.
Shared Memory Programming
CMPE 135: Object-Oriented Analysis and Design November 29 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Concurrency: Mutual Exclusion and Process Synchronization
CMPE 135: Object-Oriented Analysis and Design April 30 Class Meeting
CMPE 152: Compiler Design April 18 – 30 Labs
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
Software Engineering and Architecture
Synchronization and semaphores
Concurrency in GO CS240 23/8/2017.
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 2 Multithreaded Programming is Hard!  The execution order of the threads is not guaranteed. Your program must work no matter what the order is. You must explicitly synchronize the threads. A thread must remember to signal other threads to prevent the program from deadlocking.  It is easy to corrupt shared objects. A thread must prevent another thread from “sneaking in” before it is finished with its operations. You must manage critical regions.

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 3 Multithreaded Programming is Hard! cont’d  Debugging multithreaded programs is difficult. Different errors (or none at all) may occur on each run. Errors may be timing related.  However, multithreaded programming is a critical job skill. Learn multiprogramming or be obsolete! _

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 4 Class Exercise: Multithreaded Café  A small busy café serves coffee and burgers.  At most three customers at a time can be inside the café.  Only one server is working behind the counter.  Customers are served in the order that they enter. _

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 5 Multithreaded Café, cont’d  Coffee customers enter through any one of three coffee doors. At least one every minute. Once a server starts serving a coffee customer, it takes up to two minutes.  Burger customers enter through the one burger door. At least one every five minutes. Once a server starts serving a burger customer, it takes up to five minutes.

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 6 Multithreaded Café: The Burger Rule  Once a burger customer enters, the server needs to concentrate on making the burger.  Other customers already in the café can leave after they’ve been served.  No other customers may enter until the burger customer is served and leaves. _

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 7 Multithreaded Café: Java Program  Write a multithreaded Java program to simulate the activities of the café.  One second of simulation run time equals one minute of cafe activity. _

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 8 Multithreaded Café: Java Program, cont’d  Run the simulation until 5 coffee customers have entered through each coffee entrance and have been served. 5 burger customers have entered through the burger entrance and have been served. _

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 9 Multithreaded Café: Java Program, cont’d  Print a line for each event as it occurs. An event is a coffee or burger customer entering or leaving.  Include in each printed line: Timestamp of elapsed simulated minutes and seconds. The current number of customers in the café. The event and its sequence number.  You can print a negative sequence number for a leaving event.

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 10 Multithreaded Café: Design Questions  How to represent the cafe?  How to represent customers?

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 11 Multithreaded Café: Design Questions, cont’d  What threads?  Locks?  Condition objects?  Shared data?  Critical regions?

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 12 Multithreaded Café: Design Questions, cont’d  How to implement the burger rule?  How to start the simulation?

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 13 Message Passing  Message passing is another mechanism for thread synchronization and communication using the distributed model of parallel processing.  Threads are synchronized by waiting for messages to arrive. _

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 14 Message Passing  In its most basic form, message passing consists of two operations, send and receive. Example C code: (Assumes every sender knows its receiver and vice versa.) void send(Process to, Message msg); void receive(Process from, Message msg);

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 15 Message Passing: Go Language  Google’s Go language has built-in support for parallel programming via message passing.  The go statement starts a goroutine (thread). Example: starts function func as a goroutine. go func()

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 16 Message Passing: Go Language  A channel type provides a synchronized channel between goroutines. Example: Send a message msg over channel ch : Receive a message from channel ch : Both block until the other goroutine is ready to communicate. chan ch ch <- msg <- ch

SJSU Dept. of Computer Science Spring 2014: May 7 CS 152: Programming Language Paradigms © R. Mak 17 Message Passing: Go Language  The select statement waits on a set of channels for communication from any channel. _