Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- //#define INT_MAX 2147483647
- typedef struct job{
- int id;
- int at;
- int bt;
- int st;
- int ct;
- int wt;
- int tat;
- int pr;
- } job;
- void takeInput(job jobs[], int n, int choice, int k){
- int i;
- for(i=0; i < n; i++){
- jobs[i].id = i;
- if (k != 1){
- printf("Enter Arrival Time of Process %d: ", i);
- scanf("%d", &jobs[i].at);
- }
- else{
- jobs[i].at = 0;
- }
- printf("Enter Burst Time of Process %d: ", i);
- scanf("%d", &jobs[i].bt);
- if (choice == 5 || choice == 6){
- printf("Enter Priority of Process %d: ", i);
- scanf("%d", &jobs[i].pr);
- }
- printf("\n");
- }
- }
- void printOutput(job jobs[], int n, int choice){
- int i;
- printf("\nID\tAT\tBT");
- if (choice == 5){
- printf("\tPR");
- }
- printf("\tST\tCT\tWT\tTAT\tRT");
- for (i = 0; i < n; i++){
- printf("\n%d\t%d\t%d\t%d", jobs[i].id, jobs[i].at, jobs[i].bt, jobs[i].st-jobs[i].at);
- if (choice == 5){
- printf("\t%d", jobs[i].pr);
- }
- printf("\t%d\t%d\t%d\t%d" , jobs[i].st, jobs[i].ct, jobs[i].wt, jobs[i].tat);
- }
- float avgWT = 0;
- float avgTAT = 0;
- float avgResp = 0;
- for(i = 0; i < n; i++){
- avgWT += jobs[i].wt;
- avgTAT += jobs[i].tat;
- avgResp += jobs[i].st - jobs[i].at;
- }
- avgWT /= n;
- avgTAT /= n;
- avgResp /= n;
- printf("\nAverage Waiting Time = %f\nAverage Turnaround Time = %f\nAverage Response Time = %f", avgWT, avgTAT, avgResp);
- }
- void sortJobsAT(job jobs[], int n){
- int i, j;
- job t;
- for(i = 0; i < n-1; i++){
- for(j = i+1; j < n; j++){
- if (jobs[i].at > jobs[j].at){
- t = jobs[i];
- jobs[i] = jobs[j];
- jobs[j] = t;
- }
- }
- }
- }
- void sortJobsST(job jobs[], int n){
- int i, j;
- job t;
- for(i = 0; i < n-1; i++){
- for(j = i+1; j < n; j++){
- if (jobs[i].st > jobs[j].st){
- t = jobs[i];
- jobs[i] = jobs[j];
- jobs[j] = t;
- }
- }
- }
- }
- void sortJobsID(job jobs[], int n){
- int i, j;
- job t;
- for(i = 0; i < n-1; i++){
- for(j = i+1; j < n; j++){
- if (jobs[i].id > jobs[j].id){
- t = jobs[i];
- jobs[i] = jobs[j];
- jobs[j] = t;
- }
- }
- }
- }
- void calcFCFS(job jobs[], int n){
- jobs[0].st = jobs[0].at;
- jobs[0].ct = jobs[0].at + jobs[0].bt;
- jobs[0].wt = 0;
- jobs[0].tat = jobs[0].bt;
- int i;
- for (i = 1; i < n; i++){
- jobs[i].st = (jobs[i].at > jobs[i-1].ct) ? jobs[i].at : jobs[i-1].ct;
- //printf("\nAT[%d] CT[%d] | %d %d\n", i, i-1, jobs[i].at, jobs[i-1].ct);
- //printf("jobs[%d].st = %d\n", i, jobs[i].st);
- jobs[i].ct = jobs[i].st + jobs[i].bt;
- jobs[i].tat = jobs[i].ct - jobs[i].at;
- jobs[i].wt = jobs[i].tat - jobs[i].bt;
- }
- }
- void calcSJF(job jobs[], int n){
- int *rt = (int*)malloc(sizeof(int)*n);
- int i, j;
- for(i = 0; i < n; i++){
- rt[i] = jobs[i].bt;
- }
- int complete = 0, t = 0, minm = 2147483647;
- int shortest = 0;
- bool check = false;
- while (complete != n){
- for(j = 0; j < n; j++){
- if ((jobs[j].at <= t) && ((rt[j] < minm) && (rt[j] > 0))){
- minm = rt[j];
- shortest = j;
- check = true;
- }
- }
- if (check == false){
- t++;
- continue; //no process in ready queue
- }
- rt[shortest] = 0;
- jobs[shortest].st = t;
- jobs[shortest].ct = t + jobs[shortest].bt;
- jobs[shortest].wt = jobs[shortest].ct - jobs[shortest].bt - jobs[shortest].at;
- jobs[shortest].wt = (jobs[shortest].wt < 0 ) ? 0 : jobs[shortest].wt;
- jobs[shortest].tat = jobs[shortest].ct - jobs[shortest].at;
- complete++;
- check = false;
- minm = 2147483647;
- t+= jobs[shortest].bt;
- }
- sortJobsST(jobs, n);
- }
- void calcSRTF(job jobs[], int n){
- int *rt = (int*)malloc(sizeof(int)*n);
- int i, j;
- for(i = 0; i < n; i++){
- rt[i] = jobs[i].bt;
- }
- int complete = 0, t = 0, minm = 2147483647;
- int shortest = 0, finish_time;
- bool check = false;
- while (complete != n){
- for(j = 0; j < n; j++){
- if ((jobs[j].at <= t) && ((rt[j] < minm) && (rt[j] > 0))){
- minm = rt[j];
- shortest = j;
- check = true;
- }
- }
- if (check == false){
- t++;
- continue; //no process in ready queue
- }
- if (rt[shortest] == jobs[shortest].bt){
- jobs[shortest].st = t;
- }
- rt[shortest]--;
- minm = rt[shortest];
- if (minm == 0){
- minm = 2147483647;
- }
- if (rt[shortest] == 0){
- complete++;
- check = false;
- jobs[shortest].ct = t+1;
- jobs[shortest].wt = jobs[shortest].ct - jobs[shortest].bt - jobs[shortest].at;
- jobs[shortest].wt = (jobs[shortest].wt < 0 ) ? 0 : jobs[shortest].wt;
- jobs[shortest].tat = jobs[shortest].ct - jobs[shortest].at;
- }
- t++;
- }
- }
- bool notIn(int arr[], int size, int k){
- int i;
- bool check = true;
- for(i = 0; i < size; i++){
- if (arr[i] == k){
- check = false;
- break;
- }
- }
- return check;
- }
- void checkNew(job jobs[], int n, int t, int rt[], int queue[], int *k, int already){
- int j;
- for(j = 0; j < n; j++){
- if ( (notIn(queue, *k, j) && j != already) && ( (jobs[j].at <= t) && (rt[j] > 0) ) ){
- queue[*k] = j;
- (*k)++;
- }
- }
- }
- void calcRR(job jobs[], int n, int q){
- int *rt = (int*)malloc(sizeof(int)*n);
- int *queue = (int*)malloc(sizeof(int)*n);
- int i;
- for(i = 0; i < n; i++){
- rt[i] = jobs[i].bt;
- queue[i] = -1;
- }
- int t = 0;
- int complete = 0;
- int j;
- int k = 0; // elements in queue
- while(complete != n){
- checkNew(jobs, n, t, rt, queue, &k, -1);
- /*
- printf("t=%d, Stack = ", t);
- printArr(queue, k);
- */
- if(k == 0){
- t++;
- continue;
- }
- int temp = queue[0];
- int y;
- for(y = 0; y <= k-2; y++){
- queue[y] = queue[y+1];
- }
- k--;
- queue[k] = -1;
- if (rt[temp] == jobs[temp].bt){
- jobs[temp].st = t;
- }
- if (rt[temp] > q){
- rt[temp] -= q;
- t+=q;
- checkNew(jobs, n, t, rt, queue, &k, temp);
- queue[k] = temp;
- k++;
- //checkNew(jobs, n, t, rt, queue, &k);
- }
- else{
- t+= rt[temp];
- rt[temp] = 0;
- jobs[temp].ct = t;
- jobs[temp].tat = jobs[temp].ct - jobs[temp].at;
- jobs[temp].wt = jobs[temp].tat - jobs[temp].bt;
- complete++;
- }
- /*
- printf("t=%d, Stack = ", t);
- printArr(queue, k);
- */
- }
- }
- void calcPriority(job jobs[], int n){
- int *rt = (int*)malloc(sizeof(int)*n);
- int i, j;
- for(i = 0; i < n; i++){
- rt[i] = jobs[i].bt;
- }
- int complete = 0, t = 0, minm = 2147483647;
- int mostImportant = 0, finish_time;
- bool check = false;
- while (complete != n){
- for(j = 0; j < n; j++){
- if ((jobs[j].at > t) || ((rt[j] == 0))){
- continue;
- }
- if (jobs[j].pr < minm){
- minm = jobs[j].pr;
- mostImportant = j;
- check = true;
- }
- }
- if (check == false){
- t++;
- continue; //no process in ready queue
- }
- //printf("\nt=%d, Process %d", t, mostImportant);
- if (rt[mostImportant] == jobs[mostImportant].bt){
- jobs[mostImportant].st = t;
- }
- rt[mostImportant]--;
- if (rt[mostImportant] == 0){
- complete++;
- check = false;
- minm = 2147483647;
- jobs[mostImportant].ct = t+1;
- jobs[mostImportant].wt = jobs[mostImportant].ct - jobs[mostImportant].bt - jobs[mostImportant].at;
- jobs[mostImportant].wt = (jobs[mostImportant].wt < 0 ) ? 0 : jobs[mostImportant].wt;
- jobs[mostImportant].tat = jobs[mostImportant].ct - jobs[mostImportant].at;
- }
- t++;
- }
- }
- void all(job jobs[], int n, int q){
- printf("\nFCFS\n");
- calcFCFS(jobs, n);
- printOutput(jobs, n, 1);
- printf("\n");
- printf("\nSJF\n");
- calcSJF(jobs, n);
- printOutput(jobs, n, 2);
- printf("\n");
- printf("\nSRTF\n");
- calcSRTF(jobs, n);
- printOutput(jobs, n, 3);
- printf("\n");
- printf("\nRR\n");
- calcRR(jobs, n, q);
- printOutput(jobs, n, 4);
- printf("\n");
- printf("\nPriority\n");
- calcPriority(jobs, n);
- printOutput(jobs, n, 5);
- }
- int main(){
- int k;
- int choice;
- printf("\n\t1. FCFS Scheduling Algorithm\n");
- printf("\n\t2. SJF Scheduling Algorithm\n");
- printf("\n\t3. SRTF Scheduling Algorithm\n");
- printf("\n\t4. RR Scheduling Algorithm\n");
- printf("\n\t5. Priority Scheduling Algorithm\n");
- printf("\n\t6. All\n>>");
- scanf("%d", &choice);
- if (!(choice >= 1 && choice <=6)){
- printf("\nINVALID");
- exit(1);
- }
- printf("\n1. Same Arrival time (0)");
- printf("\n2. Different Arrival Times\n>>");
- scanf("%d", &k);
- if (!(k == 1) && !(k == 2)){
- printf("\nINVALID");
- exit(1);
- }
- int n;
- printf("Enter number of processes: ");
- scanf("%d", &n);
- job *jobs;
- jobs = (job*)malloc(sizeof(job)*n);
- int quantum;
- if (choice == 4 || choice == 6){
- printf("\nEnter Time Quantum: ");
- scanf("%d", &quantum);
- }
- takeInput(jobs, n, choice, k);
- sortJobsAT(jobs, n);
- switch (choice)
- {
- case 1:
- calcFCFS(jobs, n);
- printOutput(jobs, n, choice);
- break;
- case 2:
- calcSJF(jobs, n);
- printOutput(jobs, n, choice);
- break;
- case 3:
- calcSRTF(jobs, n);
- printOutput(jobs, n, choice);
- break;
- case 4:
- calcRR(jobs, n, quantum);
- printOutput(jobs, n, choice);
- break;
- case 5:
- calcPriority(jobs, n);
- printOutput(jobs, n, choice);
- break;
- case 6:
- all(jobs, n, quantum);
- break;
- default:
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement