CS 3733 Operating Systems, Spring 2004 Assignment 3

Due Thursday, March 11, 2004, before class

In this assignment you will extend the programs you wrote in Assignment 1 to work with named pipes. In Part 8 you will write a client that will request a program (a file) from a server (written in Part 9). The server will send the client a program list. The client will choose a program from the list and the server will send that program. For Parts 8 and 9 you may want to use some of the routines you wrote for Assignment 1.

Part 0:
If you haven't already done so, finish Part 10 of Assignment 1.

Part 1:
Make a directory called assign3 under your cs3733 directory. Get a copy of the files restart.c and restart.h. These can be gotten from the USP programs web page for Appendix B.
You may want to use some of the programs in the restart library to simplify your code. To use these functions, your programs should include restart.h. You should not modify either restart.h or restart.c.

Create a makefile that uses separate compilation to compile your programs and link them to the restart library.

Part 2:
Write a function called read_int_as_line that takes one integer parameter, an open file descriptor and returns an integer. It reads one line from the file descriptor and tries to convert it to a non-negative integer. If successful it returns the integer value. Otherwise it returns -1 and sets errno. You may assume that it is an error if the line being read is longer than 60 bytes. Use the readline function from the restart library. To convert to an integer, skip over leading blanks. It is an error if the remaining characters up to the newline are not decimal digits. Do not worry about overflow.

Part 3:
Write a main program to completely test the function from Part 2.

Part 4:
Write a function called send_int_as_line that takes two parameters, an open file descriptor and an integer. It converts the second parameter to a line (an array ending with a newline) and sends it to the open file descriptor. It returns 0 on success and -1 (with errno set) on failure. Note that this is sending a line not a string. No null characters should be sent.

Part 5:
Write a main programs to completely test the function from Part 4 with the function in Part 2. Use a pipe to send and integer from one program to another.

Part 6:
Write a function send_num_bytes that takes three parameters, two open file descriptors and one integer. The function reads exactly the number of bytes given by the last parameter from the file descriptor given by the first parameter and writes them to the file descriptor given by the second parameter. It returns 0 on success and -1 (with errno set) on failure. This function should not assume that everything to be sent will fit in memory and it should do reads in small blocks of size at most 1024.

Part 7:
Write a main program to completely test the function from Part 6.

Part 8:
Write a main program called program_client that takes two command line parameters that are named pipes. The first parameter will be referred to as to_server and the second will be referred to as to_client.

  1. Print a message to standard error showing your name, something like:
    This program was written by ...
  2. Check to make sure the program has exactly two command line arguments and print a usage message and exit if it does not.
  3. In future parts, if an error occurs, print an appropriate message and exit the program.
  4. Open both to_server and to_client for reading and writing. (However, we will only be writing to to_server and reading from to_client.)
  5. Send a single byte to to_server.
  6. Use read_int_as_line to read an integer from to_client and then read that many bytes from to_client and send them to standard output.
  7. Read an integer from standard input, as in Assignment 1, Part 10.7.
  8. Send that integer to to_server as a line using send_int_as_line.
  9. Use read_int_as_line to read an integer from to_client and then read that many bytes from to_client and send them to standard output.
  10. Print a message to standard error indicating success and exit the program.
Test this program as well as you can.

Part 9:
Write a main program called program_server that takes three command line parameters: a filename and two named pipes. The first parameter will be referred to as infile and the named pipes will be referred to as to_server and to_client. This program will be similar to the show_programs from Assignment 1, but it will read from and send to named pipes.

  1. Print a message to standard output showing your name, something like:
    This program was written by ...
    This program will print all messages to standard output. Nothing should be written to standard error. If an error occurs, print an appropriate message and exit the program.
  2. Check to make sure the program has exactly three command line arguments and print a usage message and exit if it does not.
  3. Open infile and read it into memory.
  4. Test to see that infile only contains valid program lines. If not, print a message and exit.
  5. Open the files to_server and to_client for read and write. (However, we will only be writing to to_client and reading from to_server.)
  6. Read a byte from to_server.
  7. Send the size of infile as a line using send_int_as_line and then send infile, both going to to_client.
  8. Get an integer from to_server using read_int_as_line If this corresponds to a program number of one of the lines in infile send the corresponding file to to_client by first sending the size of the file and then the file itself. Use send_num_bytes so that you do not have to read this entire file into memory.
  9. Print a message indicating success and exit the program.
Test this program with the client of Part 8. They should be able to communicate through the named pipes.

Handing in your program:
Use this cover sheet. Consecutively number all of the other pages you turn in. Each page that contains your work should have a number on it. Turn this in at the beginning of lecture on the due date.