CSCI 3323 (Principles of Operating Systems), Fall 2020:
Homework 2b

Credit:
25 points.

Reading

Be sure you have read, or at least skimmed, Chapter 2, sections 2.1 through 2.3.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to my TMail address with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., “csci 3323 hw 2b” or “O/S hw 2b”). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.

  1. (25 points) The starting point for this problem is a simple implementation of the mutual exclusion problem in C with POSIX threads m-e-problem.c. Each thread executes a loop similar to the one presented in class for this problem, except that: Currently no attempt is made to ensure that only one thread at a time is in its critical region, and if you run it you will see that in fact it can happen that more thaan one thread is in its critical region at the same time. Your mission is to correct this.

    Start by compiling the program, running it, and observing its behavior. To compile with gcc, you will need the extra flag -pthread and also -std=c99, e.g.,

    gcc -Wall -std=c99 -pthread m-e-problem.c
    (Or download this Makefile and type make m-e-problem.) The program requires several command-line arguments, described in comments at the top of the code. (If you have trouble remembering the order, note that the program prints a meant-to-be-helpful usage message if run with no arguments.) Note that the program may not exhibit the bad behavior for very short delay times but should for longer times.

    You are to produce two corrected versions of this program:

    Places in the program that should change are marked with “TODO” comments. You should not need to add much code. Confirm that your two improved versions behave as expected, i.e., when one thread starts its critical region no other thread can start its critical region until the first one finishes. Also be sure to correct the comments at the start of the code -- the ones that say the code has no synchronization!

    NOTE about shared variables: Optimizing compilers play a lot of tricks to reduce actual accesses to memory, and processors cache values for the same reason. What this means for multithreaded programs is that it is very difficult to guarantee that changes made to a shared variable in one thread are visible to other threads. Declaring shared variables volatile avoids at least some compile-time optimizations but does not provide any guarantees about what will happen at runtime, especially if there are multiple processors. What is needed is a “memory fence”, which is a way of specifying that at a particular point in the program all memory reads and writes have completed. There is no portable way to achieve this in C99; one must fall back on compiler- or processor-specific code. The starter code includes a function memory_fence that invokes a gcc-specific function providing a memory fence and recommends its use in the functions to begin and end the critical region. Note that library functions for synchronization (e.g., the ones included with POSIX threads) usually incorporate this functionality.

Pledge

Include the Honor Code pledge or just the word “pledged”, plus at least one of the following about collaboration and help (as many as apply).1Text in italics is explanatory or something for you to fill in. For programming assignments, this should go in the body of the e-mail or in a plain-text file pledge.txt (no word-processor files please).

Essay

Include a brief essay (a sentence or two is fine, though you can write as much as you like) telling me what if anything you think you learned from the assignment, and what if anything you found found interesting, difficult, or otherwise noteworthy. For programming assignments, it should go in the body of the e-mail or in a plain-text file essay.txt (no word-processor files please).



Footnotes

... apply).1
Credit where credit is due: I based the wording of this list on a posting to a SIGCSE mailing list. SIGCSE is the ACM's Special Interest Group on CS Education.



2020-10-05