c - Queue sorting function sorts to the wrong queue -
here queue sorting function, dispatchlist, realtime, , jobqueue defined queue struct
struct queue { pcbptr front; pcbptr back; }; typedef struct queue queue;
and pcbptr defined as
struct pcb { int pid; int arrival_time; int time_left; int priority; int status; int printers; int modems; int scanners; int cds; int memory; //might need struct memory block? struct pcb* next; struct pcb* prev; }; typedef struct pcb pcb; typedef pcb * pcbptr;
now actual function
void starthostd(){ int i=0; while(dispatchlist.front != null){ if(dispatchlist.front->priority == 0){ enqueue(dispatchlist.front, &realtime); dispatchlist.front = dispatchlist.front->next; printf("the %d pcb moved realtime queue\n", ); } else{ enqueue(dispatchlist.front, &jobqueue); dispatchlist.front = dispatchlist.front->next; printf("the %d pcb moved job queue\n", ); } i++; } while(realtime.front != null){ printf("blah"); realtime.front = realtime.front->next; } }
and here implementation of enqueue
void enqueue( pcbptr mypcb, queue* queue) { if (queue->front == null){ //empty queue->front = mypcb; } else{ queue->back->next = mypcb; } queue->back = mypcb; //set pcb end of queue }
basically have 7 pcbptr in dispatchlist, first 4 have priority 1, 5th has priority 0, , last 2 have priority 1.
so should happen pcbs 1,2,3,4,6,7 should moved jobqueue, , pcb 5 should moved realtime.
when run program, proper print lines printed, output, expected:
the 0 pcb moved job queue 1 pcb moved job queue 2 pcb moved job queue 3 pcb moved job queue 4 pcb moved realtime queue 5 pcb moved job queue 6 pcb moved job queue
(i know numbers in statement above behind 1)
but however, expected result should blah printed once, since there 1 pcb in realtime queue. however, printed 3 times, pcbs 5,6,7.
to me seems once 1 pcb moved realtime queue, every other element moved realtime queue well, though not supposed to.
can spot might missing?
thanks
ps: have side question, inserted usleep(5000) while loop prints "blah", not seem delay prints @ all, might cause of that?
the problem in how move elements 1 queue another. insert mypcb
in queue, don't take account other elements linked it. have original list
job: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 realtime:
then want move fifth element between queues, don't change links between nodes, , you've got this
job: 1 -> 2 -> 3 -> 4 -> 6 -> 7 realtime: 5 -> 6 -> 7
to solve need change links in queue, guess work
if(dispatchlist.front->priority == 0){ pcbptr t = dispatchlist.front; dispatchlist.front = dispatchlist.front->next; t->next = null; enqueue(t, &realtime); printf("the %d pcb moved realtime queue\n", ); }