#include
#include
#define BUF_SIZE 4
typedef struct {
enum {
BUFF_STATUS_EMPTY=0,
BUFF_STATUS_NOTEMPTY,
BUFF_STATUS_FULL
} status;
int b[BUF_SIZE];
int i;
int o;
} buff_t ;
void in(buff_t *buf)
{
int newp;
do {
newp= (buf->i+1)%BUF_SIZE;
if ( newp == buf->o ) {
buf->status=BUFF_STATUS_FULL;
break;
} else {
buf->status=BUFF_STATUS_NOTEMPTY;
buf->i=newp;
buf->b[buf->i]=buf->i;
}
} while (1) ;
}
void out(buff_t *buf)
{
int newp;
do {
newp=(buf->o+1)%BUF_SIZE;
if ( newp==buf->i ) {
buf->status = BUFF_STATUS_EMPTY;
break;
} else {
buf->o=newp;
printf("%d:%d\n", buf->o, buf->b[buf->o]);
buf->b[newp]=0x00;
}
} while (1);
}
int main(int argc, char *argv)
{
buff_t b1;
memset(&b1, 0x00, sizeof(b1));
for (;1;) {
if ( b1.status != BUFF_STATUS_FULL)
in(&b1);
if (b1.status != BUFF_STATUS_EMPTY)
out(&b1);
}
}
No comments:
Post a Comment