20.11 Exercise: Stateless File Server

Team-FLY

This exercise describes the implementation of a simple stateless file server based on UDP. A stateless server is one for which client requests are completely self-contained and leave no residual state information on the server. Not all problems can be cast in stateless form, but there are some well-known examples of stateless servers. Sun NFS (Network File System) is implemented as a stateless client-server system based on unreliable remote procedure calls (RPCs).

Program 20.19 shows a putblock function that writes a block of data to a specified file. Although the normal write function assumes an open file descriptor and manipulates a file pointer, the putblock function is stateless. The stateless form of file access does not assume that a file descriptor has been previously opened and does not leave file descriptors open after servicing the request.

Exercise 20.17

An idempotent operation is an operation that can be performed multiple times with the same effect. Is the putblock operation of Program 20.19 idempotent?

Answer:

Although the contents of the file will not change if putblock is called multiple times with the same parameters, putblock is not strictly idempotent because the modification date changes.

Exercise 20.18

Write a getblock function that is similar to putblock . Is getblock idempotent?

Answer:

POSIX specifies that the struct stat structure have a time_t st_atime field giving the time that a file was last accessed. Thus, getblock is not strictly idempotent.

Program 20.19 putblock.c

Implementation of a stateless write to a file .

 #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> #include "restart.h" #define BLKSIZE 8192 #define PUTBLOCK_PERMS (S_IRUSR  S_IWUSR) int putblock(char *fname, int blknum, char *data) {   int error = 0;   int file;   if ((file = open(fname, O_WRONLYO_CREAT, PUTBLOCK_PERMS)) == -1)      return -1;   if (lseek(file, blknum*BLKSIZE, SEEK_SET) == -1)      error = errno;   else if (r_write(file, data, BLKSIZE) == -1)      error = errno;   if ((r_close(file) == -1) && !error)      error = errno;   if (!error)      return 0;   errno = error;   return -1; } 

20.11.1 Remote File Services

A simple remote file service can be built from the getblock of Exercise 20.18 and putblock of Program 20.19. A server running on the machine containing the file system listens for client requests. Clients can send a request to read or write a block from a file. The server executes getblock or putblock on their behalf and returns the results. The client software translates user requests for reading and writing a file into requests to read and write specific blocks and makes the requests to the server.

This is a simplification of the strategy pursued by remote file services such as NFS. Real systems have caching at both endsthe client and the server keep blocks for files that have been accessed recently in memory, to give better performance. File servers often bypass the file system table and use low-level device operations to read from and write to the disk. Of course, both sides must worry about authorization and credentials for making such requests.

A typical file service might provide the following services.

  1. Read a particular block from a specified remote file.

  2. Write a particular block to a specified remote file.

  3. Create or delete a new remote file.

  4. Create or delete a special remote file such as a directory.

  5. Get the struct stat equivalent for a specified remote file.

  6. Access or modify the permissions for a specified file.

Based on the file services that you might want to implement, devise a format for the client request and the server response. Discuss your strategy for handling errors and for dealing with network byte order.

Implement and test the portion of the remote file service for getting and putting single file blocks, using UDP with a request-reply- acknowledge for the client side. Discuss how you would implement client-side libraries that would allow reading and writing a stream of bytes based on these single-block functions.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net