-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTCPServer_FileLockForkPool.c
232 lines (199 loc) · 4.73 KB
/
TCPServer_FileLockForkPool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>
#include "TimeTool.h"
#include <errno.h>
#include "AddrTool.h"
#include <fcntl.h>
#define MAXN (2 << 20)
static struct flock wlock, relock;
static int lockfd;
static char response[MAXN];
typedef void(*sig_handler)(int);
ssize_t writen(int fd, char *buf, ssize_t n)
{
ssize_t nleft = n;
ssize_t nwrite;
char *ptr = buf;
while (nleft > 0) {
if ((nwrite = write(fd, ptr, nleft)) <= 0) {
if (errno != EAGAIN && errno != EINTR)
break;
nwrite = 0;
}
ptr += nwrite;
nleft -= nwrite;
}
return n - nleft;
}
sig_handler fuck(int signo, sig_handler newhandler)
{
struct sigaction newact, oldact;
bzero(&newact, sizeof(newact));
newact.sa_handler = newhandler;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
newact.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
newact.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signo, &newact, &oldact) < 0)
return SIG_ERR;
return oldact.sa_handler;
}
void lock_init(char *pathname)
{
lockfd = open(pathname, O_RDWR);
if (lockfd < 0) {
perror("open error!");
exit(1);
}
wlock.l_type = F_WRLCK;
wlock.l_whence = SEEK_SET;
wlock.l_start = 0;
wlock.l_len = 0;
relock.l_type = F_UNLCK;
relock.l_whence = SEEK_SET;
relock.l_start = 0;
relock.l_len = 0;
}
void lock(void)
{
while (fcntl(lockfd, F_SETLKW, &wlock) < 0) {
if (errno == EINTR)
continue;
perror("fcntl error!");
exit(1);
}
}
void unlock(void)
{
if (fcntl(lockfd, F_SETLKW, &relock) < 0) {
perror("fcntl error!");
exit(1);
}
}
void sig_int(int signo)
{
cpu_time();
}
void sig_child(int signo)
{
pid_t pid;
int status;
while ((pid = wait(&status)) > 0)
printf("collect %d\n", pid);
if (errno != 0 && errno != ECHILD) {
perror("waitpid error!");
exit(1);
}
}
void handle_req(int connfd)
{
char buf[1024];
ssize_t nread, nwrite;
char *info = getPeerInfo(connfd);
ssize_t nbytes = 0;
for (;;) {
if ((nread = read(connfd, buf, sizeof(buf))) < 0) {
if (errno == EINTR)
nread = 0;
perror("read error!");
break;
} else if (nread == 0)
break;
nbytes = atoi(buf);
if (writen(connfd, response, nbytes) != nbytes) {
perror("writen error!");
break;
}
printf("send %zd bytes to client %s\n", nbytes, info);
}
free(info);
}
void child_main(int i, int fd)
{
char buf[1024];
ssize_t nread, nwrite;
int connfd = -1;
for (;;) {
lock();
if ((connfd = accept(fd, NULL, NULL)) < 0) {
if (errno == EINTR)
continue;
perror("accpet error!");
exit(1);
}
unlock();
char *info = getPeerInfo(connfd);
printf("child %d - %d accept new connection from %s\n", i, getpid(), info);
handle_req(connfd);
printf("child %d - %d cut connection to %s\n", i, getpid(), info);
free(info);
close(connfd);
}
exit(0);
}
int child_make(int i, int fd)
{
pid_t pid;
if ((pid = fork()) < 0) {
perror("fork error!");
exit(1);
} else if (pid > 0)
return pid;
child_main(i, fd);
return pid;
}
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("usage : %s <port> <nchild>\n", argv[0]);
exit(1);
}
int listenfd = -1;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error!");
exit(1);
}
struct sockaddr_in svraddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(atoi(argv[1]));
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (fuck(SIGINT, sig_int) == SIG_ERR) {
perror("fuck error!");
exit(1);
}
if (fuck(SIGCHLD, sig_child) == SIG_ERR) {
perror("fuck error!");
exit(1);
}
lock_init("haha");
if (bind(listenfd, (struct sockaddr *)&svraddr, sizeof(svraddr)) < 0) {
perror("bind error!");
exit(1);
}
if (listen(listenfd ,100000) < 0){
perror("listen error!");
exit(1);
}
int nchild = atoi(argv[2]);
pid_t *childs = calloc(nchild, sizeof(pid_t));
if (childs == NULL) {
printf("calloc error!");
exit(1);
}
for (int i = 0; i < nchild; i++)
childs[i] = child_make(i, listenfd);
for (;;)
pause();
}