forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb02.c
184 lines (156 loc) · 4.54 KB
/
web02.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
/* Doesn't work right. Main thread sucks up all the CPU time polling unless
we call thr_yield(). */
#include "unpthread.h"
#include <thread.h> /* Solaris threads */
#define MAXFILES 20
#define SERV "80" /* port number or service name */
struct file {
char *f_name; /* filename */
char *f_host; /* hostname or IP address */
int f_fd; /* descriptor */
int f_flags; /* F_xxx below */
pthread_t f_tid; /* thread ID */
} file[MAXFILES];
#define F_CONNECTING 1 /* connect() in progress */
#define F_READING 2 /* connect() complete; now reading */
#define F_DONE 4 /* all done */
#define F_JOINED 8 /* main has pthread_join'ed */
int nconn, nfiles, nlefttoconn, nlefttoread;
char get[] = "GET / HTTP/1.0\r\n\r\n"; /* for home page */
int ndone; /* number of terminated threads & mutex */
pthread_mutex_t ndone_mutex = PTHREAD_MUTEX_INITIALIZER;
void *do_get_read(void *);
void home_page(const char *, const char *);
void write_get_cmd(struct file *);
int
main(int argc, char **argv)
{
int i, n, maxnconn;
pthread_t tid;
struct file *fptr;
if (argc < 5)
err_quit("usage: web <#conns> <IPaddr> <homepage> file1 ...");
maxnconn = atoi(argv[1]);
nfiles = min(argc - 4, MAXFILES);
for (i = 0; i < nfiles; i++) {
file[i].f_name = argv[i + 4];
file[i].f_host = argv[2];
file[i].f_flags = 0;
}
printf("nfiles = %d\n", nfiles);
home_page(argv[2], argv[3]);
nlefttoread = nlefttoconn = nfiles;
nconn = 0;
while (nlefttoread > 0) {
/* printf("nconn = %d, nlefttoconn = %d\n", nconn, nlefttoconn); */
while (nconn < maxnconn && nlefttoconn > 0) {
/* find a file to read */
for (i = 0 ; i < nfiles; i++)
if (file[i].f_flags == 0)
break;
if (i == nfiles)
err_quit("nlefttoconn = %d but nothing found", nlefttoconn);
if ( (n = pthread_create(&tid, NULL, &do_get_read, &file[i])) != 0)
errno = n, err_sys("pthread_create error");
printf("created thread %d\n", tid);
file[i].f_tid = tid;
file[i].f_flags = F_CONNECTING;
nconn++;
nlefttoconn--;
}
thr_yield();
/* See if one of the threads is done */
if ( (n = pthread_mutex_lock(&ndone_mutex)) != 0)
errno = n, err_sys("pthread_mutex_lock error");
if (ndone > 0) {
for (i = 0; i < nfiles; i++) {
if (file[i].f_flags & F_DONE) {
if ( (n = pthread_join(file[i].f_tid, (void **) &fptr)) != 0)
errno = n, err_sys("pthread_join error");
if (&file[i] != fptr)
err_quit("file[i] != fptr");
fptr->f_flags = F_JOINED; /* clears F_DONE */
ndone--;
nconn--;
nlefttoread--;
printf("thread id %d for %s done\n",
file[i].f_tid, fptr->f_name);
}
}
}
if ( (n = pthread_mutex_unlock(&ndone_mutex)) != 0)
errno = n, err_sys("pthread_mutex_unlock error");
}
exit(0);
}
void *
do_get_read(void *vptr)
{
int fd, n;
char line[MAXLINE];
struct file *fptr;
fptr = (struct file *) vptr;
fd = Tcp_connect(fptr->f_host, SERV);
fptr->f_fd = fd;
printf("do_get_read for %s, fd %d, thread %d\n",
fptr->f_name, fd, fptr->f_tid);
write_get_cmd(fptr); /* write() the GET command */
/* Read server's reply */
for ( ; ; ) {
if ( (n = read(fd, line, MAXLINE)) <= 0) {
if (n == 0)
break; /* server closed connection */
else
err_sys("read error");
}
printf("read %d bytes from %s\n", n, fptr->f_name);
}
printf("end-of-file on %s\n", fptr->f_name);
close(fd);
fptr->f_flags = F_DONE; /* clears F_READING */
if ( (n = pthread_mutex_lock(&ndone_mutex)) != 0)
errno = n, err_sys("pthread_mutex_lock error");
ndone++;
if ( (n = pthread_mutex_unlock(&ndone_mutex)) != 0)
errno = n, err_sys("pthread_mutex_unlock error");
return(fptr); /* terminate thread */
}
void
write_get_cmd(struct file *fptr)
{
int n;
char line[MAXLINE];
strcpy(line, "GET ");
strcat(line, fptr->f_name);
strcat(line, " HTTP/1.0\r\n\r\n");
n = strlen(line);
if (writen(fptr->f_fd, line, n) != n)
err_sys("writen error");
printf("wrote %d bytes for %s\n", n, fptr->f_name);
fptr->f_flags = F_READING; /* clears F_CONNECTING */
}
void
home_page(const char *host, const char *fname)
{
int fd, n;
char line[MAXLINE];
fd = Tcp_connect(host, SERV);
strcpy(line, "GET ");
strcat(line, fname);
strcat(line, " HTTP/1.0\r\n\r\n");
n = strlen(line);
if (writen(fd, line, n) != n)
err_sys("writen error");
for ( ; ; ) {
if ( (n = read(fd, line, MAXLINE)) <= 0) {
if (n == 0)
break; /* server closed connection */
else
err_sys("read error");
}
printf("read %d bytes of home page\n", n);
/* do whatever with data */
}
printf("end-of-file on home page\n");
close(fd);
}