| | |
FIFO queues in C
![]() |
Any idea how to implement a FIFO queue of chars in C? It doesn't need to be too fancy, just a fixed size (say 256 chars) and can't use any library functions (this is for a hobby OS kernel I'm writing, and there's not a lot of functions available for use). I just tried to make one, then I realized... hey wait, this is an implementation of a stack, not a queue :p (I must say, it worked pretty well for a stack... but I don't need a stack here.)
One array and two indecies. One index points to the empty position for the next character to be added. Other index points at the character to be removed when needed. When either getst to the end of the array, reset to beginning.
You need to decide when the queue (this is the name of this structure) is full and empty.
You need to decide when the queue (this is the name of this structure) is full and empty.
The implementation of queue is much more difficult than a stack, especially in c.
Have you tried googling for . It should return more than enough examples.
Have you tried googling for
C Syntax (Toggle Plain Text)
queue.c
*Voted best profile in the world*
cscgal:
Um, I don't have a malloc
Thanks anyway.
OK, I dug up an old thread about doing it with 2 stacks. Here's what I came up with from that:
And then I use it like this:
However, there never seems to be anything in the buffer. It's hanging and not running any code after that. Any ideas?
C Syntax (Toggle Plain Text)
#include <stdlib.h> // malloc()
Thanks anyway.OK, I dug up an old thread about doing it with 2 stacks. Here's what I came up with from that:
C Syntax (Toggle Plain Text)
// *** fifo.c *** #include <fifo.h> void fifo_insert(fifo buffer, char data) { // wait until it's not in use (by an isr or somethign) while (buffer.locked); buffer.locked = true; push(buffer.s1, data); buffer.locked = false; } char fifo_get(fifo buffer) { // wait until it's not in use (by an isr or somethign) while (buffer.locked); buffer.locked = true; // flip the stack over so we can get the first value there while (buffer.s2.length != 0) push(buffer.s2, pop(buffer.s1)); // take that value out char rv = pop(buffer.s2); // and flip it back while (buffer.s2.length != 0) push(buffer.s1, pop(buffer.s2)); buffer.locked = false; return rv; }
C Syntax (Toggle Plain Text)
// *** fifo.h *** #ifndef __FIFO_H #define __FIFO_H #include <stack.h> #include <types.h> typedef struct { boolean locked; stack s1; stack s2; } fifo; #endif
C Syntax (Toggle Plain Text)
// *** stack.c *** #include <stack.h> void push(stack buffer, char key) { buffer.data[buffer.length] = key; // Avoid stack overflow by just continuing to put values on the top if (buffer.length < 256) buffer.length++; } // This function should have special significance in Popcorn :-P char pop(stack buffer) { // if there are no values in the stack, wait until there are while(buffer.length == 0); // now get one out char rv = buffer.data[buffer.length]; buffer.length--; return rv; } char pop_nowait(stack buffer) { if (buffer.length == 0) return 0; // now get one out char rv = buffer.data[buffer.length]; buffer.length--; return rv; }
C Syntax (Toggle Plain Text)
// *** stack.h *** #ifndef __STACK_H #define __STACK_H typedef struct { int length; char data[256]; } stack; void push(stack buffer, char key); char pop(stack buffer); char pop_nowait(stack buffer); #endif
And then I use it like this:
C Syntax (Toggle Plain Text)
// *** keyboard.c *** void keyboard_handler(regs *r) { unsigned char scancode; // Read from the keyboard's data buffer scancode = inportb(0x60); // If the top bit of the byte we read from the keyboard is // set, that means that a key has just been released if (scancode & 0x80) { if (scancode - 0x80 == 42 || scancode - 0x80 == 54) shift_pressed = true; else if (scancode - 0x80 == 29) ctrl_pressed = true; else if (scancode - 0x80 == 56) alt_pressed = true; } else { if (scancode == 42 || scancode == 54) { shift_pressed = false; } else if (scancode == 29) { ctrl_pressed = false; } else if (scancode == 56) { alt_pressed = false; } else if (shift_pressed) { //putch(kbdus_shift[scancode]); keypressed(kbdus_shift[scancode]); } else { //putch(kbdus[scancode]); keypressed(kbdus[scancode]); } } }
C Syntax (Toggle Plain Text)
#ifndef __KEYBOARD_H #define __KEYBOARD_H #include <fifo.h> #include <misc.h> boolean shift_pressed; boolean ctrl_pressed; boolean alt_pressed; fifo kb_buffer; void keyboard_handler(regs *r); void keyboard_init(); // Separate functions for these would be a bit bloatedish. #define getkey() fifo_get(kb_buffer) #define keypressed(k) fifo_insert(kb_buffer, k) #endif
However, there never seems to be anything in the buffer. It's hanging and not running any code after that. Any ideas?
![]() |
Similar Threads
- How to implement a stack using two queues! (Java)
- Secondary Ide Controller Dual Fifo (Windows 95 / 98 / Me)
Other Threads in the C Forum
- Previous Thread: self referencing C programs
- Next Thread: Taking Command Line Arguments
| Thread Tools | Search this Thread |
* adobe ansi api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h







