CS 1550 – Project 2: Threading and Synchronization

Due: Sunday, October 14, 2007, 11:59pm

Description

Anytime we have shared data between two or more processes or threads, we run the risk of having a race condition where our data could become corrupted. In order to avoid these situations, we have discussed various mechanisms to ensure that one program’s critical regions are guarded from another’s.


One place that we might use threading is to simulate real-world situations that involve multiple independently acting entities, such as people or automobiles. In this project, you will be modeling a common roadway occurrence, where a lane is closed and a flagperson is directing traffic.

The figure above shows the scenario. We have one lane closed of a two-lane road, with traffic coming from the North and South. Because of traffic lights, the traffic on the road comes in bursts. When a car arrives, there is an 80% chance another car is following it, but once no car comes, there is a 20 second delay before any new car will come.

During the times when no cars are at either end, the flagperson will fall asleep, requiring the first car that arrives to blow their horn. When a car arrives at either end, the flagperson will allow traffic from that side to continue to flow, until there are no more cars, or until there are 10 cars lined up on the opposing side, at which time they will be allowed to pass.

Each car takes 1 second to go through the construction area.

Your job is to construct a simulation of these events where under no conditions will a deadlock occur. A deadlock could either be that the flagperson does not allow traffic through from either side, or lets traffic through from both sides causing an accident.

POSIX Threads

For this assignment, we will be using the POSIX threads library (pthreads.)  A skeleton example has been provided for you to download, and is shown below:

#include      <stdio.h>

#include      <stdlib.h>

#include      <pthread.h>

#include      <time.h>

 

/******************************************************************************

 * pthread_sleep takes an integer number of seconds to pause the current thread

 * We provide this function because one does not exist in the standard pthreads

 * library. We simply use a function that has a timeout.

 *****************************************************************************/

int pthread_sleep (int seconds)

{

       pthread_mutex_t mutex;

       pthread_cond_t conditionvar;

       struct timespec timetoexpire;

 

       if(pthread_mutex_init(&mutex,NULL))

       {

              return -1;

       }

       if(pthread_cond_init(&conditionvar,NULL))

       {

              return -1;

       }

 

       //When to expire is an absolute time, so get the current time and add it

       //to our delay time

       timetoexpire.tv_sec = (unsigned int)time(NULL) + seconds;

       timetoexpire.tv_nsec = 0;

 

       return pthread_cond_timedwait(&conditionvar, &mutex, &timetoexpire);

}

 


/******************************************************************************

 * This is an example function that becomes a thread. It takes a pointer

 * parameter so we could pass in an array or structure.

 *****************************************************************************/

int *worker(void *arg)

{

       while(1)

       {

              printf("Thread Running\n");

              fflush(stdout);

              pthread_sleep(1);

       }

}

 

/******************************************************************************

 * The main function is just an infinite loop that spawns off a second thread

 * that also is an infinite loop. We should see two messages from the worker

 * for every one from main.

 *****************************************************************************/

int main()

{

       pthread_t t_id;

 

       if ( -1 == pthread_create(&t_id, NULL, worker, NULL) )

       {

              perror("pthread_create");

              return -1;

       }

 

       while(1)

       {

              printf("Main Running\n");

              fflush(stdout);

              pthread_sleep(2);

       }

 

       return 0;

}

 

A full pthreads reference can be found online at http://www.llnl.gov/computing/tutorials/pthreads/

To compile a program, you must include the pthreads library just like we included the flex library in the first assignment:

gcc –o pthreadtest pthreadtest.c -lpthread

The Environment

We’ve set up a machine to do the term projects on, which can be reached by ssh at thot.cs.pitt.edu. You can log in with your Pitt account.

Hints

The Submission

When you’re done, create a gzipped tarball of your source and compiled executable and copy them to:

 ~jrmst106/submit/

Make sure you name the file with your username, and that you have your name in the comments of your source file.

Note that this directory is insert-only, you may not delete or modify your submissions once in the directory. If you’ve made a mistake before the deadline, resubmit with a number suffix like abc123_1.tar.gz

The highest numbered file before the deadline will be the one that is graded, however for simplicity, please make sure you’ve done all the work and included all necessary files before you submit.