Concurrency: Threads CSE 333 Winter 2019

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
The University of Adelaide, School of Computer Science
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
CS333 Intro to Operating Systems Jonathan Walpole.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Threads. Readings r Silberschatz et al : Chapter 4.
Threads A thread is an alternative model of program execution
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Tutorial 2: Homework 1 and Project 1
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
PThreads.
Shared-Memory Programming with Threads
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Concurrency and Threads CSE 333 Spring 2018
Realizing Concurrency using the thread model
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Concurrency: Mutual Exclusion and Process Synchronization
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Course Wrap-Up CSE 333 Autumn 2018
Concurrency: Threads CSE 333 Summer 2018
Concurrency: Processes CSE 333 Summer 2018
Realizing Concurrency using Posix Threads (pthreads)
Concurrency and Processes CSE 333 Spring 2018
Concurrency: Threads CSE 333 Autumn 2018
Introduction to Concurrency CSE 333 Autumn 2018
Concurrency: Processes CSE 333 Autumn 2018
Server-side Programming CSE 333 Winter 2019
C++ Constructor Insanity CSE 333 Winter 2019
January 15, 2004 Adrienne Noble
Client-side Networking CSE 333 Winter 2019
Course Wrap-Up CSE 333 Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Lecture 20: Synchronization
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
EECE.4810/EECE.5730 Operating Systems
Concurrency: Processes CSE 333 Winter 2019
Introduction to Concurrency CSE 333 Winter 2019
Presentation transcript:

Concurrency: Threads CSE 333 Winter 2019 5/25/2018 Concurrency: Threads CSE 333 Winter 2019 Instructor: Hal Perkins Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht Yifan Xu Sujie Zhou

Administrivia Last exercise due Monday Concurrency using pthreads hw4 due Thursday night next week <panic>If you haven’t started yet</panic> Usual late days (max 2) available if you have any left Please fill out course evals while they are available Final exam Wed. 3/20, 2:30-4:20 Some review next Thur. in sections; review Q&A Tue. 3/19, 4:30 Old Topic list (will update shortly) and old finals on Exams page Summer final exams are 1 hour; regular quarters are usual 2 hours

Some Common hw4 Bugs Your server works, but is really, really slow Check the 2nd argument to the QueryProcessor constructor Funny things happen after the first request Make sure you’re not destroying the HTTPConnection object too early (e.g. falling out of scope in a while loop) Server crashes on a blank request Make sure that you handle the case that read() (or WrappedRead()) returns 0

Previously… We implemented a search server but it was sequential 5/25/2018 Previously… We implemented a search server but it was sequential Processes requests one at a time regardless of client delays Terrible performance, resource utilization Servers should be concurrent Different ways to process multiple queries simultaneously: Issue multiple I/O requests simultaneously Overlap the I/O of one request with computation of another Utilize multiple CPUs or cores Mix and match as desired

Outline (next two lectures) We’ll look at different searchserver implementations Sequential Concurrent via dispatching threads – pthread_create() Concurrent via forking processes – fork() Concurrent via non-blocking, event-driven I/O – select() We won’t get to this  Reference: Computer Systems: A Programmer’s Perspective, Chapter 12 (CSE 351 book)

Sequential Pseudocode: See searchserver_sequential/ listen_fd = Listen(port); while (1) { client_fd = accept(listen_fd); buf = read(client_fd); resp = ProcessQuery(buf); write(client_fd, resp); close(client_fd); }

Whither Sequential? Advantages: Disadvantages: Super(?) simple to build/write Disadvantages: Incredibly poor performance One slow client will cause all others to block Poor utilization of resources (CPU, network, disk)

Threads Threads are like lightweight processes They execute concurrently like processes Multiple threads can run simultaneously on multiple CPUs/cores Unlike processes, threads cohabitate the same address space Threads within a process see the same heap and globals and can communicate with each other through variables and memory But, they can interfere with each other – need synchronization for shared resources Each thread has its own stack

Threads and Address Spaces OS kernel [protected] Stackparent Heap (malloc/free) Read/Write Segment .data, .bss Shared Libraries Read-Only Segment .text, .rodata Before creating a thread One thread of execution running in the address space One PC, stack, SP That main thread invokes a function to create a new thread Typically pthread_create() SPparent PCparent

Threads and Address Spaces OS kernel [protected] Stackparent Heap (malloc/free) Read/Write Segment .data, .bss Shared Libraries Read-Only Segment .text, .rodata After creating a thread Two threads of execution running in the address space Original thread (parent) and new thread (child) New stack created for child thread Child thread has its own PC, SP Both threads share the other segments (code, heap, globals) They can cooperatively modify shared data SPparent Stackchild SPchild PCchild PCparent

pthreads Threads See thread_example.cc 5/25/2018 pthreads Threads See thread_example.cc int pthread_create( pthread_t* thread, const pthread_attr_t* attr, void* (*start_routine)(void*), void* arg); int pthread_detach(pthread_t thread); int pthread_join(pthread_t thread, void** retval);

Concurrent Server with Threads A single process handles all of the connections, but a parent thread dispatches (creates) a new thread to handle each connection The child thread handles the new connection and then exits when the connection terminates

Multithreaded Server server client connect accept()

Multithreaded Server server client pthread_create()

Multithreaded Server server client accept()

Multithreaded Server server client pthread_create() client

shared data structures Multithreaded Server server client client shared data structures client client client client

Concurrent Server via Threads See searchserver_threads/ Notes: When calling pthread_create(), start_routine points to a function that takes only one argument (a void*) To pass complex arguments into the thread, create a struct to bundle the necessary data How do you properly handle memory management? Who allocates and deallocates memory? How long do you want memory to stick around?

Whither Concurrent Threads? Advantages: Almost as simple to code as sequential In fact, most of the code is identical! (but a bit more complicated to dispatch a thread) Concurrent execution with good CPU and network utilization Some overhead, but less than processes Shared-memory communication is possible Disadvantages: Synchronization is complicated Shared fate within a process One “rogue” thread can hurt you badly

Threads and Data Races What happens if two threads try to mutate the same data structure? They might interfere in painful, non-obvious ways, depending on the specifics of the data structure Example: two threads try to push an item onto the head of the linked list at the same time Could get “correct” answer Could get different ordering of items Could break the data structure! 

Data Race Example If your fridge has no milk, then go out and buy some more What could go wrong? If you live alone: If you live with a roommate: if (!milk) { buy milk } ! !

Data Race Example Idea: leave a note! A. Yes, problem fixed Does this fix the problem? A. Yes, problem fixed B. No, could end up with no milk C. No, could still buy multiple milk D. We’re lost… if (!note) { if (!milk) { leave note buy milk remove note }

5/25/2018 Synchronization Synchronization is the act of preventing two (or more) concurrently running threads from interfering with each other when operating on shared data Need some mechanism to coordinate the threads “Let me go first, then you can go” Many different coordination mechanisms have been invented (see CSE 451) Goals of synchronization: Liveness – ability to execute in a timely manner (informally, “something good happens!”) Safety – avoid unintended interactions with shared data structures (informally, “nothing bad happens”) https://en.wikipedia.org/wiki/Liveness https://en.wikipedia.org/wiki/Thread_safety

5/25/2018 Lock Synchronization Use a “Lock” to grant access to a critical section so that only one thread can operate there at a time Executed in an uninterruptible (i.e. atomic) manner Lock Acquire Wait until the lock is free, then take it Lock Release Release the lock If other threads are waiting, wake exactly one up to pass lock to Pseudocode: // non-critical code lock.acquire(); // critical section lock.release(); loop/idle if locked

Milk Example – What is the Critical Section? 5/25/2018 Milk Example – What is the Critical Section? What if we use a lock on the refrigerator? Probably overkill – what if roommate wanted to get eggs? For performance reasons, only put what is necessary in the critical section Only lock the milk But lock all steps that must run uninterrupted (i.e., must run as an atomic unit) fridge.lock() if (!milk) { buy milk } fridge.unlock() milk_lock.lock() if (!milk) { buy milk } milk_lock.unlock() https://en.wikipedia.org/wiki/Linearizability

5/25/2018 pthreads and Locks Another term for a lock is a mutex (“mutual exclusion”) pthreads (#include <pthread.h>) defines datatype pthread_mutex_t pthread_mutex_init() Initializes a mutex with specified attributes pthread_mutex_lock() Acquire the lock – blocks if already locked pthread_mutex_unlock() Releases the lock int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr); https://en.wikipedia.org/wiki/Lock_(computer_science) int pthread_mutex_lock(pthread_mutex_t* mutex); int pthread_mutex_unlock(pthread_mutex_t* mutex);

C++11 Threads C++11 added threads and concurrency to its libraries <thread> – thread objects <mutex> – locks to handle critical sections <condition_variable> – used to block objects until notified to resume <atomic> – indivisible, atomic operations <future> – asynchronous access to data These might be built on top of <pthread.h>, but also might not be Definitely use in C++11 code if local conventions allow, but pthreads will be around for a long, long time Use pthreads in current exercise