CS 3733 Operating Systems, Spring 1999 Assignment 3


Warning: This is not for the current semester.


Due Friday, March 12, 1999


The assignment relates Program 4.1 from PUP and the use of select. Create a single makefile to compile and lint all of the program in this assignment. Use the cover sheet in /usr/local/courses/cs3733/spring99/assign03. A copy of Program 4.1 can also be found there. Turn in all source code, a copy of your makefile, the lint output for all programs, and sample output for each program.
Note: The cover sheet will be available during the week of March 1.

Part 0

Make a fresh directory for this assignment. Get a copy of Program 4.1 from PUP. Call this program ring.c. Modify it so that it starts by printing out a line containing your name. Run the program and make sure you understand what it does.

Part 1

Copy ring.c into ringa.c and modify it as follows. After the ring is created, have each process generate a String in the form
Process xxx sent this. The string should end with a new line. Print the string to standard error and then send it (without the string terminator) to standard output. Then read from standard input and output the string read to standard error with a header indicating which process is doing the output. Everything should be on one line. For example, if the message that comes in to process 235 is "Process 234 sent this" then process 235 should print
Process 235 got Process 234 sent this
all on one line with a new line sent after it.

Part 2

Create a function with the following prototype
int readone(char *buf, int *bufsize, int fd1, int fd2, int slp);
This function is passed a buffer, buf of size bufsize, two file descriptors, fd1 and fd2 and a time to sleep, slp in microseconds. It will use select to read from one of the file descriptors when data is available and put the result in buf. It will return the file descriptor it read from, or -1 if an error occurs. When it returns, bufsize will contain the actual number of bytes read. After select has returned and before it reads from the file descriptor, it will sleep for slp microseconds. Use usleep.

Part 3

Copy ringa.c into ringb.c and modify it as follows. Instead of having each process generate a message, each process reads from one of two places, standard input or a named pipe. Have the process use the variable i to determine the name of the pipe to read from, pipe1, pipe2, pipe3, etc. Use the mkfifo command to create a few pipes for use with this program. Have the process open the pipe for reading and writing to avoid blocking on the open.

Then start an infinite loop reading from two file descriptors. Pass the file descriptor for the pipe and the standard input file descriptor to the readone function from Part 2. Use a sleep time of 0. If something comes in from the pipe, print it to standard error on a single line and output it to standard output. If something comes in from standard input, just print it out as in Part 1.

Test this by opening several windows and writing to the pipes from these windows with commands like cat > pipe1. If you type in the string "xyz" it should generate two output messages such as:
Process 234 read from pipe: xyz
and
Process 235 read from ring: xyz

Part 4

Copy ringb.c into ringc.c and change the sleep time to 1 second (1,000,000 microseconds). Convince yourself that it still works.

Part 5

Copy ringb.c into ringd.c. This one has the sleep time of 0. Modify the program so that all processes use pipe1. Determine whether the program behaves the same as in Part 3.

Part 6

Copy ringd.c into ringe.c and change the sleep time to 1 second (1,000,000 microseconds). Determine whether the program still works as before. Experiment with different values of the sleep time to see what values affect the behavior. Explain why ringe behaves as it does.