Menu Driven
Menu Driven
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_BLOCK 100
typedef struct BlockNode
{
int index;
struct BlockNode *next;
} Blocknode;
void showBitVector(int n);
void createNewFile(int n);
void showDirectory();
void deleteFile();
int allocated[MAX_BLOCK]={NULL};
BlockNode *freeList=NULL;
BlockNode *directory[MAX_BLOCK]={NULL};
void initializeFreeList(int n)
{
int i=0;
for(i=0;i<n;++i)
{
BlockNode *newNode=(BlockNode*)malloc(sizeof(BlockNode));
newNode->index=i;
newNode->next = freeList;
freeList=newNode;
}
}
void showBitVector(int n)
{
int i=0;
printf("Bit Vector:\n");
for(i=0;i<n;++i)
{
printf("%d",allocated[i]);
}
printf("\n");
}
void createNewFile(int n)
{
int fileIndex;
BlockNode *newNode;
printf("Enter index for the new file:");
scanf("%d",&fileIndex);
if(allocated[fileIndex])
{
printf("Error: Block already allocated.\n");
return;
}
if(freeList == NULL)
{
printf("Error: No free block available.\n");
return;
}
newNode=freeList;
freeList=freeList->next;
newNode->next=directory[fileIndex];
directory[fileIndex]=newNode;
allocated[fileIndex] = 1;
printf("File created successfully.\n");
}
void showDirectory()
{
int i=0;
BlockNode *newNode,*current;
printf("Directory\n");
for(i=0;i<MAX_BLOCK;++i)
{
if(directory[i] !=NULL)
{
printf("File at index %d points to blocks:",i);
current=directory[i];
while (current !=NULL)
{
printf("%d",current->index);
current=current->next;
}
printf("\n");
}
}
}
void deleteFile()
{
int fileIndex;
BlockNode *newNode,*current;
printf("Enter index of file to delete:");
scanf("%d",&fileIndex);
if(!allocated[fileIndex])
{
printf("Error:File not found at index %d\n",fileIndex);
return;
}
current=directory[fileIndex];
while(current !=NULL)
{
BlockNode *temp=current;
current=current->next;
free(temp);
}
directory[fileIndex]=NULL;
allocated[fileIndex]=0;
void main()
{
int n,choice;
clrscr();
printf("Enter the number of blocks:");
scanf("%d",&n);
initializeFreeList(n);
while(1)
{
printf("Menu \n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory \n");
printf("4. Delete File \n");
printf("5. Exit \n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory();
break;
case 4:
deleteFile();
break;
case 5:
exit(0);
default :
printf("Invalid choice.Please Try
again.\n");
}
}
// getch();
}
Program 2:
if(startBlock<0 ||startBlock>=n||startBlock+length>n)
{
printf("Error:Invalid file allocation \n");
return;
}
for(i=startBlock;i<startBlock+length;++i)
{
if(allocated[i])
{
printf("Error: Block %d already allocated.\n",i);
return;
}
}
for(i=startBlock;i<startBlock+length;++i)
{
allocated[i]=1;
}
printf("File created successfully.\n");
}
//Function to display the directory
void showDirectory()
{
int i=0;
printf("Directory:\n");
for(i=0;i<MAX_BLOCKS;++i)
{
if(allocated[i])
{
printf("File starting at block %d\n",i);
while(i<MAX_BLOCKS && allocated[i])
{
++i;
}
--i;
}
}
}
//Function to delete a file
void deleteFile()
{
int startBlock,i=0;
printf("Enter starting block of the file to delete:");
scanf("%d",&startBlock);
if(!allocated[startBlock])
{
printf("Error:File not found at block %d\n",startBlock);
return;
}
i=startBlock;
while(i<MAX_BLOCK && allocated[i])
{
allocated[i]=0;
++i;
}
void main()
{
int n,choice;
clrscr();
printf("Enter the number of blocks on the disk:");
scanf("%d",&n);
initializeBitVector(n);
while(1)
{
printf(" \n MENU:\n ");
printf("1. Show Bit Vector \n");
printf("2. Create New File \n");
printf("3. Show Directory \n");
printf("4. Delete File\n");
printf("5. Exit\n");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory()_;
break;
case 4:
deleteFile();
case 5: exit(0);
default :
printf("Invalid choice.Please Try Again
\n");
}
}
getch();
}
Program 3:
LOOK Disk Scheduling
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int RQ[100],i,j,n,THM=0,initial,size,move,temp;
clrscr();
printf("Enter the number of request \n");
scanf("%d",&n);
printf("Enter the request sequence:\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter the initial Head Position:\n");
scanf("%d",&initial);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(RQ[j]>RQ[j+1])
{
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(move==1)
{
for(i=index;i<n;i++)
{
THM=THM+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index-1;i>=0;i--)
{
THM=THM+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for(i=index-1;i>=0;i--)
{
THM=THM+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index;i<n;i++)
{
THM=THM+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total Head Movement is %d",THM);
getch();
}