Recitation 14: Proxy Lab Part 2

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
What is the output generated by this program? Please assume that each executed print statement completes, e.g., assume that each print is followed by an.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Concurrent Programming : Introduction to Computer.
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Threads Tutorial #7 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers – It is.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Lesson One – Creating a thread
Threads Synchronization Thread-safety of Library Functions
Process Synchronization: Semaphores
Background on the need for Synchronization
PThreads.
Threads Threads.
Multithreading Tutorial
Atomic Operations in Hardware
Concurrent Programming November 13, 2008
CS703 - Advanced Operating Systems
Recitation 12: ProxyLab Part 1
Recitation 14: Proxy Lab Part 2
Recitation 12: ProxyLab Part 1
Multithreading Tutorial
Module 7a: Classic Synchronization
Recitation 9: Processes, Signals, TSHLab
Unix System Calls and Posix Threads
Lecture 2 Part 2 Process Synchronization
Pthread Prof. Ikjun Yeom TA – Mugyo
Concurrency: Mutual Exclusion and Process Synchronization
Multithreading Tutorial
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Assignment 6 Recitation
Concurrent Programming : Introduction to Computer Systems 23rd Lecture, Nov. 13, 2018
Concurrent Programming CSCI 380: Operating Systems
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Concurrent Programming November 13, 2008
January 15, 2004 Adrienne Noble
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Lecture 20: Synchronization
CSE 451 Section 1/27/2000.
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
Instructor: Brian Railing
Recitation 9: Processes, Signals, TSHLab
Concurrent Programming
Presentation transcript:

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

Outline Proxylab Threading Threads and Synchronization PxyDrive Demo

ProxyLab Checkpoint is worth 1%, due Thursday, Nov. 29th Final is worth 7%, due Thursday, Dec 6th You may use at most one grace / late day for each phase Last day to submit checkpoint: Friday, Nov. 30th Last day to submit final: Friday, Dec 7th There will be no extensions! You are submitting an entire project Modify the makefile Split source file into separate pieces Submit regularly to verify proxy builds on Autolab A proxy is a server process It is expected to be long-lived To not leak resources To be robust against user input

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 PxyDrive !

Some practice Get the tarball $ wget http://www.cs.cmu.edu/~213/ activities/pxydrive-tutorial2.tar $ tar –xvf pxydrive-tutorial.tar $ cd pxydrive-tutorial

PxyDrive Tutorial 1 What happens when you haven’t implemented a concurrent proxy and are expected to handle multiple requests? Open basic-concurrency.cmd

PxyDrive Tutorial 1 >generate random-text1.txt 2K Generates a 2K text file called random-text1.txt >generate random-text2.txt 4K Generates a 4K text file called random-text2.txt >serve s1 Launches a server called s1 >request r1 random-text1.txt s1 Requests r1 from s1 >request r2 random-text2.txt s1 Requests r2 from s1 >wait * Waits for all transactions to finish Needed in the trace, not in the command-line

PxyDrive Tutorial 1 >respond r2 Respond to client with r2 (Out of order) >respond r1 Respond to client with r1 >trace r1 Traces the transaction r1 >check r1 Checks the transaction r1 >trace r2 Traces the transaction r2 >check r2 Checks the transaction r2

PxyDrive Tutorial 1 Run trace with –f option: $ ./pxy/pxydrive –p ./serial-proxy –f basic-concurrency.cmd

What went wrong? Answer: Response was clobbered.

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); } It depends. There is a race. – Pthread_join causes main to wait until work is complete. So if that runs first, main never terminates. 12

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); } It depends. There is a race. This will probably terminate as the worker thread will detach before the call to pthread_join. 13

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

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; } 1 or 2 because if j is initialized to 0 when both threads start, then writes to count overwrite (1), if they serialize (2) No, ++ is not atomic, so just using count does not change anything 15

PxyDrive Tutorial 2 What happens when we pass a statically allocated connected descriptor to the peer thread in our proxy? connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); Pthread_create(&tid, NULL, thread, &connfd); $ ./pxy/pxydrive –f mixed-concurrency.cmd –p ./static-concurrent-proxy

What went wrong? This can happen due to a race condition between the assignment statement in the peer thread and the accept statement in the main thread! This can result if the local connfd variable in the peer thread gets the descriptor number of the next connection Make sure to dynamically allocate memory for each connected descriptor returned by accept! Answer: Response was clobbered.

Synchronization Is not cheap Is also not that expensive 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

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

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; } Mutex – acquire it before critical region (j -> count) 20

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. ... 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); } 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; } T1: Search …… print T2: … Replace entry … While T2 blocks T1 from printing, it replaces the entry that T1 was going to print. 21

Locks gone wrong RWLocks are particularly susceptible to which issue: a. Starvation b. Livelock c. Deadlock 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. Design an approach to acquiring two semaphores that avoids deadlock and livelock, while allowing progress to other threads needing only one semaphore. #3 – Trylock inside a loop is one valid approach 22

PxyDrive Tutorial 3 Debugging a proxy that suffers race conditions Remember that one of the shared resource for all the proxy threads is the cache $ ./pxy/pxydrive –f caching.cmd –p ./race-proxy Do take some time understanding the trace file for this tutorial

What went wrong? Answer: Response was clobbered.

What went wrong? We realize that resources r01n (may be different in your case), was expected to be cached Let’s understand this scenario with two threads running concurrently on the proxy. T1: At the time of check, T1 sees that it has cached the requested object. Another thread, say T2: Is trying to add a new object to the cache and is performing an eviction. This thread could possibly delete the object from the cache after the time of check but before T1 sends the cached object to the requesting client ( time-of-use) This is an example of the Time-of-check Time-of-use race condition Answer: Response was clobbered.

PxyDrive Tutorial 4 Debugging a proxy that suffers a deadlock Run the same trace but with another faulty proxy $ ./pxy/pxydrive –f caching.cmd –p ./deadlocked-proxy

What went wrong? Answer: Response was clobbered.

What went wrong? We can notice a few timeout events and also some threads were waiting for the event that caused timeout. Let’s consider two proxy threads T1 and T2 as usual. Suppose T1 holds a lock on a shared resource ( could be the cache in our case) and never releases it. ( you might have missed to perform pthread_unlock ! Or might have messed with the order of locking and unlocking) Another thread, say T2: Is trying to hold a lock on the same resource. ( worse condition could be that it is already holding a lock on another shared resource that T2 needs). T2 waits for T1 to release the lock on the first resource and T1 in turn waits for T2 to release lock on that another resource. Answer: Response was clobbered.

Proxylab Reminders Plan out your implementation Read the writeup – 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!

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