Understanding Signal Handling & Zombie Processes in Unix Sockets: TCP Client-Server, Slides of Network Programming

An in-depth explanation of a tcp client-server example, focusing on signal handling and the management of zombie processes in unix sockets. Topics covered include the role of signals, signal dispositions, posix signal semantics, handling sigchld signals, and the use of wait and waitpid functions. Students and professionals interested in network programming, unix sockets, and system administration will find this document useful.

Typology: Slides

2011/2012

Uploaded on 07/06/2012

umi
umi 🇮🇳

4.5

(13)

63 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
TCP Client-Server Example
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Understanding Signal Handling & Zombie Processes in Unix Sockets: TCP Client-Server and more Slides Network Programming in PDF only on Docsity!

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

TCP Echo Server

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); } }

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 Termination

  • tcpcli 127.0.0.

hello, world hello, world good bye good bye ^D

  • ps

19130 p1 Ss -ksh 21130 p1 I ./tcpserv 21132 p1 Z (tcpserv) (Z:zombie 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. - void handler(int signo);
  • We can ignore a signal by setting its disposition to SIG_IGN.
  • 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)

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

  • We establish the signal handler by adding the

function call

Signal (SIGCHLD, sig_chld); after the call to listen() in server.

void sig_chld(int signo) { pid_t pid; int stat; pid = wait(&stat); printf("child %d terminated\n", pid); return; } Version of SIGCHLD signal handler that calls wait

  • Tcpserv02 &
  • tcpcli01 127.0.0.

hi, there 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 // kernel causes the accept to return an error of EINTER

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.

#include <sys/wait.h> pid_t wait(int * statloc ); pid_t waitpid(pid_t pid , int * statloc , int option );

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; }

SIGPIPE Signal

  • 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. hi there hi there

bye

Nothing is echoed for bye data Reason: the default action of SIGPIPE is terminate the process.