Thread: FIFO help

  1. #1
    Unregistered
    Guest

    FIFO help

    I have a program that simulates FIFO. My professor says I need to add to the head and then move it to the tail not add directly to the tail which he says is what I am doing. I am having difficulty with this. Can anyone help. My code entire code follows, the function addQ is where the replacement takes place.


    # include<conio.h>
    # include<stdlib.h>
    # include <time.h>

    struct CQueue
    {
    int head;
    int tail;

    int Values[10];
    } Ob_queue;

    int isFull()
    {
    if(Ob_queue.tail-Ob_queue.head >= 10)
    return 1;
    return 0;
    }

    void AddQ(int value)
    {
    if (isFull ())
    RemoveQ();

    Ob_queue.Values[Ob_queue.tail++]=value;
    printf("%d\n", Ob_queue.tail-Ob_queue.head);
    }

    int RemoveQ ()
    {
    if(Ob_queue.head != Ob_queue.tail)
    return Ob_queue.Values[Ob_queue.head++];

    return -1;
    }

    int TopQ ()
    {
    if(Ob_queue.head != Ob_queue.tail)
    return Ob_queue.Values[Ob_queue.head];

    return -1;
    }

    void Init ()
    {
    Ob_queue.head=0;
    Ob_queue.tail=0;
    }

    int isPresent (int value)
    {
    int i=0;
    for(i=Ob_queue.head; i<Ob_queue.tail; i++)
    if(value == Ob_queue.Values[i])
    return 1;

    return 0;
    }

    int main()
    {
    int PageFaults=0, value=0, i, Rands[40], count=0;

    Init();
    srand(time(0));

    for (i=0; i<40; i++)
    {
    value = rand()%20;
    if (!isPresent(value))
    {
    printf("Process %d", value);
    printf(" recieves Page Frame ");

    PageFaults++;
    AddQ(value);
    printf("\t");
    }
    Rands[count++]=value;
    }

    printf("\n\nThere were ");
    printf("%d", PageFaults);
    printf(" Page Faults\n\nReference String:\n\n");

    for(i=0; i<40; i++)
    {
    printf("%d", Rands[i]);
    printf("\t");
    }

    getch();
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ew, writing a queue with arrays is a pain. Too bad you aren't using linked lists, that's much easier.

    Keep an index of both the head and tail, when you add an item to the queue, place it at the tail and increment the tail. When you remove an item, take it from the head and increment the head. Note that head is end of the array and tail is the beginning. With this technique you can wrap the queue around the array, when head becomes greater than the length of the array but the queue is not full you wrap head back to the beginning of the array.

    To determine if the queue is full, simply take the remainder of a division between head and the maximum number the queue can hold.
    Code:
    void enQueue ( int newItem )
    {
      Values[tail++] = newItem;
      tail = tail % 10;
    }
    
    int deQueue ( void )
    {
      head = head % 10;
      return Values[head++];
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. select system call with FIFO fd.
    By vlrk in forum Linux Programming
    Replies: 0
    Last Post: 05-11-2009, 04:27 AM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. working with FIFO files
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 11:35 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. FIFO list
    By Lillian176 in forum C Programming
    Replies: 4
    Last Post: 04-13-2003, 10:45 PM