Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.

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

Socket Programming 101 Vivek Ramachandran.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Concurrent vs. iterative servers
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Socket Programming CS 6378
ECE 4110 – Internetwork Programming Client-Server Model.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Computer Systems II CSC 2405 Network Programming.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,
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.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
1 CMPT 471 Networking II Transport Layer Network Programming © Janice Regan, 2013.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Elementary TCP Sockets
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Concurrency. Readings r Tanenbaum and van Steen: r Coulouris: Chapter 6 r cs402 web page links r UNIX Network Programming by W. Richard Stevens.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
TELE 402 Lecture 3: Elementary … 1 Overview Last Lecture –TCP/UDP and Sockets introduction This Lecture –Elementary TCP sockets –Source: Chapter 4 of Stevens’
Elementary Name and Address Conversions
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
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.
Socket Programming Lab 1 1CS Computer Networks.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
TCP Client-Server Example
Zombie and orphan processes. Zombie process (from wikipedia) When a process ends, all of the memory and resources associated with it are deallocated so.
Fork(), Concurrent Server, Normal Termination ( 금 ) 김 희 준
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Assignment 3 A Client/Server Application: Chatroom
Chapter 5. TCP Client-Server Example
Concurrent vs. iterative servers
Unix Process Management
UNIX PROCESSES.
Chapter4 Elementary TCP Socket
Recitation 11 – 4/29/01 Outline Sockets Interface
Fork and Exec Unix Model
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Advanced Network Programming spring 2007
Network Programming Chapter 12
Network Programming: Part II CSCI 380: Operating Systems Lecture #13
Elementary UDP Sockets connectionless, unreliable, datagram
Network Programming: Part II CSCI 380: Operating Systems
TCP Client-Server Example
Presentation transcript:

Concurrent Servers

Idea Behind Concurrent Servers Server Client 1 Server 1 X

Idea Behind Concurrent Servers Server Client 1 Server 1

Idea Behind Concurrent Servers Server Client 1 Server 1 Client 2

Idea Behind Concurrent Servers Server Client 1 Server 1 Client 2Server 2

Idea Behind Concurrent Servers Server Client 1 Server 1 Client 2Server 2

Idea Behind Concurrent Servers Server Client 1 Server 1 Client 2Server 2 X

Idea Behind Concurrent Servers Server Client 1 Server 1 Client 2Server 2

Creating a New Server - fork() listenfd = Socket( … ) Initialize server address Bind( listenfd, … ) for ( ;; ) { /* wait for client connection */ connfd = Accept(listenfd,…); if( (pid = Fork() ) = = 0) { /*I am the child */ Close(listenfd); service_client(connfd); Close(connfd); exit(0); } else /* I am the parent */ Close(connfd); }

Points to Note fork() is called once … …but it returns twice!! –Once in the parent server and –Once in the child server How to distinguish parent and child??

Points to Note fork() is called once … …but it returns twice!! –Once in the parent server and –Once in the child server How to distinguish parent and child?? –Return value in child = 0 –Return value in parent = process id of child

Points to Note fork() is called once … …but it returns twice!! –Once in the parent server and –Once in the child server How to distinguish parent and child?? –Return value in child = 0 –Return value in parent = process id of child Child server exits after servicing the client.

Running another program in child – exec()

listenfd = Socket(…) Connfd = Accept(…) Inetd daemon

Running another program in child – exec() listenfd = Socket(…) Connfd = Accept(…) Inetd daemon Close(listenfd) Fork(…) Inetd child

Running another program in child – exec() listenfd = Socket(…) Connfd = Accept(…) Inetd daemon Close(listenfd) Fork(…) Exec(…) Service telnet client Close(connfd) Inetd child Telnet server

Different Types of exec() int execl(char * pathname, char * arg0, …, (char *)0); int execv(char * pathname, char * argv[]); int execle(char * pathname, char * arg0, …, (char *)0, char envp[]); int execve(char * pathname, char * argv[], char envp[]); int execlp(char * filename, char * arg0, …, (char *)0); int execvp(char * filename, char * argv[]);

Properties of exec() Replaces current process image with new program image. –E.g. inetd image replaced by telnet image All descriptors open before exec remain open after exec.

Getting IP address/port from socket int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen) –Get the local IP/port bound to socket int getpeername(int sockfd, struct sockaddr *remoteaddr, socklen_t *addrlen) –Get the IP/port of remote endpoint Why do we need these?

Two other useful functions struct hostent *gethostbyaddr (void *addr, size_t len, int type); –Converts from IP addr to domain name struct hostent *gethostbyname (char *name); –Converts from domain name to IP address struct hostent { char *h_name;/* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* address type */ int h_length; /* address length*/ char **h_addr_list; /* address list */ }

Signals Signal is a notification to process (from OS or from another process) that an event has occurred. Type of event determined by type of signal Try listing all signal types using % kill –l Some interesting signals –SIGCHLD, SIGTERM, SIGKILL, SIGSTOP

Handling Signals Signals can be caught – i.e. an action associated with them –SIGKILL and SIGSTOP cannot be caught. Actions can be customized using sigaction(…) which associates a signal handler with the signal. Details in page 120 of Steven’s book Default action for most signals is to terminate the process –SIGCHLD and SIGURG are ignored by default. Unwanted signals can be ignored –except SIGKILL or SIGSTOP

Zombie Processes When a child server dies, a SIGCHLD is sent to the parent server. If parent doesn’t wait() on the child, child becomes a zombie (status “Z” seen with ps). Zombies hang around forever.

How to avoid zombies? Parent should install a signal handler for SIGCHLD Call wait(…)/waitpid(…) inside the signal handler void handle_sigchld(int signo) { pid_tpid; int stat; pid = wait(&stat); printf(“child %d terminated\n”, pid); }