Homework 3

Due 2/3/2012 [start of section]

Problem 1

Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials.

The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats.

Assume the agent calls the procedure

void chooseIngredients(int *paper, int *tobacco, int *match);
to randomly select 2 of the 3 ingredients. The routine randomly sets 2 of the values to "1" and one of the values to "0". You don't have to write this routine.

Write a program to synchronize the agent and smokers.

  1. What synchronization and state variables will you use in this problem? (For each variable, indicate the variable's type, the variable's name, its initial value (if any), and a short comment describing the variable's purpose.

    Variable Name        Variable Type        Initial Value        Description

  2. Write the routines Agent() and matchSmoker() (the routine for the smoker that has lots of matches.) You don't have to write the routines paperSmoker() or tobaccoSmoker(), but your solution should be general enough so that those routines would be simple variations of matchSmoker().

Problem 2

Write a solution to the dining philosophers using locks and condition variables. You may wish to use the sthread wrapper in the lab T source. Your solution must prevent philosopher starvation. Follow the coding standards discussed in class. On the exam, you will be required to follow these coding standards in all questions of this type.

Problem 3

Implement the function
void parallelDo(int n, void *(function(void *)), void *arg[])
parallelDo spawns off N worker threads. function is a pointer to a function, and arg[] is an array of arguments to that function (e.g., worker thread i is to execute (*function)(arg[i])). parallelDo returns once all worker threads have finished running the function.

Assume you are given mutex locks and condition variables with standard public interfaces.

Assume that you are given functions creating and destroying threads that are similar to those discussed in class:

void sthread_create(sthread_t *thrd,

void *(start_routine(void*)),
void *arg0ToStartRoutine,
void *arg1ToStartRoutine,
...); // Pass as many args as you like
void sthread_exit(void);

Implement this parallelDo function. Follow the coding standards specified in the handout. Important: follow an object-oriented style of programming - encapsulate all shared states in an object that manages access to it.

Problem 4

Anderson and Dahlin OSPP Chapter: 5 Problem: 12

Discussion Problem

Everyone should attempt this problem before the discussion section, but no one is required to turn it in.

Anderson and Dahlin OSPP Chapter: 5 Problem: 4

Discussion Problem

Everyone should attempt this problem before the discussion section, but no one is required to turn it in.

Anderson and Dahlin OSPP Chapter: 5 Problem: 6

Discussion Problem

Everyone should attempt this problem before the discussion section, but no one is required to turn it in.

Anderson and Dahlin OSPP Chapter: 5 Problem: 8