CMPS 3600 Lab-7 - Preventing Thread Deadlock and Starvation

Goals

resources:

Start with your 3600/7/dining_phil.c that we wrote in class together. A new copy is available in the sample programs.

   $ cd
   $ cd 3600/7
   $ cp /home/fac/gordon/public_html/3600/examples/7/* . 
   $ cp phil.c mylab7.c
The mylab7.c program will demonstrate that all philosophers get something
to eat. It is also a good program to demonstrate how Linux does thread
scheduling.

For this you need two terminal windows.
From one terminal window start top as follows.

  $ top -H -u username -d .5  # H to toggle your threads display to on

  • From within top hit 'f' to access the fields screen.
  • From that screen arrow to 'P' field [Last Used CPU].
  • Hit Space to toggle P on.
  • Arrow down to 'nth' [Number of Threads].
  • Hit 'Space' to toggle that on. Hit 'q'.

Jump to the second terminal window and execute:

    $ ./lab7 40

Watch what happens with top. You should see all threads spawned and joined.
You should also see the threads scheduled across multiple CPUs.

Add a new feature to mylab7.c

Add this fibonacci function...

int fib(int n)
{
    if (n == 1 || n == 2)
        return 1;
    return fib(n-1) + fib(n-2);
}

Call fib from within the philosopher thread loop, to control execution speed.
Get n for fibonacci from the command-line input.
We will write this in class together.

Your lab programming assignment:
1. Add a recursive Fibonacci function to the program.

2. Add command-line arguments to the main() function.
   Usage will be: Usage ./lab7 <delay> <max eats>
   We will do this together.

3. Add a feature:
   When any philosopher is full, end all threads, and end the program.
   Full means a philosopher has eaten a given number of times.
   Get this value as the 3rd command-line argument.
   For example:
       ./lab7 40 10
       This will execute lab7,
       calculate the 40th fibonacci number inside all thread loops,
       stop the program when any philosopher has eaten 10 times.

   This feature can help us to spot thread starvation happening.

4. Modify (refactor) the existing code,
   replacing the POSIX semaphores with System-V semaphores.


sample output
$ ./lab7 10 4 0 eating 1 0 0 0 0 1 eating 1 1 0 0 0 0 eating 2 1 0 0 0 0 eating 3 1 0 0 0 3 eating 3 1 0 1 0 0 eating 4 1 0 1 0 2 eating 4 1 1 1 0 1 eating 4 2 1 1 0 3 eating 4 2 1 2 0
Files to be collected:

    3600/7/mylab7.c
    3600/7/Makefile
    3600/7/foo
    3600/7/dining_phil.c