Assignment 4: IPC

Due by: Monday, February 24, 2025 at 11:59 p.m.

In this assignment, you'll use pipes and FIFOs to communicate between processes. Likewise, you'll read data from a memory-mapped file and use posix_spawn() in addition to the typical fork/exec approach.

As with all the assignments and projects in this course, it's recommended that you do the stages in the order given. Get the MIN unit tests passing before moving on to the FULL ones. Likewise, get the MIN integration tests passing before moving on to the FULL ones.

First, download the archive given below and unzip it into an appropriate directory.

Useful Functions

It may be valuable for you to consult the POSIX documentation or man pages for details about the following functions:

  • strchr(), calloc(), free()
  • fork(), execlp(), pipe(), mkfifo()
  • open(), close(), read(), write()
  • fstat(), unlink(), mmap(), munmap()
  • posix_spawn_file_actions_init(), posix_spawn_file_actions_addclose(), posix_spawn_file_actions_adddup2(), posix_spawn()

Stage 1: Pipes and FIFOs

Complete the following to set up the pipes for I/O redirection:

  1. Complete the implementations of split_string(), create_cksum_child(), and get_cksum() in pipe.c. Pass the MIN_split_string, MIN_cksum, and MIN_child unit tests before proceeding.
  2. Complete the if-block in main.c for the condition (!use_server && !use_map). This condition corresponds to running the program with only a file name. Pass the MIN_fork and MIN_fork_no_file integration tests.

Next, create a FIFO that can be used to create a server interface to the checksum calculation above. Instead of getting the file name from the command line and calling get_cksum(), main.c will open the FIFO and send the file name through that.

  1. Implement the fifo_server() in pipe.c to receive a file name from a FIFO and send the checksum value back through another FIFO. Pass the MIN_fifo unit test.
  2. Implement the if-block in main.c for the condition (use_server), which corresponds to passing the -s flag. Pass all the MIN tests.

Stage 2: Memory-mapped Files and Spawn

Pipes and FIFOs use message passing as a form of shared memory, requiring system calls by both the sender and receiver to exchange data. Memory-mapped files, on the other hand, allow data exchange at any point once the map is set up. In this lab, you will set up a memory-mapped file containing file names to compute checksum values on. Although this lab uses memory-mapped files only within a single process, the procedures for sharing such a mapping are almost identical.

  1. Complete the implementations of get_file_size(), open_index(), and get_file_name() in map.c. Pass the corresponding FULL_size, FULL_map, and FULL_file_name unit tests.
  2. Change the implementation of spawn_cksum() in map.c, which currently calls get_cksum() from pipe.c. Instead, make it create the pipe and use posix_spawn() instead of the standard fork() and exec(). After making this change, it should still pass the FULL_spawn unit test.
  3. Implement the else-block in main.c, which corresponds to passing the -m flag. When this is done, the last command-line argument (lineno) is a line number, rather than a file name. Use this line number to get the file from the memory map. Note that each line is exactly LINE_LENGTH bytes (as defined in map.c), so lineno * LINE_LENGTH is the index into the memory map where the line would start.

Turn In

First, run make clean to remove all of the executable and object files from your directory structure. Then, zip up the contents of your project directory in either a .zip or a .tar.xz file. Upload this archive to Brightspace. Your assignment must be submitted by Monday, February 24, 2025 at 11:59 p.m. Grace days are not available for assignments.

All work must be done within assigned teams, though you may discuss general concepts with your classmates. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.

Under no circumstances should any member of one team look at the code written by another team. Tools will be used to detect code similarity automatically.

Code that does not compile will automatically score zero points.

Grading

Your grade will be determined by the following categories.

Category Description Weight
Compiling Code Free points! 10%
Unit Tests Your solution passes the eight unit tests. 40%
Integration Tests Your solution passes the five integration tests. 50%

Credits

This assignment is based on material from CS 361 by Michael S. Kirkpatrick. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.