Skip to content

Instantly share code, notes, and snippets.

@lakshyaraj2006
Created February 17, 2026 11:01
Show Gist options
  • Select an option

  • Save lakshyaraj2006/40dfcad7ba3868ab07d871c036fdb900 to your computer and use it in GitHub Desktop.

Select an option

Save lakshyaraj2006/40dfcad7ba3868ab07d871c036fdb900 to your computer and use it in GitHub Desktop.
This is the C language implementation for First Come First Serve (FCFS) Scheduling policy. This gist shows the array & linked list implementation of the scheduling policy.
#include<stdio.h>
#include<stdlib.h>
void bubbleSort(int* AT, int* BT, int n)
{
int isSorted;
for (int i = 0; i < n - 1; i++)
{
isSorted = 1;
for (int j = 0; j < n - i - 1; j++)
{
if (AT[j] > AT[j + 1] ||
(AT[j] == AT[j + 1] && BT[j] > BT[j + 1]))
{
int temp = AT[j];
AT[j] = AT[j + 1];
AT[j + 1] = temp;
temp = BT[j];
BT[j] = BT[j + 1];
BT[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted)
return;
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
// Same arrival times
// Initialize arrival times to 0
// int* AT = (int *)calloc(n, sizeof(int));
// Different arrival times
// Initialize arrival times
int* AT = (int *)malloc(n * sizeof(int));
printf("Enter the arrival times for processes: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &AT[i]);
}
// Initialize burst times array
int* BT = (int *)malloc(n * sizeof(int));
printf("Enter the burst time for each process: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &BT[i]);
}
bubbleSort(AT, BT, n);
// Initialize Waiting Times, Completetion Times & Turn Around Times to 0
int* WT = (int *)calloc(n, sizeof(int));
int* CT = (int *)calloc(n, sizeof(int));
int* TAT = (int *)calloc(n, sizeof(int));
// Compute completion times
CT[0] = AT[0] + BT[0];
for (int i = 1; i < n; i++)
{
// CPU remains idle, if previous process is completed before next process arrives
if (CT[i-1] < AT[i]) {
CT[i] = AT[i] + BT[i];
} else {
CT[i] = CT[i-1] + BT[i];
}
}
// Compute turn around times
for (int i = 0; i < n; i++)
{
TAT[i] = CT[i] - AT[i];
}
// Compute waiting times
for (int i = 0; i < n; i++)
{
WT[i] = TAT[i] - BT[i];
}
// Compute total waiting & turn around times
double sumWT = 0, sumTAT = 0;
for (int i = 0; i < n; i++)
{
sumTAT += TAT[i];
sumWT += WT[i];
}
// Compute average waiting & turn around times
double avgWT = sumWT / (double) n;
double avgTAT = sumTAT / (double) n;
printf("Avg. Waiting Time: %lf\n", avgWT);
printf("Avg. Turn Around Time: %lf\n", avgTAT);
free(AT);
free(BT);
free(WT);
free(CT);
free(TAT);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
int at;
int bt;
struct ListNode* next;
};
void bubbleSort(struct ListNode* head)
{
if (head == NULL)
return;
int swapped;
struct ListNode* ptr1;
struct ListNode* lptr = NULL;
do {
swapped = 0;
ptr1 = head;
while (ptr1->next != lptr) {
if (ptr1->at > ptr1->next->at ||
(ptr1->at == ptr1->next->at &&
ptr1->bt > ptr1->next->bt)) {
int temp;
temp = ptr1->at;
ptr1->at = ptr1->next->at;
ptr1->next->at = temp;
temp = ptr1->bt;
ptr1->bt = ptr1->next->bt;
ptr1->next->bt = temp;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct ListNode *head = NULL, *temp = NULL, *newNode = NULL;
// Same arrival times
// for (int i = 0; i < n; i++)
// {
// newNode->at = 0;
// }
// Different arrival times
// Initialize arrival times
printf("Enter the arrival times for processes: ");
for (int i = 0; i < n; i++)
{
newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &newNode->at);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
temp = head;
} else {
temp->next = newNode;
temp = newNode;
}
}
printf("Enter the burst time for each process: ");
temp = head;
for (int i = 0; i < n; i++)
{
scanf("%d", &temp->bt);
temp = temp->next;
}
bubbleSort(head);
// Initialize Waiting Times, Completetion Times & Turn Around Times to 0
int* WT = (int *)calloc(n, sizeof(int));
int* CT = (int *)calloc(n, sizeof(int));
int* TAT = (int *)calloc(n, sizeof(int));
temp = head;
// Compute completion times
CT[0] = temp->at + temp->bt;
TAT[0] = CT[0] - temp->at;
WT[0] = TAT[0] - temp->bt;
temp = temp->next;
for (int i = 1; i < n; i++)
{
// CPU remains idle, if previous process is completed before next process arrives
if (CT[i-1] < temp->at) {
CT[i] = temp->at + temp->bt;
} else {
CT[i] = CT[i-1] + temp->bt;
}
TAT[i] = CT[i] - temp->at;
WT[i] = TAT[i] - temp->bt;
temp = temp->next;
}
// Compute total waiting & turn around times
double sumWT = 0, sumTAT = 0;
for (int i = 0; i < n; i++)
{
sumTAT += TAT[i];
sumWT += WT[i];
}
// Compute average waiting & turn around times
double avgWT = sumWT / (double) n;
double avgTAT = sumTAT / (double) n;
printf("Avg. Waiting Time: %lf\n", avgWT);
printf("Avg. Turn Around Time: %lf\n", avgTAT);
free(WT);
free(CT);
free(TAT);
temp = head;
while (temp != NULL) {
struct ListNode* next = temp->next;
free(temp);
temp = next;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment