Producer consumer in c, algorithm : I was reading Galvin OS book about producer consumer problem and came through this piece of code,

Initially and shared are,

    #define BUFFER SIZE 10
    typedef struct {
    . . .
    }item;

    int in =0;
    int out=0;

**Producer**

    item next produced;
    while (true) {
    while (((in + 1) % BUFFER SIZE ) == out)
    ; /* do nothing */
    buffer[in] = next produced;
    in = (in + 1) % BUFFER SIZE ;
    }


**Consumer**

    item next consumed;
    while (true) {
    while (in == out)
    ; /* do nothing */
    next consumed = buffer[out];
    out = (out + 1) % BUFFER SIZE ;
    /* consume the item in next consumed */
    }

Now this is what Galvin book says,

>This scheme allows at most BUFFER SIZE − 1 items in the buffer at the
same time. We leave it as an exercise for you to provide a solution in which
BUFFER SIZE items can be in the buffer at the same time.

This is what I came up with. Is this correct?

**Producer**

    item next produced;
    while (true) {
    buffer[in] = next produced;  //JUST MOVED THIS LINE!
    while (((in + 1) % BUFFER SIZE ) == out)
    ; /* do nothing */
    
    in = (in + 1) % BUFFER SIZE ;
    }


**Consumer**

    item next consumed;
    while (true) {
    while (in == out)
    ; /* do nothing */
    next consumed = buffer[out];
    out = (out + 1) % BUFFER SIZE ;
    /* consume the item in next consumed */
    }

I think this solves! but is this correct? Correct me if i am wrong? Any other better solution possible?
