本文中, 我们继续来做小实验, 看看linux单进程能打开的最大的句柄数(其实也是文件数, 因为linux下一切皆文件嘛), 命令如下:
xxxxxx:~/network> ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 28064
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 28064
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
xxxxxx:~/network>
xxxxxx:~/network>
xxxxxx:~/network>
xxxxxx:~/network> ulimit -n
1024
xxxxxx:~/network>
可以看到, 最大文件数是1024. 来写个实际程序测测:
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <fcntl.h>
int main()
{
int iSock = 0;
for(unsigned int i = 0; i < 1050; i++)
{
iSock = socket(AF_INET, SOCK_DGRAM, 0); // 模拟句柄泄漏
printf("iSock is %d\n", iSock);
}
printf("end\n");
return 0;
}
来, 运行看一下:
xxxxxx:~/network> ./a.out
iSock is 3
iSock is 4
iSock is 5
iSock is 6
iSock is 7
iSock is 8
iSock is 9
iSock is 10
iSock is 11
iSock is 12
iSock is 13
iSock is 14
iSock is 15
iSock is 16
iSock is 17
iSock is 18
iSock is 19
iSock is 20
iSock is 21
iSock is 22
iSock is 23
iSock is 24
iSock is 25
iSock is 26
iSock is 27
iSock is 28
iSock is 29
iSock is 30
iSock is 31
iSock is 32
iSock is 33
iSock is 34
iSock is 35
iSock is 36
iSock is 37
iSock is 38
iSock is 39
iSock is 40
iSock is 41
iSock is 42
iSock is 43
iSock is 44
iSock is 45
iSock is 46
iSock is 47
iSock is 48
iSock is 49
iSock is 50
iSock is 51
iSock is 52
iSock is 53
iSock is 54
iSock is 55
iSock is 56
iSock is 57
iSock is 58
iSock is 59
iSock is 60
iSock is 61
iSock is 62
iSock is 63
iSock is 64
iSock is 65
iSock is 66
iSock is 67
iSock is 68
iSock is 69
iSock is 70
iSock is 71
iSock is 72
iSock is 73
iSock is 74
iSock is 75
iSock is 76
iSock is 77
iSock is 78
iSock is 79
iSock is 80
iSock is 81
iSock is 82
iSock is 83
iSock is 84
iSock is 85
iSock is 86
iSock is 87
iSock is 88
iSock is 89
iSock is 90
iSock is 91
iSock is 92
iSock is 93
iSock is 94
iSock is 95
iSock is 96
iSock is 97
iSock is 98
iSock is 99
iSock is 100
iSock is 101
iSock is 102
iSock is 103
iSock is 104
iSock is 105
iSock is 106
iSock is 107
iSock is 108
iSock is 109
iSock is 110
iSock is 111
iSock is 112
iSock is 113
iSock is 114
iSock is 115
iSock is 116
iSock is 117
iSock is 118
iSock is 119
iSock is 120
iSock is 121
iSock is 122
iSock is 123
iSoc