UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

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

Semester 2, Computer Communication 352 Module 4

Chapter 5. TCP Client-Server

Chapter 8: I/O functions & socket options

ISA 563: Fundamentals of Systems Programming

I/O Models. Kartik Gopalan

More Network Programming

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

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Shell Execution of Programs. Process Groups, Session and Signals 1

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

Signals. Joseph Cordina

Network Programming TDC 561

Network Programming. Multicast on a LAN 1/4. Sending & Receiving Multicast Messages. Multicasting II. Dr. Thaier Hayajneh

Network programming(ii) Lenuta Alboaie

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

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

Asynchronous Events on Linux

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

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

IPv4 and ipv6 INTEROPERABILITY

CSE 421: Introduction to Operating Systems

Like select() and poll(), epoll can monitor multiple FDs epoll returns readiness information in similar manner to poll() Two main advantages:

10. I/O System Library

ECE 650 Systems Programming & Engineering. Spring 2018

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

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.

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

Processes. Processes (cont d)

Standards / Extensions C or C++ Dependencies POSIX.1 XPG4 XPG4.2 Single UNIX Specification, Version 3

Contents. Part 1. Introduction and TCP/IP 1. Foreword Preface. xix. I ntroduction 31

UNIT IV- SOCKETS Part A

Outline. What is TCP protocol? How the TCP Protocol Works SYN Flooding Attack TCP Reset Attack TCP Session Hijacking Attack

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CS4514 HELP Session 2 Simulation of Datalink Layer Communication

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

Inter-process communication (IPC)

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

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

VALLIAMMAI ENGINEERING COLLEGE. SRM Nagar, Kattankulathur QUESTION BANK

COMP/ELEC 429/556 Introduction to Computer Networks

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

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

Software Distributed Shared Memory System with Fault Detection Support

3.1 Introduction. Computers perform operations concurrently

UDP CONNECT TO A SERVER

The User Datagram Protocol

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

Signals. POSIX defines a variety of signal types, each for a particular

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Preview. Process Termination. wait and waitpid() System Call. wait and waitpid() System Call. waitpid() System Call 10/23/2018

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

CS240: Programming in C

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Group-A Assignment No. 6

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Concurrent Servers Dec 2, 2009"

NETWORK PROGRAMMING AND MANAGEMENT 1 KINGS DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK

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

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

Client software design

CS4514 Project 2 Help Session (B07) Feng Li Nov 8, 2007

Outline. Overview. Linux-specific, since kernel 2.6.0

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

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Problem Set: Processes

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF MCA QUESTION BANK UNIT 1

UNIT III- INTER PROCESS COMMUNICATIONS Part A

Operating Systems 2010/2011

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

MARIA COLLEGE OF ENGINEERING AND TECHNOLOGY, ATTOOR DEPARTMENT OF INFORMATION TECHNOLOGY NETWORK PROGRAMMING MANAGEMENT 2 MARKS QUESTIONS & ANSWERS

CSC209 Review. Yeah! We made it!

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Last time: Scheduling. Goals today. Recap: Hardware support for synchronization. Disabling interrupts. Disabling interrupts

System Calls & Signals. CS449 Spring 2016

Workshop on Inter Process Communication Solutions

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

Signals and Session Management. Signals. Mechanism to notify processes of system events

CISC2200 Threads Spring 2015

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

UNIX System Programming. Overview. 2. Signal Types (31 in POSIX) Signal Sources. Signals. 1. Definition

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

Concurrent Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Concurrent Server Design Multiple- vs. Single-Thread

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

CONCURRENT PROGRAMMING. Lecture 5

Unix Internals Module 07 Raju Alluri spurthi.com. Copyright 2007 Raju Alluri. All rights reserved 1

Transcription:

UNIT III - APPLICATION DEVELOPMENT TCP Echo Server TCP Echo Client Posix Signal handling Server with multiple clients boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and reboots, Server Shutdown I/O multiplexing I/O Models select function shutdown function TCP echo Server (with multiplexing) poll function TCP echo Client (with Multiplexing). TCP Echo Server 1. The Client reads a line of text from its standard input and writes the line to the server. 2. The server reads the line from its network input and echoes the line back to the client. 3. The client reads the echoed line and prints it on its standard output. voidstr_echo(intsockfd) { ssize_t n; 1 CCET

} } char line[maxline]; for ( ; ; ) { if ( (n = Readline(sockfd, line, MAXLINE)) == 0) return; /* connection closed by other end */ Writen(sockfd, line, n); TCP Echo Client } voidstr_cli(file *fp, intsockfd) { char sendline[maxline], recvline[maxline]; while (Fgets(sendline, MAXLINE, fp)!= NULL) { Writen(sockfd, sendline, strlen(sendline)); if (Readline(sockfd, recvline, MAXLINE) == 0) err_quit("str_cli: server terminated prematurely"); Fputs(recvline, stdout); } } Posix Signal Handling Signal (software interrupt): sent by one process to another process (or to itself) or by the kernel to a process 2 CCET

SIGCHLD: by the kernel to the parent Disposition of a signal: catch the signal by a specified signal handler SIG_IGN: ignore it SIG_DFL: default: terminate or ignore To enable automatic restart of an interrupted system call by the kernel -- write our own signal function. Sigfunc *Signal(int signo, Sigfunc *func) /* for our signal() function */ { Sigfunc *sigfunc; if ( (sigfunc = signal(signo, func)) == SIG_ERR) err_sys("signal error"); return(sigfunc); } POSIX signal semantics: 1. Once a signal handler is installed, it remains installed. 2. The signal being delivered is blocked while a signal handler is executing. 3. By default, signals are not queued. Boundary Conditions I) II) Crashing of Server Process Procedure: 1. Server TCP sends FIN to client TCP, which responds with an ACK. (TCP half-close) (The client process is blocked in fgets when client TCP receives FIN.) 2. SIGCHLD signal is sent to the server parent. 3. The client process calls writen to send data to server. 4. The server TCP responds with an RST. 5. The client process returns from readline, 0, when client TCP receives RST. 6. The client process terminates. Problem: The client should be aware of server process crash when FIN is received. Solution: Use select or poll to block on either socket or stdio. (when writing to a socket that has received an RST ) SIGPIPE Signal Procedure: 3 CCET

III) 1. The client writes to a crashed server process. An RST is received at the client TCP and readline returns 0 (EOF). 2. If the client ignores the error returned from readline and write more, SIGPIPE is sent to the client process. 3. If SIGPIPE is not caught, the client terminates with no output. Problem: Nothing is output even by the shell to indicate what has happened. (Have to use echo $? to examine the shell s return value of last command.) Solution: Catch the SIGPIPE signal for further processing. The write operation returns EPIPE. Crash, Reboot, Shutdown of Server Host Crash of server host: client TCP continuously retx data and timeout around 9 min readline returns ETIMEDOUT or EHOSTUNREACH To quickly detect: timeout on readline, SO_KEEPALIVE socket option, heartbeat functions Reboot of server host: After reboot, server TCP responds to client data with an RST readline returns ECONNRESET Shutdown (by operator) of server host: init process sends SIGTERM to all processes init waits 5-20 sec and sends SIGKILL to all processes I/O Multiplexing Scenarios for I/O Multiplexing client is handling multiple descriptors (interactive input and a network socket). Client to handle multiple sockets (rare) TCP server handles both a listening socket and its connected socket. Server handle both TCP and UDP. Server handles multiple services and multiple protocols I/O Models Models Blocking I/O 4 CCET

Nonblocking I/O I/O multiplexing(select and poll) Signal driven I/O (SIGIO) Asynchronous I/O Two distinct phases for an input operation Waiting for the data to be ready (for a socket, wait for the data to arrive on the network, then copy into a buffer within the kernel) Copying the data from the kernel to the process (from kernel buffer into application buffer) 1) Blocking I/O 2) Nonblocking I/O 5 CCET

3) I/O multiplexing(select and poll) 4) Signal driven I/O (SIGIO) 5) Asynchronous I/O 6 CCET

Comparison of the I/O Models Synchronous I/O, Asynchronous I/O Synchronous I/O causes the requesting process to be blocked until that I/O operation (recvfrom) completes. (blocking, nonblocking, I/O multiplexing, signaldriven I/O) Asynchronous I/O does not cause the requesting process to be blocked Select Function Allows the process to instruct the kernel to wait for any one of multiple events to occur and to wake up the process only when one or more of these events occurs or when a specified amount of time has passed. What descriptors we are interested in (readable,writable, or exception condition) and how long to wait? #include <sys/select.h> #include <sys/time.h> 7 CCET

int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *); //Returns: +ve count of ready descriptors, 0 on timeout, -1 on error struct timeval{ long tv_sec; /* seconds */ long tv_usec; /* microseconds */ } Possibilities for select function Wait forever : return only when descriptor (s) is ready (specify timeout argument as NULL) wait up to a fixed amount of time Do not wait at all : return immediately after checking the descriptors. Polling (specify timeout argument as pointing to a timeval structure where the timer value is 0) The wait is normally interrupted if the process catches a signal and returns from the signal handler select might return an error of EINTR Actual return value from function = -1 select function Descriptor Arguments readset descriptors for checking readable writeset descriptors for checking writable exceptset descriptors for checking exception conditions (2 exception conditions) arrival of out of band data for a socket the presence of control status information to be read from the master side of a pseudo terminal (Ignore) If you pass the 3 arguments as NULL, you have a high precision timer than the sleep function. maxfdp1 argument to select function specifies the number of descriptors to be tested. Its value is the maximum descriptor to be tested, plus one. (hence maxfdp1) Descriptors 0, 1, 2, up through and including maxfdp1-1 are tested example: interested in fds 1,2, and 5 maxfdp1 = 6 Your code has to calculate the maxfdp1 value constant FD_SETSIZE defined by including <sys/select.h> is the number of descriptors in the fd_set datatype. (often = 1024) Condition for a socket to be ready for select 8 CCET

str_cli Function revisited Problems with earlier version could be blocked in the call to fgets when something happened on the socket We need to be notified as soon s the server process terminates Alternatively block in a call to select instead, waiting for either standard input or the socket to be readable. Condition handled by select in str_cli Shutdown Function Close one half of the TCP connection send FIN to server, but leave the socket descriptor open for reading Limitations with close function decrements the descriptor s reference count and closes the socket only if the count reaches 0 With shutdown, can initiate TCP normal connection termination regardless of the reference count 9 CCET

terminates both directions (reading and writing) With shutdown, we can tell other end that we are done sending, although that end might have more data to send us #include<sys/socket.h> int shutdown ( int sockfd, int howto ); /* return : 0 if OK, -1 on error */ howto argument SHUT_RD read-half of the connection closed Any data in receive buffer is discarded Any data received after this call is ACKed and then discarded SHUT_WR write-half of the connection closed (half-close) Data in socket send buffer sent, followed by connection termination SHUT_RDWR - both closed Poll Function poll Provides functionality similar to select, but poll provides additional information when dealing with stream devices. #include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int timeout); Description The poll() function provides applications with a mechanism for multiplexing input/output over a set of file descriptors. For each member of the array pointed to by fds, poll() shall 10 CCET

examine the given file descriptor for the event(s) specified in events. The number of pollfd structures in the fds array is specified by nfds. The poll() function shall identify those file descriptors on which an application can read or write data, or on which certain events have occurred. The fds argument specifies the file descriptors to be examined and the events of interest for each file descriptor. It is a pointer to an array with one member for each open file descriptor of interest. The array's members are pollfd structures within which fd specifies an open file descriptor and events and revents are bitmasks constructed by OR'ing a combination of the following event flags: POLLIN Data other than high-priority data may be read without blocking. For STREAMS, this flag is set in revents even if the message is of zero length. This flag shall be equivalent to POLLRDNORM POLLRDBAND. POLLRDNORM Normal data may be read without blocking.for STREAMS, data on priority band 0 may be read without blocking. This flag is set in revents even if the message is of zero length. POLLRDBAND Priority data may be read without blocking.for STREAMS, data on priority bands greater than 0 may be read without blocking. This flag is set in revents even if the message is of zero length. POLLPRI High-priority data may be read without blocking.for STREAMS, this flag is set in revents even if the message is of zero length. POLLOUT Normal data may be written without blocking.for STREAMS, data on priority band 0 may be written without blocking. POLLWRNORM Equivalent to POLLOUT. POLLWRBAND Priority data may be written.for STREAMS, data on priority bands greater than 0 may be written without blocking. If any priority band has been written to on this STREAM, this event only examines bands that have been written to at least once. POLLERR An error has occurred on the device or stream. This flag is only valid in the revents bitmask; it shall be ignored in the events member. 11 CCET

POLLHUP The device has been disconnected. This event and POLLOUT are mutually-exclusive; a stream can never be writable if a hangup has occurred. However, this event and POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI are not mutually-exclusive. This flag is only valid in the revents bitmask; it shall be ignored in the events member. POLLNVAL The specified fd value is invalid. This flag is only valid in the revents member; it shall ignored in the events member. TCP Echo Server with multiplexing TCP echo server using select Rewrite the server as a single process that uses select to handle any number of clients, instead of forking one child per client. Before first client has established a connection TCP and UDP echo server using select Combine concurrent TCP echo server with iterative UDP server into a single server that uses select to multiplex a TCP and UDP socket Source code in udpcliserv/udpservselect01.c Source code for sig_chld function (signal handler) is in udpcliserv/sigchldpidwait.c Handles termination of a child TCP server 12 CCET