Concurrent Programming

Similar documents
Concurrent Programming

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

Concurrent programming

Concurrent Programming

Lecture #23 Concurrent Programming

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

Concurrent Programming

Concurrent Programming November 28, 2007

Concurrent Programming

Concurrent Programming

Concurrent Programming April 21, 2005

Computer Systems Laboratory Sungkyunkwan University

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

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

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

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 PROGRAMMING. Lecture 5

Concurrent Servers Dec 2, 2009"

Concurrent Programming

Carnegie Mellon Performance of Mul.threaded Code

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

Proxy: Concurrency & Caches

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

Concurrent Programming

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

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

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

Recitation 14: Proxy Lab Part 2

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

Today. Threads review Sharing Mutual exclusion Semaphores

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

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

CS333 Intro to Operating Systems. Jonathan Walpole

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

Synchronization: Basics

Lecture 2: Processes. CSE 120: Principles of Opera9ng Systems. UC San Diego: Summer Session I, 2009 Frank Uyeda

Recitation 14: Proxy Lab Part 2

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Synchronization: Advanced

CS 5523 Operating Systems: Thread and Implementation

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

Threads. Jo, Heeseung

Process Synchroniza/on

pthreads CS449 Fall 2017

CS Operating Systems: Threads (SGG 4)

Network Programming / : Introduc2on to Computer Systems 21 st Lecture, April. 4, Instructors: Todd Mowry and Anthony Rowe

Introduction to PThreads and Basic Synchronization

University of Washington What is a process?

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

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

Network Programming November 3, 2008

CS 3723 Operating Systems: Final Review

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole

Concurrency on x86-64; Threads Programming Tips

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

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

CS 3305 Intro to Threads. Lecture 6

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

Synchroniza+on: Advanced

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

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

Last class: Today: Thread Background. Thread Systems

Synchronization: Basics

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Introduction to Threads

CS 261 Fall Mike Lam, Professor. Threads

Network Programming /18-243: Introduc3on to Computer Systems 22 nd Lecture, 13 April Instructors: Bill Nace and Gregory Kesden

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Carnegie Mellon Concurrency and Synchronization

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

CSE 153 Design of Operating Systems Fall 2018

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

CS 5523: Operating Systems

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

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

LSN 13 Linux Concurrency Mechanisms

Threads. Jo, Heeseung

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Processes, Threads, SMP, and Microkernels

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

CS370 Operating Systems

Concurrent Programming. Concurrent Programming with Processes and Threads

THREADS. Jo, Heeseung

Multithreaded Programming

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

Computer Systems A Programmer s Perspective 1 (Beta Draft)

Single and mul,threaded processes

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

CSE Opera,ng System Principles

High Performance Computing Course Notes Shared Memory Parallel Programming

CS370 Operating Systems

Parallel Programming with Threads

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Transcription:

Concurrent Programming 15-213: Introduc0on to Computer Systems 22 nd Lecture, Nov. 11, 2010 Instructors: Randy Bryant and Dave O Hallaron 1

Concurrent Programming is Hard! The human mind tends to be sequen9al The no9on of 9me is o<en misleading Thinking about all possible sequences of events in a computer system is at least error prone and frequently impossible 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 alloca0on prevents forward progress Example: traffic gridlock Livelock / Starva4on / 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 3

Itera9ve Echo Server Client socket Server socket bind open_listenfd open_clientfd connect Connec9on request listen accept Client / Server Session rio_writen rio_readlineb rio_readlineb rio_writen Await connec9on request from next client close EOF rio_readlineb close 4

Itera9ve Servers Itera9ve servers process one request at a 9me client 1 server client 2 connect accept connect write call read ret read read write write call read close close accept Wait for Client 1 read write ret read 5

Where Does Second Client Block? open_clientfd Second client asempts to connect to itera9ve server Client socket connect rio_writen rio_readlineb Connec9on request Call to connect returns Even though connec0on not yet accepted Server side TCP manager queues request Feature known as TCP listen backlog Call to rio_writen returns Server side TCP manager buffers input data Call to rio_readlineb blocks Server hasn t wrizen anything for it to read yet. 6

Fundamental Flaw of Itera9ve Servers client 1 server client 2 connect write call read ret read accept read write connect write call read User goes out to lunch Client 1 blocks waiting for user to type in data Server blocks waiting for data from Client 1 Client 2 blocks waiting to read from server Solu9on: use concurrent servers instead Concurrent servers use mul0ple concurrent flows to serve mul0ple clients at the same 0me 7

Crea9ng Concurrent Flows Allow server to handle mul0ple clients simultaneously 1. Processes Kernel automa0cally interleaves mul0ple logical flows Each flow has its own private address space 2. Threads Kernel automa0cally interleaves mul0ple logical flows Each flow shares the same address space 3. I/O mul9plexing with select() Programmer manually interleaves mul0ple logical flows All flows share the same address space Relies on lower- level system abstrac0ons 8

Concurrent Servers: Mul9ple Processes Spawn separate process for each client client 1 server client 2 call connect ret connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data child 1 call read fork... call accept ret accept fork call accept ret accept child 2 call read write close call connect ret connect call fgets write call read end read close 9

Review: Itera9ve Echo Server 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(0); Accept a connec0on request Handle echo requests un0l client terminates 10

Process- Based Concurrent Server 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() == 0) { Close(listenfd); /* Child closes its listening socket */ echo(connfd); /* Child services client */ Close(connfd); /* Child closes connection with client */ exit(0); /* Child exits */ } Close(connfd); /* Parent closes connected socket (important!) */ } 11

Process- Based Concurrent Server (cont) void sigchld_handler(int sig) { while (waitpid(-1, 0, WNOHANG) > 0) ; return; } Reap all zombie children 12

Process Execu9on Model Connection Requests Listening Server Process Client 1 data Client 1 Server Process Client 2 Server 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 close connfd Child must close listenfd 13

Concurrent Server: accept Illustrated Client clientfd listenfd(3) Server 1. Server blocks in accept, wai4ng for connec4on request on listening descriptor listenfd Client Connec9on request listenfd(3) Server 2. Client makes connec4on request by calling and blocking in connect clientfd Client clientfd listenfd(3) Server Child connfd(4) Server 3. Server returns connfd from accept. Forks child to handle client. Client returns from connect. Connec4on is now established between clientfd and connfd 14

Implementa9on Must- dos With Process- Based Designs Listening server process must reap zombie children to avoid fatal memory leak Listening server process must close its copy of connfd Kernel keeps reference for each socket/open file Aaer fork, refcnt(connfd) = 2 Connec0on will not be closed un0l refcnt(connfd) == 0 15

View from Server s TCP Manager Client 1 Client 2 Server srv>./echoserverp 15213 cl1>./echoclient greatwhite.ics.cs.cmu.edu 15213 srv> connected to (128.2.192.34), port 50437 cl2>./echoclient greatwhite.ics.cs.cmu.edu 15213 srv> connected to (128.2.205.225), port 41656 Connec9on Host Port Host Port Listening --- --- 128.2.220.10 15213 cl1 128.2.192.34 50437 128.2.220.10 15213 cl2 128.2.205.225 41656 128.2.220.10 15213 16

View from Server s TCP Manager Connec9on Host Port Host Port Listening --- --- 128.2.220.10 15213 cl1 128.2.192.34 50437 128.2.220.10 15213 cl2 128.2.205.225 41656 128.2.220.10 15213 Port Demul9plexing TCP manager maintains separate stream for each connec0on Each represented to applica0on program as socket New connec0ons directed to listening socket Data from clients directed to one of the connec0on sockets 17

Pros and Cons of Process- Based Designs + Handle mul9ple connec9ons concurrently + Clean sharing model descriptors (no) file tables (yes) global variables (no) + Simple and straigh^orward Addi9onal overhead for process control Nontrivial to share data between processes Requires IPC (interprocess communica0on) mechanisms FIFO s (named pipes), System V shared memory and semaphores 18

Approach #2: Mul9ple Threads Very similar to approach #1 (mul9ple processes) but, with threads instead of processes 19

Tradi9onal View of a Process 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 0 stack shared libraries run-time heap read/write data read-only code/data 20

Alternate View of a Process Process = thread + code, data, and kernel context Thread (main thread) Code and Data SP stack Thread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) brk PC 0 shared libraries run-time heap read/write data read-only code/data Kernel context: VM structures Descriptor table brk pointer 21

A Process With Mul9ple Threads Mul9ple 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) Shared code and data Thread 2 (peer thread) stack 1 Thread 1 context: Data registers Condition codes SP1 PC1 0 shared libraries run-time heap read/write data read-only code/data Kernel context: VM structures Descriptor table brk pointer stack 2 Thread 2 context: Data registers Condition codes SP2 PC2 22

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 T1 T2 shared code, data and kernel context T4 P0 P1 sh sh sh T5 T3 foo bar 23

Thread Execu9on Single Core Processor Simulate concurrency by 0me slicing Mul9- Core Processor Can have true concurrency Thread A Thread B Thread C Thread A Thread B Thread C Time Run 3 threads on 2 cores 24

Logical Concurrency Two threads are (logically) concurrent if their flows overlap in 9me Otherwise, they are sequen9al Examples: Concurrent: A & B, A&C Sequen0al: B & C Thread A Thread B Thread C Time 25

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 (crea0ng and reaping) is twice as expensive as thread control Linux numbers: ~20K cycles to create and reap a process ~10K cycles (or less) to create and reap a thread 26

Posix Threads (Pthreads) Interface Pthreads: Standard interface for ~60 func9ons that manipulate threads from C programs Crea0ng and reaping threads pthread_create() pthread_join() Determining your thread ID pthread_self() Termina0ng 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 27

The Pthreads "hello, world" Program /* * hello.c - Pthreads "hello, world" program */ #include "csapp.h" void *thread(void *vargp); Thread attributes (usually NULL) int main() { pthread_t tid; } Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0); /* thread routine */ void *thread(void *vargp) { printf("hello, world!\n"); return NULL; } Thread arguments (void *p) return value (void **p) 28

Execu9on of Threaded hello, world main thread call Pthread_create() Pthread_create() returns call Pthread_join() main thread waits for peer thread to terminate Pthread_join() returns peer thread printf() return NULL; (peer thread terminates) exit() terminates main thread and any peer threads 29

Thread- Based Concurrent Echo Server 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 connec0on file descriptor Note use of Malloc()! Without corresponding Free() 30

Thread- Based Concurrent Server (cont) /* thread routine */ void *echo_thread(void *vargp) { int connfd = *((int *)vargp); Pthread_detach(pthread_self()); Free(vargp); echo(connfd); Close(connfd); return NULL; } Run thread in detached mode Runs independently of other threads Reaped when it terminates Free storage allocated to hold clienmd Producer- Consumer model 31

Threaded Execu9on Model Connection Requests Listening Server Client 1 data Client 1 Server Client 2 Server Client 2 data Mul0ple threads within single process Some state between them File descriptors 32

Poten9al Form of Unintended Sharing } while (1) { int connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen); Pthread_create(&tid, NULL, echo_thread, (void *) &connfd); } main thread connfd = connfd 1 Main thread stack connfd peer 1 Peer 1 stack connfd = connfd 2 Race! connfd = *vargp vargp peer 2 connfd = *vargp Peer 2 stack vargp Why would both copies of vargp point to same location? 33

Could this race occur? Main int i; for (i = 0; i < 100; 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; } 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 0 through 99. 34

Experimental Results 2 1 0 No Race 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 Single core laptop 3 2 1 0 Mul9core server 14 12 10 8 6 4 2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 Carnegie Mellon 0 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 The race can really happen! 35

Issues With Thread- Based Servers Carnegie Mellon Must run detached to avoid memory leak. At any point in 0me, 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 automa0cally reaped on termina0on. 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 func9ons called by a thread must be thread- safe Stay tuned 36

Pros and Cons of Thread- Based Designs + Easy to share data structures between threads e.g., logging informa0on, file cache. + Threads are more efficient than processes. Uninten9onal sharing can introduce subtle and hard- to- 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 tes0ng Probability of bad race outcome very low But nonzero! Future lectures 37

Event- Based Concurrent Servers Using I/O Mul9plexing Use library func9ons to construct scheduler within single process Server maintains set of ac9ve connec9ons Array of connfd s Repeat: Determine which connec0ons have pending inputs If listenfd has input, then accept connec0on Add new connfd to array Service all connfd s with pending inputs Details in book 38

I/O Mul9plexed Event Processing Read Ac9ve Descriptors listenfd = 3 Pending Inputs listenfd = 3 clientfd clientfd 0 10 10 1 7 Active 7 2 4 4 3 4-1 -1 Inactive -1-1 5 6 12 5 Active 12 5 7-1 -1 8 9-1 -1 Never Used -1-1 39

Pros and Cons of I/O Mul9plexing + One logical control flow. + Can single- step with a debugger. + No process or thread control overhead. Design of choice for high- performance Web servers and search engines. Significantly more complex to code than process- or thread- based designs. Hard to provide fine- grained concurrency E.g., our example will hang up with par0al lines. Cannot take advantage of mul9- core Single thread of control 40

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 Mul9plexing 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 mul0- core 41