| | |
FIFO queues in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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*
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
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
Views: 22775 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer lazy linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat openwebfoundation pattern pause pdf pointer pointers posix power problem program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32 win32api windows.h






