TCP Client-Server Example

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

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Remote Procedure Call (RPC)
Remote Procedure Call Design issues Implementation RPC programming
Using tcpdump. tcpdump is a powerful tool that allows us to sniff network packets and make some statistical analysis out of those dumps. tcpdump operates.
Returning to zombies and parents and children. –What happens to the children if the parent dies? /* ILLUSTRATING THAT UPON A PARENT KILL THE ZOMBIES /*
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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.
Operating Systems, Spring 2002 Ittai Abraham, Zinovi Rabinovich (recitation)
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays.
Daemon Processes and inetd Superserver
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
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.
Chapter 5 TCP Client/Server Example. TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and.
Elementary TCP Sockets
Introduction to Processes CS Intoduction to Operating Systems.
Signal-Driven I/O Concepts and steps for using signal-driven I/O
6.1. Transport Control Protocol (TCP) It is the most widely used transport protocol in the world. Provides reliable end to end connection between two hosts.
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.
TCP : Transmission Control Protocol Computer Network System Sirak Kaewjamnong.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Elementary TCP Sockets
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
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’
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
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
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.
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.
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)
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
I/O Multiplexing Chap 6. I/O Models  Blocking I/O Model  Nonblocking I/O Model  I/O Multiplexing Model  Signal Driven I/O Model  Asynchronous I/O.
System calls for Process management Process creation, termination, waiting.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
G.Jyostna.
I/O Multiplexing.
Chapter 5. TCP Client-Server Example
Task Control: Signals and Alarms Chapter 7 and 8
Unix Process Management
CH5 TCP Client - Server Example:
Exceptional Control Flow Part II
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Advanced Network Programming spring 2007
Exceptional Control Flow
Inter-Process Communication ENCE 360
Controlling Processes
TCP Client-Server Example
Signals.
Presentation transcript:

TCP Client-Server Example Chap 1 Foundation TCP Client-Server Example Chap 5

TCP Echo Client tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client Server stdin stdout fgets fputs writen readline

TCP Echo Server tcpcliserv/tcpserv01.c: lib/str_echo.c: (Parent) TCP Client Server (Child) writen readline (Parent) fork connect accept tcpcliserv/tcpserv01.c: lib/str_echo.c:

Problems in Echo Server Normal Termination후에 zombie process로 남아 있음 Zombie state of process in UNIX To maintain information about the child for the parent fetch at some later time such as child process ID, termination status, and information on the resource utilization Solution When a child process is terminated, signal SIGCHLD is delivered to the parent Abnormal Termination 경우도 처리해야 Client host crashes Client process crashes Network connectivity is lost, and so on

Posix Signal Handling Signal(software interrupt) can be sent asynchronously by one process to another process(or to itself) by the kernel to a process Signal disposition (or actions) catch: call a signal handler ignore: SIG_IGN SIGKILL and SIGSTOP cannot be caught nor ignored default: SIG_DFL Normally, the process receiving a signal is terminated Ignored for SIGCHLD and SIGURG signal() vs Posix sigaction() Posix Signal Semantics Once a signal handler is installed, it remains installed Classical UNIX signal is not… While a signal handler is executing, the signal being delivered is blocked Signals are not queued(delivered only one time)  may lost

Handling Zombies using signal When the parent process captures SIGCHLD signals, wait until termination status is delivered In main program of the server signal(SIGCHLD, sig_child); … /* Signal handler */ void sig_chld(int signo) { pid_t pid; int stat; pid = wait(&stat); printf("child %d terminated\n", pid); return; }

Problems still remain Problem-1: Problem-2: Parent가 slow system call(accept)에서 blocked 됐을 때 signal이 들어오면 accept는 EINTR error로 return하게 됨 Parent aborted ! Problem-2: 5개의 client가 거의 동시에 terminate 5개의 client가 동시에 SIGCHLD를 server의 child에게 보냄 하나의 SIGCHLD가 도착한 후 나머지는 저장(queueing)되지 않을 수 있음 여전히 zombie가 남아 있음

Correct Solution to Problem-1 Handling interrupted system calls

Correct Solution to Problem-2 using waitpid() pid: PID that we want to wait -1 to wait for the first of children to terminate WNOHANG option: not to block if there are no terminated children

TCP Echo Server handling Zombie Problem

Handling Abnormal Termination: Connection aborted before call accept due to the fact that client process killed or client host crashed ??? When accept is called, SVR4 returns error EPROTO Posix.1g returns error ECONNABORTED Berkeley-derived implementation handle completely within kernel Solution: call accept again See lib/wrapsock.c lib/Wrapsock.c:

Handling Abnormal Termination: Termination of Server Process Case 1: client blocked for reading from socket Server: process killed  close socket send FIN Client: readline() returns 0  normal termination Case 2: client blocked for reading from stdin Client: User input  writen() Server: send RST Client: readline() returns error (ECONNRESET) Socket과 Stdin 두개의 descriptor가 ready될 조건을 알아내어 별개로 처리 가능해야  use I/O Multiplexing (select(), poll()) When a process is terminated or killed Calls close() TCP sends FIN When TCP receives a segment towards terminated process (i.e. invalid destination port #)  TCP responds RST

Abnormal Termination: Crashing or Shutdown of Server Host Crashing of Server Host ETIMEOUT error return after retransmitting 12 times(9 min) or EHOSTUNREACH (or ENETUNREACH) error returns with ICMP message 데이터를 보내지 않고도 connection이 끊어졌음을 알 수는 없는가 ? 초 단위로 더 빨리 알아낼 방법은 없는가? Use SO_KEEPALIVE socket option Construct heartbeat mechanism using OOB data (TCP URG flag) Crashing and rebooting of server host loose connection information responds RST to the request form client  readlline() in client returns error ECONNRESET Shutdown of server host SIGKILL  socket closed  send FIN  same as in “Termination of Server Process”

Client’s and Server’s Perspective

Data Format Potential problems in exchanging data structure Solutions different format of binary numbers: big endian, little endian different implementation of C data type: e.g) long, int, short different implementation packing and alignment of structures Solutions pass all the numeric data as text string define and use common exchange format - e.g) RPC TCP 상의 application protocol은 통상 PDU를 사용하지 않고 textual syntax를 사용함. Why?