Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FCFS.c
- // First Come First Serve Scheduling Algorithm
- //
- // Created by Anthony Andrada on 9/27/16.
- //
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int size = 50;
- typedef int bool;
- #define true 1
- #define false 0
- struct Process
- {
- int arrival_time;
- int service_time;
- int priority;
- };
- int compare(const void * e1, const void * e2)
- {
- struct Process *x = (struct Process *)e1;
- struct Process *y = (struct Process *)e2;
- if (x->arrival_time < y->arrival_time){
- return -1;
- }
- else if (x->arrival_time > y->arrival_time){
- return 1;
- }
- else return 0;
- }
- void sort (struct Process x[])
- {
- qsort (x, size, sizeof(struct Process), compare);
- }
- void print (struct Process n[])
- {
- for (int i = 0; i < size; i++)
- {
- int x = n[i].arrival_time;
- int y = n[i].service_time;
- int z = n[i].priority;
- printf("Job: %d, AT: %d, ST: %d, P: %d\r\n",i, x, y, z);
- }
- }
- void schedule(struct Process x[])
- {
- struct Process* ptr = x;
- int job = 0;
- int start = 0;
- int finish = 0;
- bool isScheduled = false;
- float turnaround = 0;
- float wait = 0;
- float response = 0;
- for (int q = 0; q < 110; q++)
- {
- if (isScheduled == false && q > 99) break;
- printf("Quantum: %d. ", q);
- if (isScheduled == true && q == (start + ptr->service_time))
- {
- turnaround += (q - ptr->arrival_time);
- ptr++;
- printf("Finished Job %d. ", job);
- job++;
- isScheduled = false;
- if (q >= 99)
- {
- printf("\r\n");
- break;
- }
- finish = q;
- }
- else if (isScheduled == true)
- {
- printf("Working Job %d.", job);
- }
- if (isScheduled == false && q >= ptr->arrival_time)
- {
- response += (q - finish);
- wait += (q - ptr->arrival_time);
- printf("Starting Job %d. ", job);
- isScheduled = true;
- start = q;
- }
- printf("\r\n");
- }
- printf("\r\n");
- printf("Average turnaround time: %.2f", turnaround / job);
- printf("\r\n");
- printf("Average wait time: %.2f", wait / job);
- printf("\r\n");
- printf("Average response time: %.2f", response / job);
- printf("\r\n");
- printf("Thoroughput : %f", (float)job / 100);
- printf("\r\n");
- }
- int main()
- {
- int seed = time(NULL);
- srand(seed);
- struct Process queue[size];
- int t;
- for (int i = 0; i < size; i++)
- {
- queue[i].arrival_time = rand() % 100;
- t = rand() % 11;
- if (t == 0) t +=1;
- queue[i].service_time = t;
- t = rand() % 5;
- if (t == 0) t += 1;
- queue[i].priority = t;
- }
- sort(queue);
- printf("\r\n");
- print(queue);
- printf("\r\n");
- schedule(queue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement