Concurrency and Threads CSE 333 Spring 2018

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.
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
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
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,
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Pthreads: A shared memory programming model
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.
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
Pthreads.
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
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:
pThread synchronization
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
Server-side Programming CSE 333 Spring 2018
Realizing Concurrency using Posix Threads (pthreads)
Client-side Networking CSE 333 Spring 2018
EECE.4810/EECE.5730 Operating Systems
Realizing Concurrency using the thread model
Multithreading Tutorial
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
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
References Revisited CSE 333 Spring 2018
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
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
CSE 153 Design of Operating Systems Winter 2019
Lecture 20: Synchronization
EECE.4810/EECE.5730 Operating Systems
Concurrency: Threads CSE 333 Winter 2019
Concurrency: Processes CSE 333 Winter 2019
POSIX Threads(pthreads)
Presentation transcript:

Concurrency and Threads CSE 333 Spring 2018 5/25/2018 Concurrency and Threads CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon Huang Wei Lin

Administrivia Exercise 17 released yesterday, due Wednesday (5/30) Concurrency via pthreads hw4 due next Thursday (5/31) Submissions accepted until Sunday (6/3) Final is Tuesday (6/5), 12:30-2:20 pm, KNE 120 Review Session: Sunday (6/3), 4-6:30 pm, EEB 125 Two double-sided, handwritten sheets of notes allowed Topic list and past finals on Exams page on website

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

Review Servers should be concurrent 5/25/2018 Review Servers should be concurrent Sequential query processing has terrible performance, as client interactions block for arbitrarily long periods of time 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

Outline searchserver Reference: CSPP, Chapter 12 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: CSPP, Chapter 12

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); }

Why Sequential? Advantages: Disadvantages: Super simple to build/write 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 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 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 Extra stack created Child thread maintains separate values for its SP and PC Both threads share the other segments They can cooperatively modify shared data SPparent Stackchild SPchild PCchild PCparent

5/25/2018 pthreads Threads 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);

Thread Example See thread_example.cc Remember process graphs? They work for threads, too!

Concurrency with Threads A single process handles all of the connections, but a parent thread dispatches 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 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 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?

Why Concurrent Threads? Advantages: Code is still straightforward Can write threaded code like sequential, but be careful with dispatch 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 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? Vote at http://PollEv.com/justinh 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 (CSE451) Goals of synchronization: Liveness – ability to execute in a timely manner Safety – avoid unintended interactions with shared data structures 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

Data Race Example With Locks 5/25/2018 Data Race Example With Locks 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 fridge.lock() if (!milk) { buy milk } fridge.unlock() https://en.wikipedia.org/wiki/Linearizability milk_lock.lock() if (!milk) { buy milk } milk_lock.unlock()

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, but pthreads will be around for a long, long time Use pthreads in Exercise 17