FCFS Scheduling Program in C

FCFS Scheduling Program in C


First Come First Serve FCFS Scheduling Algorithm Program Code in C++ with Gantt Chart

Operating System 14,924 Views

First Come First Serve FCFS Scheduling Algorithm Program Code in C and C++ with Gantt Chart .

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

#include <bits/stdc++.h>

using namespace std;

 

const int N=100005;

 

int n;

struct process

{

    int id;

    int burst_time;

    int arrival_time;

    int waiting_time;

    int finishing_time;

    int turn_around_time;

};

process P[N];

 

 

void FCFS()

{

    double total_waiting_time = 0.0;

    double total_turn_around_time = 0.0;

    for(int i=0; i<n; i++)

    {

        P[i].finishing_time = P[i-1].finishing_time + P[i].burst_time;

        P[i].turn_around_time = P[i].finishing_time - P[i].arrival_time;

        P[i].waiting_time = P[i].turn_around_time - P[i].burst_time;

 

        total_waiting_time += P[i].waiting_time;

        total_turn_around_time += P[i].turn_around_time;

    }

    cout<<fixed<<setprecision(2);

    cout<<"Average Waiting Time: "<<(total_waiting_time/n)<<"\n";

    cout<<"Average Turn Around Time: "<<(total_turn_around_time/n)<<"\n";

    return;

}

 

 

int main()

{

    cout<<"Number of Processes: ";

    cin>>n;

 

    cout<<"Process Ids:\n";

    for(int i=0; i<n; i++) cin>>P[i].id;

 

    cout<<"Process Burst Times:\n";

    for(int i=0; i<n; i++) cin>>P[i].burst_time;

 

    cout<<"Process Arrival Times:\n";

    for(int i=0; i<n; i++) cin>>P[i].arrival_time;

 

    FCFS();

 

    return 0;

}

/**

3

1 2 3

24 3 3

0 0 0

*/

In C Program Code
First Come First Serve Scheduling Algorithm in C with Gantt Chart

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

#include <stdio.h>

#include <stdlib.h>

 

#define MAX 100

 

typedef struct

{

    int pid;

    int burst_time;

    int waiting_time;

    int turnaround_time;

} Process;

 

void print_table(Process p[], int n);

void print_gantt_chart(Process p[], int n);

 

int main()

{

    Process p[MAX];

    int i, j, n;

    int sum_waiting_time = 0, sum_turnaround_time;

    printf("Enter total number of process: ");

    scanf("%d", &n);

    printf("Enter burst time for each process:\n");

    for(i=0; i<n; i++) {

        p[i].pid = i+1;

        printf("P[%d] : ", i+1);

        scanf("%d", &p[i].burst_time);

        p[i].waiting_time = p[i].turnaround_time = 0;

    }

 

    // calculate waiting time and turnaround time

    p[0].turnaround_time = p[0].burst_time;

 

    for(i=1; i<n; i++) {

        p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;

        p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;

    }

 

    // calculate sum of waiting time and sum of turnaround time

    for(i=0; i<n; i++) {

            sum_waiting_time += p[i].waiting_time;

            sum_turnaround_time += p[i].turnaround_time;

    }

 

    // print table

    puts(""); // Empty line

    print_table(p, n);

    puts(""); // Empty Line

    printf("Total Waiting Time      : %-2d\n", sum_waiting_time);

    printf("Average Waiting Time    : %-2.2lf\n", (double)sum_waiting_time / (double) n);

    printf("Total Turnaround Time   : %-2d\n", sum_turnaround_time);

    printf("Average Turnaround Time : %-2.2lf\n", (double)sum_turnaround_time / (double) n);

 

    // print Gantt chart

    puts(""); // Empty line

    puts("          GANTT CHART          ");

    puts("          ***********          ");

    print_gantt_chart(p, n);

    return 0;

}

 

 

void print_table(Process p[], int n)

{

    int i;

 

    puts("+-----+------------+--------------+-----------------+");

    puts("| PID | Burst Time | Waiting Time | Turnaround Time |");

    puts("+-----+------------+--------------+-----------------+");

 

    for(i=0; i<n; i++) {

        printf("| %2d  |     %2d     |      %2d      |        %2d       |\n"

               , p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time );

        puts("+-----+------------+--------------+-----------------+");