【发布时间】:2016-12-15 14:18:40
【问题描述】:
我有以下使用 Pthreads 的 Linux 套接字服务器代码。
如果我开始对此进行压力测试:
for ((;;)); do echo STAT | nc 127.0.0.1 5555 ; done
内存增加到大约 270MB,然后服务器变得无响应:
271688 ./test
在 ARMv7 架构上,这需要更短的时间。如果我使用 for 循环停止请求,内存不会被释放,这意味着该服务器迟早会在生产中崩溃。
编译:
$ gcc -lpthread -o test test.c
下面的代码有什么问题?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
char *Match(const char *instr, const char *pattern) {
const char *p, *q;
for (; *instr; instr++) {
for (p = instr, q = pattern; *p && *q; p++, q++)
if (*p != *q) break;
if (p == q || *q == 0) return (char *)instr;
}
return NULL;
}
void *connection_handler2(void *socket_desc) {
int sock = *(int*)socket_desc;
int comm, i;
int read_size;
char *message, client_message[500], data[50];
char buf[256];
strcpy(data, "Test message back");
message = "Hello this is test Server\n";
write(sock, message, strlen(message));
while ((read_size = recv(sock, client_message, 500, 0)) > 0) {
if (Match(client_message, "STAT") != NULL) {
write(sock, data, strlen(data));
}
if (Match(client_message, "QUIT") != NULL) break;
}
if (read_size == 0) {
puts("Client disconnected");
fflush(stdout);
} else
if (read_size == -1) {
perror("recv failed");
}
close(sock);
//Free the socket pointer
free(socket_desc);
return 0;
}
void *s2_thread() {
int n, i;
int opt = 1;
int socket_desc, client_sock, c, *new_sock;
struct sockaddr_in server, client;
struct termios toptions;
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf(" Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(5555);
//bind the socket to the address
setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(int));
//Bind
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
//print the error message
perror("port 5000 bind failed");
return;
}
//Listen
listen(socket_desc, 3);
//Accept
c = sizeof(struct sockaddr_in);
//Endless main loop
while ((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c))) {
pthread_t t2;
new_sock = malloc(1);
*new_sock = client_sock;
if (pthread_create(&t2, NULL, connection_handler2, (void*)new_sock) < 0) {
return;
}
}
if (client_sock < 0) {
return;
}
return;
}
int main(int argc, char *argv[]) {
int n, i;
int opt = 1;
int socket_desc, client_sock, c, *new_sock;
struct sockaddr_in server, client;
pthread_t ss2t;
if (pthread_create(&ss2t, NULL, s2_thread, NULL) < 0) {
return 1;
}
puts("Telnet server started");
while (1) sleep(5);
return 0;
}
【问题讨论】:
-
我认为这通常不适用于 ARMv7。你的意思是ARMv7A?还是 ARMv7R?很可能不是 ARMv7M!为什么你认为这是架构的问题?应用程序级代码不太可能。回覆。代码:见How to Ask,提供minimal reproducible example。这不是调试服务。
-
new_sock = malloc(1);你不是说new_sock = malloc(sizeof(int)); -
在
Match中删除const限定符instr是一件非常 坏事!其他狂野演员也值得怀疑。为什么不让编译器帮助你?警告旨在帮助您,而不是添加另一个有问题的演员表。 -
主线程启动一个其他线程然后永远等待是什么意思?如果主线程无事可做,那么它可以并且应该直接完成它以前的子线程的工作。您甚至可以直接调用线程的 start 函数从这里到达那里。
标签: c linux sockets memory-leaks pthreads