Operating System
Operating System
Submitted By Submitted To
NAME: DEV BHARDWAJ DR. DEVENDER KUMAR
Semester: 2
ASSOCIATE PROFESSSOR
Programme:
UNIVERSITY ROLL NO. Bachelor of Computer Applications
10. Write a C program to simulate the Best Fit contiguous memory allocation
technique.
11. Write a C program to simulate the First Fit contiguous memory allocation
technique.
Program code:
#include<stdio.h>
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
findavgTime(processes, n, burst_time);
return 0;
}
Output:
#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting Process Id.
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}
Output:
Program code:
#include <stdio.h>
void main() {
int i, processes, sum = 0, cnt = 0, y, q, wt = 0, tat = 0, at[10], bt[10], temp[10];
float avg_waitt, avg_turnat;
// Scheduling loop
for(sum = 0, i = 0; y != 0;) {
if(temp[i] <= q && temp[i] > 0) {
sum = sum + temp[i];
temp[i] = 0;
cnt = 1;
} else if(temp[i] > 0) {
temp[i] = temp[i] - q;
sum = sum + q;
}
if(i == processes - 1) {
i = 0;
} else if(at[i + 1] <= sum) {
i++;
} else {
i = 0;
}
}
Output:
//Input
Total number of processes in the system: 3
//Output