1. 说明
第5.10的wait与waitpid
当client有5个连接同时关闭时,
server端的wait来不及处理同时发生的5个sigchld,导致僵死进程产生。
1.1 现象
-
cong@msi:/work/test/tcpip/7tcp/client$ ./client
-
client.c:main[59]: connect success
-
client.c:main[59]: connect success
-
client.c:main[59]: connect success
-
client.c:main[59]: connect success
-
client.c:main[59]: connect success
-
cong@msi:/work/test/tcpip/7tcp/client$
当client连接上时,共执行了5次fork,但是只收到了3次sigchld
-
cong@msi:/work/test/tcpip/7tcp/serv$ ./tcpserv
-
tcpserv.c:main[50]: accept:127.0.0.1,port=45388
-
tcpserv.c:main[50]: accept:127.0.0.1,port=45389
-
tcpserv.c:main[58]: fork --> fork1
-
tcpserv.c:main[50]: accept:127.0.0.1,port=45390
-
tcpserv.c:main[58]: fork --> fork2
-
tcpserv.c:main[50]: accept:127.0.0.1,port=45391
-
tcpserv.c:main[58]: fork --> fork3
-
tcpserv.c:main[50]: accept:127.0.0.1,port=45392
-
tcpserv.c:main[58]: fork --> fork4
-
tcpserv.c:main[58]: fork --> fork5
-
tcpserv.c:sig_chld[9]: child 16165 terminated --> term1
-
tcpserv.c:sig_chld[9]: child 16166 terminated --> term2
-
tcpserv.c:sig_chld[9]: child 16167 terminated --> term3
ps查看发现,正好有2个处于僵死状态的子进程
-
cong@msi:/work/test/tcpip/7tcp/client$ ps -u
-
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
-
cong 16197 0.0 0.0 4196 504 pts/1 S+ 18:00 0:00 ./tcpserv
-
cong 16202 0.0 0.0 0 0 pts/1 Z+ 18:01 0:00 [tcpserv] <defunct>
-
cong 16203 0.0 0.0 0 0 pts/1 Z+ 18:01 0:00 [tcpserv] <defunct>
2. 代码
2.1 修改了的client端
-
cong@msi:/work/test/tcpip/7tcp/client$ cat client.c
-
#include "utils.h"
-
-
//#define TIME_SERV_ADDR "192.168.4.98"
-
#define TIME_SERV_ADDR "127.0.0.1"
-
#if 0
-
ssize_t readline(int fd, void* vptr, size_t maxlen)
-
{
-
ssize_t n, rc;
-
char c,*ptr;
-
ptr = vptr;
-
for(n=1; n<maxlen; n++)
-
{
-
again:
-
if((rc=read(fd,&c,1)) == 1)
-
{
-
*ptr++ = c;
-
if(c == '\n')
-
break;
-
} else if(rc == 0) {
-
*ptr = 0;
-
return (n-1);
-
}else {
-
if( errno == EINTR)
-
goto again;
-
return -1;
-
}
-
}
-
}
-
#endif
-
ssize_t readline(int fd, void* vptr, size_t maxlen)
{
ssize_t n, rc;
char c,*ptr;
ptr = vptr;
for(n=0; n
again:
if((rc=read(fd,&c,1)) == 1)
{
*ptr++ = c;
if(c == '\n')
break;
} else if(rc == 0) {
*ptr = 0;
return n;
}else {
if( errno == EINTR)
goto again;
return -1;
}
}
*ptr = '\0';
return n;
}
-
void str_cli(FILE* fp, int sockfd)
-
{
-
char sendline[MAXLINE], recvline[MAXLINE];
-
while(fgets(sendline, MAXLINE, fp) != NULL)
-
{
-
write(sockfd, sendline, strlen(sendline));
-
if(readline(sockfd, recvline, MAXLINE) == 0)
-
dbmsg("server terminated");
-
fputs(recvline, stdout);
-
}
-
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int i;
-
int sockfd, n;
-
struct sockaddr_in servaddr;
-
char recvline[1024];
-
for(i=0; i<5; i++) //产生了5个client
-
{
-
sockfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(sockfd < 0)
-
return -1;
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999);
-
inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
dbmsg("connect success");
-
}
-
str_cli(stdin, sockfd); //str_cli返回后,进程退出,这会发送5个FIN
-
return EXIT_SUCCESS;
-
}
2.2 server.c出的问题分析
-
cong@msi:/work/test/tcpip/7tcp/serv$ cat tcpserv.c
-
#include "utils.h"
-
-
#define LISTENQ 10
-
void sig_chld(int signo)
-
{
-
pid_t pid;
-
int stat;
-
pid = wait(&stat); //3.同时产生5个SIGCHLD,会同时调用wait
-
dbmsg("child %d terminated", pid); //但是wait是阻塞的,当wait在处理sigchld信号时再有sigchld信号过来
-
return ; //wait是没有办法处理的,所以导致了僵死进程产生
-
}
-
void str_echo(int sockfd)
-
{
-
ssize_t n;
-
char buf[MAXLINE];
-
again:
-
while( (n=read(sockfd, buf, MAXLINE)) > 0)
-
write(sockfd, buf, n);
-
-
if( (n<0) && (errno==EINTR) )
-
goto again;
-
else if(n < 0)
-
dbmsg("read_error");
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int listenfd, connfd;
-
struct sockaddr_in servaddr, cliaddr;
-
signal(SIGCHLD, sig_chld);
-
pid_t childpid;
-
char buf[1024];
-
int len;
-
listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(listenfd< 0)
-
return -1;
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999);
-
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
//inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
-
bind(listenfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
listen(listenfd, LISTENQ);
-
while(1)
-
{
-
len = sizeof(cliaddr);
-
//connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
-
connfd = accept(listenfd, (struct sockaddr*)&cliaddr,(socklen_t*)&len);
-
dbmsg("accept:%s,port=%d", inet_ntop(AF_INET,&cliaddr.sin_addr,buf, sizeof(buf)), ntohs(cliaddr.sin_port));
-
if(errno == EINTR)
-
{
-
dbmsg("EINTR");
-
continue;
-
}
-
if( (childpid=fork()) == 0)
-
{
-
dbmsg("fork");
-
close(listenfd);
-
str_echo(connfd); //1.client:str_cli返回后,进程退出,这会发送5个FIN
-
exit(0); //2.这会导致serve的5个进程同时终止,并同时产生5个SIGCHLD
-
}
-
close(connfd);
-
}
-
-
return EXIT_SUCCESS;
-
}
2.3 源码
client.rar (下载后改名为client.tar.gz)
serv_old.rar (下载后改名为serv_old.tar.gz)
3. 修改后的server.c
-
cong@msi:/work/test/tcpip/7tcp/serv$ cat tcpserv.c
-
#include "utils.h"
-
-
#define LISTENQ 10
-
void sig_chld(int signo)
-
{
-
pid_t pid;
-
int stat;
-
//pid = wait(&stat); //WNOHANG: return immediately if no child has exited
-
while( (pid = waitpid(-1,&stat,WNOHANG)) > 0) //3.同时产生5个SIGCHLD,会同时调用waitpid,此时waitpid是非阻塞的
-
dbmsg("child %d terminated", pid); //接收到sigchld信号时会立即返回并等侍下一个sigchld信号的到来,不会丢信号
-
return ;
-
}
-
void str_echo(int sockfd)
-
{
-
ssize_t n;
-
char buf[MAXLINE];
-
again:
-
while( (n=read(sockfd, buf, MAXLINE)) > 0)
-
write(sockfd, buf, n);
-
-
if( (n<0) && (errno==EINTR) )
-
goto again;
-
else if(n < 0)
-
dbmsg("read_error");
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
int listenfd, connfd;
-
struct sockaddr_in servaddr, cliaddr;
-
signal(SIGCHLD, sig_chld);
-
pid_t childpid;
-
char buf[1024];
-
int len;
-
listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
if(listenfd< 0)
-
return -1;
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_port = htons(13999);
-
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
//inet_pton(AF_INET, TIME_SERV_ADDR, &servaddr.sin_addr);
-
-
bind(listenfd, (const struct sockaddr*)&servaddr, sizeof(servaddr));
-
listen(listenfd, LISTENQ);
-
while(1)
-
{
-
len = sizeof(cliaddr);
-
//connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
-
connfd = accept(listenfd, (struct sockaddr*)&cliaddr,(socklen_t*)&len);
-
dbmsg("accept:%s,port=%d", inet_ntop(AF_INET,&cliaddr.sin_addr,buf, sizeof(buf)), ntohs(cliaddr.sin_port));
-
if(errno == EINTR)
-
{
-
dbmsg("EINTR");
-
continue;
-
}
-
if( (childpid=fork()) == 0)
-
{
-
dbmsg("fork");
-
close(listenfd); //1.client:str_cli返回后,进程退出,这会发送5个FIN
-
str_echo(connfd); //2.这会导致serve的5个进程同时终止,并同时产生5个SIGCHLD
-
exit(0);
-
}
-
close(connfd);
-
}
-
-
return EXIT_SUCCESS;
-
}
client.rar (下载后改名为client.tar.gz)
serv_new.rar (下载后改名为serv_new.tar.gz)
阅读(1164) | 评论(0) | 转发(0) |