Os Practical Exam
Os Practical Exam
PROGRAM 1
for(i=0;i<nb;i++){
avail[i]=9999;
}
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;
int main() {
int blockSize[20], processSize[20]; // Arrays to store block and process sizes
int m, n, i;
clrscr();
getch();
return 0;
}
PROGRAM 3
int main() {
// Declare Variables
int nb,blockSize[100],n,jobSize[100],i,j,alloc[100];
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);
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
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
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;
//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];
}
//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;
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");
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");
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
getch();
}
PROGRAM 7
int main() {
int blockSize[20], processSize[20]; // Arrays to store block and process sizes
int m, n, i;
clrscr();
getch();
return 0;
}
PROGRAM 8
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;
PROGRAM 9
// 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]);
}
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
int main() {
clrscr();
int n, i, head;
if (n > MAX_REQUESTS) {
printf("Error: Number of requests exceeds the maximum limit of %d.\n", MAX_REQUESTS);
return 1; // Exit with error
}
PROGRAM 11
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
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
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 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;
}
}
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);