Ok i half have a working scheduler. The idea is to print out input from a text file,
with the process id quantum and priority, sort them out into priority order, 0-20(0 highest) ad print them out the number of times the quatum is.

here is what i have so far

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 
int main(int argc, char **argv)
{
	char filename[256];
	strcpy(filename, argv[1]);
	FILE *file = fopen(filename, "r");
	
	int ProcessID, Quantum, Priority;

	if ( file )
	{
		char line[20];
		
		if fgets(line, sizeof line, file) ) //reads in line at time
		{
			  sscanf(line, "Process%d %d %d",&ProcessID, &Quantum, &Priority) == 3; 	//assigns input to process id, quatum and priority//					
	     }		
	}
	else
	{
		printf("Error: Unable to open file"); //error
	}

////////////////////////////////////////////////////////////////////////////////////////////////////
	int i = 1;
	
	if(Priority == 0) // do this if the priority is 0 (highest)
	{
		while(i <= Quantum)
		{
			printf("Process id %d ->  Quantum No %d -> Quantum Total %d->Priority %d\n",ProcessID,i,Quantum,Priority); //print out info number of times the quatum is
			i++;
		}
			
	}
	else
	{
		printf("\nNo Priority %d processes\n\n",Priority); // if is no input data on certain priority
	}
	
	if(Priority == 1)
	{
		while(i <= Quantum)
		{
			printf("Process id %d ->  Quantum No %d -> Quantum Total %d->Priority %d\n",ProcessID,i,Quantum,Priority); //prints out number of times
			i++;
		}
	}
   else
	{
		printf("\nNo Priority %d processes\n\n",Priority);
	}

	fclose(file);

	return 0;
}

//would carry on with    if(prioity == 2) { do }  all way to 20.

The problem im getting is when i run it only prints out the first line of the text file.

if i run first part ( above the line of ////) with a printf statement it will print out all info in the text file.

So im thinking i need to store ProcessID, Quantum, Priority in some kind of buffer or array or something along those lines??? I just dont now how to get this working!

also once this is working i need to print the contents of priority as round robin,
e.g
process 1 proirty 0
process 2 priority 0
process 1 priority 0
process 2 priority 0

etc......

anyone have any suggustions would be greatful :)

Erm, what you posted won't even compile - line 19 is missing a (
So how can you have run results?

Unless you just typed this "from memory", in which case the code is useless as we have no confidence that it represents reality. We could point out lots of trivial mistakes and all you would say is "the real code isn't like that".

So post what you actually COMPILED and run.


FWIW, changing line 19 into a while loop might help.

It seems to me that you want to be reading the file data into an array of structures rather than simply a single "struct" of ProcessID, Quantum, Priority. And then when you have all of the data from the file stored in this array of structures, then you go forward with the rest of the handling.

Ok sorry i just added comments before i posted it and obviously deleted the (

here is the full code working

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 
int main(int argc, char **argv)
{
	char filename[256];
	strcpy(filename, argv[1]);
	FILE *file = fopen(filename, "r");
	
	int ProcessID, Quantum, Priority;

	if ( file )
	{
		char line[20];
		
		while (fgets(line, sizeof line, file) ) //reads in line at time
		{
			  sscanf(line, "Process%d %d %d",&ProcessID, &Quantum, &Priority) == 3; 	//assigns input to process id, quatum and priority//					
	     }		
	}
	else
	{
		printf("Error: Unable to open file"); //error
	}

////////////////////////////////////////////////////////////////////////////////////////////////////
	int i = 1;
	
	if(Priority == 0) // do this if the priority is 0 (highest)
	{
		while(i <= Quantum)
		{
			printf("Process id %d ->  Quantum No %d -> Quantum Total %d->Priority %d\n",ProcessID,i,Quantum,Priority); //print out info number of times the quatum is
			i++;
		}
			
	}
	else
	{
		printf("\nNo Priority %d processes\n\n",Priority); // if is no input data on certain priority
	}
	
	if(Priority == 1)
	{
		while(i <= Quantum)
		{
			printf("Process id %d ->  Quantum No %d -> Quantum Total %d->Priority %d\n",ProcessID,i,Quantum,Priority); //prints out number of times
			i++;
		}
	}
   else
	{
		printf("\nNo Priority %d processes\n\n",Priority);
	}

	fclose(file);

	return 0;
}

//would carry on with    if(prioity == 2) { do }  all way to 20.

here is the sample text im using

Process1 12 1
Process2 23 1
Process3 45 2
Process4 67 3
Process5 3 2
Process6 2 0
Process7 23 0

Now if i run this through first part of program it just prints out the output to stdin
Using this code ( same as first part above just with printf statement for output)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 

int main(int argc, char **argv)

{


     
	char filename[256];
	strcpy(filename, argv[1]);
	FILE *file = fopen(filename, "r");

	

	if (file != NULL )
	{
		char line[20];
		int ProcessID, Quantum, Priority;



		while ( fgets(line, sizeof line, file) ) //reads in line at time
		{
			if ( sscanf(line, "Process%d %d %d",&ProcessID, &Quantum, &Priority) == 3 )
			{
				printf("ProcessID: %d -- Quantum = %d, Priority = %d\n",ProcessID, Quantum, Priority);
			}
		}
		fclose(file);
	}
	else
	{
		printf("Error: Unable to open file");
	}

return 0;

}

but if i run the first program i get the following output

Process id 7 -> Quantum No 1 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 2 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 3 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 4 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 5 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 6 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 7 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 8 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 9 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 10 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 11 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 12 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 13 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 14 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 15 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 16 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 17 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 18 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 19 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 20 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 21 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 22 -> Quantum Total 23->Priority 0
Process id 7 -> Quantum No 23 -> Quantum Total 23->Priority 0

No Priority 0 processes


So since adding while loop it changed from reading in the first line, to the last line.

So since adding while loop it changed from reading in the first line, to the last line.

Your code is only set up to handle "one line"...

It seems to me that you want to be reading the file data into an array of structures rather than simply a single "struct" of ProcessID, Quantum, Priority. And then when you have all of the data from the file stored in this array of structures, then you go forward with the rest of the handling.

[edit]I'm not following your expected output very well since I'm not sure if you've mentioned what you want it to be. In my tinkering, though, I just read all the data into an array of structures and sort the array. A single pass through the array then shows me:

Process id 6, Quantum No 2, Priority 0
Process id 7, Quantum No 23, Priority 0
Process id 1, Quantum No 12, Priority 1
Process id 2, Quantum No 23, Priority 1
Process id 5, Quantum No 3, Priority 2
Process id 3, Quantum No 45, Priority 2
Process id 4, Quantum No 67, Priority 3

Edited 6 Years Ago by Dave Sinkula: n/a

The outcome i want is similar to what you have, except it prints the process id the number of times the quantum is

e.g.

Process id 7, Quantum No 23, Priority 0 // would print this 23 times
Process id 1, Quantum No 12, Priority 1 // would print this 12 times
Process id 2, Quantum No 23, Priority 1 // would print this 23 times
etc....

Im not familar with building a array of structure???

im guessing it something like

struct assign
{
int ProcessID];
int Quantum;
int Priority];
};

but then i do not now how to call this struct??

Edited 3 Years Ago by happygeek: fixed formatting

I dont now if im going down the right line to try and make the struct array
but this is what i have so far
(only top part of program, bottom half remains unchanged

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
# include <ctype.h>


 

typedef struct
{
int *ProcessID;
int *Quantum;
int *Priority;
} assign;


int main(int argc, char **argv)
{
	char filename[256];
	strcpy(filename, argv[1]);
	FILE *file = fopen(filename, "r");

	assign *x;

char line[20];

	if ( file )
	{
		LoadData(&x, &line);
		
		while (fgets(line, sizeof line, file) ) //reads in line at time
		{
			  sscanf(line, "Process%d %d %d",&x.ProcessID, &x.Quantum, &x.Priority) == 3; 	//assigns input to process id, quatum and priority//					
	     }		
	}
	else
	{
		printf("Error: Unable to open file"); //error
	}

Well the struct in post #5 was OK.

Then you need an array of them
Say struct assign myData[10]; In your code, you would have a loop counter, and assign values to say myData[loop].ProcessID = 0; // or value from file.

Im getting abit confussed, sorry.

How would i use loop to assign the process id etc... in the code

sscanf(line, "Process%d %d %d",&ProcessID, &Quantum, &Priority) == 3

??

Well the struct in post #5 was OK.

Then you need an array of them
Say struct assign myData[10]; In your code, you would have a loop counter, and assign values to say myData[loop].ProcessID = 0; // or value from file.

Im having trouble calling function / assigned them to the struct
i have this

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct assign
{
int ProcessID;
int Quantum;
int Priority;
};

 
int main(int argc, char **argv)
{
	char filename[256];
	strcpy(filename, argv[1]);
	FILE *file = fopen(filename, "r");
	
	

	

	if ( file )
	{
		char line[20];
struct assign myData[50];
		
		while (fgets(line, sizeof line, file) ) //reads in line at time
		{
			  sscanf(line, "Process%d %d %d",&myData.ProcessID, &myData.Quantum, &myData.Priority) == 3; 	//assigns input to process id, quatum and priority. myData. used to call from struct???					
	     }		
	}
	else
	{
		printf("Error: Unable to open file"); //error
	}

Yes, you're on the right track.

You should have covered arrays already, and how to use them.
You just need to think a bit more about how to combine the ideas.

Still lost,
tried a number of different options and non compile.

I think i understand now what i need to do i just need to do it.

be like

myData( loop round the while loop to access each line and on each line
get the ProcessID).ProcessID = whatever is in the file

i think anyway

ignore last working now :) just creating for loop and function to get sorted out :)

This article has been dead for over six months. Start a new discussion instead.