-1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3037172
  • 577
  • 1
  • 7
  • 25
  • 3
    Your answer for b is incorrect. Multiple threads can run at the same time on a multi-core machine. Even on a single core machine, the OS will pause thread A and run thread B every so often. However, since thread A doesn't access the shared `global_positives` variable (because the if condition in it always fails), only thread B will access `global_positives`, so there won't be any concurrency issues. – Colonel Thirty Two Feb 27 '15 at 00:21
  • 2
    A number of typos there: *list should read list, p = 1 (one) should read p = l (el), p -> xyz should be p->xyz. – jarmod Feb 27 '15 at 00:24
  • @jamrod: you can put spaces around `->` if you want to. I don't know anyone who does, but it's certainly legal. – rici Feb 27 '15 at 00:28
  • there are no typos, this is directly from the textbook – user3037172 Feb 27 '15 at 00:35
  • 2
    I beg to differ. `(p = 1;` is *definitely* wrong. If not a typo on your part, notify the publisher and amend the errata, because a pointer being assigned the value of `1` (one) is utterly wrong. – WhozCraig Feb 27 '15 at 00:45
  • For the sake of the problem can you explain if using these threads in parallel causes any problems if the syntax was correct? – user3037172 Feb 27 '15 at 00:49
  • You deduction based on the statement "... because since only one can run at a time.." is where the wheels falls off. That simply isn't true. True concurrent threads is a commonality with modern multi-core hardware and MPS os'es. The reason there is no currency issue *here* is solely because only two threads are running, and due to circumstance one will never modify otherwise-potentially-contentious shared data, *even if both threads are actively running simultaneously*. Run two instances of `B` and you have a recipe for disaster. – WhozCraig Feb 27 '15 at 01:08
  • See [Is it a good idea to `typedef` pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). Succinctly, the primary answer is "No, it isn't". Completely separately, there is either a typo in the question or in the book. The `p = 1;` in the loop is erroneous in and of itself, and it also means that `l`, the function parameter, is unused. It is undoubtedly intended to be `p = l;` (where the distinction is between digit one and letter lower-case ell). This is rehashing what was already said in other comments — but the unused parameter wasn't mentioned. – Jonathan Leffler Feb 27 '15 at 02:33
  • Also notice that C11 does discuss multi-threaded execution. – Jonathan Leffler Feb 27 '15 at 02:42
  • @user3037172 There is no answer. C doesn't say what the rules for concurrent threads are. You'd have to check the appropriate documentation for the threading standard you happen to be using, and you don't mention what that standard might be. – David Schwartz Sep 04 '18 at 22:17

1 Answers1

0

a) count positives, does not update global positive count, and hence the two threads operate on distinct global data, and require no locking.

b) In a single-threaded environment, it is consistent. However, in a multithreaded environment, it assigns to global positive count, even if the list contains only negative elements. Our original program is now broken, since the update of global positive count by thread B may be lost, as a result of thread A writing back an earlier value of global positive count. By Pthread rules, a thread-unaware compiler has turned a perfectly legitimate program into one with undefined semantics.