使用setrlimit修改进程打开的句柄数上限

2022-08-19 08:49:52 浏览数 (1)

#include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <sys/time.h> #include <sys/resource.h>

int main(void) { int i = 0; int socket_descriptor = -1; rlimit ResLimit;

ResLimit.rlim_cur = SHRT_MAX; ResLimit.rlim_max = SHRT_MAX;

setrlimit(RLIMIT_NOFILE, &ResLimit);

while (true) { socket_descriptor = socket(AF_INET, SOCK_STREAM, 0); if (socket_descriptor == -1) break;

i; } printf("%dn", i);

return 0; }

0 人点赞