I have pulled a question from Operating Systems - Internals and Design Principles Eighth Edition.
I have tried my answer but really would like to know the proper response to this question.
Considering the following C declarations and function definition:
int global_positives = 0; typedef struct list { struct list *next; double val; } *list; void count_positives(list l) { list p; for (p = 1; p; p = p -> next) if (p -> val > 0.0) ++global_positives; }
While thread A does:
count_positives(<list containing only negative numbers>);
and thread B does:
++global_positives;
a) what does the function do?
b) The C language only addresses single threaded execution. Does the use of two parallel threads create any problems?
So my answers I would like to know if correct are:
a) The function as of now will do nothing in the case of thread A calling the function with only negative numbers since nothing will be greater than zero as well as...
b) the problem of having two threads run in parallel in C because since only one can run at a time global_positives will never increment and the answer will always be zero.
In the ballpark?