University of Minnesota
Machine Architecture and Organization (sec 010)
index.php
CSci 2021 Lab 0xD

CSci 2021 Lab 0xD: Shared Memory

Introduction

In this lab you will explore shared memory between processes via the mmap system call. Shared memory is commonly used by processes to share data and communicate, e.g. for the purpose of synchronization. For this lab, we have three programs that are in competition over a hunk of shared memory. In other words, they are not sharing nicely!

The memory that will be shared is treated as a square grid called the arena. Two programs, make-red and make-blue, fight over ownership of the arena by switching cells to red or to blue. This is done by setting the byte of the cell to character 'b' for blue or 'R' red. The arena_watcher program observes the state of the arena, and draws each cell as a colored pixel in an X11 screen [1].

Unpack the source files for this lab with these command:

linux> cp /web/classes/Fall-2018/csci2021-010/labs/0xD/lab0xD.tar .
linux> tar -xvf lab0xD.tar

Finishing Red vs. Blue

Your task is to finish these programs where indicated. In particular, you will implement shared memory using mmap. Now is good time to read up on the manual page to get a feel for the effects and the syntax: man 2 mmap.

Each program is missing its call to mmap. Also, make-red and make-blue are missing the logic that determines when to flip the color of a cell in the arena. These areas are marked with TODO comments.

Managing Processes in Linux

The three programs will run concurrently, where arena_watcher should start first and the two makers should start at the same time (or as close as we can get). We can conveniently start multiple processes in a single linux shell with the '&' shell operator. Adding '&' to the end of a command causes it to be run in the background, making stdin (terminal input) available for more commands. For example:

linux> arena_watcher &
[1] 21904

The second line outputs the processs ID (PID) of the arena_watcher process. Another vital linux command, ps, is used to view active processes. For example:

linux> ps
PID TTY TIME CMD
16384 pts/22 00:00:00 bash
21904 pts/22 00:00:00 arena_watcher
22005 pts/22 00:00:00 ps

The final command we'll need is the kill command. One of the better-names unix commands, it does exactly what the name implies: kill a process. In it's most simple form, it takes a PID as an argument, such as the one we saw when we ran arena_watcher in the background, and attempts to end the process. killall works like kill but instead it takes a program name as it's argument (but beware! killall will end all programs by that name). To end arena_watcher we can run:

linux> kill 21904

or

linux> killall arena_watcher
[1]+ Terminated ./arena_watcher

Note that the operating system will prevent an unpriviledged user (such as yourself) from killing processes that belong to other users, so don't be too afraid to use these commands.

Running the Arena

First start arena_watcher and verify the X11 window appears. Run it in a separate terminal or use '&' to background it. Then use '&' to run make-red and make-blue; how would you write these commands to make them start as close in time as possible? Observe what happens in the if you but some space between starting the makers. Finally, use the processe management commands we've discussed to clean up your processes. What happens if you leave one of the makers running?

Notes:
[1] If you are remotely logged into a machine, you must forward X Windows traffic back to your local machine (your PC). With ssh you can simply add the -XY options.