Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a...
-
Upload
robert-dale-stokes -
Category
Documents
-
view
215 -
download
0
Transcript of Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a...
Operating Systems
Recitation 9, May 19-20, 2002
Iterative server
• Handle one connection request at a time.
• Connection requests stored in queue associated with socket (avoid loosing them)
serverserver applicationthread
operating system socket forconnection requests
Q
socket forindividual
connection
Concurrent server
• Handle multiple connection requests at a time.
masterserver applicationthread
operating system socket forconnection requests
Q
sockets forindividual
connections
slave slaveslave
Using socket calls
client sidesocket
connectsendrecv
closesocket
server side
socket
bind
listen
accept
recv
send
closesocket
bind
• Specifies the local address for a socket; well-known port at which server will wait for connections.
int bind(int socket,
struct sockaddr* local_addr, int local_addr_len);
listen
• OS creates queue of connection requests: hold new connection requests that arrive while server is busy handling a previous request; separate queue (initially empty) for each socket.
• Inserts connection requests arriving from clients.
• When server asks to retrieve an incoming connection request from socket, the OS returns the next request from queue.
• If queue is full when connection request arrives then it’s rejected.
listen
• Server calls listen to place a socket in passive mode and make it ready to accept incoming connections. Tells the OS to enqueue connection requests for a socket
• Input:– socket descriptor– size of the queue
int listen(int socket, int queue_size);
accept
• Extracts the next incoming connection request.
• If connection request queue is not empty, accept returns immediately, otherwise system blocks the server until a client forms a connection.
• Before accept, a request ties up the socket that is bound to well-known port. Accept re-routes it to a new socket allowing original socket to receive additional clients.
accept
• All the different sockets created by accept share the same port on the server side.
client A sd=3
IP
client B sd=3
IP
host A
host B
port 1234
port 5678
serverprocess
sd=4
IP
well-known port
sd=5
sd=3
src=1234@A
src=5678@B
src=other
accept• Input:
– descriptor of a socket (that server has created and bound to a specific port with wildcard foreign destination) from which a connection should be accepted.
• Output (when connection request arrives):– address structure of client that formed the connection
and its length.– descriptor of new socket.
• Server uses each new socket to transfer data for each new connection, and original socket to accept additional connection requests.
int accept(int socket, struct sockaddr* client_addr, int* client_addr_len);
Exercise description
• Concurrent server, implemented by forking a child process to handle each connection request.
• HTTP server providing dynamic content: client requests that server run a program; server runs program and returns output to client.
Exercise description
socket
bind
listen
loop
accept
fork and child handles connection
child forks & it’s child executes program
Exercise description: server argument
• Initial (main) program argument:
port number that server binds to (above 1024 otherwise permission denied).
Exercise description: server’s reply
• Server runs a program as requested by a client and sends back:– status– output length– output type– output
• Sending the output length before the output itself requires the server to use a buffer to store the output.
Exercise description: server’s reply
• Format:HTTP/1.0 200 OKContent-Length: 29Content-Type: text/plaindata
• Status: 200 OK, if program run is successful; 300 ERROR, if program doesn’t exist or execv fails.
• Actual length of program output.• Each line ends with \r\n
Pipe
• Half-duplex• Used only between processes that have a
common ancestor.#include <unistd.h>int pipe(int filedes[2]);• Returns 0 if OK, -1 on error• filedes[0] is open for reading, filedes[1] for
writing: output of filedes[1] is the input of filedes[0].
Using a pipe between parent and child
• Pipe created by a process.
• Process calls fork.
• Pipe is used between parent and child in one direction.
parent
fd[0] fd[1]
pipekernel
childfork
fd[0] fd[1]
Exercise description: reading from standard output using a pipe & dup2
childpipefork
parent childclose(pipefd[1]) close(pipefd[0])
read(pipefd[0],buf,size) dup2(pipefd[1],1)execv standard
output
Duplicating an existing file descriptor
#include <unistd.h>
int dup(int filedes);
int dup2(int filedes, int filedes2);
• Returns new file descriptor if OK, -1 on error.
Exercise description
• Server replies only to GET requests from clients, receiving an entire GET request from client (message ends with \r\n\r\n)
• Parses client requests in the format:GET http://nova.math.tau.ac.il:2000/date+-u HTTP/1.0\r\n
• The server prints on screen all the HTTP requests it receives, from which you can tell the format of the browser request.
programto run
program argumentsdelimited with +
portmachine running the server
Exercise description: server tests
• Using a commercial web browser (IE), check your server, printout the results of two requests:
http://nova.math.tau.ac.il:2000/ls+-l+-a
and verify that the servers directory appears in the browser.
http://nova.math.tau.ac.il:2000/date+-u
and verify that you receive an updated date each time you refresh the browser.
• Copy the programs you run, such as date and ls, from /bin to the servers directory.
• Use the first four digits (after 0) of your ID number as the port.
Exercise description
You can incrementally implement the serverin several steps, testing each one:1. Iteratively handle each connection
request, ignoring the URL and sending a constant message to the client, with the proper header.
2. Concurrency handle each client connection request by a new process.
3. Parse the URL and execute the requested program, replying to the client.
Exercise submission
• Chapter 7.8 in Toledo’s book, pages 135-139.• Submission: Monday, June 17th.• Software
Directory: ~username/os02b/ex-serveFiles: ex-serve.cPermissions: chmod ugo+rx (to above)
• Hardcopyname, ID, login, CIDex-serve.cprintout result of the two requests from web browser.submit in 281, Nir Neumark, [email protected]
• Environment: Unix, Linux
Exercise notes
• You can use the functions recv/send or read/write (the latter receiving three arguments instead of four).
References
• Operating Systems, Sivan Toledo, Akademon, 2001.
• Internetworking with TCP/IP, Vol 3: client-server programming and applications, Douglas Comer & David Stevens, Prentice-Hall, 1997.
• Advanced programming in the Unix environment, Richard Stevens, Addison-Wesley, 1993.
• Example program on course webpage.