CONCURRENT PROGRAMMING. Lecture 5

Similar documents
Concurrent Programming November 28, 2007

Concurrent Programming April 21, 2005

Concurrent Servers Dec 2, 2009"

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

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

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

Concurrent Programming

Lecture #23 Concurrent Programming

Concurrent Programming

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Concurrent Programming

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

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

Concurrent programming

Concurrent Programming

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

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

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

Concurrent Programming

Concurrent Programming

Network Programming September 6, 2005

Computer Systems Laboratory Sungkyunkwan University

I/O Models. Kartik Gopalan

Network Programming November 3, 2008

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

CISC2200 Threads Spring 2015

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

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

Approaches to Concurrency

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

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

Concurrency. Alan L. Cox Some slides adapted from CMU slides

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

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

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

COMP/ELEC 429/556 Introduction to Computer Networks

CS118 Discussion Week 2. Taqi

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

Global Employee Location Server

ECE 650 Systems Programming & Engineering. Spring 2018

CS118 Discussion 1A, Week 3. Zengwen Yuan Dodd Hall 78, Friday 10:00 11:50 a.m.

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 ~

Concurrent Programming

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

ISA 563: Fundamentals of Systems Programming

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

Any of the descriptors in the set {1, 4} have an exception condition pending

CS4514 HELP Session 3. Concurrent Server (in APP) Using Go-Back-N (in DLL)

Concurrent Programming

CSE 333 Lecture fork, pthread_create, select

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

ECE 650 Systems Programming & Engineering. Spring 2018

Introduction to Client-Server Model

Network Programming TDC 561

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Internetworking II: Network programming. April 20, 2000

Group-A Assignment No. 6

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

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

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

#include <sys/socket.h> int listen(int socket, int backlog); socket. socket. backlog

Recitation 14: Proxy Lab Part 2

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

serverbase.txt (1) Server Client1 (3) (2) Client2 Child Process1 Child Process2 Note: each child process keeps a separate copy of the DB.

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Piotr Mielecki Ph. D.

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

Experiential Learning Workshop on Basics of Socket Programming

Problem Set: Processes

CSE 153 Design of Operating Systems Fall 2018

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Part Two - Process Management. Chapter 3: Processes

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

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

CSC209 Review. Yeah! We made it!

Socket Programming TCP UDP

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

Project 1: A Web Server Called Liso

Operating System Structure

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Network Games Part II. Architecture of Network Game

Concurrent Server Using Selective Repeat Data Link Layer

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Network Programming: Part I

Asynchronous Events on Linux

Today: VM wrap-up Select (if time) Course wrap-up Final Evaluations

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Problem Set: Processes

Example Questions for Midterm EE122, Fall 2008 EECS Berkeley

Redesde Computadores(RCOMP)

Transcription:

CONCURRENT PROGRAMMING Lecture 5

Last lecture TCP I/O RIO: buffered I/O. Unix IO Question: difference between send() and write()? MSG_DONTWAIT, MSG_NOSIGNAL 2

Concurrent Programming is Hard! 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 frequen tly impossible 3

Concurrent Programming is Hard! Classical problem classes of concurrent programs: Races: outcome depends on arbitrary scheduling decisio ns elsewhere in the system Example: who gets the last seat on the airplane? Deadlock: improper resource allocation prevents forwar d progress Example: traffic gridlock Livelock / Starvation / Fairness: external events and/or s ystem scheduling decisions can prevent sub-task progre ss Example: people always jump in front of you in line 4

Iterative Echo Server Client socket Server socket bind open_listenfd open_clientfd connect Connection request listen accept Client / Server Session rio_writen rio_readlineb rio_readlineb rio_writen Await connection request from next client close EOF rio_readlineb close 5

Iterative Servers Iterative servers process one request at a time client 1 server client 2 connect write call read ret read accept read write connect write call read close close accept Wait for Client 1 read write ret read 6

Where Does Second Client Block? Second client attempts to conne ct to iterative server Client open_clientfd socket connect rio_writen Connection request Call to connect returns Even though connection not yet accepted Server side TCP manager que ues request Feature known as TCP listen backlog Call to rio_writen returns Server side TCP manager buff ers input data Call to rio_readlineb blocks Server hasn t written anything for it to read yet. rio_readlineb 7

Fundamental Flaw of Iterative Servers User goes out to lunch connect write call read ret read Client 1 blocks waiting for user to type in data client 1 server client 2 accept read write Server blocks waiting for data from Client 1 connect write call read Client 2 blocks waiting to read from server Solution: use concurrent servers instead Concurrent servers use multiple concurrent flows to serve multip le clients at the same time 8

Creating Concurrent Flows 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 9

Concurrent Servers: Multiple 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 10

Review: Iterative 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 connection request Handle echo requests until client terminates 11

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!) */ } 12

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

Process Execution Model Connection Requests Client 1 data Client 1 Server Process Listening 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 14

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

Implementation 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 con nfd Kernel keeps reference for each socket/open file After fork, refcnt(connfd) = 2 Connection will not be closed until refcnt(connfd) == 0 16

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 Connection 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 17

Pros and Cons of Process-Based Designs + Handle multiple connections concurrently + Clean sharing model 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 18

#2) Event-Based Concurrent Servers Using I/O Multiplex ing Use library functions to construct scheduler within sing le process Server maintains set of active connections Array of connfd s Repeat: Determine which connections have pending inputs If listenfd has input, then accept connection Add new connfd to array Service all connfd s with pending inputs 19

Adding Concurency: Step One Start with allowing address re-use int sock, opts; sock = socket( ); // getting the current options setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts)); Then we set the socket to non-blocking // getting current options if (0 > (opts = fcntl(sock, F_GETFL))) printf( Error \n ); // modifying and applying opts = (opts O_NONBLOCK); if (fcntl(sock, F_SETFL, opts)) printf( Error \n ); bind( ); 20

Adding Concurrency: Step Two Monitor sockets with select() int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout); So what s an fd_set? Bit vector with FD_SETSIZE bits maxfd Max file descriptor + 1 readfs Bit vector of read descriptors to monitor writefds Bit vector of write descriptors to monitor exceptfds Read the manpage, set to NULL timeout How long to wait with no activity before retur ning, NULL for eternity 21

How does the code change? if (listen(sockfd, 5) < 0) { // listen for incoming connections printf( Error listening\n ); init_pool(sockfd, &pool); while (1) { pool.ready_set = &pool.read_set; pool.nready = select(pool.maxfd+1, &pool.ready_set, &pool.write_set, NULL, NULL); if (FD_ISSET(sockfd, &pool.ready_set)) { if (0 > (isock = accept(sockfd, ))) printf( Error accepting\n ); add_client(isock, &caddr, &pool); } check_clients(&pool); } // close it up down here 22

What was pool? A struct something like this: typedef struct s_pool { int maxfd; // largest descriptor in sets fd_set read_set; // all active read descriptors fd_set write_set; // all active write descriptors fd_set ready_set; // descriptors ready for reading int nready; // return of select() int maxi; /* highwater index into client array */ int clientfd[fd_setsize]; /* set of active descriptors */ rio_t clientrio[fd_setsize]; /* set of active read buffers */ } pool; 23

void init_pool(int sockfd, pool * p); { int i; p->maxfd = -1; for (i=0;i<fd_setsize;i++) p->clientfd[i]=-1; p->maxfd = sockfd; FD_ZERO(&p->read_set); FD_SET(listenfd, &p->read_set); } // close it up down here 24

void add_client(int connfd, pool *p) { int i; p->nready--; for (i = 0; i < FD_SETSIZE; i++) /* Find an available slot */ if (p->clientfd[i] < 0) { /* Add connected descriptor to the pool */ p->clientfd[i] = connfd; Rio_readinitb(&p->clientrio[i], connfd); /* Add the descriptor to descriptor set */ FD_SET(connfd, &p->read_set); /* Update max descriptor and pool highwater mark */ if (connfd > p->maxfd) p->maxfd = connfd; if (i > p->maxi) p->maxi = i; break; } if (i == FD_SETSIZE) /* Couldn't find an empty slot */ app_error("add_client error: Too many clients"); }// close it up down here 25

void check_clients(pool *p) { int i, connfd, n; char buf[maxline]; rio_t rio; for (i = 0; (i <= p->maxi) && (p->nready > 0); i++) { connfd = p->clientfd[i]; rio = p->clientrio[i]; /* If the descriptor is ready, echo a text line from it */ if ((connfd > 0) && (FD_ISSET(connfd, &p->ready_set))) { p->nready--; if ((n = Rio_readlineb(&rio, buf, MAXLINE))!= 0) { byte_cnt += n; //line:conc:echoservers:beginecho printf("server received %d (%d total) bytes on fd %d\n", n, byte_cnt, connfd); Rio_writen(connfd, buf, n); //line:conc:echoservers:endecho } } } } /* EOF detected, remove descriptor from pool */ else { Close(connfd); //line:conc:echoservers:closeconnfd FD_CLR(connfd, &p->read_set); //line:conc:echoservers:beginremove p->clientfd[i] = -1; //line:conc:echoservers:endremove } 26

So what about bit vectors? void FD_ZERO(fd_set *fdset); Clears all the bits void FD_SET(int fd, fd_set *fdset); Sets the bit for fd void FD_CLR(int fd, fd_set *fdset); Clears the bit for fd int FD_ISSET(int fd, fd_set *fdset); Checks whether fd s bit is set 27

What about checking clients? The code only tests for new incoming connections But we have many more to test! Store all your client file descriptors In pool is a good idea! Several scenarios Clients are sending us data We may have pending data to write in a buffer Keep the while(1) thin Delegate specifics to functions that access the appropriate d ata Keep it orthogonal! 28

Back to that lifecycle Client(s) socket() Server socket() bind() listen() connect() Connection Request select() FD_ISSET(sfd) accept() write( ) read() close( ) check_clients() main loop Client / Server Session(s) EOF read() write() read() close() 29

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

Time out? int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); 31

Pros and Cons of I/O Multiplexing + 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- o r thread-based designs. Hard to provide fine-grained concurrency E.g., our example will hang up with partial lines. Cannot take advantage of multi-core Single thread of control 32