Machine Problem 3, due Mar. 23
Assignment: Implement a disk cache (file mp3.c)supporting the operations defined in the following header file:
/* mp3.h */ #include
#include #define CACHEBLOCK 512 #define CACHEBLOCKS 64 #define FILEDESCRIPTORS 32 int cache_open(const char *pathname, int flags, mode_t mode); int cache_close(int fd); ssize_t cache_read(int fd, void *buf, size_t count); ssize_t cache_write(int fd, const void *buf, size_t count); off_t cache_lseek(int fildes, off_t offset, int whence);
These have exactly the function of the Unix/Linux kernel calls with the same names excluding the cache_ prefix. You are strongly advised to begin by writing a skeleton implementation where each of these routines directly calls the corresponding system call and does no other computation. You can use this to test your main program, you can use it for performance comparisons with the raw system, and you can use it for testing your main program.
You are responsible for testing your code. The main program should be separately compiled from the test. You may use but need not use a makefile. Your main program will not be turned in. Just turn in, as a single source file, your implementation of the above routines.
Your code should statically allocate CACHEBLOCK times CACHEBLOCKS bytes of RAM for use as a cache. This memory should be used to cache all files that are opened using cache_open() and manipulated using cache_ routines. Your code should attempt to detect use of cache_ operations on files not opened correctly. Your code need not try to detect corruption of the cache caused by use of write() instead of cache_write() on files that are cached.
Your cache should try to retain the most recently used blocks of the files being cached, while discarding older blocks. It may use an approximation of this replacement scheme, but it should document the approximation it uses. It may implement either a write-through or a write-back replacement policy, but it should document the policy it uses.
Grading will focus on style as much as function, so working code will not earn more than half credit unless it is clear and readable, and clean code will earn some credit even if not functional.
As usual, submit your solution using ICON.