Sunday, September 10, 2017

Program to implement FCFS CPU Scheduling Algorithm in C

#include<stdio.h>
struct fcfs
{
    int at;
    int bt;
    int st;
    int ct;
    int wt;
    int tt;
};

int main()
{
    int n;
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("* * * * * * * FCFS CPU Scheduling Algorithm * * * * * * * *\n");
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    int i, tot_wt=0,tot_tt=0,avgwt=0,avgtt=0;
    printf("How many processes you want to run: ");
    scanf("%d",&n);
    struct fcfs p[n];
    for(i=0; i<n; i++)
    {
        printf("\tEnter AT of Process %d:",i+1);
        scanf("%d",&p[i].at);
        printf("\tEnter BT of Process %d:",i+1);
        scanf("%d",&p[i].bt);
    }
    //Schedule Time and Completion Time Calculation  
    p[0].st=p[0].at;
    p[0].ct=p[0].st+p[0].bt;
    for(i=1; i<n; i++)
    {
        p[i].st=p[i-1].ct;
        p[i].ct=p[i].st+p[i].bt;
    }
    //Turn Around Time Calculation
    for(i=0; i<n; i++)
    {
        p[i].tt=p[i].ct-p[i].at;
        tot_tt=tot_tt+p[i].tt;//Total TAT
    }
    //Waiting Time Calculation
    for(i=0; i<n; i++)
    {
        p[i].wt=p[i].tt-p[i].bt;
        tot_wt=tot_wt+p[i].wt;//Total WT
    }
  
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("\tAT\tBT\tST\tCT\tWT\tTAT\n");
    printf("\t__\t__\t__\t__\t__\t__\n");
    for(i=0; i<n; i++)
    {
        printf("\t%d\t%d\t%d\t%d\t%d\t%d\n",p[i].at,p[i].bt,p[i].st,p[i].ct,p[i].wt,p[i].tt);
    }
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    //Average Waiting Time Calculation
    avgwt=tot_wt/n;
    printf("\tTotal Waiting Time = %d\n",tot_wt);
    printf("\tAverage Waiting Time = %d\n",avgwt);
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    avgtt=tot_tt/n;
    printf("\tTotal Turn Around Time = %d\n",tot_tt);
    printf("\tAverag Turn Around Time = %d\n",avgtt);
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
    printf("* * * * * * * End FCFS CPU Scheduling Algorithm * * * * * *\n");
    printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

OutPut:
manoj@ubuntu:~/os$ gcc fcfs.c -o fcfs
manoj@ubuntu:~/os$ ./fcfs

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* * * * * * * FCFS CPU Scheduling Algorithm * * * * * * * *
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
How many processes you want to run: 5
    Enter AT of Process 1:1
    Enter BT of Process 1:2
    Enter AT of Process 2:2
    Enter BT of Process 2:3
    Enter AT of Process 3:3
    Enter BT of Process 3:4
    Enter AT of Process 4:4
    Enter BT of Process 4:5
    Enter AT of Process 5:5
    Enter BT of Process 5:6
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    AT    BT    ST    CT    WT    TAT
    __    __    __    __    __    __
    1    2    1    3    0    2
    2    3    3    6    1    4
    3    4    6    10    3    7
    4    5    10    15    6    11
    5    6    15    21    10    16
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    Total Waiting Time = 20
    Average Waiting Time = 4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    Total Turn Around Time = 40
    Averag Turn Around Time = 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* * * * * * * End FCFS CPU Scheduling Algorithm * * * * * *
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~/os$

No comments:

Post a Comment