operating systems recitation 9, may 19-20, 2002. iterative server handle one connection request at a...
TRANSCRIPT
![Page 1: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/1.jpg)
Operating Systems
Recitation 9, May 19-20, 2002
![Page 2: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/2.jpg)
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
![Page 3: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/3.jpg)
Concurrent server
• Handle multiple connection requests at a time.
masterserver applicationthread
operating system socket forconnection requests
Q
sockets forindividual
connections
slave slaveslave
![Page 4: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/4.jpg)
Using socket calls
client sidesocket
connectsendrecv
closesocket
server side
socket
bind
listen
accept
recv
send
closesocket
![Page 5: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/5.jpg)
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);
![Page 6: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/6.jpg)
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.
![Page 7: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/7.jpg)
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);
![Page 8: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/8.jpg)
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.
![Page 9: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/9.jpg)
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
![Page 10: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/10.jpg)
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);
![Page 11: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/11.jpg)
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.
![Page 12: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/12.jpg)
Exercise description
socket
bind
listen
loop
accept
fork and child handles connection
child forks & it’s child executes program
![Page 13: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/13.jpg)
Exercise description: server argument
• Initial (main) program argument:
port number that server binds to (above 1024 otherwise permission denied).
![Page 14: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/14.jpg)
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.
![Page 15: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/15.jpg)
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
![Page 16: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/16.jpg)
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].
![Page 17: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/17.jpg)
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]
![Page 18: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/18.jpg)
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
![Page 19: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/19.jpg)
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.
![Page 20: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/20.jpg)
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
![Page 21: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/21.jpg)
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.
![Page 22: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/22.jpg)
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.
![Page 23: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/23.jpg)
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
![Page 24: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/24.jpg)
Exercise notes
• You can use the functions recv/send or read/write (the latter receiving three arguments instead of four).
![Page 25: Operating Systems Recitation 9, May 19-20, 2002. Iterative server Handle one connection request at a time. Connection requests stored in queue associated](https://reader033.vdocuments.us/reader033/viewer/2022051516/56649eaa5503460f94baf877/html5/thumbnails/25.jpg)
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.