CIT 595 Spring 2011

Homework #3

Due Mon Feb 14 1:30pm


Introduction

In this assignment, you will compare two different means of Interprocess Communication by measuring the time each takes using more fine-grained measurements than the UNIX "time" command. You will perform one million data transfers using shared memory, and one million using the POSIX message queue, and use the "gettimeofday" function (described below) to measure how long each takes.

In some parts of this assignment, you will need to write C code; in others, you will need to provide written solutions. All submissions should be electronic for this assignment, as described below.

Because of the nature of this assignment, you should use the "speclab.seas.upenn.edu" machines instead of eniac (unless you're using your own machine, of course, which is totally fine, too). Please SSH to speclab.seas.upenn.edu before running the programs in this assignment.


Before You Begin

Unlike in Java, where getting the current time of day is fairly straightforward, it's not so easy in C. The "gettimeofday" function obtains the current time, expressed as seconds and microseconds (millionths of seconds) since Jan 1, 1970 (seriously!) and stores it in a timeval struct. The file time.c shows how you would use the "gettimeofday" function, and calls a "timediff" function from time.h that expresses the difference between two timeval structs in microseconds. Download the two files, compile time.c, and run the program to make sure that you can use this functionality.


Part I (5 points)

First, we will start by evaluating IPC using shared memory. The files shm_write.c and shm_read.c use shared memory to pass one piece of data from the former to the latter. They are synchronized such that the Writer writes to shared memory and then waits for the Reader to modify the shared data (so that it knows the Reader read it) before proceeding.

Review this code to make sure you understand it. Before you attempt to execute it, change the "KEY" in both files to a different number, so that you are not sharing memory with all your classmates!

After changing the KEY, compile and run both programs. Make sure to execute the Writer first! You can execute it from the command line using "./shm_write &" so that it runs in the background, and then you can run "./shm_read". Your output should look something like this:

writer: about to write
writer: done writing
reader: waiting to read
reader: read hello
reader is done!
writer is done!

 

Part II (5 points)

Modify the code from Part I so that the Writer and Reader share 10 messages. The two programs must be synchronized: the Writer writes "hello-0" to shared memory, the Reader reads it, and the Writer only writes again (this time it writes "hello-1") after it knows the data has been read.

As in Part I, make sure to execute the Writer first when testing your code. Your output should look something like this (interleaving may not be exactly like this, but close):

writer: about to write hello-0
writer: done writing
reader: waiting to read
reader: read hello-0
reader: waiting to read
writer: about to write hello-1
writer: done writing
reader: read hello-1
reader: waiting to read
writer: about to write hello-2
writer: done writing
reader: read hello-2
[... and so on...]
reader is done!
writer is done!

 

Part III (15 points)

Now modify the code from Part II so that the Writer and Reader share 1,000,000 messages, and use the "gettimeofday" and "timediff" functions from time.h and time.c to annote each program to measure how long the Writer and Reader spend communicating. Be sure to remove any printf statements from the section of code being timed, as they will severely affect the accuracy of your results.

Make sure to execute the Writer first! Note: if your results indicate that the time your Writer spends communicating is significantly greater than the time the Reader does, it's because you're probably including the time difference between the start of each program. The Writer and Reader should report approximately the same amount of time. In your write-up, indicate how much time each program spends communicating.

Part IV (5 points)

Now we will do the same three steps, but for the POSIX message queue. The files mq_write.c and mq_read.c use a message queue to pass one piece of data from the former to the latter. In this case, the Reader goes first and blocks until some data appears, then the Writer writes to the queue and both proceed.

Review this code to make sure you understand it. Before you attempt to execute it, change the "MQ_NAME" in both files to a different string, so that you are not using the same queue as all your classmates!

After changing the MQ_NAME, compile (using the -lrt flag) and run both programs. This time, make sure to execute the READER first! You can execute it from the command line using "./mq_read &" so that it runs in the background, and then you can run "./mq_write". Your output should look something like this:

reader: waiting to read
writer: about to write
reader: read hello
reader is done!
writer: done writing
writer is done!

 

Part V (5 points)

Modify the code from Part IV so that the Writer and Reader share 10 messages. Unlike the shared memory example from Part II, the programs do not need to be synchronized (i.e., the Writer does not have to be notified when the Reader is done reading), but make sure to execute the READER first when testing your code. Your output should look something like this (interleaving may not be exactly like this, but close):

reader: waiting to read
writer: about to write hello-0
reader: read hello-0
reader: waiting to read
writer: done writing
writer: about to write hello-1
reader: read hello-1
reader: waiting to read
writer: done writing
writer: about to write hello-2
reader: read hello-2
[... and so on...]
reader is done!
writer is done!

 

Part VI (15 points)

Now modify the code from Part V so that the Writer and Reader share 1,000,000 messages, and use the "gettimeofday" and "timediff" functions from time.h and time.c to annote each program to measure how long the Writer and Reader spend communicating. Be sure to remove any printf statements from the section of code being timed, as they will severely affect the accuracy of your results.

Make sure to execute the READER first! Note: if your results indicate that the time your Reader spends communicating is significantly greater than the time the Writer does, it's because you're probably including the time difference between the start of each program. The Writer and Reader should report approximately the same amount of time. In your write-up, indicate how much time each program spends communicating, and explain why the results here differ from the results in Part III.

 

Extra Credit! (10 points)

Repeat the above experiment (measuring the time it takes to pass one million messages) using pipes between a parent process and its child.


Academic Honesty

You must work on this assignment individually. Collaboration is explicitly not allowed. That includes (but is not limited to) working together on the assignment, providing solutions to or receiving solutions from another student, or receiving assistance from an outside source. Whereas it is okay to discuss the intent of the assignment or address other clarification issues with other students, you must not discuss approaches, algorithms, or code.

If you need help with this assignment, please visit a member of the teaching staff during office hours, or contact one of us to set up an appointment.


Submission
For this assignment:
  1. Put your C source code files (shm_write.c, shm_read.c, mq_write.c, mq_read.c, and anything you submit for extra credit) in a directory called "homework3_[your-SEAS-id]". For instance, mine would be homework3_cdmurphy.
  2. Also put your writeup file in the same directory.
  3. Additionally, include a Makefile that compiles all your programs and put that in the same directory, too.
  4. Last, from the directory ABOVE your "homework3_[your-SEAS-id]" directory (ie, its parent), tar and/or zip the directory with all your files in it. For instance, on eniac you could do "tar -cvf homework3_[your-SEAS-id].tar ./homework3_[your-SEAS-id]".
  5. Submit the tar/zip file in Blackboard.

Failure to properly follow the submission instructions could result in a delay of grading your assignment and/or a lateness penalty, so please be sure to do it correctly!

Homeworks are to be submitted via Blackboard, as described on the course overview page. Please be sure to tar and/or zip your files into a single submission file!