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

Similar documents
Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

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

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

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

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

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

Concurrent Programming April 21, 2005

Concurrent Programming

Concurrent Servers Dec 2, 2009"

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

Concurrent Programming November 28, 2007

Lecture #23 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

Concurrent Programming

Approaches to Concurrency

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

Concurrent Programming

CONCURRENT PROGRAMMING. Lecture 5

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Concurrent Programming

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Proxy: Concurrency & Caches

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

I/O Models. Kartik Gopalan

Carnegie Mellon Performance of Mul.threaded Code

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

CISC2200 Threads Spring 2015

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

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

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

Threads. Jo, Heeseung

Εργαστήριο 9 I/O Multiplexing

Today. Threads review Sharing Mutual exclusion Semaphores

pthreads CS449 Fall 2017

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

ANSI/IEEE POSIX Standard Thread management

CS 5523 Operating Systems: Thread and Implementation

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

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

NETWORK AND SYSTEM PROGRAMMING. I/O Multiplexing: select and poll function

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

CS Operating Systems: Threads (SGG 4)

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

CSE 306/506 Operating Systems Threads. YoungMin Kwon

CS510 Operating System Foundations. Jonathan Walpole

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Last class: Today: Thread Background. Thread Systems

CS 3305 Intro to Threads. Lecture 6

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

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

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

LSN 13 Linux Concurrency Mechanisms

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

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

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Introduction to PThreads and Basic Synchronization

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Threads. studykorner.org

CS 3723 Operating Systems: Final Review

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Threads. Jo, Heeseung

CS510 Operating System Foundations. Jonathan Walpole

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

THREADS. Jo, Heeseung

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

Concurrent Server Design Multiple- vs. Single-Thread

Computer Systems Laboratory Sungkyunkwan University

ECE 650 Systems Programming & Engineering. Spring 2018

COMP/ELEC 429/556 Introduction to Computer Networks

Synchronization: Basics

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Concurrency. Johan Montelius KTH

CS 261 Fall Mike Lam, Professor. Threads

Processes, Threads, SMP, and Microkernels

Introduction to Client-Server Model

ECE 650 Systems Programming & Engineering. Spring 2018

Threads. Threads (continued)

Light-Weight Processes: Dissecting Linux Threads

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Transcription:

Concurrent Programming Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet, SOCK_STREAM, 0); bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(inaddr_any); saddr.sin_port = htons(port); bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)); listen(listenfd, 5); while (1) { connfd = accept(listenfd, (struct sockaddr *)&caddr, &clen); while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ( got %d bytes from client.\n, n); write(connfd, buf, n); close(connfd); 2

Iterative Servers (1) One request at a time client 1 server client 2 call connect ret connect call read ret read close call accept ret accept write close call accept ret accept write close call connect ret connect call read ret read close 3

Iterative Servers (2) Fundamental flaw call connect ret connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data client 1 server client 2 Server blocks waiting for data from Client 1 call accept ret accept call read Solution: use concurrent servers instead Use multiple concurrent flows to serve multiple clients at the same time. call connect Client 2 blocks waiting to complete its connection request until after lunch! 4

Creating Concurrent Flows Processes Kernel automatically interleaves multiple logical flows. Each flow has its own private address space. Threads Kernel automatically interleaves multiple logical flows. Each flow shares the same address space. Hybrid of processes and I/O multiplexing I/O multiplexing with select() User manually interleaves multiple logical flows Each flow shares the same address space Popular for high-performance server designs. 5

Concurrent Programming Process-based

Process-based Servers client 1 server client 2 call accept call connect call connect ret connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data call read child 1 fork... ret accept fork call accept ret accept child 2 call read write close ret connect call fgets write call read end read close 7

Echo Server Iterative version int main (int argc, char *argv[]) {... while (1) { connfd = accept (listenfd, (struct sockaddr *)&caddr, &caddrlen)); while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ( got %d bytes from client.\n, n); write(connfd, buf, n); close(connfd); 8

Echo Server: Process-based int main (int argc, char *argv[]) {... signal (SIGCHLD, handler); while (1) { connfd = accept (listenfd, (struct sockaddr *)&caddr, &caddrlen)); if (fork() == 0) { close(listenfd); while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ( got %d bytes from client.\n, n); write(connfd, buf, n); close(connfd); exit(0); close(connfd); void handler(int sig) { pid_t pid; int stat; while ((pid = waitpid(-1, &stat, WNOHANG)) > 0); return; 9

Implementation Issues Servers should restart accept() if it is interrupted by a transfer of control to the SIGCHLD handler Not necessary for systems with POSIX signal handling. Required for portability on some older Unix systems. Server must reap zombie children to avoid fatal memory leak Server must close its copy of connfd. Kernel keeps reference for each socket. After fork(), refcnt(connfd) = 2 Connection will not be closed until refcnt(connfd) = 0 10

Process-based Designs Pros Handles multiple connections concurrently. Clean sharing model. Descriptors (no), file tables (yes), global variables (no) Simple and straightforward. Cons Additional overhead for process control. Process creation and termination Process switching Nontrivial to share data between processes. Requires IPC (InterProcess Communication) mechanisms: FIFO s, System V shared memory and semaphores 11

Concurrent Programming Thread-based

Traditional View Process = process context + address space Process context Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Kernel context: VM structures Descriptor table brk pointer SP brk PC 0 Code, data, and stack stack shared libraries run-time heap read/write data read-only code/data 13

Alternate View Process = thread context + kernel context + address space 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 14

A Process with Multiple Threads Multiple threads can be associated with a process. Each thread has its own logical control flow (sequence of PC values) Each thread shares the same code, data, and kernel context Each thread has its own thread id (TID) Thread 1 (main thread) Shared code and data stack 1 Thread 1 context: Data registers Condition codes SP1 PC1 shared libraries run-time heap read/write data read-only code/data 0 Kernel context: VM structures Descriptor table brk pointer Thread 2 (peer thread) stack 2 Thread 2 context: Data registers Condition codes SP2 PC2 15

Logical View of Threads Threads associated with a 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 16

Threads vs. Processes How threads and processes are similar Each has its own logical control flow. Each can run concurrently. Each is context switched. How threads and processes are different Threads share code and data, processes (typically) do not. Threads are somewhat less expensive than processes. Linux 2.4 Kernel, 512MB RAM, 2 CPUs -> 1,811 forks()/second -> 227,611 threads/second (125x faster) 17

Pthreads Interface POSIX Threads Interface Creating and reaping threads pthread_create() pthread_join() Determining your thread ID pthread_self() Terminating threads pthread_cancel() pthread_exit() exit (terminates all threads), return (terminates current thread) Synchronizing access to shared variables pthread_mutex_init() pthread_mutex_[un]lock() pthread_cond_init() pthread_cond_[timed]wait() pthread_cond_signal(), etc. 18

hello, world Program (1) /* * hello.c - Pthreads "hello, world" program */ #include pthread.h" void *thread(void *vargp); 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 attributes (usually NULL) Thread arguments (void *p) return value (void **p) 19

hello, world Program (2) Execution 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 20

Echo Server: Thread-based int main (int argc, char *argv[]) { int *connfdp; pthread_t tid;... while (1) { connfdp = (int *) malloc(sizeof(int)); *connfdp = accept (listenfd, (struct sockaddr *)&caddr, &caddrlen)); void *thread_main(void *arg) { int n; char buf[maxline]; int connfd = *((int *)arg); pthread_detach(pthread_self()); free(arg); while((n = read(connfd, buf, MAXLINE)) > 0) write(connfd, buf, n); pthread_create(&tid, NULL, thread_main, connfdp); close(connfd); return NULL; 21

Implementation Issues (1) 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. Exit state and return value are not saved. Default state is joinable. Use pthread_detach(pthread_self()) to make detached. 22

Implementation Issues (2) Must be careful to avoid unintended sharing For example, what happens if we pass the address connfd to the thread routine? int connfd;... pthread_create(&tid, NULL, thread_main, &connfd);... All functions called by a thread must be thread-safe. A function is said to be thread-safe or reentrant, when the function may be called by more than one thread at a time without requiring any other action on the caller s part. 23

Thread-based Designs Pros Easy to share data structures between threads. e.g., logging information, file cache, etc. Threads are more efficient than processes. Cons Unintentional 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. 24

Concurrent Programming Event-based

I/O Multiplexing Event-based Concurrent Servers Maintain a pool of connected descriptors. Repeat the following forever: Use the Unix select() system call to block until:» (a) New connection request arrives on the listening descriptor.» (b) New data arrives on an existing connected descriptor. If (a), add the new connection to the pool of connections. If (b), read any available data from the connection» Close connection on EOF and remove it from the pool. I/O multiplexing provides more control with less overhead. 26

select() int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) readfds n Opaque bit vector (max FD_SETSIZE bits) that indicates membership in a descriptor set. If bit k is 1, then descriptor k is a member of the descriptor set. Maximum descriptor in descriptor set plus 1 Tests descriptors 0, 1, 2,, n-1 for set membership. select() returns the number of ready descriptors and sets each bit of readfds/writefds/exceptfds to indicate the ready status of its corresponding descriptor. 27

Macros Macros for Manipulating Set Descriptors void FD_ZERO(fd_set *fdset) Turn off all bits in fdset void FD_SET(int fd, fd_set *fdset) Turn on bit fd in fdset void FD_CLR (int fd, fd_set *fdset) Turn off bit fd in fdset int FD_ISSET (int fd, fd_set *fdset) Is bit fd in fdset turned on? 28

Echo Server: Event-based (1) typedef struct { int maxfd; // largest descriptor in read_set int nready; // number of ready desc. from select fd_set read_set; // set of all active descriptors fd_set ready_set; // subset of desc. ready for reading pool; int main (int argc, char *argv[]) { int listenfd, connfd, val; pool p;... listenfd =... // socket(), bind(), listen() // initialize pool p.maxfd = listenfd; FD_ZERO(&p.read_set); FD_SET(listenfd, &p.read_set); 29

Echo Server: Event-based (2) while (1) { p.ready_set = p.read_set; p.nready = select(p.maxfd+1, &p.ready_set, NULL, NULL, NULL); if (FD_ISSET(listenfd, &p.ready_set)) { connfd = accept (listenfd, (struct sockaddr *)&caddr, &caddrlen)); FD_SET(connfd, &p.read_set); if (connfd > p.maxfd) p.maxfd = connfd; p.nready--; check_clients (listenfd, &p); 30

Echo Server: Event-based (3) void check_clients (int listenfd, pool *p) { int s, n; char buf[maxline]; for (s = 0; s < p->maxfd+1 && p->nready > 0; s++) { if (s == listenfd) continue; if (FD_ISSET(s, &p->read_set) && FD_ISSET(s, &p->ready_set)) { p->nready--; if ((n = read(s, buf, MAXLINE)) > 0) write(s, buf, n); if (n == 0) { // EOF close(s); FD_CLR(s, &p->read_set); if (s == p->maxfd) { p->maxfd--; while (!FD_ISSET(p->maxfd, &p->read_set)) p->maxfd--; 31

Event-based Designs Pros One logical control flow Can single-step with a debugger No process or thread control overhead Cons Design of choice for high-performance Web servers and search engines Significantly more complex to code than process- or thread-based designs Can be vulnerable to Denial-of-Service attack! 32