Operating Systems
Download
Report
Transcript Operating Systems
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)
server application
thread
operating system
server
socket for
connection requests
Q
socket for
individual
connection
Concurrent server
• Handle multiple connection requests at a
time.
server application
thread
master
slave slave slave
operating system
socket for
connection requests
Q
sockets for
individual
connections
Using socket calls
client side
socket
connect
send
recv
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 reroutes 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.
host A
port 1234
client A
sd=3
sd=3
src=other
well-known
port
IP
src=1234@A
host B
sd=4 server
process
src=5678@B
client B
sd=3
sd=5
port 5678
IP
IP
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 OK
Content-Length: 29
Content-Type: text/plain
data
• 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]
fork
fd[1]
child
fd[0]
pipe
kernel
fd[1]
Exercise description: reading from
standard output using a pipe & dup2
child
pipe
fork
parent
close(pipefd[1])
read(pipefd[0],buf,size)
child
close(pipefd[0])
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
machine running
the server
port program program
to run arguments
delimited
with +
• The server prints on screen all the HTTP
requests it receives, from which you can
tell the format of the browser request.
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 server
in 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-serve
Files: ex-serve.c
Permissions: chmod ugo+rx (to above)
• Hardcopy
name, ID, login, CID
ex-serve.c
printout 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: clientserver programming and applications,
Douglas Comer & David Stevens,
Prentice-Hall, 1997.
• Advanced programming in the Unix
environment, Richard Stevens, AddisonWesley, 1993.
• Example program on course webpage.