NAME ringbuffer -- implement the ring buffer "interface" and test SYNOPSIS ringbuffer [-v] [-s _size_] _commands_ DESCRIPTION Creates a ring buffer of characters of size _size_ and applies the enqueue/dequeue commands from string _commands_. The _commands_ string is understood as follows: For each character c in _commands_, in order, either: * enqueue the character c or * if c is the hyphen character, dequeue a character from the ring buffer After each command the program writes either "deq: _X_" or "enq: _X_" to standard out, where _X_ is either the character enqueued or dequeued, or the words "empty" or "full", according to the result of dequeue or enqueue. The following options are available: -v verbose output -s ring buffer size. Default is 8. HISTORY Introduced in csc421.161 to prepare for the in-kernel ring buffer introduced in csc421.151. BUGS
You have been provided with the structure "struct RingBuffer". This fields in this structure will be used to implement a ring buffer according the algorithm given here.
next_in
is the index in buffer
to where the next character
should be enqueued. If the ring buffer pointer is named rb, then this is
rb->buffer[rb->next_in].If the buffer is not full, place the character to enqueue at index
next_in
then increment
next_in
, taking it mod size
to wrap around to the beginning of the buffer.
The value returned is the value just enqueued.
If the buffer is full, return -1, and do not enqueue the character, do not increment
next_in
.
next_out
is the index in buffer
from where the next character
should be dequeued. If the ring buffer pointer is named rb, then this is
rb->buffer[rb->next_out].If the buffer is not empty, return the character at index
next_out
, then increment
next_out
, taking it mod size
to wrap around to the beginning of the buffer.
If the buffer is empty, return -1, and do not increment
next_out
.
next_in != next_out
, the ring buffer is neither empty nor full. next_in
gives
the "empty space" ready to take a character; next_out
gives the "full space" ready
to provide a character.
next_in == next_out
, the ring buffer might be empty or might be full. You
need to keep track of which of the two situations is the case by maintaining is_full
properly.
is_full
is false (zero), because although next_in == next_out
that is because there is nothing
in the ring buffer.
is_full
true (to one) when an enqueue operation causes next_in
to equal next_out
.
is_full
false (to zero) whenever a character is dequeued — the buffer
cannot be full if something has just been removed.
author: burton rosenberg
created: 08 sep 2015
update: 08 sep 2015