Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
remzi-arpacidusseau
GitHub Repository: remzi-arpacidusseau/ostep-projects
Path: blob/master/concurrency-webserver/src/spin.c
910 views
1
#include <assert.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include <sys/time.h>
6
#include <unistd.h>
7
8
#define MAXBUF (8192)
9
10
//
11
// This program is intended to help you test your web server.
12
// You can use it to test that you are correctly having multiple threads
13
// handling http requests.
14
//
15
16
double get_seconds() {
17
struct timeval t;
18
int rc = gettimeofday(&t, NULL);
19
assert(rc == 0);
20
return (double) ((double)t.tv_sec + (double)t.tv_usec / 1e6);
21
}
22
23
24
int main(int argc, char *argv[]) {
25
// Extract arguments
26
double spin_for = 0.0;
27
char *buf;
28
if ((buf = getenv("QUERY_STRING")) != NULL) {
29
// just expecting a single number
30
spin_for = (double) atoi(buf);
31
}
32
33
double t1 = get_seconds();
34
while ((get_seconds() - t1) < spin_for)
35
sleep(1);
36
double t2 = get_seconds();
37
38
/* Make the response body */
39
char content[MAXBUF];
40
sprintf(content, "<p>Welcome to the CGI program (%s)</p>\r\n", buf);
41
sprintf(content, "%s<p>My only purpose is to waste time on the server!</p>\r\n", content);
42
sprintf(content, "%s<p>I spun for %.2f seconds</p>\r\n", content, t2 - t1);
43
44
/* Generate the HTTP response */
45
printf("Content-Length: %lu\r\n", strlen(content));
46
printf("Content-Type: text/html\r\n\r\n");
47
printf("%s", content);
48
fflush(stdout);
49
50
exit(0);
51
}
52
53
54