COP4610: Operating Systems & Concurrent Programming up ↑

Programming Assignment P1: C Programming Warm-up

Spring 2015

Deadline:

See course calendar.

Educational Objectives:

This assignment is intended as a warm-up for the assignments that will come later, but it also intended to introduce you to a number of concepts and skills. You will make a set of specified modifications to a simple discrete-event simulation program. This program was a partial solution to one of the COP 4610 assignments another term, and may (no promise) be used as the bare-bones basis for your last progamming project this term. The main changes you will make will be replace the ad hoc linked list structures by uses of the circular linked list structure used in the Linux kernel, based on struct list_head. The other change will be to correct an error in one of the functions that handles command-line arguments. The main effort should be reading and understanding the existing code, and then working through any misunderstandings. You will also be expected to verify that your code compiles and works the same on both Linux and Solaris systems.

Deliverables:

Modified versions of the provided files random.c, events.c, and simulator.c.

Tasks:

  1. On one of the linprog systems, create a working directory, P1, and copy into it the file ~baker/opsys/assign/P1/P1.tgz.
  2. Use tar -xzvpf P1.tgz to extract the necessary files.
  3. Enter the shell command make. This should make two subdirectories, Linux and SunOS, each containing some symbolic links, and then compile the programs sim and testrand in the Linux subdirectory. In general, which directory the compilation is done in will depend on whether you are running on a Linux (linprog.cs.fsu.edu) or a Solaris (program.cs.fsu.edu) system). For example, on a Linux system, you may see the following:
    baker@linprog3.cs.fsu.edu: make
          make[1]: Entering directory `/home/faculty/baker/courses/cop4610.S15/assign/P1/test/Linux'
          make[1]: Leaving directory `/home/faculty/baker/courses/cop4610.S15/assign/P1/test/Linux'
          make[1]: Entering directory `/home/faculty/baker/courses/cop4610.S15/assign/P1/test/SunOS'
          make[1]: Leaving directory `/home/faculty/baker/courses/cop4610.S15/assign/P1/test/SunOS'
          make[1]: Entering directory `/home/faculty/baker/courses/cop4610.S15/assign/P1/test/Linux'
          gcc -Wall -Wextra -pedantic -DDEBUG   -c -o random.o random.c
          gcc -Wall -Wextra -pedantic -DDEBUG -o testrand testrand.c random.o -lm
          gcc -Wall -Wextra -pedantic -DDEBUG -o testargs testargs.c random.o -lm
          gcc -Wall -Wextra -pedantic -DDEBUG   -c -o simulator_old.o simulator_old.c
          gcc -Wall -Wextra -pedantic -DDEBUG   -c -o events_old.o events_old.c
          gcc -Wall -Wextra -pedantic -DDEBUG -o sim_old sim.c simulator_old.o events_old.o random.o -lm
          gcc -Wall -Wextra -pedantic -DDEBUG   -c -o simulator.o simulator.c
          gcc -Wall -Wextra -pedantic -DDEBUG   -c -o events.o events.c
          events.c: In function 'queue_dump':
          events.c:89: warning: ISO C forbids braced-groups within expressions
          events.c:89: warning: value computed is not used
          events.c:89: warning: ISO C forbids braced-groups within expressions
          events.c:89: warning: value computed is not used
          events.c: In function 'dispatch':
          events.c:103: warning: ISO C forbids braced-groups within expressions
          events.c:105: warning: ISO C forbids braced-groups within expressions
          gcc -Wall -Wextra -pedantic -DDEBUG -o sim sim.c simulator.o events.o random.o -lm
          make[1]: Leaving directory `/home/faculty/baker/courses/cop4610.S15/assign/P1/test/Linux'
  4. Try to read and understand all the code in the directory P1 You should ask questions about any parts you don't understand, in the Blackboard Discussion Board forum for this assignment, well before the program is due. Also try to understand the associated makefile, but if you don't understand all of it right now, that is OK. You will have time to learn more about makefiles as the term goes on.
  5. Modify the code of the files events.c and simulator.c to replace one of the ad hoc list structures with type struct list_head and the corresponding list_* functions and macros from list.h. When you have completed the changes, it should work exactly as it did before your changes.

    There are three list structures that you can/need to modify:

    You will need to change all the lists to doubly linked circular lists, to fit the model of "list.h". You should be able to use macros and functions from "list.h", directly, for most of the operations, resulting in a net reduction in the code of the files you are modifying. The one place you will need to "roll your own" algorithm is in the function simulator_schedule, where a node is inserted into a list that is used as a priority queue, ordered by increasing event time, and ordered with equal event times by event kind.

    See how I already replaced one of the four original list structures, by comparing the files events.c against events_old.c. (I have also created a back-up copy of simulator.c, called simulator_old.c, but there are not yet any modifications.) Leave the "_old.c" files unchanged.

    Some important aspects to keep in mind:

    The following diagram may be helpful in understanding the list structure and the macro container_of(), also known as list_entry(). The diagram is incomplete in not showing how the list is circular. The blue arrow corresponding to New->list.next might be directly equal to &P->list, or point to another node in a chain that eventually gets back to P->list. Similarly, the orange arrow corresponding to P->list->prev should eventually lead back to New->list.

    diagram of list structure

    There is a little bit of explanation of Linux kernel lists on pages 99-101 of the text, but for more detailed explanation I'd recommend on the of the tutorials listed under References below.

    Some typical C programming errors to look out for:

  6. Modify the comments in the file(s) you changed to:
  7. Compile the modified code, using make. By default, it will make three programs: sim, sim_old, and testrand, in one of the two subdirectories Linux and SunOS (depending on the OS you are using). You can ignore testrand, except as an example of a unit test program. The program sim_oldis for comparison. The code you have modifed should only affect sim. Correct any defects that show up in compilation.
  8. Test your code to make sure the program sim still works, by executing ./Linux/sim or ./SunOS/sim. Correct any defects that show up in your testing.
  9. Now switch to the other operating system (to Solaris if you were working on Linux before, or vice versa) and repeat the testing as above, correcting any problems that show up. Then, if you make any changes, switch back to the other operating system and repeat, until you are sure your program works correctly on both systems.
  10. Repeat the above steps until the only list structures left are those from list.h, or until you have reached the deadline for this assignment and must turn it in.
  11. When you have completed the above, look at the program command_check_args(), in random.c. You will see comments to the effect that it has at least one serious coding error. It is one of the errors that I warn against in my notes on robust coding - see point 7. If you compile and run the program testargs (another example of a unit test program) you will see that this function has several defects and failure modes. If you comment out the call that causes a segmentation error fault, it will go on and fail again on the next call. Your assignment is to (at least) correct the error related to point 7 in the notes, and add a comment to the file header explaining what you changed and why. If you can correct some of the other defects, consider it a challenge.

    I appreciate that some of the vulnerabilities trace back to the parameters, but please don't change the header file! The submit script only copies random.c, simulator.c, and events.c, so if you rely on changes to any other files your submission will fail when we attempt to compile and test it.

Further Instructions:

References:

Submission:

First read the Study Guide section on Submitting Assignments, for general instructions.

  1. Log into shell.cs.fsu.edu
  2. cd to the directory P1 where you have your working solution, and make certain the versions of the files you want to submit are in that directory.
  3. Execute the shell script submitP1.sh using the Bourne shell, by entering sh submitP1.sh or by setting the permission bits to executable and entering ./submitP1.sh. If it works correctly, you should see a successful completion message, and later receive two e-mail confirmations, including one with a copy of what you submitted. For this first assignment, the script also does a few sanity checks, to make sure you are executing it in the right directory. In particular, it will check that the deliverable files exist in the local directory, and that executable versions of sim existin in both the Linux and SunOS subdirectories.

Assessment:

Review the general Grading Criteria for Programming Assignments section of the Study Guide.

You can expect to have your score on this assignment reduced if your code does not adhere to the instructions above.

For a minumum passing score (70) you must replace at least one of the three existing list structures listed under step 4 above with the ones from list.h. Your score will be higher if you successfully replace more of the list structures with those from list.h and correct at least the one bug in command_check_args().

CriterionPoints
One of the three list structures correctly replaced by application of struct_list_header, and program sim produces exactly the same output as original version, using unmodified versions of files Makefile, sim.c, events.h, random.h, and simulator.h, when tested with a variety of command line parameters. 60
In addition, comments revised appropriately in all modified files, and coding style preserved. 10
In addition, another of the three list structures replaced. 10
In addition, the third of the list structures replaced. 10
In addition, type 7 string comparison error in command_check_args() corrected 10
T. P. Baker. ($Id)