Recitation 14: Proxy Lab Part 2

Similar documents
Recitation 14: Proxy Lab Part 2

Proxy: Concurrency & Caches

Synchronization: Advanced

Concurrent Programming

Concurrent Programming is Hard! Concurrent Programming. Reminder: Iterative Echo Server. The human mind tends to be sequential

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Concurrent Programming

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Threaded Programming. Lecture 9: Alternatives to OpenMP

Lecture #23 Concurrent Programming

Concurrent Programming

Concurrent Programming

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Computer Systems Laboratory Sungkyunkwan University

CSE 160 Lecture 7. C++11 threads C++11 memory model

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

Recitation 12: ProxyLab Part 1

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

Concurrent programming

Carnegie Mellon Performance of Mul.threaded Code

Concurrent Programming

Carnegie Mellon Synchronization: Advanced

Approaches to Concurrency

CS3733: Operating Systems

Concurrent Programming April 21, 2005

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Concurrent Programming November 28, 2007

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Synchronization: Basics

Process Synchronization

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

ANSI/IEEE POSIX Standard Thread management

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Thread Fundamentals. CSCI 315 Operating Systems Design 1

Threads. lightweight processes

Carnegie Mellon Concurrency and Synchronization

CS533 Concepts of Operating Systems. Jonathan Walpole

CS 6400 Lecture 11 Name:

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

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Synchronization: Basics

Threads. studykorner.org

Process Synchroniza/on

Multicore and Multiprocessor Systems: Part I

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

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

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

call connect call read call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch!

CSE-160 (Winter 2017, Kesden) Practice Midterm Exam. volatile int count = 0; // volatile just keeps count in mem vs register

About me Now that you know the pthread API

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

CS 3305 Intro to Threads. Lecture 6

Threads. Jo, Heeseung

Systèmes d Exploitation Avancés

Programming with Threads Dec 7, 2009"

Lecture 5 Threads and Pthreads II

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming

Multithreading Programming II

High Performance Computing Course Notes Shared Memory Parallel Programming

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

CS 3723 Operating Systems: Final Review

Three Basic Mechanisms for Creating Concurrent Flows. Systemprogrammering 2009 Föreläsning 10 Concurrent Servers. Process-Based Concurrent Server

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

CSE 153 Design of Operating Systems

User Space Multithreading. Computer Science, University of Warwick

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

Chapter 6: Process Synchronization

Chapter 4 Concurrent Programming

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

CS 153 Design of Operating Systems Winter 2016

Concurrency on x86-64; Threads Programming Tips

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

Concurrent Servers Dec 2, 2009"

Today. Threads review Sharing Mutual exclusion Semaphores

CS370 Operating Systems

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Assignment #2. Problem 2.1: airplane synchronization

Recitation 2/18/2012

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

[537] Semaphores. Tyler Harter

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Iterative Servers The course that gives CMU its Zip! Iterative servers process one request at a time. Concurrent Servers

CS Operating Systems: Threads (SGG 4)

CS Operating system

Threading Language and Support. CS528 Multithreading: Programming with Threads. Programming with Threads

Concurrency: Signaling and Condition Variables

Operating System Labs. Yuanbin Wu

CSE 306/506 Operating Systems Concurrency: Mutual Exclusion and Synchronization YoungMin Kwon

CS 105, Spring 2007 Ring Buffer

[537] Concurrency Bugs. Tyler Harter

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

CS 5523 Operating Systems: Thread and Implementation

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Transcription:

Recitation 14: Proxy Lab Part 2 Instructor: TA(s) 1

Outline Proxylab Threading Threads and Synchronization 2

ProxyLab ProxyLab is due in 1 week. No grace days Late days allowed (-15%) Make sure to submit well in advance of the deadline in case there are errors in your submission. Build errors are a common source of failure A proxy is a server process It is expected to be long-lived To not leak resources To be robust against user input 3

Proxies and Threads Network connections can be handled concurrently Three approaches were discussed in lecture for doing so Your proxy should (eventually) use threads Threaded echo server is a good example of how to do this Multi-threaded cache design Need to have multiple readers or one writer Be careful how you use mutexes you do not want to serialize your readers Be careful how you maintain your object age Tools Use Firefox s Network Monitor (Developer > Network) to see if all requests have been fulfilled 4

Join / Detach Does the following code terminate? Why or why not? int main(int argc, char** argv) pthread_create(&tid, NULL, work, NULL); if (pthread_join(tid, NULL)!= 0) printf( Done.\n ); void* work(void* a) pthread_detatch(pthread_self()); while(1); } 5

Join / Detach cont. Does the following code terminate now? Why or why not? int main(int argc, char** argv) pthread_create(&tid, NULL, work, NULL); sleep(1); if (pthread_join(tid, NULL)!= 0) printf( Done.\n ); void* work(void* a) pthread_detach(pthread_self()); while(1); } 6

When should threads detach? In general, pthreads will wait to be reaped via pthread_join. When should this behavior be overridden? When termination status does not matter. pthread_join provides a return value When result of thread is not needed. When other threads do not depend on this thread having completed 7

Threads What is the range of value(s) that main will print? A programmer proposes removing j from thread and just directly accessing count. Does the answer change? volatile int count = 0; void* thread(void* v) int j = count; j = j + 1; count = j; } int main(int argc, char** argv) pthread_t tid[2]; for(int i = 0; i < 2; i++) pthread_create(&tid[i], NULL, thread, NULL); for (int i = 0; i < 2; i++) pthread_join(tid[i]); printf( %d\n, count); return 0; } 8

Synchronization Is not cheap 100s of cycles just to acquire without waiting Is also not that expensive Recall your malloc target of 15000kops => ~100 cycles May be necessary Correctness is always more important than performance 9

Which synchronization should I use? Counting a shared resource, such as shared buffers Semaphore Exclusive access to one or more variables Mutex Most operations are reading, rarely writing / modifying RWLock 10

Threads Revisited Which lock type should be used? Where should it be acquired / released? volatile int count = 0; void* thread(void* v) int j = count; j = j + 1; count = j; } int main(int argc, char** argv) pthread_t tid[2]; for(int i = 0; i < 2; i++) pthread_create(&tid[i], NULL, thread, NULL); for (int i = 0; i < 2; i++) pthread_join(tid[i]); printf( %d\n, count); return 0; } 11

Associating locks with data Given the following key-value store Key and value have separate RWLocks: klock and vlock When an entry is replaced, both locks are acquired. Describe why the printf may not be accurate. typedef struct _data_t int key; size_t value; } data_t; #define SIZE 10 data_t space[size]; int search(int k) for(int j = 0; j < SIZE; j++) if (space[j].key == k) return j; return -1; }... pthread_rwlock_rdlock(klock); match = search(k); pthread_rwlock_unlock(klock); if (match!= -1) pthread_rwlock_rdlock(vlock); printf( %zd\n, space[match]); pthread_rwlock_unlock(vlock); } 12

Locks gone wrong 1. RWLocks are particularly susceptible to which issue: a. Starvation b. Livelock c. Deadlock 2. If some code acquires rwlocks as readers: LockA then LockB, while other readers go LockB then LockA. What, if any, order can a writer acquire both LockA and LockB? No order is possible without a potential deadlock. 3. Design an approach to acquiring two semaphores that avoids deadlock and livelock, while allowing progress to other threads needing only one semaphore. 13

Client-to-Client Communication Clients don t have to fetch content from servers Clients can communicate with each other In a chat system, a server acts as a facilitator between clients Clients could also send messages directly to each other, but this is more complicated (peer-to-peer networking) Running the chat server./chatserver <port> Running the client telnet <hostname> <port> What race conditions could arise from having communication between multiple clients? 14

Proxylab Reminders Plan out your implementation Weeks of programming can save you hours of planning Anonymous Arbitrarily using mutexes will not fix race conditions Read the writeup Submit your code (days) early Test that the submission will build and run on Autolab Final exam is only a few weeks away! 15

Appendix Calling exit() will terminate all threads Calling pthread_join on a detached thread is technically undefined behavior. Was defined as returning an error. 16