Chapter 5. 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

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.
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.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
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.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Operating Systems Chapter 2
Creating and Executing Processes
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.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
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’
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Signals and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
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 *
1 UNIX System Programming Signals. 2 Overview 1. Definition 2. Signal Types 3. Generating a Signal 4. Responding to a Signal 5. Common Uses of Signals.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
TCP Client-Server Example
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
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)
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
G.Jyostna.
Signals What is a Signal?
I/O Multiplexing.
Elementary UDP Sockets
Threads Threads.
Unix Process Management
CH5 TCP Client - Server Example:
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Exceptional Control Flow Part II
Chapter 8 Elementary UDP Socket
UNIT-IV Process and Signals
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
CSC Advanced Unix Programming, Fall 2015
Advanced Network Programming spring 2007
Elementary UDP Sockets connectionless, unreliable, datagram
Controlling Processes
TCP Client-Server Example
Signals.
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

Chapter 5. TCP Client-Server Example

Contents Introduction TCP Echo Server TCP Echo Client Normal Startup and Termination Posix Signal Handling Handling SIGCHLD Signals Data Format and so on...

5.1 Introductioon TCP client server stdin stdout fgets fputs writen readline 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.

5.2 TCP Echo Server main function & str_echo function for ( ; ; ) { clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); if ( (childpid = Fork()) == 0) {/* child process */ Close(listenfd);/* close listening socket */ str_echo(connfd);/* process the request */ exit(0); } Close(connfd); /* parent closes connected socket */

void str_echo(int sockfd) { ssize_t n; char line[MAXLINE]; for ( ; ; ) { if ( (n = Readline(sockfd, line, MAXLINE)) == 0) return; /* connection closed by other end */ Writen(sockfd, line, n); }

5.4 TCP Echo Client main function & str_cli function Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); str_cli(stdin, sockfd); /* do it all */ exit(0);

void str_cli(FILE *fp, int sockfd) { 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); }

Normal Startup How the client and server start, how they end What happens when something goes wrong the client host crashes, the client process crashes, network connectivity is lost Test method tcpserv & netstat -a tcpcli 127.0.0.1 (local test) ps -l

Normal Termination tcpcli 127.0.0.1 netstat -a | grep procID ps hello, world good bye ^D netstat -a | grep procID ps 19130 p1 Ss -ksh 21130 p1 I ./tcpserv 21132 p1 Z (tcpserv) (Z:zombie process)

5.8 Posix Signal Handling A signal (software interrupt) : a notification to a process that an event has occurred. Signals can be sent by one process to another process(or itself) by the kernel to a process SIGCHLD signal: a signal sent by the kernel whenever a process terminates, to the parent of the terminating process

Every signal has a disposition (action associated with the signal) We can provide a function hat is called whenever a specific signal occurs. This function is called a signal handler and this action is called catching the signal. (SIGKILL(x) and SIGSTOP(X)), void handler(int signo); We can ignore a signal by setting its disposition to SIG_IGN. (SIGKILL(x) and SIGSTOP(X)), We can set the default disposition for a signal by setting its disposition to SIG_DFL. (terminate a process on the receipt of a signal) SIGCHLD(X), SIGURG(x)

5.8 Posix Signal Handling signal Function Sigfunc * signal(int signo, Sigfunc *func) { struct sigaction act, oact; act.sa_handler = func; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (signo == SIGALRM) { #ifdef SA_INTERRUPT act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */ #endif } else { #ifdef SA_RESTART act.sa_flags |= SA_RESTART; /* SVR4, 44BSD */ } if (sigaction(signo, &act, &oact) < 0) return(SIG_ERR); return(oact.sa_handler);

5.8 Posix Signal Handling 2-3 normal function prototype void (*signal(int signo, void (*func) (int) ) ) (int); define Sigfunc type in unp.h header typedef void Sigfunc(int); The function prototype Sigfunc *signal(int signo, Sigfunc *func); 6 : The sa_handler member of the sigaction structure is set to the func argument. members of sigaction structure void (*) (int) sa_handler sigset_t sa_mask int sa_flag

5.8 Posix Signal Handling 7 : Set signal mask for handler Posix allow us to specify a set of signals that will be blocked when our signal handler is called. Any signal that is blocked cannot be delivered to the process. #include <signal.h> int sigaddset(sigset_t *set, int signo); int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigdelset(sigset_t *set, int signo); int sigismember(sigset_t *set, int signo); int sigprocmask(int how, const sigset_t *set, sigset_t *oset); SIG_BLOCK SIG_SETMASK SIG_UNBLOCK

5.8 Posix Signal Handling SIGALRM SA_RESTART place a timeout on an I/O operation SA_RESTART a system call interrupted by this signal will be automatically restarted by the kernel

5.8 Posix Signal Handling Posix Signal Semantics Once a signal handler is installed, it remains installed. While a signal handler is executing, the signal being delivered is blocked. If a signal is generated one or more times while it is blocked, it is normally delivered only one time after the signal is unblocked. That is, by default Unix signals are not queued. It is possible to selectively block and unblock a set of signals using the sigprocmask function.

Handling SIGCHLD Signals Zombie State maintain information about the child for the parent to fetch at some later time the process ID of the child, its termination status, the resource of the child(CPU time, memory) the parent process ID of all the zombie children: 1(init process)-inherit the children and clean them up <defunct> Handling Zombies space waste of the kernel, out of process wait for the children to prevent them from becoming zombies

Handling SIGCHLD Signals We establish the signal handler by adding the function call Signal (SIGCHLD, sig_chld); in Figure 5.2, after the call to listen. #include "unp.h" void sig_chld(int signo) { pid_t pid; int stat; pid = wait(&stat); printf("child %d terminated\n", pid); return; } Figure 5.7 Version of SIGCHLD signal handler that calls wait

Tcpserv02 & tcpcli01 127.0.0.1 hi, there ^D child 16942 terminated accept error: Interrupted system call // the parent is blocked in its call to accept when the SIGCHLD is delivered //sig_chld function executes, wait fetches the child’PID and termination status, printf // kernel causes the accept to return an error of EINTER

5.9 Handling SIGCHLD Signals Handling Interrupted System Calls for ( ; ; ) { clilen = sizeof(cliaddr); if( (connfd=accept(listenfd,(SA *) &cliaddr,&clilen)) < 0) { if( errno == EINTER ) continue; else err_sys(“accept error”); }

5.10 wait and waitpid Functions pit_t: the process ID of the terminated child statloc : the termination status of the child(an integer) is returned through the statloc pointer. pid : specify the process ID that we want to wait for. A value of -1 say to wait for the first of our children to terminate. option : specify additional option. The most common option is WNOHANG. #include <sys/wait.h> pid_t wait(int *statloc); pid_t waitpid(pid_t pid, int *statloc, int option);

>tcpserv03 & >tcpcli03 206.62.226.35 hello ^D child 21288 terminated 21282 p1 S ./tcpserv03 21284 p1 Z (tcpcli03) 21285 p1 Z (tcpcli03) 21286 p1 Z (tcpcli03) 21287 p1 Z (tcpcli03)

5.10 wait and waitpid Functions Difference between wait and waitpid the problem is that all five signals are generated before the signal handler is executed, and the signal handler is executed only one time because Unix signals are normally not queued. waitpid we must specify the WNOHANG option: this tells waitpid not to block if there exist running children that have not yet terminated. void sig_chld(int signo) { pid_t pid; int stat; while((pid = waitpid(-1,&stat,WNOHANG)) > 0) printf("child %d terminated\n", pid); return; }

5.11 Connection Abort before accept Returns The three-way handshake completes, the connection is established, and then the client TCP sends an RST(reset). On the server side the connection is queued by its TCP, waiting for the server process to call accept when the RST arrives. Some time later the server process calls accept.

Implementation BSD : kernel SVR4 : return an errno of EPROTO Posix.1g : return an errno of ECONNABORTED EPROTO : returned when some fatal protocol-related events occur on the streams subsystem. In the case of the ECONNABORTED error, the server can ignore the error and just call accept again.

5.12 Termination of Server Process Our client is not expecting to receive an end-of-file at this point so it quits with the error message “server terminated prematurely”. solaris % tcpcli01 206.62.226.35 hello another line str_cli: server terminated prematurely

SIGPIPE Signal What happens if the client the error return from readline and writes more data to the server ? When a process writes to a socket that has received an RST, the SIGPIPE signal is sent to the process Default action of SIGPIPE: terminate the process

>tcpcli11 206.62.226.34 hi there bye Nothing is echoed for bye data Reason: the default action of SIGPIPE is terminate the process.

What happens when the server host crashes, reboots, shutdowns