Put all of the programs from this assignment is a directory called
assign4.
Use a separate subdirectory for each part.
Do not delete anything from assignment 3.
Part 0:
Write a function called fdreceiver that takes an open file descriptor
as its only parameter.
It reads from this open file descriptor in the same way as
pipereceiver from Assignment 3.
It reads two lines, the name of the file and the size of the file.
It then reads the rest of the file and creates a new file with the given name.
It returns the number of bytes in the file on success and -1 on error.
On error, errno is set.
This function should not output any error messages.
If it gets an end of file when reading the first line, it returns 0.
Rewrite your pipereceiver from Assignment 3 to use this function. This version of pipereceiver opens the pipe, calls fdreceiver, and displays a message indicating success or failure.
Part 1:
Write a function called fdsender that takes the name of a file,
an open file descriptor, and a sleep time in seconds as parameters.
It then behaves like pipesender from Assignment 3,
but it sends to the open file descriptor rather than a pipe.
After sending each block of 1024
bytes it sleeps for the given number of seconds.
It returns the number of bytes in the file on success and -1 on error.
On error, errno is set.
This function should not output any error messages.
Rewrite your pipesender from Assignment 3 to use this function. This version of pipesender takes an additional command line argument, the sleep time in seconds. It opens the pipe, calls fdsender, and displays a message indicating success or failure.
Part 2:
Write a program called filesreceiver that takes a named pipe as
a command line argument.
It opens the pipe for reading and calls fdreceiver in a loop until an
error occurs. It accumulates the the total number of bytes transferred and the number
of files transferred and outputs this information after the loop.
If fdreceiver
reports an end-of-file,
treat this as success and display an appropriate message.
The message should contain the number of files received and the number
of bytes received.
If fdreceiver reports any other error, display an error message.
Part 3:
Write a program called filesender that takes three command line
arguments, the name of a a directory, the name of a pipe and a sleep time
in seconds.
It opens the pipe for writing
and sends all of the regular files that do not have length 0
to the pipe using fdsender.
In sending the
files, send only the filename, not the directory. Display the total
number of files sent, the total number of bytes sent, a message indicating
success or one indicating failure.
Part 4:
Look at your programs from Part 0 and make sure they behave correctly in
the presence of signals.
The restart library may be helpful here if you did not use it earlier.
Rewrite the programs as necessary.
Rewrite filesreceiver so that it starts by
displaying its process ID. Whenever it receives a SIGUSR1 signal,
it displays the total number of files and bytes successfully received.
You may assume that fprintf is async-signal safe.
Additional comments about Part 4:
In Part 2 you wrote a main function that is in the file
filesreceiver.c. This main function calls the function
fdreceiver that is in the file fdreceiver.c. The two
files will be compiled together to produce an executable called
filesreceiver. You should be able to do Part 4 without modifying
fdreceiver.c as long as it only uses functions that behave well in
the presence of signals.
Before starting its loop, the main program should print its message and set up a signal handler. You will have to keep the number of files transferred and the bytes transferred in global variables so that the signal handler has access to them.
Extra credit:
Rewrite the programs in Part 4 so that when a signal is received, it displays
the names of the files received so far along with their file sizes.
Only those files
received completely should be displayed. You should not make any assumptions
about the number of files that will be received.
Handing in your programs
Note the four changes in boldface to parts 0, 2, 3 and 4 and the additional
comments about Part 4.
Do the following on one of the machines in the Sun or Linux labs: