CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

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

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

Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming

Concurrent Servers Dec 2, 2009"

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

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

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

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

CONCURRENT PROGRAMMING. Lecture 5

Concurrent Programming

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

Concurrent Programming April 21, 2005

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming November 28, 2007

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

I/O Models. Kartik Gopalan

Concurrent Programming

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

Lecture #23 Concurrent Programming

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

Concurrent Programming

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

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

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

Concurrent Programming

Concurrent Programming

Concurrent Programming

Concurrent programming

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

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

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

ECE 650 Systems Programming & Engineering. Spring 2018

CISC2200 Threads Spring 2015

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

Example Questions for Midterm EE122, Fall 2008 EECS Berkeley

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

ISA 563: Fundamentals of Systems Programming

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

Global Employee Location Server

Concurrent Programming

Introduction to Client-Server Model

socketservertcl a Tcl extension for using SCM_RIGHTS By Shannon Noe - FlightAware

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

Concurrent Programming

Network Programming September 6, 2005

Network Programming TDC 561

Experiential Learning Workshop on Basics of Socket Programming

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.

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

Semester 2, Computer Communication 352 Module 4

Network Programming November 3, 2008

Chapter 5. TCP Client-Server

ECE 650 Systems Programming & Engineering. Spring 2018

COMP/ELEC 429/556 Introduction to Computer Networks

CSC209 Review. Yeah! We made it!

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

10. I/O System Library

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

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

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

Network Games Part II. Architecture of Network Game

Light-Weight Processes: Dissecting Linux Threads

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

CS118 Discussion Week 2. Taqi

Inter-process communication (IPC)

Approaches to Concurrency

UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Operating System Structure

Socket Programming TCP UDP

Signal Example 1. Signal Example 2

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CMPSC 311- Introduction to Systems Programming Module: Concurrency

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CSE 333 Lecture fork, pthread_create, select

Piotr Mielecki Ph. D.

Topics for this Week

CS307 Operating Systems Processes

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

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

CMPSC 311- Introduction to Systems Programming Module: Network Programming

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

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

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

Group-A Assignment No. 6

CMPSC 311- Introduction to Systems Programming Module: Network Programming

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

I/O Multiplexing. Dec 2009

Transcription:

CONCURRENCY MODEL UNIX Programming 2014 Fall by Euiseong Seo

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

Iterative Model 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

Only One Client at a Time call connect ret connect call fgets User goes out to lunch Client 1 blocks wai5ng for user to type in data client 1 server client 2 Server blocks wai5ng for data from Client 1 call accept ret accept call read Solution: use concurrent servers instead call connect Client 2 blocks wai5ng to complete its connec5on request un5l a<er lunch! Use multiple concurrent flows to serve multiple clients at the same time.

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

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 wai5ng 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

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

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;

Implementation Issues Servers should restart accept() if it is interrupted by a transfer of control to 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(), ref. count of connfd becomes 2 Connection will not be closed until ref. count becomes 0

Process-based Designs Pros Handles multiple connections concurrently Easy to implement and clear to understand sharing model Simple and straightforward Cons Additional overhead for process control n Process creation and termination n Process switching Nontrivial to share data between processes n Requires IPC (Inter-Process Communication) mechanisms n FIFO, shared memory and semaphores

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

Select on Sockets Prototype #include <sys/select.h>! int select(int nfds, fd_set *readfds, fd_set *writefds,! fd_set *exceptfds, struct timeval *timeout); select() returns number of FDs contained in the three returned descriptor sets select() blocks for timeout waiting for a file descriptor to become ready FDs in readfds will be tested whether they have something to read FDs in writefds will be tested whether they are available for writing

Select on Sockets FDs in exceptfds will be watched for exceptions Three given FD sets will be changed accordingly Only FDs in ready will be included in the result sets

Macros for FD Sets Macros for manipulating set descriptors void FD_ZERO (fd_set *fdset) n Turn off all bits in fdset void FD_SET (int fd, fd_set *fdset) n Turn on bit fd in fdset void FD_CLR (int fd, fd_set *fdset) n Turn off bit fd in fdset int FD_ISSET (int fd, fd_set *fdset) n Is bit fd in fdset turned on?

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

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

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

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