多进程服务器 UDP 套接字比单进程慢?
Multiprocess server UDP socket goes slower than single process?
我已经编写了 2 个服务器程序来读取多个客户端消息并通过 UDP 使用套接字将它们发回; 2个服务器程序不同于接收和发送回消息的管理;第一个简单地获取消息并将其发回,第二个接收消息并创建另一个进程来发回消息。客户端向服务器发送了 10000 条消息,我注意到第一个重新发送的消息比第二个多很多,比如 100:1;
这是只有一个进程的服务器的代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 3000
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN], buf_to_send_back[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
while(1)
{
//receive message
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
{
diep("recvfrom()");
} else
{
//create buffer to send it back
sprintf(buf_to_send_back, "Hi %s:%d,\nyour packet has this content: %s\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
//send a message back
if (sendto(s, buf_to_send_back, BUFLEN, 0, &si_other, slen)==-1)
{
diep("sendto()");
} else
{
printf("Response sent\n");
}
}
}
close(s);
return 0;
}
这是我在第二个服务器中使用的代码,一个有 2 个进程,一个用于接收,一个用于发回:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdbool.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 3000
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i = 0, slen = sizeof(si_other);
char buf[BUFLEN], buf_to_send_back[BUFLEN];
pid_t pid;
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
while(1)
{
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)!=-1)
{
pid = fork();
if (pid == 0)
{
//create buffer to send it back
sprintf(buf_to_send_back, "Hi %s:%d,\nyour packet has this content: %s\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
//send a message back
if (sendto(s, buf_to_send_back, BUFLEN, 0, &si_other, slen)==-1)
{
diep("sendto()");
} else
{
printf("Response sent\n");
}
} else if(pid > 0)
{
printf("a message arrived\n");
}
}
}
close(s);
return 0;
}
我用的客户端程序是这样的:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 3000
#define SERVER_IP "127.0.0.1"
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SERVER_IP, &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
while(1)
{
printf("\nInsert a message to send:\n");
scanf("%s", buf);
for(i = 0; i < 10000; i++)
{
if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
{
diep("sendto()");
} else
{
printf("Message sent:\n %s\n", buf);
}
}
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
{
diep("recvfrom()");
} else {
printf("A message has been received:\n %s\n", buf);
}
}
return 0;
}
在此先感谢您的帮助!
有几处错误。您已经在无限循环中添加了 fork() 。我怀疑这是你想要的,因为你没有在 child 中调用 exit 并且你没有在 parent 中收获 children 即你可能会去 运行 超出您正在使用的机器上的文件描述符。
如果您想在收到每条消息时退出 child,您需要在 child 上等待,否则会有很多僵尸进程使用此
int status;
waitpid( -1, &status, WNOHANG);
在 parent 中,因此它会收获 child 而你不会 运行 资源不足。您还应该检查以确保 fork 确实工作以查看您是否 运行 资源不足,即如果 if(pid < 0) 出现错误。
如果您修复了代码但它仍然很慢并且您知道代码是正确的那么我会查看进程创建时间。您可以将文件描述符传递给 child 进程,这是许多 HTTP 服务器(即 Apache 等)所做的,这避免了进程创建开销。笔记。流程创建非常便宜,使用一个好的基准测试工具可能会告诉您时间的确切位置。
我已经编写了 2 个服务器程序来读取多个客户端消息并通过 UDP 使用套接字将它们发回; 2个服务器程序不同于接收和发送回消息的管理;第一个简单地获取消息并将其发回,第二个接收消息并创建另一个进程来发回消息。客户端向服务器发送了 10000 条消息,我注意到第一个重新发送的消息比第二个多很多,比如 100:1;
这是只有一个进程的服务器的代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 3000
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN], buf_to_send_back[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
while(1)
{
//receive message
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
{
diep("recvfrom()");
} else
{
//create buffer to send it back
sprintf(buf_to_send_back, "Hi %s:%d,\nyour packet has this content: %s\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
//send a message back
if (sendto(s, buf_to_send_back, BUFLEN, 0, &si_other, slen)==-1)
{
diep("sendto()");
} else
{
printf("Response sent\n");
}
}
}
close(s);
return 0;
}
这是我在第二个服务器中使用的代码,一个有 2 个进程,一个用于接收,一个用于发回:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdbool.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 3000
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i = 0, slen = sizeof(si_other);
char buf[BUFLEN], buf_to_send_back[BUFLEN];
pid_t pid;
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
while(1)
{
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)!=-1)
{
pid = fork();
if (pid == 0)
{
//create buffer to send it back
sprintf(buf_to_send_back, "Hi %s:%d,\nyour packet has this content: %s\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
//send a message back
if (sendto(s, buf_to_send_back, BUFLEN, 0, &si_other, slen)==-1)
{
diep("sendto()");
} else
{
printf("Response sent\n");
}
} else if(pid > 0)
{
printf("a message arrived\n");
}
}
}
close(s);
return 0;
}
我用的客户端程序是这样的:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 3000
#define SERVER_IP "127.0.0.1"
void diep(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SERVER_IP, &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
while(1)
{
printf("\nInsert a message to send:\n");
scanf("%s", buf);
for(i = 0; i < 10000; i++)
{
if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
{
diep("sendto()");
} else
{
printf("Message sent:\n %s\n", buf);
}
}
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
{
diep("recvfrom()");
} else {
printf("A message has been received:\n %s\n", buf);
}
}
return 0;
}
在此先感谢您的帮助!
有几处错误。您已经在无限循环中添加了 fork() 。我怀疑这是你想要的,因为你没有在 child 中调用 exit 并且你没有在 parent 中收获 children 即你可能会去 运行 超出您正在使用的机器上的文件描述符。
如果您想在收到每条消息时退出 child,您需要在 child 上等待,否则会有很多僵尸进程使用此
int status;
waitpid( -1, &status, WNOHANG);
在 parent 中,因此它会收获 child 而你不会 运行 资源不足。您还应该检查以确保 fork 确实工作以查看您是否 运行 资源不足,即如果 if(pid < 0) 出现错误。
如果您修复了代码但它仍然很慢并且您知道代码是正确的那么我会查看进程创建时间。您可以将文件描述符传递给 child 进程,这是许多 HTTP 服务器(即 Apache 等)所做的,这避免了进程创建开销。笔记。流程创建非常便宜,使用一个好的基准测试工具可能会告诉您时间的确切位置。