Study carefully the following code: { ... } //Producer while (1) { while (((in +
ID: 3883038 • Letter: S
Question
Study carefully the following code: { ... } //Producer while (1) { while (((in + 1) BUFFER_SIZE) - out): // do nothing buffer[in] =: in (in + 1) % BUFFER_SIZE } //Consumer while (1) { while (in -- out): nextconsumed = butter[out]: out= (out + 1) % BUFFER_SIZE } a. State and explain the effect of assigning a value of 1 to BUFFER_SIZF? b. Suppose your friend rewrote the producer code and called it "MyVersionOfProducer However, after carefully studying it, you found a problem/issue in this version. Explain the issue with an example. //Producer while (1) { while (in == out): // do nothing buffer[in] = nextproduced: in = (in + 1) % BUFFER_SIZE }Explanation / Answer
a) in this case there will be no difference in the output because the outer while condition is
ie while(1) will always be true
because Basically before each iteration the condition is evaluated to be TRUE or FALSE. In C TRUE is represented by non-zero value and FALSE by zero. while (1) indicates an always TRUE condition so any loop with having this statement will run forever. This is also known as infinite loop.
so the producer loop won't exit;
and now if you assign a value of 1 to BUFFER_SIZE
in the inner while
ie while(((in + 1) % BUFFER_SIZE) == out)
here (in +1) % BUFFER_SIZE will always return 0 (zero) if you assign BUFFER_SIZE to 1
because anynumber can be divided by 1 so the mod will be zero
b )
in this case the difference is in the while loop
ie while(in == out)
so the loop will wait untill in != out ie in not equal to out
only after that the next two lines execute
if any thing wrong please replay with the full code and make sure it's readability
instead of screen shot like this please paste the code