-
Notifications
You must be signed in to change notification settings - Fork 28
/
null_server_thread.cpp
167 lines (142 loc) · 3.82 KB
/
null_server_thread.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <pthread.h>
#define DEFAULT_PORT (5000)
using namespace std;
static int listener; //fd
static volatile unsigned long process_count;
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
static int setup_server_socket(int port, bool block=false)
{
int sock;
struct sockaddr_in sin;
int yes=1;
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(-1);
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
memset(&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(port);
if (bind(sock, (struct sockaddr *) &sin, sizeof sin) < 0) {
close(sock);
perror("bind");
exit(-1);
}
if (listen(sock, 5) < 0) {
close(sock);
perror("listen");
exit(-1);
}
return sock;
}
void* worker(void *data)
{
char *buf = (char*)malloc(128);
if (!buf) {
fprintf(stderr, "worker: no memory.\n");
return NULL;
}
pthread_mutex_lock(&g_mutex);
pthread_mutex_unlock(&g_mutex);
for (;;) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof client_addr;
int client = accept(listener, (struct sockaddr*)&client_addr, &client_addr_len);
if (client < 0) {
perror("accept");
continue;
}
__sync_fetch_and_add(&process_count, 1);
close(client);
//for (;;) {
// int n = ::read(client, buf, 128);
// if (n > 0) {
// int m = 0;
// while (m < n) {
// int o = ::write(client, buf+m, n-m);
// if (o < 0) {
// perror("write");
// close(client);
// goto restart;
// }
// m += o;
// }
// __sync_fetch_and_add(&process_count, 1);
// continue;
// }
// if (n < 0) {
// perror("read");
// }
// close(client);
// goto restart;
//}
}
return NULL;
}
void *busy_loop(void* notused)
{
int a=1,b=1,c;
for (;;) {
c = a+b;
a = b;
b = c;
}
return NULL;
}
int main(int argc, char *argv[])
{
int opt, port=DEFAULT_PORT;
int num_thread=1;
int num_busy=0;
while (-1 != (opt = getopt(argc, argv, "p:c:b:"))) {
switch (opt) {
case 'p':
port = atoi(optarg);
break;
case 'c':
num_thread = atoi(optarg);
break;
case 'b':
num_busy = atoi(optarg);
break;
default:
fprintf(stderr, "Unknown option: %c\n", opt);
return 1;
}
}
listener = setup_server_socket(port);
printf("Listening port (-p): %d\n", port);
printf("Worker thread (-c): %d\n", num_thread);
printf("Busy thread (-b): %d\n", num_busy);
pthread_mutex_lock(&g_mutex);
pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t) * num_thread);
for (int i = 0; i < num_thread; ++i) {
pthread_create(&threads[i], NULL, worker, NULL);
}
for (int i = 0; i < num_busy; ++i) {
pthread_t t;
pthread_create(&t, NULL, busy_loop, NULL);
}
puts("Start!!");
pthread_mutex_unlock(&g_mutex);
unsigned long prev=0, count=0;
for (;;) {
sleep(1);
count = __sync_fetch_and_add(&process_count, 0);
printf("processed %lu requests.\n", count-prev);
prev = count;
}
return 0;
}