[英]Multiprocess server UDP socket goes slower than single process?
我編寫了2個服務器程序來讀取多個客戶端消息,並使用套接字通過UDP發送回去; 這兩個服務器程序不同於對接收和發送消息的管理。 第一個簡單地接收消息並將其發送回去,第二個簡單地接收消息並創建另一個過程以將消息發送回。 客戶端向服務器發送了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()。 我懷疑這是您想要的,因為您沒有在子級中調用exit,並且沒有在父級中獲得子級,即您可能會在使用的計算機上用完文件描述符。
如果您想在收到的每條消息中退出子進程,則需要等待該子進程,否則您將有很多僵屍進程使用此子進程
int status;
waitpid( -1, &status, WNOHANG);
在父級中,這樣可以收獲子級,並且您不會耗盡資源。 您還應該檢查以確保fork確實起作用,以查看是否耗盡了資源,即if(pid <0)是否有錯誤。
如果您修復了代碼,但仍然很慢,並且知道代碼正確,那么我將研究流程創建時間。 您可以將文件描述符傳遞給子進程,這是許多HTTP服務器所做的,即Apache等,這避免了進程創建的開銷。 注意。 流程創建非常便宜,使用好的基准測試工具可能會告訴您確切的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.