问题遇到
问题遇到
僵尸进程百度解释
由于子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束. 那么会不会因为父进程太忙来不及wait子进程,或者说不知道 子进程什么时候结束,而丢失子进程结束时的状态信息呢? 不会。因为UNⅨ提供了一种机制可以保证只要父进程想知道子进程结束时的状态信息, 就可以得到。这种机制就是: 在每个进程退出的时候,内核释放该进程所有的资源,包括打开的文件,占用的内存等。但是仍然为其保留一定的信息(包括进程号the process ID,退出状态the termination status of the process,运行时间the amount of CPU time taken by the process等)。直到父进程通过wait / waitpid来取时才释放. 但这样就导致了问题,如果进程不调用wait / waitpid的话,那么保留的那段信息就不会释放,其进程号就会一直被占用,但是系统所能使用的进程号是有限的,如果大量的产生僵死进程,将因为没有可用的进程号而导致系统不能产生新的进程. 此即为僵尸进程的危害,应当避免。
僵尸解决方案
1.ps命令查看进程
使用linux的ps命令查看进程,这个在不同的版本下面好像显示的方式不一样。这里我在说下,我的ps -u后面开始不明白是个啥,
后来我查看了相关的资料才晓得就是登陆明,郁闷了几分钟。我是在mac上进行测试的,测试如下:
1.测试前,客户端和服务器都正常运行
这样我们可以看出有一个僵尸进程
2.使用信号,让内核处理
使用最简单的方式就是使用信号直接处理又简单又实用。
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nread = read(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nread == 0)
return count - nleft;
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nwritten == 0)
continue;
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while (1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK);
if (ret == -1 && errno == EINTR)
continue;
return ret;
}
}
//maxline 一行最大数
//先提前peek一下缓冲区,如果有数据从缓冲区的读数据,
//1、缓冲区数据中带\n
//2 缓存区中不带\n
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = buf;
int nleft = maxline;
while (1)
{
//看一下缓冲区有没有数据,并不移除内核缓冲区数据
ret = recv_peek(sockfd, bufp, nleft);
if (ret < 0) //失败
return ret;
else if (ret == 0) //对方已关闭
return ret;
nread = ret;
int i;
for (i = 0; i < nread; i++)
{
if (bufp[i] == '\n') //若缓冲区有\n
{
ret = readn(sockfd, bufp, i + 1); //读走数据
if (ret != i + 1)
exit(EXIT_FAILURE);
return ret; //有\n就返回,并返回读走的数据
}
}
if (nread > nleft) //如果读到的数大于 一行最大数 异常处理
exit(EXIT_FAILURE);
nleft -= nread; 若缓冲区没有\n, 把剩余的数据读走
ret = readn(sockfd, bufp, nread);
if (ret != nread)
exit(EXIT_FAILURE);
bufp += nread; //bufp指针后移后,再接着偷看缓冲区数据recv_peek,直到遇到\n
}
return -1;
}
void do_service(int conn)
{
char recvbuf[1024];
while (1)
{
memset(recvbuf, 0, sizeof(recvbuf));
int ret = readline(conn, recvbuf, 1024);
if (ret == -1)
ERR_EXIT("readline");
if (ret == 0)
{
printf("client close\n");
break;
}
fputs(recvbuf, stdout);
//将接受的数据再发送
writen(conn, recvbuf, strlen(recvbuf));
}
}
int main(void)
{
//僵尸进程的地种方式忽略信号
signal(SIGCHLD,SIG_IGN);
int listenfd; //监听socket的文件描述符
if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
/* if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)*/
ERR_EXIT("socket");
//TCP/IP 结构体
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8001);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");*/
/*inet_aton("127.0.0.1", &servaddr.sin_addr);*/
//设置地址复用
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");
//绑定
if (bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
ERR_EXIT("bind");
//监听
if (listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen");
//定义 socket信息结构体,在新的链接中会返回新的已经链接的socket的信息。
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
int conn;
pid_t pid;
while (1)
{
if ((conn = accept(listenfd, (struct sockaddr*) &peeraddr, &peerlen))
< 0)
ERR_EXIT("accept");
printf("ip=%s port=%d\n", inet_ntoa(peeraddr.sin_addr),
ntohs(peeraddr.sin_port));
pid = fork();
if (pid == -1)
ERR_EXIT("fork");
if (pid == 0)
{
//关闭监听socket
close(listenfd);
do_service(conn);
exit(EXIT_SUCCESS);
} else
//关闭链接socket
close(conn);
}
return 0;
}
3.使用wait进行处理
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nread = read(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nread == 0)
return count - nleft;
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nwritten == 0)
continue;
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while (1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK);
if (ret == -1 && errno == EINTR)
continue;
return ret;
}
}
//maxline 一行最大数
//先提前peek一下缓冲区,如果有数据从缓冲区的读数据,
//1、缓冲区数据中带\n
//2 缓存区中不带\n
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = buf;
int nleft = maxline;
while (1)
{
//看一下缓冲区有没有数据,并不移除内核缓冲区数据
ret = recv_peek(sockfd, bufp, nleft);
if (ret < 0) //失败
return ret;
else if (ret == 0) //对方已关闭
return ret;
nread = ret;
int i;
for (i = 0; i < nread; i++)
{
if (bufp[i] == '\n') //若缓冲区有\n
{
ret = readn(sockfd, bufp, i + 1); //读走数据
if (ret != i + 1)
exit(EXIT_FAILURE);
return ret; //有\n就返回,并返回读走的数据
}
}
if (nread > nleft) //如果读到的数大于 一行最大数 异常处理
exit(EXIT_FAILURE);
nleft -= nread; 若缓冲区没有\n, 把剩余的数据读走
ret = readn(sockfd, bufp, nread);
if (ret != nread)
exit(EXIT_FAILURE);
bufp += nread; //bufp指针后移后,再接着偷看缓冲区数据recv_peek,直到遇到\n
}
return -1;
}
void do_service(int conn)
{
char recvbuf[1024];
while (1)
{
memset(recvbuf, 0, sizeof(recvbuf));
int ret = readline(conn, recvbuf, 1024);
if (ret == -1)
ERR_EXIT("readline");
if (ret == 0)
{
printf("client close\n");
break;
}
fputs(recvbuf, stdout);
//将接受的数据再发送
writen(conn, recvbuf, strlen(recvbuf));
}
}
void handle_sigid(int slg)
{
wait(NULL);
}
int main(void)
{
//1.僵尸进程的地种方式忽略信号
//signal(SIGCHLD,SIG_IGN);
//2.僵尸进程的第二种方式
signal(SIGCHLD,handle_sigid);
int listenfd; //监听socket的文件描述符
if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
/* if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)*/
ERR_EXIT("socket");
//TCP/IP 结构体
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8001);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");*/
/*inet_aton("127.0.0.1", &servaddr.sin_addr);*/
//设置地址复用
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");
//绑定
if (bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
ERR_EXIT("bind");
//监听
if (listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen");
//定义 socket信息结构体,在新的链接中会返回新的已经链接的socket的信息。
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
int conn;
pid_t pid;
while (1)
{
if ((conn = accept(listenfd, (struct sockaddr*) &peeraddr, &peerlen))
< 0)
ERR_EXIT("accept");
printf("ip=%s port=%d\n", inet_ntoa(peeraddr.sin_addr),
ntohs(peeraddr.sin_port));
pid = fork();
if (pid == -1)
ERR_EXIT("fork");
if (pid == 0)
{
//关闭监听socket
close(listenfd);
do_service(conn);
exit(EXIT_SUCCESS);
} else
//关闭链接socket
close(conn);
}
return 0;
}
多并发的处理
我们作为服务器的开发人员已经要放弃以前那种客户端的思想,只对自己负责的情况,于是我用客户端同时五个链接的情况做了一个测试,发现服务器并不能直接处理这么多的反馈。下面大家可以自己测试,我已经测试过了不能全部进行消灭僵尸进程最后使用while(mypid =waitpod(-1,NULL,WNOHANG))>0)来删除僵尸进程。(ps当然时在信号处理的函数里面)
客户端代码
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nread = read(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nread == 0)
return count - nleft;
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nwritten == 0)
continue;
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
//读数据,但不把数据缓冲区清空
//@ssize_t返回值:返回缓冲区数据的长度 -1失败
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while (1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK);
if (ret == -1 && errno == EINTR)
continue;
return ret;
}
}
//功能:按行读取文件,只要遇到\n就,读走数据,返回,
//@buf 接收数据内存首地址
//@maxline 接收数据内存最大值
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = buf;
int nleft = maxline;
while (1)
{
//读数据,但不把数据缓冲区清空
//成功:ret是报文的长度
ret = recv_peek(sockfd, bufp, nleft);
if (ret < 0)
return ret;
else if (ret == 0)
return ret;
nread = ret;
int i;
for (i = 0; i < nread; i++)
{
//读数据,但不把数据缓冲区清空,避免了一个字节一个字节的读数据
//先利用recv的MSG_PEEK功能,预读数据,然后查找\n
//根据\n的位置,根据指定长度,再真正的读数据
if (bufp[i] == '\n')
{
//将数据从缓存区读走
ret = readn(sockfd, bufp, i + 1);
if (ret != i + 1)
exit(EXIT_FAILURE);
return ret;
}
}
//若数据长度nread > 缓冲区最大长度maxline 退出
if (nread > nleft)
exit(EXIT_FAILURE);
//若没有\n 说明消息还没有结束,不是完整的一条消息,就把这些数据也读到buf缓冲区中。
//依此循环,直到遇到\n,把整个一行数据,全部读完,放入buf中
//bufp记录了每次需追加的位置
nleft -= nread;
ret = readn(sockfd, bufp, nread);
if (ret != nread)
exit(EXIT_FAILURE);
bufp += nread; //bufp每次跳到追加的末尾
}
return -1;
}
int main(void)
{
int sock[100];//socketid存储数组
int i = 0;
for(i = 0;i<5;i++)
{
//定义socket
if ((sock[i] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
ERR_EXIT("socket");
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8001);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sock[i], (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
ERR_EXIT("connect");
struct sockaddr_in localaddr;
socklen_t addrlen = sizeof(localaddr);
//获取本地的地址 注意是已连接以后的套接字
if (getsockname(sock[i], (struct sockaddr*) &localaddr, &addrlen) < 0)
ERR_EXIT("getsockname");
printf("本机的ip=%s port=%d\n", inet_ntoa(localaddr.sin_addr),
ntohs(localaddr.sin_port));
}
char sendbuf[1024] = { 0 };
char recvbuf[1024] = { 0 };
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
{
//写数据(本身带有\n)所以不需要再单独加 \n
writen(sock[0], sendbuf, strlen(sendbuf));
//按行读数据
int ret = readline(sock[0], recvbuf, sizeof(recvbuf));
if (ret == -1)
ERR_EXIT("readline");
else if (ret == 0)
{
printf("client close\n");
break;
}
fputs(recvbuf, stdout);
memset(sendbuf, 0, sizeof(sendbuf));
memset(recvbuf, 0, sizeof(recvbuf));
}
close(sock[0]);
return 0;
}
服务器
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nread = read(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nread == 0)
return count - nleft;
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char *bufp = (char*) buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
} else if (nwritten == 0)
continue;
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
ssize_t recv_peek(int sockfd, void *buf, size_t len)
{
while (1)
{
int ret = recv(sockfd, buf, len, MSG_PEEK);
if (ret == -1 && errno == EINTR)
continue;
return ret;
}
}
//maxline 一行最大数
//先提前peek一下缓冲区,如果有数据从缓冲区的读数据,
//1、缓冲区数据中带\n
//2 缓存区中不带\n
ssize_t readline(int sockfd, void *buf, size_t maxline)
{
int ret;
int nread;
char *bufp = buf;
int nleft = maxline;
while (1)
{
//看一下缓冲区有没有数据,并不移除内核缓冲区数据
ret = recv_peek(sockfd, bufp, nleft);
if (ret < 0) //失败
return ret;
else if (ret == 0) //对方已关闭
return ret;
nread = ret;
int i;
for (i = 0; i < nread; i++)
{
if (bufp[i] == '\n') //若缓冲区有\n
{
ret = readn(sockfd, bufp, i + 1); //读走数据
if (ret != i + 1)
exit(EXIT_FAILURE);
return ret; //有\n就返回,并返回读走的数据
}
}
if (nread > nleft) //如果读到的数大于 一行最大数 异常处理
exit(EXIT_FAILURE);
nleft -= nread; 若缓冲区没有\n, 把剩余的数据读走
ret = readn(sockfd, bufp, nread);
if (ret != nread)
exit(EXIT_FAILURE);
bufp += nread; //bufp指针后移后,再接着偷看缓冲区数据recv_peek,直到遇到\n
}
return -1;
}
void do_service(int conn)
{
char recvbuf[1024];
while (1)
{
memset(recvbuf, 0, sizeof(recvbuf));
int ret = readline(conn, recvbuf, 1024);
if (ret == -1)
ERR_EXIT("readline");
if (ret == 0)
{
printf("client close\n");
break;
}
fputs(recvbuf, stdout);
//将接受的数据再发送
writen(conn, recvbuf, strlen(recvbuf));
}
}
void handle_sigid(int slg)
{
//wait(NULL);
//第三种方式操作
int mypid = 0;
while((mypid = waitpid(-1,NULL,WNOHANG))>0)
{
printf("子进程被消灭%d\n",mypid);
}
}
int main(void)
{
//1.僵尸进程的地种方式忽略信号
//signal(SIGCHLD,SIG_IGN);
//2.僵尸进程的第二种方式
signal(SIGCHLD,handle_sigid);
int listenfd; //监听socket的文件描述符
if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
/* if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)*/
ERR_EXIT("socket");
//TCP/IP 结构体
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8001);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");*/
/*inet_aton("127.0.0.1", &servaddr.sin_addr);*/
//设置地址复用
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
ERR_EXIT("setsockopt");
//绑定
if (bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
ERR_EXIT("bind");
//监听
if (listen(listenfd, SOMAXCONN) < 0)
ERR_EXIT("listen");
//定义 socket信息结构体,在新的链接中会返回新的已经链接的socket的信息。
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
int conn;
pid_t pid;
while (1)
{
if ((conn = accept(listenfd, (struct sockaddr*) &peeraddr, &peerlen))
< 0)
ERR_EXIT("accept");
printf("ip=%s port=%d\n", inet_ntoa(peeraddr.sin_addr),
ntohs(peeraddr.sin_port));
pid = fork();
if (pid == -1)
ERR_EXIT("fork");
if (pid == 0)
{
//关闭监听socket
close(listenfd);
do_service(conn);
exit(EXIT_SUCCESS);
} else{
//关闭链接socket
close(conn);
}
}
return 0;
}
由于本人经验和经历有限如果有错误的地方还希望大家提出来一起学习探讨。