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.
A. stack
B. main() function
C. registers
D. global variables
E. program code
F. heap
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; }
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.
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:
Variable Name Variable Type Initial Value Description
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()
.
Write your answers in a text file. Name the file answers.txt and upload it on GauchoSpace
Last updated: 2020-03-27 23:24:28 -0700 [validate xhtml]