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

Menu Driven

The document contains three separate programs: a menu-driven linked file allocation program, a contiguous file allocation program, and a LOOK disk scheduling program. Each program allows users to manage file allocation or disk scheduling with options to create, delete, and display files or requests. The programs utilize data structures and algorithms to handle memory allocation and disk scheduling efficiently.

Uploaded by

asankursingh7621
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 views7 pages

Menu Driven

The document contains three separate programs: a menu-driven linked file allocation program, a contiguous file allocation program, and a LOOK disk scheduling program. Each program allows users to manage file allocation or disk scheduling with options to create, delete, and display files or requests. The programs utilize data structures and algorithms to handle memory allocation and disk scheduling efficiently.

Uploaded by

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

//Menu Driven Linked File Allocation Program

#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;

printf("File deleted successfully.\n");


}

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:

//Contiguous File Allocation Program


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX_BLOCK 100
void showBitvector(int n);
void createNewFile(int n);
void deleteFile();
int allocated[MAX_BLOCK]={0};
void initializeBitVector(n)
{
int i,numAllocated;
printf("Enter the number of blocks allocated:");
scanf("5d",&numAllocated);
for(i=0;i<numAllocated;++i)
{
int index=rand()%n;
if(!allocated[index])
{
allocated[index]=1;
}
else
{
--i;
}
}
}
void showBitVector(n)
{
int i=0;
printf("Bit Vector:\n");
for(i=0;i<n;++i)
{
printf("%d",allocated[i]);
}
printf("\n");
}
void creatNewFile(n)
{
int startBlok,length,i=0;
printf("Enter starting block for the new file:");
scanf("%d",&startBlock);
printf("Enter Length of the file:");
scanf("%d",&length);

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);

printf("Enter total disk size:\n");


scanf("%d",&size);

printf("Enter the head movement direction for High press 1 or


for low press 0\n");
scanf("%d",&move);

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();
}

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