fcfs.c
50 linesc
DOWNLOAD
1// Aim: Program to perform FCFS operations (FCFS).
2#include<stdio.h>
3#include<stdlib.h>
4
5struct process{
6       int process_id,at,bt,tat,wt,ct;       
7       };
8int main()
9{
10    struct process p[100], temp;
11    int n, i, j, ct;
12    printf("Enter the total number of processes: ");
13    scanf("%d", &n);
14    for (i = 0; i < n; i++) {
15        p[i].process_id = i + 1;
16        printf("Enter the arrival time of process %d: ", i + 1);
17        scanf("%d", &p[i].at);
18        printf("Enter the burst time of process %d: ", i + 1);
19        scanf("%d", &p[i].bt);
20    }
21
22    for (i = 0; i < n - 1; i++) {
23        for (j = 0; j < n - i - 1; j++) {
24            if (p[j].at > p[j + 1].at) {
25                temp = p[j];
26                p[j] = p[j + 1];
27                p[j + 1] = temp;
28            }
29        }
30    }
31    p[0].ct = p[0].at + p[0].bt;
32    p[0].tat = p[0].ct - p[0].at;
33    p[0].wt = p[0].tat - p[0].bt;
34
35    for (i = 1; i < n; i++) {
36        ct = p[i - 1].ct;
37        if (p[i].at > ct) {
38            p[i].ct = p[i].at + p[i].bt;
39        } else {
40            p[i].ct = ct + p[i].bt;
41        }
42        p[i].tat = p[i].ct - p[i].at;
43        p[i].wt = p[i].tat - p[i].bt;
44    }
45    printf("Process id\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n");
46    for (i = 0; i < n; i++) {
47        printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].process_id, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
48    }
49    return 0;
50}