0% found this document useful (0 votes)
70 views31 pages

Os Practical Exam

Uploaded by

ANJALI MODI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views31 pages

Os Practical Exam

Uploaded by

ANJALI MODI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

OS PRACTICAL EXAM

PROGRAM 1

//=============Best Fit Memory Allocation===========//


#include<stdio.h>
#include<conio.h>
int main() {
clrscr();
// Declare Variables
int nb,blockSize[100],n,jobSize[100],i,j,alloc[100],avail[100],min;

//Input initial values


printf("Enter the number of available memory blocks: ");
scanf("%d",&nb);
printf("Enter the size of each memory block -> \n");
for(i=0;i<nb;i++) {
printf("Size of block%d: ",i+1);
scanf("%d",&blockSize[i]);
}

printf("Enter the number of processes: ");


scanf("%d",&n);
printf("Enter the size of each process -> \n");
for(i=0;i<n;i++) {
printf("Size of process%d: ",i+1);
scanf("%d",&jobSize[i]);

// Initialize alloc vector to -1 and avail to 99999


for(i=0;i<n;i++) {
alloc[i]=-1;
}

for(i=0;i<nb;i++){
avail[i]=9999;
}

// Check for each process the blocks available


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

if(blockSize[j]>jobSize[i]){
avail[j]=blockSize[j]-jobSize[i];
}
}

min=0;
for(j=0;j<nb;j++) {
if(avail[min]>avail[j]) {
min=j;
}
}

alloc[i]=min;
if(avail[min]>=9999){
alloc[i]=-1;
}

blockSize[min]=-1;

// Initialize avail to 99999


for(j=0;j<n;j++) {
avail[j]=9999;
}
}

// Print the results


printf("Process P of {size} is allocated to block \n");
for(i=0;i<n;i++) {
if(alloc[i]!=-1)
printf("Process %d of %d --> Block %d\n",i+1,jobSize[i],alloc[i]+1);
else
printf("Process %d of %d --> is not allocated \n",i+1,jobSize[i]);
getch();
}
}
PROGRAM 2

//==========Worst fit memory allocation==========//


#include <stdio.h>
#include <conio.h>

// Function for worst-fit memory allocation


void worstFit(int blockSize[], int m, int processSize[], int n) {

int allocation[20]; // Adjust the size if needed


int i, j;

// Initialize allocation array with -1 meaning unallocated.


for (i = 0; i < n; i++) {
allocation[i] = -1;
}

// Loop through each process to find a suitable block


for (i = 0; i < n; i++) {
int worstIdx = -1;

// Find the worst fit block for the current process


for (j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {
worstIdx = j;
}
}
}

// If a suitable block was found


if (worstIdx != -1) {
allocation[i] = worstIdx; // Allocate block j to process i
blockSize[worstIdx] -= processSize[i]; // Reduce the block size
}
}

// Display allocation results


printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

int main() {
int blockSize[20], processSize[20]; // Arrays to store block and process sizes
int m, n, i;

clrscr();

printf("Enter the number of blocks: ");


scanf("%d", &m);
printf("Enter the sizes of the blocks:\n");
for (i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}

printf("\nEnter the number of processes: ");


scanf("%d", &n);
printf("Enter the sizes of the processes:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}

worstFit(blockSize, m, processSize, n);

getch();
return 0;
}

PROGRAM 3

//==========First fit memory allocation==========//


#include<stdio.h>

int main() {

// Declare Variables
int nb,blockSize[100],n,jobSize[100],i,j,alloc[100];

//Input initial values


printf("Enter the number of available memory blocks: ");
scanf("%d",&nb);
printf("Enter the size of each memory block -> \n");
for(i=0;i<nb;i++) {
printf("Size of block%d: ",i+1);
scanf("%d",&blockSize[i]);
}
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter the size of each process -> \n");
for(i=0;i<n;i++) {
printf("Size of process%d: ",i+1);
scanf("%d",&jobSize[i]);
}

// Put initial values in alloc vector


for(i=0;i<n;i++) {
alloc[i] = -1;
}

// Allocate the processes


for(i=0;i<n;i++){
for(j=0;j<nb;j++) {
if(blockSize[j]>jobSize[i]) {
alloc[i]=j;
j=nb;
blockSize[j]=0;
}
}
}

// Print the results


printf("Process P of {size} is allocated to block \n");
for(i=0;i<n;i++) {
if(alloc[i]!=-1)
printf("Process %d of %d --> Block %d\n",i+1,jobSize[i],alloc[i]+1);
else
printf("Process %d of %d --> is not allocated \n",i+1,jobSize[i]);
}}
PROGRAM 4

//==========LOOK Disk Scheduling==========//


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
clrscr();
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);

// logic for look disk scheduling


/*logic for sort the request array */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
} }

int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}

// if movement is towards high value


if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}

// if movement is towards low value


else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is %d",TotalHeadMoment);
getch();
return 0;
}
PROGRAM 5

//==========C-LOOK Disk Scheduling==========//


#include<stdio.h>
#include<conio.h>

int absoluteValue(int);

void main()
{
clrscr();
int queue[25],n,headposition,i,j,k,seek=0, maxrange,
difference,temp,queue1[20],queue2[20],temp1=0,temp2=0;
float averageSeekTime;

// Reading the maximum Range of the Disk.


printf("Enter the maximum range of Disk: ");
scanf("%d",&maxrange);

// Reading the number of Queue Requests(Disk access requests)


printf("Enter the number of queue requests: ");
scanf("%d",&n);

// Reading the initial head position.


//(ie. the starting point of execution)
printf("Enter the initial head position: ");
scanf("%d",&headposition);
// Reading disk positions to be read in the order of arrival
printf("Enter the disk positions to be read(queue): ");
for(i=1;i<=n;i++) // Note that i varies from 1 to n instead of 0 to n-1
{
scanf("%d",&temp); //Reading position value to a temporary variable

//Now if the requested position is greater than current headposition,


//then pushing that to array queue1
if(temp>headposition)
{
queue1[temp1]=temp; //temp1 is the index variable of queue1[]
temp1++; //incrementing temp1
}
else //else if temp < current headposition,then push to array queue2[]
{
queue2[temp2]=temp; //temp2 is the index variable of queue2[]
temp2++;
}
}

//Now we have to sort the two arrays


//SORTING array queue1[] in ascending order
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}

//SORTING array queue2[] in descending order


for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}}

//Copying first array queue1[] into queue[]


for(i=1,j=0;j<temp1;i++,j++)
{
queue[i]=queue1[j];
}

//Setting queue[i] to maxrange because the head goes to


//end of disk and comes back in scan Algorithm
queue[i]=maxrange;

//Copying second array queue2[] after that first one is copied, into queue[]
for(i=temp1+2,j=0;j<temp2;i++,j++)
{
queue[i]=queue2[j];
}

//Setting queue[i] to 0. Because that is the innermost cylinder.


queue[i]=0;

//At this point, we have the queue[] with the requests in the
//correct order of execution as per scan algorithm.
//Now we have to set 0th index of queue[] to be the initial headposition.
queue[0]=headposition;

// Calculating SEEK TIME. seek is initially set to 0 in the declaration part.


for(j=0; j<=n; j++) //Loop starts from headposition. (ie. 0th index of queue)
{

// Finding the difference between next position and current position.


difference = absoluteValue(queue[j+1]-queue[j]);
// Adding difference to the current seek time value
seek = seek + difference;

// Displaying a message to show the movement of disk head


printf("Disk head moves from position %d to %d with Seek %d \n", queue[j], queue[j+1],
difference);
}

// Calculating Average Seek time


averageSeekTime = seek/(float)n;

//Display Total and Average Seek Time(s)


printf("Total Seek Time= %d\n", seek);
printf("Average Seek Time= %f\n", averageSeekTime);
getch();
}
// Defining function absoluteValue
int absoluteValue(int x)
{
if(x>0)
{
return x;
}
else
{
return x*-1;
}
}

PROGRAM 6
//==========SJF CPU SCHEDULING==========//
#include <stdio.h>
#include <conio.h>

int main()
{
clrscr();
// 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);
getch();
}

PROGRAM 7

//==========priority cpu scheduling==========//


#include <stdio.h>
#include <conio.h>

// Function for worst-fit memory allocation


void worstFit(int blockSize[], int m, int processSize[], int n) {

int allocation[20]; // Adjust the size if needed


int i, j;

// Initialize allocation array with -1 meaning unallocated.


for (i = 0; i < n; i++) {
allocation[i] = -1;
}

// Loop through each process to find a suitable block


for (i = 0; i < n; i++) {
int worstIdx = -1;

// Find the worst fit block for the current process


for (j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {
worstIdx = j;
}
}
}

// If a suitable block was found


if (worstIdx != -1) {
allocation[i] = worstIdx; // Allocate block j to process i
blockSize[worstIdx] -= processSize[i]; // Reduce the block size
}
}

// Display allocation results


printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

int main() {
int blockSize[20], processSize[20]; // Arrays to store block and process sizes
int m, n, i;

clrscr();

printf("Enter the number of blocks: ");


scanf("%d", &m);
printf("Enter the sizes of the blocks:\n");
for (i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}

printf("\nEnter the number of processes: ");


scanf("%d", &n);
printf("Enter the sizes of the processes:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}

worstFit(blockSize, m, processSize, n);

getch();
return 0;
}
PROGRAM 8

//==========ROUND ROBIN CPU SCHEDULING==========//


#include<stdio.h>
#include<conio.h>
#define MAX 100 // Define a maximum size for arrays

int main() {
clrscr();
int n, i, time_slot, x, total, counter, wait_time, ta_time;
int arr_time[MAX], burst_time[MAX], temp_burst_time[MAX];
float average_wait_time, average_turnaround_time;

printf("Enter Total Number of Processes: ");


scanf("%d", &n);

x = n; // Number of processes to execute


wait_time = 0;
ta_time = 0;

// Input process details


for (i = 0; i < n; i++) {
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i]; // Copy burst time
}

// Input time slot


printf("Enter Time Slot: ");
scanf("%d", &time_slot);

// Initialize variables for the round-robin algorithm


total = 0;
counter = 0;

printf("Process ID Burst Time Turnaround Time Waiting Time\n");

i = 0; // Initialize index for process iteration


while (x != 0) {
// Check if the process can finish in the given time slot
if (temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0) {
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter = 1;
} else if (temp_burst_time[i] > 0) {
temp_burst_time[i] -= time_slot;
total += time_slot;
}

// If the process is completed


if (temp_burst_time[i] == 0 && counter == 1) {
x--; // Decrement the remaining process count
printf("\nProcess No %d \t\t %d\t\t %d\t\t %d", i + 1, burst_time[i],
total - arr_time[i], total - arr_time[i] - burst_time[i]);
wait_time += total - arr_time[i] - burst_time[i];
ta_time += total - arr_time[i];
counter = 0;
}

// Move to the next process


if (i == n - 1) {
i = 0;
} else if (arr_time[i + 1] <= total) {
i++;
} else {
i = 0;
}
}

// Calculate average wait time and turnaround time


average_wait_time = (float)wait_time / n;
average_turnaround_time = (float)ta_time / n;

printf("\nAverage Waiting Time: %f", average_wait_time);


printf("\nAverage Turnaround Time: %f", average_turnaround_time);
getch();
return 0;
}

PROGRAM 9

//==========FCFS CPU SCHEDULING==========//


#include <stdio.h>
#include<conio.h>
#define MAX 100 // Maximum number of processes

// Function to find the waiting time for all processes


void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
wt[0] = 0; // Waiting time for the first process is 0

// Calculating waiting time for all other processes


for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
}

// Function to calculate turn around time


void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}

// Function to calculate average time


void findAvgTime(int processes[], int n, int bt[]) {
int wt[MAX], tat[MAX];
int total_wt = 0, total_tat = 0;
int i;

// Calculate waiting time for all processes


findWaitingTime(processes, n, bt, wt);

// Calculate turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with their burst time, waiting time, and turn around time
printf("\nProcesses Burst Time Waiting Time Turn Around Time\n");
for (i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d %d %d %d\n", processes[i], bt[i], wt[i], tat[i]);
}

// Calculate and display average waiting and turn around times


printf("\nAverage waiting time = %.2f", (float)total_wt / n);
printf("\nAverage turn around time = %.2f\n", (float)total_tat / n);
}
// Driver code
int main() {
clrscr();
int processes[MAX];
int bt[MAX];
int n, i;

printf("Enter the number of processes: ");


scanf("%d", &n);

// Input process IDs


for (i = 0; i < n; i++) {
processes[i] = i + 1; // Process IDs are 1, 2, 3, ...
}

// Input burst times for all processes


for (i = 0; i < n; i++) {
printf("Enter Burst Time for Process %d: ", i + 1);
scanf("%d", &bt[i]);
}

// Call the function to calculate average time


findAvgTime(processes, n, bt);
getch();
return 0;
}

PROGRAM 10
//==========FCFS DISK SCHEDULING==========//
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>

#define MAX_REQUESTS 100 // Define a maximum size for the requests array

void fcfsDiskScheduling(int requests[], int n, int head) {


int totalSeekTime = 0; // Total seek time
int currentPosition = head;
int i;

printf("\nSeek Sequence: ");


for (i = 0; i < n; i++) {
printf("%d -> ", currentPosition); // Display the current head position
totalSeekTime += abs(requests[i] - currentPosition); // Calculate seek time
currentPosition = requests[i]; // Move the head to the next request
}
printf("%d\n", currentPosition); // Display the final head position

printf("\nTotal Seek Time: %d\n", totalSeekTime);


printf("Average Seek Time: %.2f\n", (float)totalSeekTime / n);
}

int main() {
clrscr();
int n, i, head;

// Input the number of disk requests


printf("Enter the number of disk requests (max %d): ", MAX_REQUESTS);
scanf("%d", &n);

if (n > MAX_REQUESTS) {
printf("Error: Number of requests exceeds the maximum limit of %d.\n", MAX_REQUESTS);
return 1; // Exit with error
}

int requests[MAX_REQUESTS]; // Define array with fixed size

// Input the disk requests


printf("Enter the disk requests:\n");
for (i = 0; i < n; i++) {
printf("Request %d: ", i + 1);
scanf("%d", &requests[i]);
}

// Input the initial position of the disk head


printf("Enter the initial position of the disk head: ");
scanf("%d", &head);

// Call the FCFS Disk Scheduling function


fcfsDiskScheduling(requests, n, head);
getch();
return 0;
}

PROGRAM 11

//==========SSTF DISK SCHEDULING==========//


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
clrscr();
int RQ[100],i,n,TotalHeadMovement=0,initial,count=0;
printf("Enter the number of requests: ");
scanf("%d",&n);
printf("Enter the rquests sequence: ");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position: ");
scanf("%d",&initial);
while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}

TotalHeadMovement=TotalHeadMovement+min;
initial=RQ[index];
RQ[index]=1000;
count++;
}
printf("Total head movement is: %d",TotalHeadMovement);
getch();
//return 0;
}

PROGRAM 12

//==========SCAN DISK SCHEDULING==========//


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
int RQ[100],i,j,n, TotalHeadMoment = 0,initial,size,move,index,temp;
printf("Enter the number of Requests: ");
scanf("%d",&n);
printf("Enter the Requests sequence: ");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position: ");
scanf("%d",&initial);
printf("Enter total disk size: ");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0: ");
scanf("%d",&move);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-i);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head movement is: %d",TotalHeadMoment);
getch();
return 0;
}

PROGRAM 13

//==========C-SCAN DISK SCHEDULING==========//


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main(){int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
getch();
return 0;
}

PROGRAM 14

//==========BANKER'S ALGORITHM==========//
#include <stdio.h>
#include <conio.h>

void calculateNeed(int need[][10], int max[][10], int alloc[][10], int processes, int resources) {
int i, j; // Declare loop variables outside
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
}

int isSafe(int processes, int resources, int avail[], int max[][10], int alloc[][10]) {
int i, j, k, p; // Declare loop variables outside
int need[10][10];
calculateNeed(need, max, alloc, processes, resources);

int finish[10] = {0}, safeSeq[10], work[10];


for (i = 0; i < resources; i++) {
work[i] = avail[i];
}

int count = 0;
while (count < processes) {
int found = 0;
for (p = 0; p < processes; p++) {
if (!finish[p]) {
for (j = 0; j < resources; j++) {
if (need[p][j] > work[j]) {
break;
}
}
if (j == resources) {
for (k = 0; k < resources; k++) {
work[k] += alloc[p][k];
}
safeSeq[count++] = p;
finish[p] = 1;
found = 1;
}
}
}
if (!found) {
printf("System is not in a safe state.\n");
return 0;
}
}

printf("System is in a safe state.\nSafe sequence is: ");


for (i = 0; i < processes; i++) {
printf("P%d ", safeSeq[i]);
}
printf("\n");
return 1;
}

int main() {
clrscr();
int processes, resources, i, j; // Declare loop variables outside
printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);

int alloc[10][10], max[10][10], avail[10];


printf("Enter allocation matrix (Process x Resource):\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Allocation for process P%d and resource R%d: ", i, j);
scanf("%d", &alloc[i][j]);
}
}

printf("Enter maximum matrix (Process x Resource):\n");


for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Maximum for process P%d and resource R%d: ", i, j);
scanf("%d", &max[i][j]);
}
}
printf("Enter available resources for each type:\n");
for (i = 0; i < resources; i++) {
printf("Available instances of resource R%d: ", i);
scanf("%d", &avail[i]);
}

isSafe(processes, resources, avail, max, alloc);


getch();
return 0;
}

You might also like