0

I have an assignment to create a CPU scheduler by reading in process information from an input file like this:

q 1 tq 4 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1
q 2 tq 5 p1 1 p2 2 p3 2 p4 9 p5 8 p6 5 p7 12 p8 11 p9 15 p10 1 p11 4 p12 8 p13 22 p14 21 p15 30
q 3 tq 30 p1 30 p2 10 p3 24 p4 20 p5 17 p6 4 p7 7 p8 11 p9 8 p10 9 p11 5 p12 6 p13 3 p14 2 p15 1

where q x is "ready queue x", tq y is time quantum y and the rest in the line is in the format pv w, where pv is p1, p2, p3,..., p15 and are the process names and w is the CPU burst time. each queue are to be read from the same input file and stored in separate arrays of struct that contains the time quantum, process name and the CPU burst time:

struct process{
       char name[4];
       int cputime;
       int timequantum;
};

How would I accomplish this? I should mention that I have only just started with this C course and have never been taught how to read input files in C.

Pengibaby
  • 373
  • 2
  • 11
  • What have you tried? How did your attempt work or not work? Where's the [mcve] of your attempt? And please refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 04 '19 at 17:19
  • What have you tried? Where are you running into problems? It looks like you need to read the lines with `fgets()` and then carefully scan the line [using `sscanf()` in a loop](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops). When you've got a concrete problem (other than the absence of an element name for your `char[3];` line – should that be `char procid[3];` (though it needs to be 4 to be safe for `p12` etc)), then we can help. – Jonathan Leffler Oct 04 '19 at 17:20
  • @Someprogrammerdude Thank you for your response. I have only started to study C, and we have not been taught how to read input files at all, which is why I am so confused at the assignment and why they asked us to read input files. So I have no idea where to start. – Pengibaby Oct 04 '19 at 17:22
  • Start by writing the hello world program. Solve the problem without reading the file (just hard code the data). You will learn as you go. – john elemans Oct 04 '19 at 17:24
  • 1
    @johnelemans yep, I have just created 3 arrays of structs with hard coded data, so that i can at least write the rest of the program. It's just the reading input file I am stuck on at the moment. – Pengibaby Oct 04 '19 at 17:27
  • Maybe you should show (a subset of) the code you use to hard-code the arrays. You must have some extra structure, even if it is just three named arrays (possibly dynamically allocated with `malloc()`?) to hold the three queues worth of data. Are you sure you only ever have to worry about 3 queues? Note that the time quantum is a property of the queue more than the processes within the queue. – Jonathan Leffler Oct 05 '19 at 00:03
  • @Pengibaby "_I have just created 3 arrays of structs with hard coded data_". A good first step; well done. Now back-up a step and hard-code some strings (or, possibly, an array of strings) that hold the three lines of data, one string per line (e.g. `char* inp[3]; inp[0] = "q 1 tq 4 p1 30 p2 10 p3...";`. Then read up (by searching the web) on how `sscanf()` works, and explore ways of converting each string in turn into the arrays of structures you already have. – TripeHound Oct 07 '19 at 08:07

0 Answers0