本文讲的是关于wait和waitpid两者的区别与联系。为避免僵尸进程的产生,无论我们什么时候创建子进程时,主进程都需要等待子进程返回,以便对子进程进行清理。为此,我们在服务器程序中添加SIGCHLD信号处理函数。  
客户端断开连接后,服务器端存在大量僵尸进程。这是由于服务器子进程终止后,发送SIGCHLD信号给父进程,而父进程默认忽略了该信号。为避免僵尸进程的产生,无论我们什么时候创建子进程时,主进程都需要等待子进程返回,以便对子进程进行清理。为此,我们在服务器程序中添加SIGCHLD信号处理函数。

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#define SERV_PORT 1113
#define LISTENQ 32
#define MAXLINE 1024
/***连接处理函数***/
void str_echo(int fd);
void
sig_chld(int signo)
{
pid_t pid;
int stat;
pid = wait(&stat);//获取子进程进程号
printf("child %d terminated\n", pid);
return;
}
int
main(int argc, char *argv[]){
int listenfd,connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in servaddr;
struct sockaddr_in cliaddr;
//struct sockaddr_in servaddr;
//struct sockaddr_in cliaddr;
if((listenfd = socket(AF_INET, SOCK_STREAM,))==-){
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit();
}
/* 服务器端填充 sockaddr结构*/
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
signal(SIGCHLD,sig_chld);//处理SIGCHLD信号
/* 捆绑listenfd描述符 */
if(bind(listenfd,(struct sockaddr*)(&servaddr),sizeof(struct sockaddr))==-){
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit();
}
/* 监听listenfd描述符*/
if(listen(listenfd,)==-){
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit();
}
for ( ; ; ) {
clilen = sizeof(cliaddr);
/* 服务器阻塞,直到客户程序建立连接 */
if((connfd=accept(listenfd,(struct sockaddr*)(&cliaddr),&clilen))<){
/*当一个子进程终止时,执行信号处理函数sig_chld,
而该函数返回时,accept系统调用可能返回一个EINTR错误,
有些内核会自动重启被中断的系统调用,为便于移植,将考虑对EINTR的处理*/
if(errno==EINTR)
continue;
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit();
}
//有客户端建立了连接后
if ( (childpid = fork()) == ) { /*子进程*/
close(listenfd); /* 关闭监听套接字*/
str_echo(connfd); /*处理该客户端的请求*/
exit ();
}
close(connfd);/*父进程关闭连接套接字,继续等待其他连接的到来*/
}
}
void str_echo(int sockfd){
ssize_t n;
char buf[MAXLINE];
again:
while ( (n = read(sockfd, buf, MAXLINE)) > )
write(sockfd, buf, n);
if (n < && errno == EINTR)//被中断,重入
goto again;
else if (n < ){//出错
fprintf(stderr,"read error:%s\n\a",strerror(errno));
exit();
}
} 修改代码后,当客户端断开连接后,服务器端父进程收到子进程的SIGCHLD信号后,会执行sig_chld函数,对子进程进行了清理,便不会再出现僵尸进程。此时,一个客户端主动断开连接后,服务器端会输出类似如下信息:
child terminated
wait和waitpid
上述程序中sig_chld函数,我们使用了wait()来清除终止的子进程。还有一个类似的函数wait_pid。我们先来看看这两个函数原型:
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
官方描述:All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child ter minated; the child was stopped by a signal; or the child was resumed by a signal. In the case of a terminated child, performing a wait allows the system to release the resources associated with the child; if a wait is not performed, then the terminated child remains in a "zombie" state (see NOTES below).
关于wait和waitpid两者的区别与联系:
The wait() system call suspends execution of the calling process until one of its children terminates. The call wait(&status) is equivalent to:
waitpid(-, &status, );
The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described below.
  也就是说,wait()系统调用会挂起调用进程,直到它的任意一个子进程终止。调用wait(&status)的效果跟调用waitpid(-, &status, )的效果是一样一样的。
  waitpid()会挂起调用进程,直到参数pid指定的进程状态改变,默认情况下,waitpid() 只等待子进程的终止状态。如果需要,可以通过设置options的值,来处理非终止状态的情况。比如:
The value of options is an OR of zero or more of the following constants:
WNOHANG return immediately if no child has exited.
WUNTRACED also return if a child has stopped (but not traced via ptrace()). Status for traced children which have stopped is provided even if this option is not specified.
WCONTINUED (since Linux 2.6.)also return if a stopped child has been resumed by delivery of SIGCONT.
等等一下非终止状态。
现在来通过实例看看wait()和waitpid()的区别。
通过修改客户端程序,在客户端程序中一次性建立5个套接字连接到服务器,状态如下图所示(附代码):
#include <stdlib.h>
#include <stdio.h>
#include
<errno.h>
#include <string.h>
#include
<unistd.h>
#include <sys/socket.h>
#include
<netinet/in.h>
#include <sys/types.h>
#include
<netdb.h>
#define SERV_PORT 1113
#define MAXLINE 1024
void
str_cli(FILE *fp, int sockfd);
int
main(int argc, char **argv)
{
int
i,sockfd[5];
struct sockaddr_in servaddr;
if (argc !=
2){
fprintf(stderr,"usage: tcpcli
<IPaddress>\n\a");
exit(0);
}
for(i=0;i<5;++i){//与服务器建立五个连接,以使得服务器创建5个子进程
if((sockfd[i]=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Socket
error:%s\n\a",strerror(errno));
exit(1);
}

/*
客户程序填充服务端的资料*/
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
if
(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <=
0){
fprintf(stderr,"inet_pton
Error:%s\a\n",strerror(errno));
exit(1);
}
/* 客户程序发起连接请求*/

if(connect(sockfd[i],(struct sockaddr *)(&servaddr),sizeof(struct
sockaddr))==-1){
fprintf(stderr,"connect
Error:%s\a\n",strerror(errno));
exit(1);
}
}
str_cli(stdin,
sockfd[0]);/*仅用第一个套接字与服务器交互*/
exit(0);
}
void
str_cli(FILE *fp, int
sockfd)
{
int nbytes=0;
char
sendline[MAXLINE],recvline[MAXLINE];
while (fgets(sendline, MAXLINE, fp) !=
NULL){//从标准输入中读取一行
write(sockfd, sendline,
strlen(sendline));//将该行发送给服务器
if ((nbytes=read(sockfd, recvline, MAXLINE)) ==
0){//从sockfd读取从服务器发来的数据
fprintf(stderr,"str_cli: server terminated
prematurely\n");
exit(1);
}
recvline[nbytes]='\0';
fputs(recvline,
stdout);
}
}

当客户终止时,所以打开的描述子均由内核自动关闭,因此5个连接基本在同一时刻发生,相当于同时引发了5个FIN发往服务器,这会导致5个服务器子进程基本在同一时刻终止,从而导致5个SIGCHLD信号几乎同时递送给服务器父进程,示意图如下所示:

也就是说,几乎在同一时刻,递送5个SIGCHLD信号给父进程,这又会僵尸进程进程的出现。因为unix一般不对信号进行排队,这就导致了5个SIGCHLD递交上去,只执行了一次sig_chld函数,剩下四个子进程便成为了僵尸进程。对于这种情况,正确的做法是调用waitpid(),而不是wait()。
因此,我们最后的服务器端代码中的信号处理函数做一点小改动,改成如下:
代码如下:
void
sig_chld(int signo)
{
pid_t
pid;
int stat;
while ( (pid = waitpid(-1, &stat, WNOHANG)) >
0)
printf("child %d terminated\n",
pid);
return;
}

至此,我们解决了网络编程中可能遇到的三类情况:
1.当派生子进程时,必须捕获SIGCHLD信号。代码片段:signal(SIGCHLD,sig_chld);
2.当捕获信号时,必须处理被中断的系统调用。代码片段:if(errno==EINTR)
continue;
3.SIGCHLD信号处理函数必须编写正确,以防出现僵尸进程。代码片段:while ( (pid = waitpid(-1,
&stat, WNOHANG)) > 0) 
http://www.jb51.net/LINUXjishu/113671.html

Linux网络编程wait()和waitpid()的讲解的更多相关文章

  1. linux网络编程-(socket套接字编程UDP传输)

    今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作! 在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中, ...

  2. Linux网络编程&内核学习

    c语言: 基础篇 1.<写给大家看的C语言书(第2版)> 原书名: Absolute Beginner's Guide to C (2nd Edition) 原出版社: Sams 作者: ...

  3. Linux网络编程(三)

    Linux网络编程(三) wait()还是waitpid() Linux网络编程(二)存在客户端断开连接后,服务器端存在大量僵尸进程.这是由于服务器子进程终止后,发送SIGCHLD信号给父进程,而父进 ...

  4. Proxy源代码分析——谈谈如何学习Linux网络编程

    Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到, Linux和Windows这样的"傻瓜"操作系统(这里丝毫没有贬低Windows的意思,相反这应该 ...

  5. 第5章 Linux网络编程基础

    第5章 Linux网络编程基础 5.1 socket地址与API 一.理解字节序 主机字节序一般为小端字节序.网络字节序一般为大端字节序.当格式化的数据在两台使用了不同字节序的主机之间直接传递时,接收 ...

  6. Proxy源代码分析--谈谈如何学习Linux网络编程

    http://blog.csdn.net/cloudtech/article/details/1823531 Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到,Linux ...

  7. Linux 网络编程的5种IO模型:信号驱动IO模型

    Linux 网络编程的5种IO模型:信号驱动IO模型 背景 上一讲 Linux 网络编程的5种IO模型:多路复用(select/poll/epoll) 我们讲解了多路复用等方面的知识,以及有关例程. ...

  8. 【深入浅出Linux网络编程】 "开篇 -- 知其然,知其所以然"

    [深入浅出Linux网络编程]是一个连载博客,内容源于本人的工作经验,旨在给读者提供靠谱高效的学习途径,不必在零散的互联网资源中浪费精力,快速的掌握Linux网络编程. 连载包含4篇,会陆续编写发出, ...

  9. 【linux草鞋应用编程系列】_5_ Linux网络编程

    一.网络通信简介   第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章.   二.linux网络通信     在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...

随机推荐

  1. mysql5.5.15配置主从数据库

    1.编辑主库的my.cnf 在[mysqld]下添加如下配置 server-i=1 #一般默认为1,不需要修改(一般都以ip的后两位为server-id,保证全局的一致) read-only=0#主库 ...

  2. springboot中generator相关配置文件

    generator.properties # jdbc jdbc.driverClass = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localho ...

  3. C#基础视频教程5.1 如何编写简单的超级热键

    我们上一节介绍了编写简单计算器,实际上也是出于实用角度(这个计算器只要你肯改,肯定能做的比微软自带的计算器好用).这一节介绍做简单的超级热键(所谓的超级热键是指自定义快捷键的功能) 超级热键的最关键一 ...

  4. 软件调试工具——GDB

    1.GDB调试器概述 GDB是GNU开源组织发布的一个强大的程序调试工具,具有查看程序运行状态.设置断点.查看表达式.显示变量等众多功能,是程序员进行Linux编程必须要掌握的一种调试技术. GDB调 ...

  5. spring测试实例

    我们以前要进行单元测试,必须先得到ApplicationContext对象,再通过它得到业务对象,非常麻烦,重复代码也多.基于spring3的单元测试很好的解决了这个问题 基于spring3的单元测试 ...

  6. linux系统中的DNS服务器介绍

    http://lq2419.blog.51cto.com/1365130/1172269 DNS:Domain Name Service,linux上的DNS服务是基于一种软件BIND实现的.BIND ...

  7. Bitmap尺度变换

    Bitmap bitMap = BitmapFactory.decodeFile(path); int width = bitMap.getWidth(); int height = bitMap.g ...

  8. 基于微信小程序的用户列表点赞功能

    代码地址如下:http://www.demodashi.com/demo/13997.html 一.前言 (1).适合人群 1.微信小程序开发者 2.前端工程师 3.想入门学习小程序开发的人员 4.想 ...

  9. ASP.NET MVC之Html.RenderAction(无操作方法 传参数)

    WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...

  10. Android的xml/assets/raw资源使用具体解释

    一.assets/xml/raw资源介绍 1.assets资源文件夹:assets文件夹下存放的资源代表应用无法直接訪问的原生资源,这些文件将原封不动的存储到设备上,不会被编译为二进制形式,訪问方式是 ...