Linux网络编程wait()和waitpid()的讲解
网络知识 2023-02-09 13:14www.1681989.comseo网站推广
客户端断开连接后,服务器端存在大量僵尸进程。这是由于服务器子进程终止后,发送SIGCHLD信号给父进程,而父进程默认忽略了该信号。为避免僵尸进程的产生,无论我们什么时候创建子进程时,主进程都需要等待子进程返回,以便对子进程进行清理。为此,我们在服务器程序中添加SIGCHLD信号处理函数。
复制代码
代码如下:复制代码
#clude <stdlib.h>
#clude <stdio.h>
#clude <errno.h>
#clude <strg.h>
#clude <unistd.h>
#clude <sys/socket.h>
#clude <i/.h>
#clude <sys/types.h>
#clude <db.h>
#defe SERV_PORT 1113
#defe LISTENQ 32
#defe MAXLINE 1024
/连接处理函数/
void str_echo(t fd);
void
sig_chld(t signo)
{
pid_t pid;
t stat;
pid = wait(&stat);//获取子进程进程号
prtf("child %d termated\n", pid);
return;
}
t
ma(t argc, char argv[]){
t listenfd,connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_ servaddr;
struct sockaddr_ cliaddr;
//struct sockaddr_ servaddr;
//struct sockaddr_ cliaddr;
if((listenfd = socket(AF_INET, SOCK_STREAM,0))==-1){
fprtf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
/ 服务器端填充 sockaddr结构/
bzero(&servaddr, sizeof(servaddr));
servaddr.s_family = AF_INET;
servaddr.s_addr.s_addr = htonl (INADDR_ANY);
servaddr.s_port = htons(SERV_PORT);
signal(SIGCHLD,sig_chld);//处理SIGCHLD信号
/ 捆绑listenfd描述符 /
if(bd(listenfd,(struct sockaddr)(&servaddr),sizeof(struct sockaddr))==-1){
fprtf(stderr,"Bd error:%s\n\a",strerror(errno));
exit(1);
}
/ 监听listenfd描述符/
if(listen(listenfd,5)==-1){
fprtf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
for ( ; ; ) {
clilen = sizeof(cliaddr);
/ 服务器阻塞,直到客户程序建立连接 /
if((connfd=aept(listenfd,(struct sockaddr)(&cliaddr),&clilen))<0){
/当一个子进程终止时,执行信号处理函数sig_chld,
而该函数返回时,aept系统调用可能返回一个EINTR错误,
有些内核会自动重启被中断的系统调用,为便于移植,将考虑对EINTR的处理/
if(errno==EINTR)
contue;
fprtf(stderr,"Aept error:%s\n\a",strerror(errno));
exit(1);
}
//有客户端建立了连接后
if ( (childpid = fork()) == 0) { /子进程/
close(listenfd); / 关闭监听套接字/
str_echo(connfd); /处理该客户端的请求/
exit (0);
}
close(connfd);/父进程关闭连接套接字,继续等待其他连接的到来/
}
}
void str_echo(t sockfd){
ssize_t n;
char buf[MAXLINE];
aga:
while ( (n = read(sockfd, buf, MAXLINE)) > 0)
write(sockfd, buf, n);
if (n < 0 && errno == EINTR)//被中断,重入
goto aga;
else if (n < 0){//出错
fprtf(stderr,"read error:%s\n\a",strerror(errno));
exit(1);
}
}
修改代码后,当客户端断开连接后,服务器端父进程收到子进程的SIGCHLD信号后,会执行sig_chld函数,对子进程进行了清理,便不会再出现僵尸进程。此时,一个客户端主动断开连接后,服务器端会输出类似如下信息
child 12306 termated
wait和waitpid
上述程序中sig_chld函数,我们使用了wait()来清除终止的子进程。还有一个类似的函数wait_pid。我们先来看看这两个函数原型
pid_t wait(t status);
pid_t waitpid(pid_t pid, t status, t options);
官方描述All of these system calls are used to wait for state changes a child of the callg process, and obta formation about the child whose state has changed. A state change is considered to be: the child ter mated; the child was sped by a signal; or the child was resumed by a signal. In the case of a termated child, performg a wait allows the system to release the resources associated with the child; if a wait is not performed, then the termated child remas a "zombie" state (see NOTES below).
关于wait和waitpid两者的区别与联系
The wait() system call suspends execution of the callg process until one of its children termates. The call wait(&status) is equivalent to:
waitpid(-1, &status, 0);
The waitpid() system call suspends execution of the callg process until a child specified by pid argument has changed state. By default, waitpid() waits only for termated children, but this behavior is modifiable via the options argument, as described below.
也就是说,wait()系统调用会挂起调用进程,直到它的任意一个子进程终止。调用wait(&status)的效果跟调用waitpid(-1, &status, 0)的效果是一样一样的。
waitpid()会挂起调用进程,直到参数pid指定的进程状态改变,默认情况下,waitpid() 只等待子进程的终止状态。如果需要,可以通过设置options的值,来处理非终止状态的情况。比如
The value of options is an OR of zero or more of the followg constants:
WNOHANG return immediately if no child has exited.
WUNTRACED also return if a child has sped (but not traced via ptrace(2)). Status for traced children which have sped is provided even if this option is not specified.
WCONTINUED (sce Lux 2.6.10)also return if a sped child has been resumed by delivery of SIGCONT.
等等一下非终止状态。
现在来通过实例看看wait()和waitpid()的区别。
通过修改客户端程序,在客户端程序中一次性建立5个套接字连接到服务器,状态如下图所示(附代码)
复制代码