CS170 Homework 3

Handed out Tuesday, January 21, 2019
Due 9:00 PM, Friday, January 25, 2019

Homework 3

These problems should be done on your own. We're not going to be grading them strictly (we'll mainly look at whether you attempted them). But they will be reinforcing knowledge and skills, so you should totally work through them carefully.

The uses of threading

Define time-to-completion as the time between when a process begins handling a workload and finishes handling that workload. Is the following statement true always, sometimes, or never? "For a given workload, a multi-threaded process has lower time-to-completion than the equivalent single-threaded process." Explain your answer in 2-3 sentences.

Threads vs. processes

A thread within a process has its own: (Choose all that apply)

A. stack

B. main() function

C. registers

D. global variables

E. program code

F. heap

Race conditions

Identify the potential concurrency issues with the function add below by giving an example. (Hint: what happens when one thread runs add(a,b) while another runs add(b,a)?)
  struct Point {
    int x;
    int y;
  };

  void add(struct Point *a, const struct Point *b) {
    a->x += b->x;
    a->y += b->y;
  }

Synchronization: warmup

This problem is based on the following code (which is similar to Problem 4 of Homework 2):
    int i = 0; 

    /* ADD SOME THINGS HERE */

    void
    foo(void *)
    {
        printf("I am foo!!!\n");
        
        /* ADD SOME CODE HERE */
    }

    void
    boo(void *)
    {
        /* ADD SOME CODE HERE */

        printf("I am boo!!!\n");
    }

    int
    main(int argc, char** argv)
    {
        create_thread(foo);
        create_thread(boo);

        // wait for threads to finish
        // before exiting
        join_thread(foo);
        join_thread(boo);

        exit(0);
    }
Modify the code above to ensure that I am foo!!! prints before I am boo!!!. Use mutexes and condition variables.

More practice with synchronization

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 ints to "1" and one of them 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().

Handing in the homework

Write your answers in a text file. Name the file answers.txt and upload it on GauchoSpace


Last updated: 2019-01-21 13:36:42 -0800 [validate xhtml]