












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 20
This page cannot be seen from the preview
Don't miss anything!













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); } }
hello, world hello, world good bye good bye ^D
19130 p1 Ss -ksh 21130 p1 I ./tcpserv 21132 p1 Z (tcpserv) (Z:zombie process)
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
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
#include <sys/wait.h> pid_t wait(int * statloc ); pid_t waitpid(pid_t pid , int * statloc , int option );
wait and waitpid Functions
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.