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

Similar documents
Concurrent Programming

Lecture #23 Concurrent Programming

Concurrent Programming

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

Concurrent Programming

Concurrent programming

Concurrent Programming

Concurrent Programming

Concurrent Programming November 28, 2007

Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming April 21, 2005

Concurrent Programming

Approaches to Concurrency

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

Concurrent Programming

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

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

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

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

Concurrent Servers Dec 2, 2009"

Concurrent Programming

CONCURRENT PROGRAMMING. Lecture 5

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

Concurrent Programming

Announcement (1) Due date for PA3 is changed (~ next week) PA4 will also be started in the next class. Not submitted. Not scored

Proxy: Concurrency & Caches

Carnegie Mellon Performance of Mul.threaded Code

Concurrent Programming

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

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Recitation 14: Proxy Lab Part 2

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Today. Threads review Sharing Mutual exclusion Semaphores

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization: Basics

Recitation 14: Proxy Lab Part 2

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

CSE 306/506 Operating Systems Threads. YoungMin Kwon

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

CS333 Intro to Operating Systems. Jonathan Walpole

Synchronization: Advanced

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

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

Threads. Jo, Heeseung

CS 5523 Operating Systems: Thread and Implementation

pthreads CS449 Fall 2017

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

Introduction to PThreads and Basic Synchronization

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole

CS Operating Systems: Threads (SGG 4)

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 3723 Operating Systems: Final Review

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Synchronization: Basics

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr.

CS 3305 Intro to Threads. Lecture 6

Last class: Today: Thread Background. Thread Systems

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CSE 153 Design of Operating Systems Fall 2018

CS 261 Fall Mike Lam, Professor. Threads

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Carnegie Mellon Concurrency and Synchronization

Network Programming November 3, 2008

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

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

Multithreaded Programming

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Threads. Jo, Heeseung

Parallel Programming with Threads

Processes, Threads, SMP, and Microkernels

CS370 Operating Systems

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

CS 5523: Operating Systems

CS370 Operating Systems

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

CMPSC 311- Introduction to Systems Programming Module: Concurrency

LSN 13 Linux Concurrency Mechanisms

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Concurrency. Johan Montelius KTH

Multicore and Multiprocessor Systems: Part I

Concurrent Programming. Concurrent Programming with Processes and Threads

High Performance Computing Course Notes Shared Memory Parallel Programming

THREADS. Jo, Heeseung

CISC2200 Threads Spring 2015

Threads. lightweight processes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

ECE 650 Systems Programming & Engineering. Spring 2018

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

ANSI/IEEE POSIX Standard Thread management

CS533 Concepts of Operating Systems. Jonathan Walpole

Processes. 4: Threads. Problem needs > 1 independent sequential process? Example: Web Server. Last Modified: 9/17/2002 2:27:59 PM

Concurrency on x86-64; Threads Programming Tips

Threads. Threads (continued)

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Transcription:

Concurrent Programming is Hard! Concurrent Programming 15 213 / 18 213: Introduction to Computer Systems 23 rd Lecture, April 11, 213 Instructors: Seth Copen Goldstein, Anthony Rowe, and Greg Kesden The human mind tends to be sequential The notion of time is often misleading Thinking about all possible sequences of events in a computer system is at least error prone and frequently impossible 1 2 Concurrent Programming is Hard! Classical problem classes of concurrent programs: Races: outcome depends on arbitrary scheduling decisions elsewhere in the system Example: who gets the last seat on the airplane? Deadlock: improper resource allocation prevents forward progress Example: traffic gridlock Livelock / Starvation / Fairness: external events and/or system scheduling decisions can prevent sub task progress Example: people always jump in front of you in line Many aspects of concurrent programming are beyond the scope of 15 213 but, not all Reminder: Iterative Echo open_clientfd Client / Session Client socket rio_n rio_readlineb Connection request EOF socket bind listen accept rio_readlineb rio_n rio_readlineb open_listenfd Await ion request from next client 3 4

Iterative s Iterative servers process one request at a time client 1 server client 2 accept read ret read accept Wait for Client 1 read ret read Where Does Second Client Block? Second client attempts to to iterative server open_clientfd Client socket rio_n rio_readlineb Connection request Call to returns Even though ion not yet accepted side TCP manager queues request Feature known as TCP listen backlog Call to rio_n returns side TCP manager buffers input data Call to rio_readlineb blocks hasn t written anything for it to read yet. 5 6 Fundamental Flaw of Iterative s ret read User goes out to lunch Client 1 blocks waiting for user to type in data client 1 server client 2 accept read blocks waiting for data from Client 1 Client 2 blocks waiting to read from server Solution: use concurrent servers instead Concurrent servers use multiple concurrent flows to serve multiple clients at the same time 7 concurrency (3 approaches) Allow server to handle multiple clients simultaneously 1. Processes Kernel automatically interleaves multiple logical flows Each flow has its own private address space 2. Threads Kernel automatically interleaves multiple logical flows Each flow shares the same address space 3. I/O multiplexing with select() Programmer manually interleaves multiple logical flows All flows share the same address space Relies on lower level system abstractions 8

Concurrent s: Multiple Processes Spawn separate process for each client call ret call fgets User goes out to lunch Client 1 blocks waiting for user to type in data client 1 server client 2 child 1 fork... call accept ret accept fork call accept ret accept child 2 call read call ret call fgets end read 9 Review: Iterative Echo int main(int argc, char **argv) { int listenfd, connfd; int port = atoi(argv[1]); struct sockaddr_in clientaddr; int clientlen = sizeof(clientaddr); listenfd = Open_listenfd(port); while (1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); echo(connfd); Close(connfd); exit(); Accept a ion request Handle echo requests until client terminates 1 Process Based Concurrent Echo int main(int argc, char **argv) { int listenfd, connfd; int port = atoi(argv[1]); struct sockaddr_in clientaddr; int clientlen=sizeof(clientaddr); Fork separate process for each client Does not allow any communication between different client handlers Signal(SIGCHLD, sigchld_handler); listenfd = Open_listenfd(port); while (1) { connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen); if (Fork() == ) { Close(listenfd); /* Child s its listening socket */ echo(connfd); /* Child services client */ Close(connfd); /* Child s ion with client */ exit(); /* Child exits */ Close(connfd); /* Parent s ed socket (important!) */ Process Based Concurrent Echo (cont) void sigchld_handler(int sig) { while (waitpid(-1,, WNOHANG) > ) ; return; Reap all zombie children 11 12

Process Execution Model Connection Requests Client 1 data Client 1 Process Listening Process Client 2 data Each client handled by independent process No shared state between them Both parent & child have copies of listenfd and connfd Parent must connfd Child must listenfd Client 2 Process 13 Concurrent : accept Illustrated Client clientfd Connection request Client clientfd Client clientfd listenfd(3) listenfd(3) listenfd(3) Child connfd(4) 1. blocks in accept, waiting for ion request on listening descriptor listenfd 2. Client makes ion request by calling 3. returns connfd from accept. Forks child to handle client. Connection is now established between clientfd and connfd 14 Implementation Must dos With Process Based Designs Listening server process must reap zombie children to avoid fatal memory leak Listening server process must its copy of connfd Kernel keeps reference for each socket/open file After fork, refcnt(connfd) = 2 Connection will not be d until refcnt(connfd) == Pros and Cons of Process Based Designs + Handle multiple ions concurrently + Clean sharing model descriptors (no) file tables (yes) global variables (no) + Simple and straightforward Additional overhead for process control Nontrivial to share data between processes Requires IPC (interprocess communication) mechanisms FIFO s (named pipes), System V shared memory and semaphores 15 16

Approach #2: Multiple Threads Traditional View of a Process Very similar to approach #1 (multiple processes) but, with threads instead of processes Process = process context + code, data, and stack Process context Code, data, and stack Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Kernel context: VM structures Descriptor table brk pointer SP brk PC stack shared libraries run-time heap read/ data read-only code/data 17 18 Alternate View of a Process Process = thread + code, data, and kernel context SP Thread (main thread) stack Thread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) brk PC Code and Data shared libraries run-time heap read/ data read-only code/data Kernel context: VM structures Descriptor table brk pointer A Process With Multiple Threads Multiple threads can be associated with a process Each thread has its own logical control flow Each thread shares the same code, data, and kernel context Share common virtual address space (inc. stacks) Each thread has its own thread id (TID) Thread 1 (main thread) stack 1 Thread 1 context: Data registers Condition codes SP1 PC1 Shared code and data shared libraries run-time heap read/ data read-only code/data Kernel context: VM structures Descriptor table brk pointer Thread 2 (peer thread) stack 2 Thread 2 context: Data registers Condition codes SP2 PC2 19 2

Logical View of Threads Threads associated with process form a pool of peers Unlike processes which form a tree hierarchy Threads associated with process foo Process hierarchy Thread Execution Single Core Processor Simulate concurrency by time slicing Multi Core Processor Can have true concurrency T1 T2 shared code, data and kernel context T4 P P1 sh sh sh Thread A Thread B Thread C Thread A Thread B Thread C Time T5 T3 foo bar Run 3 threads on 2 cores 21 22 Logical Concurrency Two threads are (logically) concurrent if their flows overlap in time Otherwise, they are sequential Examples: Concurrent: A & B, A&C Sequential: B & C Time Thread A Thread B Thread C Threads vs. Processes How threads and processes are similar Each has its own logical control flow Each can run concurrently with others (possibly on different cores) Each is context switched How threads and processes are different Threads share code and some data Processes (typically) do not Threads are somewhat less expensive than processes Process control (creating and reaping) twice as expensive as thread control Linux numbers: ~2K cycles to create and reap a process ~1K cycles (or less) to create and reap a thread 23 24

Posix Threads (Pthreads) Interface Pthreads: Standard interface for ~6 functions that manipulate threads from C programs Creating and reaping threads pthread_create() pthread_join() Determining your thread ID pthread_self() Terminating threads pthread_cancel() pthread_exit() exit() [terminates all threads], RET [terminates current thread] Synchronizing access to shared variables pthread_mutex_init pthread_mutex_[un]lock pthread_cond_init pthread_cond_[timed]wait 25 The Pthreads "hello, world" Program /* * hello.c - Pthreads "hello, world" program */ #include "csapp.h" void *thread(void *vargp); int main() { pthread_t tid; Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(); /* thread routine */ void *thread(void *vargp) { printf("hello, world!\n"); return NULL; Thread attributes (usually NULL) Thread arguments (void *p) return value (void **p) 26 Execution of Threaded hello, world Thread Based Concurrent Echo call Pthread_create() Pthread_create() returns call Pthread_join() main thread waits for peer thread to terminate Pthread_join() returns exit() terminates main thread and any peer threads main thread peer thread printf() return NULL; (peer thread terminates) 27 int main(int argc, char **argv) { int port = atoi(argv[1]); struct sockaddr_in clientaddr; int clientlen=sizeof(clientaddr); pthread_t tid; int listenfd = Open_listenfd(port); while (1) { int *connfdp = Malloc(sizeof(int)); *connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen); Pthread_create(&tid, NULL, echo_thread, connfdp); Spawn new thread for each client Pass it copy of ion file descriptor Note use of Malloc()! Without corresponding Free() 28

Thread Based Concurrent (cont) Threaded Execution Model /* thread routine */ void *echo_thread(void *vargp) { int connfd = *((int *)vargp); Pthread_detach(pthread_self()); Free(vargp); echo(connfd); Close(connfd); return NULL; Connection Requests Client 1 data Client 1 Listening Client 2 Client 2 data Run thread in detached mode Runs independently of other threads Reaped automatically (by kernel) when it terminates Free storage allocated to hold clientfd Producer Consumer model Multiple threads within single process Some state between them e.g., file descriptors 29 3 Potential Form of Unintended Sharing main thread while (1) { int connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen); Pthread_create(&tid, NULL, echo_thread, (void *) &connfd); connfd = connfd 1 Main thread stack connfd Main Could this race occur? int i; for (i = ; i < 1; i++) { Pthread_create(&tid, NULL, thread, &i); Thread void *thread(void *vargp) { int i = *((int *)vargp); Pthread_detach(pthread_self()); save_value(i); return NULL; connfd = connfd 2 Race! peer 1 connfd = *vargp Peer 1 stack vargp Race Test If no race, then each thread would get different value of i Set of saved values would consist of one copy each of through 99 peer 2 connfd = *vargp Peer 2 stack vargp Why would both copies of vargp point to same location? 31 32

Experimental Results No Race 2 1 2 4 6 8 1 12 14 16 18 2 22 24 26 28 3 32 34 36 38 4 42 44 46 48 5 52 54 56 58 6 62 64 66 68 7 72 74 76 78 8 82 84 86 88 9 92 94 96 98 Single core laptop 3 2 1 2 4 6 8 1 12 14 16 18 2 22 24 26 28 3 32 34 36 38 4 42 44 46 48 5 52 54 56 58 6 62 64 66 68 7 72 74 76 78 8 82 84 86 88 9 92 94 96 98 Multicore server 14 12 1 8 6 4 2 2 4 6 8 112141618222242628332343638442444648552545658662646668772747678882848688992949698 The race can really happen! 33 Issues With Thread Based s Must run detached to avoid memory leak At any point in time, a thread is either joinable or detached Joinable thread can be reaped and killed by other threads must be reaped (with pthread_join) to free memory resources Detached thread cannot be reaped or killed by other threads resources are automatically reaped on termination Default state is joinable use pthread_detach(pthread_self()) to make detached Must be careful to avoid unintended sharing For example, passing pointer to main thread s stack Pthread_create(&tid, NULL, thread, (void *)&connfd); All functions called by a thread must be thread safe (next lecture) 34 Pros and Cons of Thread Based Designs + Easy to share data structures between threads e.g., logging information, file cache + Threads are more efficient than processes Unintentional sharing can introduce subtle and hardto reproduce errors! The ease with which data can be shared is both the greatest strength and the greatest weakness of threads Hard to know which data shared & which private Hard to detect by testing Probability of bad race outcome very low But nonzero! Future lectures 35 Approaches to Concurrency Processes Hard to share resources: Easy to avoid unintended sharing High overhead in adding/removing clients Threads Easy to share resources: Perhaps too easy Medium overhead Not much control over scheduling policies Difficult to debug Event orderings not repeatable I/O Multiplexing Tedious and low level Total control over scheduling Very low overhead Cannot create as fine grained a level of concurrency Does not make use of multi core 36

View from s TCP Manager Client 1 Client 2 srv>./echoserverp 15213 cl1>./echoclient greatwhite.ics.cs.cmu.edu 15213 srv> ed to (128.2.192.34), port 5437 cl2>./echoclient greatwhite.ics.cs.cmu.edu 15213 srv> ed to (128.2.25.225), port 41656 Connection Host Port Host Port Listening --- --- 128.2.22.1 15213 cl1 128.2.192.34 5437 128.2.22.1 15213 cl2 128.2.25.225 41656 128.2.22.1 15213 View from s TCP Manager Connection Host Port Host Port Listening --- --- 128.2.22.1 15213 cl1 128.2.192.34 5437 128.2.22.1 15213 cl2 128.2.25.225 41656 128.2.22.1 15213 Port Demultiplexing TCP manager maintains separate stream for each ion Each represented to application program as socket New ions directed to listening socket Data from clients directed to one of the ion sockets 37 38