简体   繁体   中英

Calculating Wait Time for a First Come First Serve in ANSI C

While programming a CPU Scheduling in ANSI C for a linux environment, I have not been able to calculate the wait time for each queued process. Basically, I need to run 20+ processes with 4 cpus, and measure the time it takes for each process, average time it takes, CPU consumption, etc.

Here is an analogy for the process that I am trying to code:

Click me for the image

The image I drew was trying to simplify my problems... but this is a first come first serve CPU schedualer, counter A is the CPU burst, counter B is the Device Burst, line 1 is Ready Queue, line 2 is Device Queue...

Till now, I'm able to output the tasks into an array, but for my assignment I need to find out how long it took for each of the people to wait, the turn around time for each processes, and their average times.

Each person struct have an "arrivalTime" and can be changed anytime Each person struct also have an array of time required at each counter "timeRequired[]", it's alternating, index 0 is for counter A, index 1 is for counter B, index 2 is for A again. Each person struct have an array pointer for the timeRequired[], "arrayPointer"

The picture I drew says seconds, but it is actually any unit of time, an integer number, I don't really need the actual time clock...

What I did so far is I created 2 thread function, 1 for counterA and 1 for counterB, and when ever A finished serving a person in line, A will stuff the person to B line, when ever B finished a person, it will stuff it into the A line.

I feel like everything is there for me already, but I really dont know what I need to do from here on to calculated their wait times...

Or is there a easier solution than using threads?

The professor gave us 2 files

the helper methods

[ http://pastebin.com/qF7nQsUR]

the header file

[ http://pastebin.com/nQQNXnmq]

gettimeofday(2).

somewhere inside your process' struct

struct timeval bornon;    /* time this process was born */

and then call gettimeofday on it in your allocation path

if (gettimeofday(&process->bornon, NULL) == -1)
    err(1, "ohnoes! gettimeofday!");

and then call it again when your process is done

struct timeval doneon;
int secs, usecs;

gettimeofday(&doneon, NULL);
secs = doneon.tv_secs - process->bornon.tv_secs;
usecs = doneon.tv_usecs - process->bornon.tv_usecs;

and then do conversions for seconds/microseconds.

I'd approach it this way:

Figure out how to encode the input. The program needs to have the input "person 1 needs..." available to it in some way.
These definitions can work:

struct need {
   int person_id, counter_id, duration
};
struct need needs[] = {
   { 1, COUNTER_A, 500 },
   { 1, COUNTER_B, 200 },
...
};

Next, you need to keep track of where each person is at any time.
This structure can describe what a person's doing now:

struct person_activity {
    int person_id;
    struct need *activity;
    int start_time;
}

An array of these can describe what each is doing at any tie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM