Recitation 14: Proxy Lab Part 2

Slides:



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

Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multi-core Programming Programming with Posix Threads.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
– 1 – , F’02 Traditional View of a Process Process = process context + code, data, and stack shared libraries run-time heap 0 read/write data Program.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
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.
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
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.
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.
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.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
CS703 - Advanced Operating Systems
Lesson One – Creating a thread
Threads Synchronization Thread-safety of Library Functions
COT 4600 Operating Systems Fall 2009
Process Synchronization: Semaphores
Background on the need for Synchronization
PThreads.
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Multithreading Tutorial
Concurrent Programming November 13, 2008
Operating Systems CMPSC 473
HW1 and Synchronization & Queuing
Recitation 12: ProxyLab Part 1
Multithreading Tutorial
Semaphore Originally called P() and V() wait (S) { while S <= 0
Module 7a: Classic Synchronization
Recitation 14: Proxy Lab Part 2
Recitation 9: Processes, Signals, TSHLab
Pthread Prof. Ikjun Yeom TA – Mugyo
Concurrency: Mutual Exclusion and Process Synchronization
Multithreading Tutorial
CSCI1600: Embedded and Real Time Software
Project #3 Threads and Synchronization
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Assignment 6 Recitation
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 2019
Concurrent Programming November 13, 2008
CSCI1600: Embedded and Real Time Software
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Lecture 20: Synchronization
CSE 451 Section 1/27/2000.
EECE.4810/EECE.5730 Operating Systems
Process/Thread Synchronization (Part 2)
Instructor: Brian Railing
Recitation 9: Processes, Signals, TSHLab
Presentation transcript:

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

Outline Proxylab Threading Threads and Synchronization

ProxyLab ProxyLab is due in 1 week. A proxy is a server process No grace days 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

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

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

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 8

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) 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. ... 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. 12

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 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?

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.