FIFO queues in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

FIFO queues in C

 
0
  #1
Jun 7th, 2006
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.)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: FIFO queues in C

 
0
  #2
Jun 8th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: FIFO queues in C

 
0
  #3
Jun 8th, 2006
The implementation of queue is much more difficult than a stack, especially in c.

Have you tried googling for
  1. queue.c
. It should return more than enough examples.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: FIFO queues in C

 
0
  #4
Jun 8th, 2006
The implementation of queue is much more difficult than a stack, especially in c.
Really? I didn't know (jk)

Have you tried googling for
Code:
queue.c
. It should return more than enough examples.
Hmm, googling for a filename. I need to remember that one
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: FIFO queues in C

 
0
  #5
Jun 8th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: FIFO queues in C

 
0
  #6
Jun 8th, 2006
cscgal:
  1. #include <stdlib.h> // malloc()
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:
  1. // *** fifo.c ***
  2. #include <fifo.h>
  3.  
  4. void fifo_insert(fifo buffer, char data)
  5. {
  6. // wait until it's not in use (by an isr or somethign)
  7. while (buffer.locked);
  8. buffer.locked = true;
  9.  
  10. push(buffer.s1, data);
  11.  
  12. buffer.locked = false;
  13. }
  14.  
  15. char fifo_get(fifo buffer)
  16. {
  17. // wait until it's not in use (by an isr or somethign)
  18. while (buffer.locked);
  19. buffer.locked = true;
  20.  
  21. // flip the stack over so we can get the first value there
  22. while (buffer.s2.length != 0)
  23. push(buffer.s2, pop(buffer.s1));
  24. // take that value out
  25. char rv = pop(buffer.s2);
  26. // and flip it back
  27. while (buffer.s2.length != 0)
  28. push(buffer.s1, pop(buffer.s2));
  29.  
  30. buffer.locked = false;
  31. return rv;
  32. }
  1. // *** fifo.h ***
  2.  
  3. #ifndef __FIFO_H
  4. #define __FIFO_H
  5.  
  6. #include <stack.h>
  7. #include <types.h>
  8.  
  9. typedef struct
  10. {
  11. boolean locked;
  12. stack s1;
  13. stack s2;
  14. } fifo;
  15.  
  16. #endif
  1. // *** stack.c ***
  2. #include <stack.h>
  3.  
  4. void push(stack buffer, char key)
  5. {
  6. buffer.data[buffer.length] = key;
  7. // Avoid stack overflow by just continuing to put values on the top
  8. if (buffer.length < 256)
  9. buffer.length++;
  10. }
  11.  
  12. // This function should have special significance in Popcorn :-P
  13. char pop(stack buffer)
  14. {
  15. // if there are no values in the stack, wait until there are
  16. while(buffer.length == 0);
  17.  
  18. // now get one out
  19. char rv = buffer.data[buffer.length];
  20. buffer.length--;
  21. return rv;
  22. }
  23.  
  24. char pop_nowait(stack buffer)
  25. {
  26. if (buffer.length == 0) return 0;
  27.  
  28. // now get one out
  29. char rv = buffer.data[buffer.length];
  30. buffer.length--;
  31. return rv;
  32. }
  1. // *** stack.h ***
  2. #ifndef __STACK_H
  3. #define __STACK_H
  4.  
  5.  
  6. typedef struct
  7. {
  8. int length;
  9. char data[256];
  10. } stack;
  11.  
  12. void push(stack buffer, char key);
  13. char pop(stack buffer);
  14. char pop_nowait(stack buffer);
  15.  
  16. #endif

And then I use it like this:
  1. // *** keyboard.c ***
  2. void keyboard_handler(regs *r)
  3. {
  4. unsigned char scancode;
  5.  
  6. // Read from the keyboard's data buffer
  7. scancode = inportb(0x60);
  8.  
  9. // If the top bit of the byte we read from the keyboard is
  10. // set, that means that a key has just been released
  11. if (scancode & 0x80)
  12. {
  13. if (scancode - 0x80 == 42 || scancode - 0x80 == 54)
  14. shift_pressed = true;
  15.  
  16. else if (scancode - 0x80 == 29)
  17. ctrl_pressed = true;
  18.  
  19. else if (scancode - 0x80 == 56)
  20. alt_pressed = true;
  21. }
  22.  
  23. else
  24. {
  25. if (scancode == 42 || scancode == 54)
  26. {
  27. shift_pressed = false;
  28. }
  29. else if (scancode == 29)
  30. {
  31. ctrl_pressed = false;
  32. }
  33. else if (scancode == 56)
  34. {
  35. alt_pressed = false;
  36. }
  37. else if (shift_pressed)
  38. {
  39. //putch(kbdus_shift[scancode]);
  40. keypressed(kbdus_shift[scancode]);
  41. }
  42. else
  43. {
  44. //putch(kbdus[scancode]);
  45. keypressed(kbdus[scancode]);
  46. }
  47. }
  48. }
  1. #ifndef __KEYBOARD_H
  2. #define __KEYBOARD_H
  3.  
  4. #include <fifo.h>
  5. #include <misc.h>
  6.  
  7. boolean shift_pressed;
  8. boolean ctrl_pressed;
  9. boolean alt_pressed;
  10.  
  11. fifo kb_buffer;
  12.  
  13. void keyboard_handler(regs *r);
  14. void keyboard_init();
  15.  
  16. // Separate functions for these would be a bit bloatedish.
  17. #define getkey() fifo_get(kb_buffer)
  18. #define keypressed(k) fifo_insert(kb_buffer, k)
  19.  
  20. #endif

However, there never seems to be anything in the buffer. It's hanging and not running any code after that. Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: FIFO queues in C

 
0
  #7
Jun 8th, 2006
Hmm, got it now. Dunno what I was thinking using 2 stacks like that
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 22811 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC