0% found this document useful (0 votes)
806 views

TYBCS SEM 6 Operating System Code Solution

The document discusses different disk scheduling algorithms like FCFS, SSTF, SCAN and C-SCAN. It provides code implementations for each algorithm and explains how they work. The code includes functions to calculate head movement for requests and display the total movement for each algorithm.

Uploaded by

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

TYBCS SEM 6 Operating System Code Solution

The document discusses different disk scheduling algorithms like FCFS, SSTF, SCAN and C-SCAN. It provides code implementations for each algorithm and explains how they work. The code includes functions to calculate head movement for requests and display the total movement for each algorithm.

Uploaded by

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

Operating Systems-2 Practical Programs

1. Bankers Algorithm
#include<stdio.h>
int A[10][10],M[10][10],N[10][10],Av[10],Safe[10],Finish[10],nor,nop;
void AcceptData(int X[][10])
{
int i,j;
for(i=0;i<nop;i++)
{
printf("P%d\n",i);
for(j=0;j<nor;j++)
{
printf("%c: ",65+j);
scanf("%d",&X[i][j]);
}
}
}

void AcceptAvailability()
{
int i;
for(i=0;i<nor;i++)
{
printf("%c: ",65+i);
scanf("%d",&Av[i]);
}
}

void DisplayData()
{
int i,j;
printf("\n\tAllocation\t\tMax\t\tNeed\n");
printf("\t");
for(i=0;i<3;i++)
{
for(j=0;j<nor;j++)
printf("%4c",65+j);
printf("\t");
}
for(i=0;i<nop;i++)
{
printf("\nP%d\t",i);
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",M[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
}
printf("\nAvailable\n");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
}
void CalcNeed()
{
int i,j;
for(i=0;i<nop;i++)
for(j=0;j<nor;j++)
N[i][j] = M[i][j] - A[i][j];
}

int checkNeed(int pno)


{
int i;
for(i=0;i<nor;i++)
if(N[pno][i] > Av[i])
return(0);
return(1);
}

void Banker()
{
int i=0,j=0,k=0,flag=0;
while(flag<2)
{
if(!Finish[i])
{
printf("\nNeed%d",i);
for(j=0;j<nor;j++)
printf("%d",N[i][j]);
if(!checkNeed(i))
{
printf("\b>Available(");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf("\b)");
printf("\nNeed Cannot be satisfied, consider next
process");
}
else
{
printf("\b)<=Available(");
for(j=0;j<nor;j++)
printf("%d,",Av[j]);
printf("\b)");
printf("\nNeed can be satisfied, so allocate required
resources");
printf("\nAllocation%d\tAvailable\tNeed%d\n",i,i);
for(j=0;j<nor;j++)
{
A[i][j] = M[i][j];
Av[j] -= N[i][j];
N[i][j] = 0;
}
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
printf("\nAfter P%d terminates it will release all its
resources",i);
printf("\nAllocation%d\tAvailable\tNeed%d\n",i,i);
for(j=0;j<nor;j++)
{
Av[j] += A[i][j];
A[i][j] = 0;
}
for(j=0;j<nor;j++)
printf("%4d",A[i][j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",Av[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%4d",N[i][j]);
Safe[k++] = i;
Finish[i]=1;
}
}
if((i+1)%nop == 0)
flag++;
i=(i+1)%nop;
}
if(k==nop)
{
printf("\nSystem is in safe state...");
printf("\nSafe Sequence: ");
for(i=0;i<k;i++)
printf("P%d->",Safe[i]);
printf("\b\b ");
}
else
{
printf("\nSystem is in not safe state...");
}
}

void main()
{
clrscr();
printf("\nEnter No.of Processes & No.of Resources: ");
scanf("%d %d",&nop,&nor);
printf("Enter Allocation\n");
AcceptData(A);
printf("Enter Max Requirement\n");
AcceptData(M);
printf("Enter Availability\n");
AcceptAvailability();
CalcNeed();
DisplayData();
Banker();
getch();
}
2. FCFS Disk scheduling Algorithm
void main()
{
int i,j,sum=0,n,disk,ar[20],tm[20];
clrscr();
printf("enter number of element to scan");
scanf("%d",&n);
printf("enter position of head\n ");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
tm[i]=disk-ar[i];
if(tm[i]<0)
{
tm[i]=ar[i]-disk;
}
disk=ar[i];
sum=sum+tm[i];
}
printf("\nTotal head movement of total cylinders %d",sum);
getch();
}
3. SSTF Disk Scheduling Algorithm
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,k,req[50],mov=0,cp,index[50],min,a[50],j=0,mini,cp1;
clrscr();
printf("enter the current position\n");
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
cp1=cp;
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
index[i]=abs(cp-req[i]); // calculate distance of each request from current
position
}
// to find the nearest request
min=index[0];
mini=0;
for(i=1;i<n;i++)
{
if(min>index[i])
{
min=index[i];
mini=i;
}
}
a[j]=req[mini];
j++;
cp=req[mini]; // change the current position value to next request
req[mini]=999;
} // the request that is processed its value is changed so that it is not
processed again
printf("Sequence is : ");
printf("%d",cp1);
mov=mov+abs(cp1-a[0]); // head movement
printf(" -> %d",a[0]);
for(i=1;i<n;i++)
{
mov=mov+abs(a[i]-a[i-1]); ///head movement
printf(" -> %d",a[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
getch();
return 0;
}
4. SCAN Disk Scheduling Algorithm
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move,index;
clrscr();
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 Scan 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;
}
}
}
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];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
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];
}
// last movement for min size
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;

}
5. C-SCAN Disk Scheduling Algorithm
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move,index;
clrscr();
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 C-Scan 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;
}

}
}

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];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
/*movement max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for( i=0;i<index;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];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}

printf("Total head movement is %d",TotalHeadMoment);


getch();
return 0;

}
6. Sequential File Allocation Method
#include<stdio.h>
void main()
{
char name[10][30];
int start[10],length[10],num,k,j,i,count=0;
printf("Enter the number of files to be allocated\n");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("Enter the name of the file %d\n",i+1);
scanf("%s",&name[i][0]);
printf("Enter the start block of the file %d\n",i+1);
scanf("%d",&start[i]);
printf("Enter the length of the file %d\n",i+1);
scanf("%d",&length[i]);

for(j=0,k=1;j<num && k<num;j++,k++)


{
if(start[j+1]<=start[j] || start[j+1]>=length[j])
{

}
else
{
count++;
}
}
if(count==1)
{
printf("%s cannot be allocated disk space\n",name[i]);
}
}
printf("File Allocation Table\n");
printf("%s%40s%40s\n","File Name","Start Block","Length");
printf("%s%50d%50d\n",name[0],start[0],length[0]);

for(i=0,j=1;i<num && j<num;i++,j++)


{
if(start[i+1]<=start[i] || start[i+1]>=length[i])
{
printf("%s%50d%50d\n",name[j],start[j],length[j]);
}
}
getch();
}
7. Indexed File Allocation Method
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on
the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
8. Linked File Allocation Method
#include <stdio.h>
#include <stdlib.h>
int main()
{
int f[50], p, i, st, len, j, c, k, a;
clrscr();
for (i = 0; i < 50; i++)
f[i] = 0;

printf("Enter how many blocks already allocated: ");


scanf("%d", &p);
printf("Enter blocks already allocated: ");
for (i = 0; i < p; i++)
{
scanf("%d", &a);
f[a] = 1;
}
x:printf("Enter index starting block and length: ");
scanf("%d%d", &st, &len);
k = len;
if (f[st] == 0)
{
for (j = st; j < (st + k); j++)
{
if (f[j] == 0)
{
f[j] = 1;
printf("%d-------->%d\n", j, f[j]);
}
else
{
printf("%d Block is already allocated \n", j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n", st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if (c == 1)
goto x;
else
exit(0);
getch();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy