Programming Assignments for CS 302

The programs that you turn in will be graded according to the criteria given in the first of the following two links. The second link gives detailed instructions on exactly how you are to turn in your finished assignments.

Program grading criteria
Turning in your assignments

Below are your programming assignments. The due date for each one is contained in the assignment description.

Assignment 8.
Download the following file to your disk drive.
ftp://ftp.ibiblio.org/pub/docs/books/gutenberg/etext01/wrnpc10.txt
This is a text file of the novel War and Peace and it is provided by the Project Gutenberg. This text file is approximately 3.2 MB in size. Write a program that creates an empty file and then copies the contents of War and Peace into the file 300 times. This will create a file on your disk drive that is almost one gigabyte in size. Your goal is to get your program to run in a reasonable amount of time. I have written a version of the program that, on a 500MHz PIII with 256 MB or RAM, runs in 60 seconds; your version should also take about that much time.

Your program should call the Win32 function GetTickCount() at the very beginning and at the very end so that it can print out a message saying how long the copy operation took. You can use any technique that you want to in order to speed up your program. Read pages 155-160 from Lab Exercise 9: Fast File I/O, in the lab manual to get some ideas of how to speed up disk I/O. The only limitation on your program is that it should run on a computer with 256MB of RAM. This assignment is due on Monday, May 6 (since that is the beginning of final exam week, late assignments may end up being too late to count, so turn your assignment in on time).

Assignment 7.
This assignment is a simplification of Lab Exercise 8 from the lab manual. You are to write two programs that together implement a simple producer-consumer model that uses processes (instead of threads) and shared virtual memory, backed by a memory mapped file, for interprocess communication. This assignment is due Monday, April 22.

Read pages 143-148 of the lab manual (and you can also read pages 433-435 from our operating systems textbook). These pages give you information on how to use the Win32 functions for creating memory mapped files. Complete the programs that are outlined in the following two files.

producer.c
consumer.c
The producer simply reads characters from its standard input and stores them in its virtual memory locations that are backed by the memory mapped file. The consumer reads characters out of its virtual memory locations that are backed by the memory mapped file and writes the characters to its standard output. The memory mapped file is the "buffer" that the producer and consumer share. (Notice that the memory mapped file does not have to appear at the same virtual memory address in the two processes.) You will need to sync the consumer process with the producer. The consumer should not block the producer unless the memory mapped file has become full, and if you wish you can terminate the producer when the memory mapped file fills up.

While these two processes are running, use the VMmapper program from the last assignment to see the memory mapped file appear in the memory map of each process. As the producer is filling up its virtual memory locations backed by the memory mapped file, use the Task Manager to watch for page faults (use cut and paste to input a large amount of text into the producer at one time). When the processes are done, open the file that you used as the memory mapped file and see in it the characters that were passed from the producer to the consumer.

Assignment 6.
This assignment is based on Lab Exercise 7 from the lab manual. You are to write a program that performs virtual memory operations. You will run the program along with a virtual memory mapping program so that you can observe the virtual memory operations and answer some questions. This assignment is due Monday, April 8.

Read pages 123-136 of the lab manual. Particularly important are pages 134-136 that describe the Win32 virtual memory functions that you need to use. Complete the program that is outlined in the following file.

VMdriver.c
Create a text file with commands to exercise your virtual memory operations. (The form of the command file is described in the comments of VMdriver.c.) Your command file should demonstrate all of the commands described in the comments of VMdriver.c. Call your command file VMcmds.txt. Run your program using a command at the DOS prompt of the form c:\VMdriver <VMcmds.txt. Your program VMdriver.exe will create a process from the following program.
VMmapper.c
(You need to download and compile this program so that you have the executable VMmapper.exe.) The program VMmapper.exe will display a memory map of your program VMdriver.exe as it is running so that you can see the virtual memory operations take place.

When you are done, turn in the following three files: your version of VMdriver.c, your command file VMcmds.txt, and a text file containing answers to the following questions.
1) What happens if you try to reserve a huge amount of memory, say 1 gigabyte?
2) What happens if you try to commit a huge amount of memory, say 1 gigabyte?
3) What approximately is the largest amount of memory that you can commit? What determines this number?
4) After you have committed a block of pages, how can you observe the effect of the "Touch pages in a block" command? (Hint: You will need to use another program.)
5) What is the effect of committing and then touching as large a block of memory as you possibly can?
6) How can you observe the effect of the "Lock a block of pages" command?
7) What happens when you touch a noncommitted page of virtual memory?
8) What happens when you touch a guard page?

Assignment 5.
This assignment is based on Lab Exercise 4 from lab manual. You are to write a program that creates several buffers, producer threads, and consumer threads. Each producer thread will be in an endless loop where it randomly chooses one of the buffers, puts an "item" into the buffer, and then displays the contents of the buffer in the console window. Each consumer thread will be in an endless loop where it randomly chooses one of the buffers, takes an "item" from the buffer, and then displays the contents of the buffer in the console window. This is due on Monday, March 11.

Read the following parts of "Lab Exercise 4: Thread Synchronization": the Introduction, pages 81-85, the section "Wait Functions", pages 85-86, the section "Mutex Objects", pages 88-90, and the section "Semaphores", pages 90-92.

The following file outlines a producer-consumer program that creates multiple producer and consumer threads and multiple buffers. In the file are directions for what parts of the program you need to implement.

ProducersConsumers.c
The above file does not define what a "Buffer" is. Your finished program should work with two different implementations of the Buffer data structure. The following file outlines a header file that defines an array-based stack buffer (a Last-In-First-Out buffer). This header file is included by ProducersConsumers.c and this is where the kind of buffer used by the producer and consumer threads is defined. In the file are directions for what you need to implement in the definition of the stack buffer.
StackBuffer.h
Here is an example of the program's output using 3 stack buffers and 3 producer and 3 consumer threads.

The following file defines an array-based circular queue buffer (a circular, First-In-First-Out buffer). In the file are directions for what you need to implement in the definition of the circular buffer.

CircularBuffer.h
Here is an example of the program's output using 3 circular buffers and 3 producer and 3 consumer threads. You switch the program ProducersConsumers.c to use circular buffers instead of stack buffers by changing only which header file is included into ProducersConsumers.c. Everything else in ProducersConsumers.c is independent of which kind of buffer is being used.

When you work on this, get the stack version of the buffer working before you try to do the circular buffer version. Here are links to some new Win32 API functions that you will need to use in this assignment.

When you are done, turn in your versions of the above three files. This is due on Monday, March 11.

Assignment 4.
This assignment is a slight modification of Lab Exercise 2 on pages 43-63 of the lab manual. You are to modify two C programs. The first program will create several processes. The second program will create several threads. So you will see how processes and threads are created in the Windows operating system. This assignment is due Monday, February 18, one week after assignment 3.

Read Lab Exercise 2 in the lab manual. The section called "Creating a Process" describes in detail how to use the Win32 API function CreateProcess. There are a couple of small misprints in this section. Check the author's Errata Sheet. The section called "Creating a Thread" describes in detail how to use the Win32 API function CreateThread. You can ignore the section called "A Complication in Using CreateThread". For this assignment, use the CreateThread function (not the _beginthreadex function).

For the first program, do Part A on page 56. Below is an outline of the program. This outline contains all of the code given by the author in the "Attacking the Problem: Part A" section on page 60.

launch.c
When this program runs, it needs a file containing command lines for processes. You can send me the file you use to test your program. I will also test your program on my own file of command lines.

For the second program, do Part B on pages 56-57. In addition, your program should create dynamic arrays to hold the handles and ID numbers of the threads that your program creates. (If you have not worked with dynamic arrays, read this short explanation. Another good reference on pointers and dynamical arrays is this Pointers and Memory tutorial.) Your program should print out the ID numbers of the threads it creates, and then just before your program quits it should close the handles of the threads it created. Here is an outline of the program. This outline contains all of the code given by the author in the "Attacking the Problem: Part B" section on pages 61-63.

mthread.c
For this assignment you have to read a lot but you do not have to write very much. Most of the code that you need is already in the above two programs. You do not need to add too much. You just need to figure out what to add. As mentioned above, this assignment is due Monday, February 18, one week after assignment 3.

Assignment 3.
For this assignment you are to write two programs that access a floppy disk at a fairly low level. The point of this assignment is to emphasize why you want the operating system to provide "device abstractions" (because accessing a floppy disk at a low level is not all that easy) and to also give you an idea of how an operating system can maintain a file system on a disk. This assignment is due Monday, February 11.

The material that you need for this assignment is a bit scattered through out our textbook and the lab manual. This assignment is a modification of Part A and Part B of "Lab Exercise: A Floppy Disk Driver" from pages 153-160 at the end of Chapter 5 of our operating systems textbook. Do not be surprised if you need to read the description of this exercise and all the background material about 10 times before it all starts to sink in (and I'm not exaggerating).

The 1st program: First, read pages 153-155 and Subsection "Using the Win32 Floppy Disk API" on pages 157-158. (If you want, you can also read the "Attacking The Problem" subsection, but you will not be using it and it is a pretty confusing mess.) Also read "In the Hanger: Windows Files" on pages 33-36 of the textbook.

Write three functions

HANDLE physicalDisk(char driveLetter);
void sectorDump(HANDLE theDisk, unsigned int logicalSectorNumber);
BOOL sectorRead(HANDLE theDisk, unsigned int logicalSectorNumber, unsigned char* buffer);
Notice the slight modifications made to the prototypes given on page 153 of the textbook. Specifically, for the physicalDisk function you do not need to use the Disk data structure (defined on page 158) and you do not need to make any call to the Win32 function DevideIOControl. Put your definition of the three functions in a file floppy.h. Here is an outline of this header file.
floppy.h
Your definitions of the three functions must work with the following driver program.
floppy.c
Note: This assignment is very similar to "Lab Exercise 10: Floppy I/O" in the lab manual. You may want to read pages 167-171 and pages 175-176 in the lab manual (for the most part, these pages are the same as pages from the operating systems textbook).

When you have this program done, experiment with it. Take a blank floppy disk and look at a bunch of its sectors. Are they empty? Format the floppy and look at the sectors again. Add a small text file to the newly formatted disk. Find the sectors that the file's data are written to. Now delete the file. Is the data still there in the sectors?

The 2nd program: You are to write a program that "unpacks" the FAT on the floppy disk so that the FAT can be more easily understood and manipulated (this is Part B from page 153 of the textbook). To understand what this means, you need to read pages 183-189 from "Lab Exercise 11: Files Systems and Directories" in the lab manual. In particular, the example on page 188 using Figure 30 is very useful.

Write three functions

BOOL fatRead(HANDLE theDisk, unsigned char *buffer);
void unpackFAT(unsigned char *buffer, int* unpackedFAT);
int countObjects(int* unpackedFAT);
Put your definition of the three functions in a file floppyFAT.h. Here is an outline of this header file. This outline of the header file contains a description of what each of these functions is supposed to do.
floppyFAT.h
Your definitions of the three functions must work with the following driver program.
floppyFAT.c
Note: Even though this is essentially Part B from page 153 of the operating systems textbook, that book contains no information about FAT12. So it is not at all clear how the author expects readers of his textbook to do this part of his Lab Exercise. Luckily, we have the lab manual which contains the information we need. But "Lab Exercise 11" from the lab manual is very difficult, and we are not doing any part of it, we are just using its description of FAT12.

When you have this program done, experiment with it. Format a floppy and then look at its FAT. Add a small file to the floppy and look at the fat again. Add a few small and medium size files to the floppy. Look at the FAT again. Delete the first couple of files that you put on the floppy. Look at the FAT again. Add a couple of new files to the floppy. Did they use up the FAT entries left empty by the deleted files? What does it mean for a floppy to become "fragmented"?

When you are done, put your four files floppy.h, floppy.c, floppyFAT.h, and floppyFAT.c in a zip file with the appropriate name and e-mail me the zip file. As mentioned above, this assignment is due Monday, February 11.

Assignment 2.
This assignment is a modification of Exercise 2 on page 220 of the Appendix of the Lab Manual. First, read the Appendix. Then write a Win32 console application that uses the C runtime open function (fopen) to open a file for reading and to open another file for writing. Copy all of the characters from the input file to the output file, but convert every third character to uppercase (find a function in the C runtime library that does this for you). Your program should allow the user to put the names of the input and output files on the command line (so you will need to use argc and argv) and if the names are not there on the command line your program should prompt the user for the file names. Here are two things you should think about. What should you do if the input file does not exist? What should you do if the output file already exists? This assignment is due on Monday, January 28.

Assignment 1.
Do "Lab Exercise 1: Managing Multiple Tasks" from pages 31-41 of the lab manual. You will need to complete (and turn in) this work sheet. You will also need to download and run these two small programs, cpuload.exe and diskload.exe (these files are being downloaded from author's web page for our lab manual; see the "Supplementary Code" section of that web page). This assignment is due on Monday, January 21.

Note: Here are some corrections, taken from the other's Errata Sheet, for the pages of this assignment in the Lab manual.


Page 39, Line 7: Change "Select 'Edit' form the 'Add to Chart' menu. ..." to "Select 'Add to Chart' form the 'Edit' menu. ..."

Page 39, Line -2: Change "g. What is the pview process?" to "g. What is the priority of the pview process?"

Page 41, Insert after Line 2: "Be sure you have included a copy of the dummyin2.txt file in the directory where you execute diskload.exe. (see page 37)."

Return to the CS 302 home page.


compliments and criticisms