我有一个用C语言编写的服务器,每当有新客户端连接时,服务器就会生成一个新线程。为了测试我的服务器,我编写了一个脚本来模拟500个客户端。服务器可以处理前几百个客户端,但最后Valgrind会报错:
Thread 456: status = VgTs_WaitSys
==4182== at 0x4E383EC: recv (recv.c:34)
==4182== by 0x4017F1: process_data (socket2.h:45)
==4182== by 0x40195E: thread (FBServer.c:82)
==4182== by 0x4E30A03: start_thread (pthread_create.c:300)
==4182== by 0x532DD4C: clone (clone.S:112)
Thread 457: status = VgTs_WaitSys
==4182== at 0x4E383EC: recv (recv.c:34)
==4182== by 0x4017F1: process_data (socket2.h:45)
==4182== by 0x40195E: thread (FBServer.c:82)
==4182== by 0x4E30A03: start_thread (pthread_create.c:300)
==4182== by 0x532DD4C: clone (clone.S:112)
...
Thread 499: status = VgTs_WaitSys
==4182== at 0x4E383EC: recv (recv.c:34)
==4182== by 0x4017F1: process_data (socket2.h:45)
==4182== by 0x40195E: thread (FBServer.c:82)
==4182== by 0x4E30A03: start_thread (pthread_create.c:300)
==4182== by 0x532DD4C: clone (clone.S:112)
在FBServer.c的第82行,线程调用一个名为process_data的函数,该函数接收来自客户端的数据。如下所示:
在FBServer.c的第82行,线程调用一个称为process_data的函数,该函数从客户端接收数据。process_data函数如下所示:
void process_data(int clientSock)
{
size_t n;
char jstring[MAX_LEN + 1];
int bytes_received_so_far = 0;
int bytes_count;
char *buf = NULL;
while(bytes_count = recv(clientSock, jstring, MAX_LEN, 0))
{
if(bytes_count <= 0)
{
close(clientSock);
pthread_exit(NULL);
}
printf("Bytes received = %d\n", bytes_count);
jstring[bytes_count] = '\0';
...
...
}
}
有人能帮我解释这些错误信息吗?