

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































nd
Client /
Server
Session
socket socket
bind
listen
rio_readlineb
rio_readlineb rio_writen
rio_writen
Connec9on
request
rio_readlineb
close
close
EOF
Await connec9on
request from
next client
open_listenfd
open_clientfd
connect accept
connect
accept
connect
write
read
call read
close
accept
write
read
close
Wait for Client 1
call read
write
ret read
write ret read
Concurrent servers use mul0ple concurrent flows to serve mul0ple
User goes
out to lunch
Client 1 blocks
waiting for user
to type in data
Client 2 blocks
waiting to read from server
Server blocks
waiting for
data from
Client 1
connect
accept
connect
write
read
call read
write
call read
write ret read
Allow server to handle mul0ple clients simultaneously
Kernel automa0cally interleaves mul0ple logical flows
Each flow has its own private address space
Kernel automa0cally interleaves mul0ple logical flows
Each flow shares the same address space
Programmer manually interleaves mul0ple logical flows
All flows share the same address space
Relies on lower-‐level system abstrac0ons
int main(int argc, char **argv)
int listenfd, connfd;
int port = atoi(argv[1]);
struct sockaddr_in clientaddr;
int clientlen = sizeof(clientaddr);
listenfd = Open_listenfd(port);
while (1) {
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
echo(connfd);
Close(connfd);
exit(0);
Accept a connec0on request
Handle echo requests un0l client terminates
int main(int argc, char **argv)
{
int listenfd, connfd;
int port = atoi(argv[1]);
struct sockaddr_in clientaddr;
int clientlen=sizeof(clientaddr);
Signal(SIGCHLD, sigchld_handler);
listenfd = Open_listenfd(port);
while (1) {
connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);
if (Fork() == 0) {
Close(listenfd); /* Child closes its listening socket */
echo(connfd); /* Child services client */
Close(connfd); /* Child closes connection with client */
exit(0); /* Child exits */
}
Close(connfd); /* Parent closes connected socket (important!) */
}
}
Each client handled by independent process
No shared state between them
Both parent & child have copies of listenfd and connfd
Client 1
Server
Process
Client 2
Server
Process
Listening
Server
Process
listenfd(3)
Client
1. Server blocks in accept,
wai4ng for connec4on request
on listening descriptor
listenfd
clientfd
Server
listenfd(3)
Client
clientfd
Server
2. Client makes connec4on request by
calling and blocking in connect
Connec9on
request
listenfd(3)
Client
clientfd
Server
3. Server returns connfd from
accept. Forks child to handle client.
Client returns from connect.
Connec4on is now established between
clientfd and connfd
Server
Child
connfd(4)
cl1> ./echoclient greatwhite.ics.cs.cmu.edu 15213
srv> ./echoserverp 15213
srv> connected to (128.2.192.34), port 50437
cl2> ./echoclient greatwhite.ics.cs.cmu.edu 15213
srv> connected to (128.2.205.225), port 41656
Connec9on Host Port Host Port
Listening --- --- 128.2.220.10 15213
cl1 128.2.192.34 50437 128.2.220.10 15213
cl2 128.2.205.225 41656 128.2.220.10 15213
TCP manager maintains separate stream for each connec0on
Connec9on Host Port Host Port
Listening --- --- 128.2.220.10 15213
cl1 128.2.192.34 50437 128.2.220.10 15213
cl2 128.2.205.225 41656 128.2.220.10 15213
but, with threads instead of processes
shared libraries
run-time heap
0
read/write data
Program context:
Data registers
Condition codes
Stack pointer (SP)
Program counter (PC)
Kernel context:
VM structures
Descriptor table
brk pointer
Code, data, and stack
read-only code/data
stack
brk
Process context