Notes for September 23rd
Iterative server
Socket -> bind(ms..) -> listen(ms..) ->
while (1) { ss = accept(ms...); process req(ss); close(ss);
}
Concurrent server configurations:
Fork: create new process for every request
Newthread: create a new thread to process every
request
Pool of threads: have a pre-created pool of threads
to handle request
Fork:
while(1) {
ss = accept(ms, ...);
if (ss < 0 && errno ==
EINTR) {
continue; //
sometimes signals make accept return prematurely
}
int ret = fork();
if (ret == 0) {
// This is the
child process
process
request(ss); // put call to close(ss) inside process request so
we don't need to close it here
exit(0);
}
if (ret < 0) {
// error
perror("fork");
exit(1);
}
close(ss);
}
New Thread:
while(1) {
ss = accept(ms...);
ptrhreadcreate(0, processReq,
(void *)ss); // set processReq close ss
}
Thread Pool:
void processingThread(int ms) {
while(1) {
pthread_mutex_lock(&mutex);
int ss =
accept(ms, ...);
pthread_mutex_unlock(&mutex);
processRequest(ss);
}
}
int main () {
for (int i = 0; i < maxthr;
i++) {
pthread_iterate(0, processingThread((void *)ms);
}
processingThread(ms);
}
Pool of threads is faster than the other approaches since no new
process is created and no new thread is created
Fork implementation is more robust. A crash in process request
will only affect the child but not the parent, the parent continues
running.
Process Request (SS)
The http client sends
GET /file http/1.0 <crlf>
<client-info><crlf>
<cookies><crlf>
...
..
..
<....><crlf>
<crlf>
read the first line that contains the file requested.
keep reading until you reach <crlf><crlf> even if you do'nt
need that info, otherwise connection can be closed prematurely.
file contains the document requested
when using sockets
n = read(ss, buff, maxLength)
n can be less than maxLength since sockets will
return the bytes available so far
answer request:
use write(ss, buff, len)
content type: text/html <crlf> //
check website to double check doc/html. maybe text/html
<crlf> // empty line seperates
header from data
document data
close connection