当前位置: 技术问答>linux和unix
◎为何出现僵尸进程◎请教
来源: 互联网 发布时间:2015-04-30
本文导语: 如下代码 #indlue .... #define.... int main(int argc, char * argv[]) { int sockfd, new_fd; pid_t pid; // pid int len; char msg[BUFSIZE] = {0}; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; soc...
如下代码
#indlue ....
#define....
int main(int argc, char * argv[])
{
int sockfd, new_fd;
pid_t pid; // pid
int len;
char msg[BUFSIZE] = {0};
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_port = htons(MYPORT);
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, BACKLOG);
while(1)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd,(struct sockaddr *)&their_addr, &sin_size);
if((pid=fork()) == 0)
{
close(sockfd);
recv(new_fd, msg, BUFSIZE, 0);
printf("STRINGS: %sn", msg);
close(new_fd);
exit(0);
}
else
close(new_fd);
}
close(sockfd);
exit(0);
}
执行完 开始接受后
接收到一条
系统出现一个
[ThreadListen ]
怎么办 啊
#indlue ....
#define....
int main(int argc, char * argv[])
{
int sockfd, new_fd;
pid_t pid; // pid
int len;
char msg[BUFSIZE] = {0};
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_port = htons(MYPORT);
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
listen(sockfd, BACKLOG);
while(1)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd,(struct sockaddr *)&their_addr, &sin_size);
if((pid=fork()) == 0)
{
close(sockfd);
recv(new_fd, msg, BUFSIZE, 0);
printf("STRINGS: %sn", msg);
close(new_fd);
exit(0);
}
else
close(new_fd);
}
close(sockfd);
exit(0);
}
执行完 开始接受后
接收到一条
系统出现一个
[ThreadListen ]
怎么办 啊
|
父进程要给子进程收尸的,
使用waitpid.
signal( SIGCHLD, sig_chld );
....
static void sig_chld(int signo)
{
pid_t pid;
int stat;
while( (pid=waitpid(-1, &stat, WNOHANG)) > 0 );
return;
}
使用waitpid.
signal( SIGCHLD, sig_chld );
....
static void sig_chld(int signo)
{
pid_t pid;
int stat;
while( (pid=waitpid(-1, &stat, WNOHANG)) > 0 );
return;
}