Originally Posted by
iMalc
Wait, what?! You're calling qsort from inside the while loop?
If so then there's your bug right there!
I also don't see where you're incrementing i. I think you better post the real code.
no, qsort is not in a while loop...
Let me start fresh
Heres the source code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> /* strtok() */
#include <unistd.h>
#include <stdbool.h>
#define BUFFSIZE 10000 /* buffer allocation for file size */
#define MAXPROCS 1000 /* number of max processes */
#define INIT /* Process states */
#define READY
#define RUNNING
#define DONE
struct process {
char *id;
int state;
int priority;
int qaunta;
int temp;
int working;
int waiting;
};
struct process proc[MAXPROCS]; /* Current process */
int comp(const void *a, const void *b)
{
return ((struct process*)a)->priority - ((struct process*)b)->priority;
}
int main (int argc, char **argv)
{
int fd, i;
char buffer[BUFFSIZE+1]; /* extra byte for null */
char *procstr = NULL;
char *delim = " \n";
int status;
fd = open(argv[1], O_RDONLY);
if ( fd == -1 )
{
printf("There was an error opening the file \n");
exit(1);
}
i = 0;
while ((status = read(fd, buffer, BUFFSIZE)) > 0) /* read until EOF or error */
{
/* null terminate buffer */
buffer[status] = 0;
/* Tokenize 'process string' attributes */
procstr = strtok(buffer, delim);
while (procstr)
{
proc[i].id = strdup(procstr);
//printf("id: %s\n", proc[i].id);
procstr = strtok(NULL, delim);
proc[i].qaunta = atoi(procstr);
//printf("Qaunta: %d\n", proc[i].qaunta);
procstr = strtok(NULL, delim);
proc[i].priority = atoi(procstr);
//printf("Priority: %d\n", proc[i].priority);
procstr = strtok(NULL, delim);
}
}
/* order process priority from 0 to 20 */
qsort(proc, 199, sizeof(*proc), comp);
printf("%s %d %d\n", proc[i].id, proc[i].qaunta, proc[i].priority); //test to see whats in the variables after qsort
ool needToContinue = 0;
//keep processing until all have been processed (ie all quanta == 0)
while(needToContinue)
{
//reset the flag each loop of the array
needToContinue = false;
//loop thru the entire array
for(i=0;i<199;++i)//loop through the array
{
printf("Id: %s Qaunta: %d Priority: %d\n", proc[i].id, proc[i].qaunta, proc[i].priority);
//check to see if this element is finished
if(proc[i].qaunta > 0)
{
//not finished so process
proc[i].qaunta--;
printf("ID: %s Qaunta Remaining: %d \n",proc[i].id, proc[i].qaunta);
//now check to see if we have finished processing this element
//or need to keep going next loop
if(proc[i].qaunta > 0)
{
//we found an element that will need to be processed in the next loop,
//so set the flag to continue looping
needToContinue = true;
}
}
}
}
//show processing is finished
printf("Goodbye\n");
close(fd);
return 0;
}
When i compile it my output is as follows:
(null) 0 0
Goodbye
What i desire it to output is something as follows:
Processid Qaunta Priority
Process5 33 0
Process67 7 0
Process 34 87 1
Process 11 99 2
.......................
Process 188 156 20