CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

Similar documents
CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Dealing with Issues for Interprocess Communication

Data Structures Review 1 Vectors... 1 Linked List... 2 Trees... 2 Heaps... 2 Hash tables... 2

CSCI-1200 Data Structures Fall 2009 Lecture 12 Advanced Recursion

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

CS 153 Design of Operating Systems Winter 2016

Concurrent & Distributed Systems Supervision Exercises

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CSCI-1200 Data Structures Fall 2014 Lecture 12 Recursion

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CSCI-1200 Data Structures Spring 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

CSE 153 Design of Operating Systems

CSE 374 Programming Concepts & Tools

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Lecture 10 Midterm review

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Operating Systems. Synchronization

Operating Systems (234123) Spring (Homework 3 Wet) Homework 3 Wet

Lecture 15a Persistent Memory & Shared Pointers

CSCI-1200 Data Structures Spring 2017 Lecture 13 Advanced Recursion

Processor speed. Concurrency Structure and Interpretation of Computer Programs. Multiple processors. Processor speed. Mike Phillips <mpp>

Synchronization I. Jo, Heeseung

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

CSE 153 Design of Operating Systems Fall 2018

CS533 Concepts of Operating Systems. Jonathan Walpole

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 160: Interactive Programming

CISC 3130 Data Structures Spring 2018

Parallel Code Smells: A Top 10 List

Midterm Exam. October 20th, Thursday NSC

Programmazione di sistemi multicore

Computer Science 210: Data Structures

Computer Science II CSci 1200 Test 1 Overview and Practice

CS-537: Midterm Exam (Fall 2013) Professor McFlub

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Concurrency: Mutual Exclusion (Locks)

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Parallel access to linked data structures

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

CS2013 Course Syllabus Spring 2017 Lecture: Friday 8:00 A.M. 9:40 A.M. Lab: Friday 9:40 A.M. 12:00 Noon

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

Concurrency: Locks. Announcements

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Synchronization

CS11 Introduction to C++ Fall Lecture 7

CSCI-1200 Data Structures Spring 2018 Lecture 25 Garbage Collection & Smart Pointers

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion

Written by John Bell for CS 342, Spring 2018

Midterm Exam Solutions and Grading Guidelines March 3, 1999 CS162 Operating Systems

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Synchronization. Silvina Hanono Wachman Computer Science & Artificial Intelligence Lab M.I.T.

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

CSE Traditional Operating Systems deal with typical system software designed to be:

Lecture 5: Synchronization w/locks

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CMSC421: Principles of Operating Systems

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

CSE 333 Final Exam June 8, 2016 Sample Solution

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Beijing Jiaotong University CS-23: C++ Programming Summer, 2019 Course Syllabus

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Introduction to Data Structures

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Lecture 5 Threads and Pthreads II

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Last Class: Monitors. Real-world Examples

Transcription:

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing Final Exam General Information The final exam will be held Monday, Dec 21st, 2009, 11:30am-2:30pm, DCC 308. A makeup exam will only be offered if required by the RPI rules regarding final exam conflicts -OR- if a written excuse from the Dean of Students office is provided. Contact Professor Cutler ASAP if you require a makeup exam. Coverage: Lectures 1-25, Labs 1-14, HW 1-10. Closed-book and closed-notes except for 2 sheets of 8.5x11 inch paper (front & back) that may be handwritten or printed. Computers, cell-phones, palm pilots, calculators, PDAs, MP3 players, etc. are not permitted and must be turned off. All students must bring their Rensselaer photo ID card. The best thing you can do to prepare for the final is practice. Try the review problems (posted on the course website) with pencil & paper first. Then practice programming (with a computer) the exercises and other exercises from lecture, lab, homework and the textbook. Solutions to the review problems will be posted next week. Review from Lecture 24 & Lab What is garbage? Memory which cannot (or should not) be accessed by the program. It is available for reuse. Explicit memory management (C++) vs. automatic garbage collection. Reference Counting, Stop & Copy, Mark-Sweep. Cyclical data structures, memory overhead, incremental vs. defragmentation. pause in execution, ratio of good to garbage, 25.1 Today s Class Computing with multiple threads/processes and one or more processors Shared resources & mutexes/locks Deadlock: the Dining Philosopher s Problem 25.2 The Role of Time in Evaluation Sometimes the order of evaluation does matter, and sometimes it doesn t. The behavior of objects with state depends on sequence of events that have occurred. Referential transparency: when equivalent expressions can be substituted for one another without changing the value of the expression. For example, a complex expression can be replaced with its result if repeated evaluations always yield the same result, independent of context. What happens when objects don t change one at a time but rather act concurrently? We may be able to take advantage of this by letting threads/processes run at the same time (a.k.a., in parallel). However, we will need to think carefully about the interactions and shared resources.

25.3 Concurrency Example: Joint Bank Account Consider the following bank account implementation: class Account { Account(int amount) : balance(amount) { void deposit(int amount) { int tmp = balance; tmp += amount; balance = tmp; void withdraw(int amount) { int tmp = balance; if (amount > tmp) cout << "Error: Insufficient Funds!" << endl; else { tmp -= amount; balance = tmp; int balance; ; // A // B // C // D // E1 // E2 // F We create a joint account that will be used by two people (threads/processes): Account account(100); Now, enumerate all of the possible interleavings of the sub-expressions (A-F) if the following two function calls were to happen concurrently. What are the different outcomes? account.deposit(50); account.withdraw(125); 25.4 Correct/Acceptable Behavior of Concurrent Programs No two operations that change any shared state variables may occur at the same time. Certain low-level operations are guaranteed to execute atomic-ly (from start to finish without interruption), but this varies based on the hardware and operating system. We need to know which operations are atomic on our hardware. In the bank account example we cannot assume that the deposit and withdraw functions are atomic. The concurrent system should produce the same result as if the threads/processes had run sequentially in some order. We do not require that the threads/processes run sequentially, only that they produce results as if they had run sequentially. Note: There may be more than one correct result! Exercise: What are the acceptable outcomes for the bank account example? 2

25.5 Serialization via a Mutex We can serialize the important interactions using a primitive, atomic synchronization method called a mutex. Once one thread has acquired the mutex (locking the resource), no other thread can acquire the mutex until it has been released. The call to pthread mutex lock blocks until the mutex is available. Consider the following class: class Chalkboard { Chalkboard() { pthread_mutex_init(&lock,null); void write(drawing d) { drawing = d; Drawing read() { Drawing answer = drawing; return answer; Drawing drawing; pthread_mutex_t lock; ; What does the mutex do in this code? 25.6 The Professor & Student Classes Here are two simple classes that can communicate through a shared Chalkboard: class Professor { Professor(Chalkboard *c) { chalkboard = c; virtual void Lecture() {... chalkboard->write(...)... protected: Chalkboard* chalkboard; ; class Student { Student(Chalkboard *c) { chalkboard = c; void TakeNotes() {... chalkboard->read()... Chalkboard* chalkboard; ; 25.7 Launching Concurrent Threads And here s an example of how to launch a second thread using the pthread library. The new thread begins execution in the provided function (student thread, in this example). This code may look different on different platforms (operating system, compiler, programming language, etc.) 3

void* student_thread(void* chalkboard) { Student *student = new Student((Chalkboard*)chalkboard); while (1) student->takenotes(); int main() { Chalkboard *chalkboard = new Chalkboard(); pthread_t thread; int code = pthread_create(&thread,null,student_thread,chalkboard); Professor *prof = new Professor(chalkboard); while (1) prof->lecture(); What can still go wrong? How can we fix it? 25.8 Condition Variables Here we ve added a condition variable, student done: class Chalkboard { Chalkboard() { pthread_mutex_init(&lock,null); student_done = true; void write(drawing d) { while (1) { if (student_done) { drawing = d; student_done = false; break; Drawing read() { while (1) { if (!student_done) { Drawing answer = drawing; student_done = true; return answer; Drawing drawing; pthread_mutex_t lock; bool student_done; ; Note: This implementation is actually quite inefficient due to busy waiting. A better solution is to use a operating system-supported condition variable that yields to other threads if the lock is not available and is signaled when the lock becomes available again. 4

25.9 Exercise: Multiple Students and/or Multiple Professors Now consider that we have multiple students and/or multiple professors. How can you ensure that each student is able to copy a complete set of notes? 25.10 Multiple Locks & Deadlock Now we derive two different types of lecturer from the base class Professor. The professors can lecture concurrently, but they must share the chalk and the book (each controlled with their own mutex). class CautiousLecturer : public Professor { CautiousLecturer(Chalkboard *c) : Professor(c) { void Lecture() { pthread_mutex_lock(&chalkboard->book); Drawing d = Drawing(); checkdrawing(d); pthread_mutex_lock(&chalkboard->chalk); chalkboard->write(d); pthread_mutex_unlock(&chalkboard->chalk); pthread_mutex_unlock(&chalkboard->book); ; class BrashLecturer : public Professor { BrashLecturer(Chalkboard *c) : Professor(c) { void Lecture() { pthread_mutex_lock(&chalkboard->chalk); Drawing d = Drawing(); chalkboard->write(d); pthread_mutex_lock(&chalkboard->book); checkdrawing(d); pthread_mutex_unlock(&chalkboard->book); pthread_mutex_unlock(&chalkboard->chalk); ; What can go wrong? How can we fix it? Why might philosophers discuss this problem over dinner? 5

25.11 Topics Covered Algorithm analysis: big O notation STL classes: strings, vectors, lists, maps, & sets Classes: operator overloading, constructors, assignment operator, & destructor, inheritance, polymorphism Subscripting vs. iteration Recursion & problem solving techniques Memory: pointers & arrays, dynamic allocation & deallocation of memory, garbage collection Implementing data structures: vectors, linked lists, trees (for sets & maps), hash sets Hash tables, priority queues, leftist heaps Concurrency & asynchronous computing 25.12 Course Summary Approach any problem by studying the requirements carefully, playing with hand-generated examples to understand them, and then looking for analogous problems that you already know how to solve. The standard library offers container classes and algorithms that simplify the programming process and raise your conceptual level of thinking in designing solutions to programming problems. Just think how much harder some of the homework problems would have been without generic container classes. When choosing between algorithms and between container classes (data structures) you should consider: efficiency, naturalness of use, and ease of programming. Use classes with well-designed public and private member functions to encapsulate sections of code. Writing your own container classes and data structures usually requires building linked structures and managing memory through the big three: copy constructor, assignment operator, and destructor. When testing and debugging: Test one function and one class at a time, Figure out what your program actually does, not what you wanted it to do, Use small examples and boundary conditions when testing, and Find and fix the first mistake in the flow of your program before considering other apparent mistakes. Above all, remember the excitement and satisfaction of developing a deep, computational understanding of a problem and turning it into a program that realizes your understanding flawlessly. 6