0% found this document useful (0 votes)
14 views14 pages

OS Program 5,6,8,10

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)
14 views14 pages

OS Program 5,6,8,10

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/ 14

5.Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

#include<stdio.h>
int main()
{
/*array will store at most 5process with 3 Resources if your process orresources is greater than 5
and 3 then increase the size of array */
int p, c, count= 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5],available[3], finish[5], terminate=
0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
//pisprocessandc isdifferentresources
printf(" Enter allocation ofresource of allprocess %d x %d matrix",p,c);
for (i=0; i<p; i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&alc[i][j]);
}
}
printf("Enterthe maximum resource required for each processin %d x %d matrix",p,c);
for (i = 0; i <p; i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter theavailable resources ");
for (i = 0; i <c; i++)
scanf("%d",&available[i]);
printf("\n Need resources matrix are\n");
for (i = 0; i <p; i++)
{
for(j=0;j<c;j++)
{
need[i][j]=max[i][j]-alc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
/*Once process executes,finish[i] will prevent its execution again by setting finish[i]=l*/
for (i = 0; i <p; i++)
{
finish[i]=0;
}
while(count<p)
{
for(i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<c;j++)
{
if(need[i][j] >available[j]) break;
}
//when need matrix is not greater than availablematrix;then;j=cwill betrue
if (j == c)
{
safe[count]=i;
finish[i]=1;
/*nowprocessgetsexecuted &release theresources andaddthemtoavailableresources*/ for (j=0;j
<c;j++)
{
available[j]+=alc[i][j];
}
count++;
terminate=0;
}
else
{
terminate++;
}

}
}
if(terminate==(p - 1))
{
printf("safe sequence doesnotexist");
break;
}
}
if(terminate!=(p-1))
{
printf("\n available resource after completion\n");
for (i = 0; i <c; i++)
{
printf("%d\t",available[i]);
}
printf("\nsafesequence are\n");
for (i = 0; i <p; i++)
{
printf("p%d\t",safe[i]);
}
}
return 0;
}
6.Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.

//Worst fit

#include<stdio.h>
int main()
{
int i,nb,np,block_size[20],process_size[20],rm[20],allocation[20];
printf("\n\t-------->Memory Management Scheme-WorstFit >");
printf("\n\nEnter the number of free blocks in Main memory: ");
scanf("%d",&nb);
printf("\nEnter the number of processes to be stored in Main memory: ");
scanf("%d",&np);
printf("\n Enter the size(MB) of the memory blocks :-\n");
for(i=0;i<nb;i++)
{
printf("\nBlock%dsizeinMB:",i+1);
scanf("%d",&block_size[i]);
}
printf("\nEnter the size of the Process :-\n");
for(i=0;i<np;i++)
{
printf("\nProcess%dsizeinMB:",i+1);
scanf("%d",&process_size[i]);
}
//initially assigning-1 to all allocation indexes,that means nothing is allocated currently
for(i=0;i<np;i++)
{
allocation[i]=-1;
}
//pick each process and finds suitable blocks according to its size and assign to it.
for(i=0;i<np;i++)
{
int indexPlaced=-1;
for(int j= 0;j<nb;j++)
{
if(block_size[j]>=process_size[i])
{
//place the process at the first block fit to accommodate process
if(indexPlaced ==-1)
indexPlaced = j;
// if; any future block is larger than the current block where process is placed, change the block
and thus indexPlaced
else if(block_size[indexPlaced] < block_size[j])
indexPlaced = j;
}
}
//If we were successfully able to find block for the process
if(indexPlaced!=-1)
{
//allocate this block j to process p[i]
allocation[i]=indexPlaced;
//Reduce available memory size for the block.
block_size[indexPlaced] -= process_size[i];
rm[i]=block_size[indexPlaced];
}
}
printf("\nProcess No.\tProcess Size(MB)\t\t Allocated Block no\t\t Remaining block memory
size\n");
for(i=0;i<np;i++)
{
printf("%d \t\t\t %d \t\t\t\t ", i+1,process_size[i]);
if(allocation[i] != -1)
{
printf("%d\t\t",allocation[i]+1);
printf("\t\t%d\n",rm[i]);
}
else
printf("Not Allocated due to fragmentation problem\n");
}
return 0;
}
//BestFit
#include<stdio.h>
int main()
{
int i,nb,np, block_size[20],process_size[20],rm[20],allocation[20];
printf("\n\t-------->Memory Management Scheme-BestFit >");
printf("\n\nEnter the number of free blocks in Main memory: ");
scanf("%d",&nb);
printf("\nEnter the number of processes to be stored in Main memory: ");
scanf("%d",&np);
printf("\n Enter the size(MB) of the memory blocks :-\n");
for(i=0;i<nb;i++)
{
printf("\nBlock%dsize in MB: ",i+1);
scanf("%d",&block_size[i]);
}
printf("\nEnter the size ofthe Process :-\n");
for(i=0;i<np;i++)
{
printf("\nProcess%dsize in MB: ",i+1);
scanf("%d",&process_size[i]);
}
//initially assigning -1 to all allocation indexes,that means nothing is allocated currently
for(i=0;i<np;i++)
{
allocation[i]= -1;
}
//pick each process and finds suitable blocks according to its size and assign to it.
for(i=0;i<np;i++)
{
int indexPlaced=-1;
for(int j=0;j<nb;j++)
{
if(block_size[j] >=process_size[i])
{
//place the process at the first block fit to accommodate process
if(indexPlaced==-1)
indexPlaced=j;
// if; any future block is larger than the current block where process is placed, change the block
and thus indexPlaced
else if(block_size[j]<block_size[indexPlaced])
indexPlaced=j;
}
}
//If we were successfully able to find block for the process
if(indexPlaced!=-1)
{
// allocate this block j to processp[i]
allocation[i]= indexPlaced;
//Reduce available memory size for the block.
block_size[indexPlaced]-= process_size[i];
rm[i]=block_size[indexPlaced];
}
}
printf("\nProcess No.\tProcess Size(MB)\t\t Allocated Block no\t\t Remaining block memory
size\n");
for(i=0;i<np;i++)
{
printf("%d \t\t\t %d \t\t\t\t ", i+1,process_size[i]);
if(allocation[i] != -1)
{
printf("%d\t\t",allocation[i]+1);
printf("\t\t%d\n",rm[i]);
}
else
printf("Not Allocated\n");
}
return 0;
}
//First fit
#include<stdio.h>
int main()
{
int i,nb,np,block_size[20],process_size[20], rm[20];
int allocation[20];
printf("\n\t-------->Memory Management Scheme-FirstFit >");
printf("\n\nEnter the number of free blocks in Main memory:");
scanf("%d",&nb);
printf("\nEnterthe number of processes to be stored in Main memory: ");
scanf("%d",&np);
printf("\n Enter the size(MB) of the memory blocks :-\n");
for(i=0;i<nb;i++)
{
printf("\nBlock%dsizeinMB:",i+1);
scanf("%d",&block_size[i]);
}
printf("\nEnterthe size of the Process :-\n");
for(i=0;i<np;i++)
{
printf("\nProcess%dsizeinMB:",i+1);
scanf("%d",&process_size[i]);
}
//initially assigning -1 to all allocation indexes; that means nothing is allocated currently
for(i=0;i<np; i++)
{
allocation[i]=-1;
}
//pick each process and finds suitable blocks according to its size and assign to it.
for(i=0;i<np;i++)
{
for(int j=0;j<nb; j++)
{
if(block_size[j] >=process_size[i])
{
//allocateblockjto p[i]process
allocation[i]=j;
//Reduce the available memory size in this block.
block_size[j] -= process_size[i];
rm[i]=block_size[j]; break;
}
}
}
printf("\nProcess No.\tProcess Size(MB)\t\t Allocated Block no\t\t Remaining block memory
size\n");
for(i=0;i<np;i++)
{
printf("%d \t\t\t %d \t\t\t\t ", i+1,process_size[i]);
if(allocation[i] != -1)
{
printf("%d\t\t",allocation[i]+1);
printf("\t\t%d\n",rm[i]);
}
else
printf("Not Allocated\n");
}
return 0;
}
8.Simulate following File Organization Techniques a) Single level directory b) Two level directory

//Single level directory


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct
{
char dname[10], fname[10][10];
int fcnt;
} dir;
int main()
{
int i, ch;
char
f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while (1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n4. Display Files\t5.Exit\nEnter your choice-- ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2:
printf("\nEnter the name of the file -- ");
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++)
{
if (strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is deleted ", f);
strcpy(dir.fname[i], dir.fname[dir.fcnt - 1]);
break;
}
}
if (i == dir.fcnt)
printf("File %s not found", f);
else
dir.fcnt--;
break;
case 3:
printf("\nEnter the name of the file -- ");
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++)
{
if (strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is found ", f);
break;
}
}
if (i == dir.fcnt)
printf("File %s not found", f);
break;
case 4:if (dir.fcnt ==0)
printf("\nDirectory Empty");
else {
printf("\nThe Files are --");
for (i=0;i< dir.fcnt; i++)
printf("\t%s", dir.fname[i]);
}
break;
default:exit(0);
}
}
}
10.Develop a C program to simulate SCAN disk scheduling algorithm.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, j, seekTime= 0, n, head, queue[20],temp, maxTrack, currentTrack, direction, maxRequest;
printf("Enter the number of disk requests: ");
scanf("%d",&n);
printf("Enter the elements of disk request queue\n");
for(i=0; i<n; i++)
{
scanf("%d", &queue[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
printf("Enter the maximum track number: ");
scanf("%d", &maxTrack);
printf("Enter the direction of disk movement (0: for left, 1: for right.) ");
scanf("%d", &direction);
printf("\n");
queue[n] = head;
n = n+1;
for(int i=0; i<n; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(queue[j] > queue[j+1])
{
temp = queue[j];
queue[j] = queue[j+1];
queue[j+1]= temp;
}
}
}
maxRequest = queue[n-1];
for(i=0; i<n; i++)
{
if(head==queue[i])
{
currentTrack = i;
break;
}
}
if (direction==0)
{
printf(" ------------------ Seek sequence for left movement of disk is ----------------- \n\n");
for(i= currentTrack; i >=0; i--)
{
if(queue[i]==head)
{
printf(" %d -->",head);
continue;
}
printf(" %d - -> ", queue[i]);
}
printf("0 - -> ");
for(i =currentTrack+1; i < n; i++)
{
if(queue[i]== head)
{
i = i +1;
}
printf("%d - -> ",queue[i]);
}
seekTime = head + maxRequest;
}
else
{
printf("---------------- Seek sequence for right movement of disk is ------------------ \n\n");
for(i= currentTrack; i <n; i++)
{
printf(" %d - -> ", queue[i]);
}
printf("%d - -> ",maxTrack);
for(i =currentTrack-1; i >=0; i--)
{
printf("%d - -> ",queue[i]);
}
seekTime = (maxTrack-head)+ (maxTrack-queue[0]);
}
printf("\n The total distance traveled (in cylinders) by the disk-arm using SCAN = %d",
seekTime);
return 0;
}

You might also like