Reading assignment

Here is the source code for a simple C program that lists all of the files in the current directory. This program makes use of the getcwd(), opendir(), readdir(), and closedir() functions. You can read about these functions in chapter eight of the textbook in the section on directories.

Programming assignment

In this assignment you will write a pair of C programs. These programs will implement a folder watching system that watches a directory on a computer for files that have been updated and keeps a log of which files have been updated at what times.

The two programs you will write are a folder watcher control program, fw, and the folder watcher daemon, fwd.

fw will take commands from the command line to tell it to start or stop the fwd program. To start fwd you will invoke fw from the command line using a command

./fw start <path>

where <path> is the path to a directory that you want fwd to watch for changes. The fw program will then do these steps:

  1. Call fork() to start a new child process.
  2. The child process will call execve() to launch the fwd program.
  3. The parent process will save the process id of the child process to a text file and then exit.

You can read more about the execve() function in the section "The Exec Family of Calls" in chapter five.

To stop fwd you will invoke fw from the command line using a command

./fw stop

fw will open the text file that contains the process id it saved earlier and then call

kill(pid,SIGKILL);

to send a signal to the fwd program to stop running. To use the kill() function, add the include statement

#include <signal.h>

to your program.

The fwd program will start by reading the path to the folder it needs to watch from the command line. The fwd program will open a log file for writing and drop into an infinite loop. On each iteration through the loop fwd will note the current time, sleep for a period of five minutes, and then scan the directory it is watching for files that have changed. If it finds any files in the directory with modification times after the time it went to sleep, it will write the names of those files along with their modification times to the log file. fwd should only watch the files located in the target directory. It does not have to watch any files located in subdirectories of the target directory.

For this assignment you will also need to make use of the sleep() function to have the fwd program sleep for a span of time. You can read more about the sleep() function here. To get further information about a file, including the time when it was last modified, you will need to use the stat() function. You can read more about the stat() function in chapter six of the textbook in the section titled "The Stat Family" in chapter eight. You will also need to make use of some time functions from the C standard library. Documentation on these functions is available here.

You can confirm that fwd is running by using the ps command in a terminal to list all running processes.