Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Operating Systems Lecture 7.
Zombie and orphan processes. Zombie process (from wikipedia) When a process ends, all of the memory and resources associated with it are deallocated.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
A ‘minimal’ web-server We can glean insights about the operation of the HTTP protocol if we create our own web-server.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Concurrent vs. iterative servers
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Some server “stress” tests Can our TCP concurrent server handle many successive and/or simultaneous connections?
Fundamentals of Python: From First Programs Through Data Structures
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
Elementary TCP Sockets
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Introduction to Processes CS Intoduction to Operating Systems.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Chapter 2 Applications and Layered Architectures Sockets.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
1 Server Design Discuss Design issues for Servers Review Server Creation in Windows.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
System calls for Process management
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Position of application layer. Application layer duties.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems Process Creation
Client/Server Socket Programming Project
Operating Systems Recitation 4, April th, 2002 Signals.
Part 4: Network Applications Client-server interaction, example applications.
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
TCP Client-Server Example
Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Chapter 3: Processes Process Concept.
System calls for Process management Process creation, termination, waiting.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Network Communications A Brief Introduction. 2 Network Communications.
Operating Systems Review ENCE 360.
Chapter 3: Process Concept
Concurrent vs. iterative servers
Chapter 4: Threads.
Chapter 3: Processes.
UNIX PROCESSES.
Client-Server Interaction
Chapter 3: Processes.
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
CGS 3763 Operating Systems Concepts Spring 2013
Threaded Programming in Python
Chapter 3: Processes.
Lab 6: Process Management
Processes Creation and Threads
Chapter 4: Threads.
Presentation transcript:

Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays

Recall design-paradigm socket() bind() listen() accept() read() write() close() socket() bind() connect() write() read() close() The ‘server’ application The ‘client’ application 3-way handshake data flow to server data flow to client 4-way handshake

Three sockets used server’s listening socket server’s connection socket client’s connection socket The ‘server’ processThe ‘client’ process The server’s ‘listening socket’ is strictly for one-way communication: it can only receive connection-requests from clients, but it does not receive a client’s data, nor is it able to send any data back to a client The server’s ‘connected socket’ is for doing two-way communication: it can be used by the server to receive data from its connected client, and it can be used by the server to send data to that connected client

Fast service only The design-paradigm we just described is OK for servers that reply very quickly to a single client request (as with our original echo application: a short sentence is sent by the client, the server capitalizes all its letters and sends that sentence back, and then the connection is immediately closed But this design-paradigm is not well-suited for a more general kind of TCP application

Original ‘echo’ example socket() bind() listen() accept() read() write() close() socket() connect() write() read() close() Our ‘server’ application Our ‘client’ application 3-way handshake data flow to server data flow to client 4-way handshake Ask user to type in a short sentence and read reply The duration of this connection is very brief

Delayed-service problem To demonstrate the problem that arises with our original “iterative server” design, we need to make a small revision in our client’s code – to prolong the duration of the connection of the server to the client If we ‘cut-and-paste’ a few lines of code, we can arrange for our client to connect to the server before it reads the user’s input

socket() bind() listen() accept() read() write() close() socket() connect() write() read() close() Our ‘server’ application Our ‘client’ application 3-way handshake data flow to server data flow to client 4-way handshake Ask user to type in a short sentence and read reply New ‘echo’ example An indeterminate delay after connection occurs while user types input Ask user to type in a short sentence and read reply cut and paste

Demo: ‘tcpclient2.cpp’ Run our original ‘tcpserver.cpp’ in one of the windows on your graphical desktop Then run our revised ‘tcpclient2.cpp’ demo in several other windows at the same time $./tcpserver Server is listening on port $./tcpclient2 localhost Please type a short sentence: $./tcpclient2 localhost Please type a short sentence: $./tcpclient2 localhost Please type a short sentence:

A ‘concurrent’ server To avoid such service delays, most TCP servers use a different design-paradigm, taking advantage of UNIX’s multitasking Each connection-request that the server receives will handled by a different task The operating system will schedule these multiple tasks to be executed concurrently, so delays by one task will not affect others

Server’s code-outline // the basic steps in an initial ‘concurrent server’ design intsock = socket( AF_INET, SOCK_DGRAM, IPPROTO_TCP ); bind( sock, (sockaddr*)&serveraddr, salen ); listen( sock, 5 ); for(;;){ intconn = accept( sock, (sockaddr*)&clientaddr, &calen ); intpid = fork(); if ( pid == 0 )// child-process { close( sock ); intrx = read( conn, buf, sizeof( buf ) ); if ( rx > 0 ) write( conn, buf, rx ); close( conn ); exit(0); } // parent-process close( conn ); continue; }

No change in ‘connection-setup’ server’s listening socket server’s connection socket client’s connection socket The ‘server’ processThe ‘client’ process The server’s ‘listening socket’ is strictly for one-way communication: it can only receive connection-requests from clients, but it does not receive a client’s data, nor is it able to send any data back to a client

Connect, then fork server’s listening socket server’s connection socket client’s connection socket The ‘server’ parent-process The ‘client’ process The server’s ‘listening socket’ will not used by the child-process, so it immediately gets closed, and the server’s ‘connection socket’ will not be used by the parent-process, so it immediately gets closed server’s listening socket server’s connection socket The ‘server’ child-process parent closes connection-socket child closes listening-socket

Server continues ‘listening’ server’s listening socket client’s connection socket The ‘server’ parent-process The ‘client’ process The server’s ‘listening socket’ can continue to receive connection-requests from other clients that are made to the server’s parent-process, while the earlier client is maintaining its connection with the server’s child-process server’s connection socket The ‘server’ child-process client’s connection socket Next ‘client’ process

Demo: ‘tcpserver2.cpp’ Execute our revised ‘tcpserver2.cpp’ in one of your windows, and again run our ‘tcpclient2.cpp’ demo in other windows The ‘service delay’ problem has vanished! $./tcpserver2 Server is listening on port $./tcpclient2 localhost Please type a short sentence: $./tcpclient2 localhost Please type a short sentence: $./tcpclient2 localhost Please type a short sentence:

New problem: ‘zombies’ When you use the ‘ps’ command to look at the list of all of your processes, you notice that our revised server’s ‘child-processes’ are still residing within the system -- even though they have already terminated – as so called ‘zombie’ processes, and they are using system resources (e.g., memory): $ ps -a

Parent didn’t ‘wait’ When a child-process exits, its existence is remembered within the Linux system until its parent-process calls one of the ‘wait()’ functions, to find out that child’s status -- and to relinquish its resources Failure to ‘wait’ could eventually exhaust the system’s memory, preventing further useful work from being done!

But ‘wait()’ blocks! If a parent calls the usual ‘wait()’ function before its child has terminated, the parent will be put to sleep, and is awakened only when one of its child-processes exits But putting out server-process to sleep would delay it from accepting any more connection-requests from new clients To avoid this we need a new mechanism

The SIGCHLD signal Whenever a process exits, the operating system will automatically notify its parent by delivering a ‘signal’ to the parent We can arrange for our concurrent server to ‘catch’ any such signals, because then there’s no risk of sleeping if it calls ‘wait()’ That way, the resources owned by child- processes will get released (no zombies!)

Signal-handler #include // for signal() #include // for wait() void sigchld_action( int signo ) { wait( NULL );// release a child-process’s resources signal( SIGCHLD, sigchld_action );// reinstall handler } int main( int argc, char *argv[] ) { signal( SIGCHLD, sigchld_action );// install handler … }

In-class exercise Try running our ‘tcpserver3.cpp’ example, which invokes a signal-handler to ‘wait()’ as soon as a child-process calls ‘exit()’ Now run ‘tcpclient2.cpp’ to satisfy yourself that a ‘zombie’ process is no longer being left in the system to consume resources