Created
April 27, 2022 03:25
-
-
Save hatim-the-dark-knight/d3d192919dbd1fd57bb30c650638d48d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Program to implement Indexed File allocation | |
#include <stdio.h> | |
#define MAX 100 | |
int blocks[MAX]; | |
int indices[10]; | |
typedef struct { | |
int start; | |
int len; | |
int alloc[25]; | |
int flag; | |
}files; | |
files file[10]; | |
void allocate(int fno) | |
{ | |
int i = file[fno].start; | |
int count = 0; | |
do { | |
if((i == file[fno].start) && (blocks[i] == 1)) | |
{ | |
file[fno].flag = 0; | |
break; | |
} | |
if(blocks[i] == 0) | |
{ | |
blocks[i] = 1; | |
file[fno].alloc[count] = i; | |
count++; | |
} | |
i = (i+1) % MAX; | |
}while(i!=file[fno].start && count<file[fno].len); | |
if(count == file[fno].len) | |
file[fno].flag = 1; | |
else | |
file[fno].flag = 0; | |
} | |
void display(int n) | |
{ | |
int i,j; | |
printf("\n-----------------------------\n"); | |
printf("The indices locations are: \n"); | |
printf("File No.\tIndex block\tStarting block\tLength\tStatus\n"); | |
for (int i = 0; i < n; i++) | |
{ | |
if(indices[i] != -1 && file[i].flag == 1) | |
printf("%d\t\t%d\t\t%d\t\t%d\tAllocated\n", (i+1), indices[i], file[i].start, file[i].len); | |
else | |
printf("%d\t\t-\t\t-\t\t-\tUnallocated\n", (i+1)); | |
} | |
for (i = 0; i < n; i++) | |
{ | |
if(file[i].flag == 1) | |
{ | |
printf("\nIndex Block of File %d\n", (i+1)); | |
printf("\nBlock No.\tBlock Location\n"); | |
for(j=0; j < file[i].len; j++) | |
printf("%d\t\t%d\n", (j+1), file[i].alloc[j]); | |
} | |
} | |
} | |
void main() | |
{ | |
int n, filled, x; | |
for(int i=0;i<MAX;i++) | |
blocks[i] = 0; | |
printf("Enter the number of blocks already occupied: "); | |
scanf("%d", &filled); | |
for(int i=0; i<filled; i++) | |
{ | |
printf("Enter the location of the occupied block: "); | |
scanf("%d", &x); | |
blocks[x] = 1; | |
} | |
printf("Enter the number of files to be allocated: "); | |
scanf("%d", &n); | |
for (int i = 0; i < n; i++) | |
{ | |
printf("\nEnter the location of the Index block for File %d: ", (i+1)); | |
scanf("%d", &x); | |
if(blocks[x] == 0) | |
{ | |
blocks[x] = 1; | |
indices[i] = x; | |
} | |
else | |
{ | |
indices[i] = -1; | |
printf("Index block is already occupied! Unable to store File %d\n", (i+1)); | |
continue; | |
} | |
printf("Enter the starting location of File %d: ", (i+1)); | |
scanf("%d", &file[i].start); | |
printf("Enter the length of File %d: ", (i+1)); | |
scanf("%d", &file[i].len); | |
allocate(i); | |
if(file[i].flag == 1) | |
printf("File %d was successfully allocated in the disk!\n", (i+1)); | |
else | |
printf("Starting location is already occupied! Unable to allocate disk space to File %d\n", (i+1)); | |
} | |
display(n); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Program to implement Linked File allocation | |
#include <stdio.h> | |
#define MAX 100 | |
int blocks[MAX]; | |
typedef struct { | |
int start; | |
int len; | |
int alloc[25]; | |
int flag; | |
}files; | |
files file[10]; | |
void allocate(int fno) | |
{ | |
int i = file[fno].start; | |
int count = 0; | |
do { | |
if((i == file[fno].start) && (blocks[i] == 1)) | |
{ | |
file[fno].flag = 0; | |
break; | |
} | |
if(blocks[i] == 0) | |
{ | |
blocks[i] = 1; | |
file[fno].alloc[count] = i; | |
count++; | |
} | |
i = (i+1) % MAX; | |
}while(i!=file[fno].start && count<file[fno].len); | |
if(count == file[fno].len) | |
file[fno].flag = 1; | |
else | |
file[fno].flag = 0; | |
} | |
void display(int n) | |
{ | |
int i,j; | |
printf("File No.\tStarting block\tLength\tStatus\t\tBlocks\n"); | |
for (i = 0; i < n; i++) | |
{ | |
if(file[i].flag == 1) | |
{ | |
printf("%d\t\t%d\t\t%d\tAllocated\t", (i+1), file[i].start, file[i].len); | |
for(j=0; j < file[i].len-1; j++) | |
printf("%d -> ", file[i].alloc[j]); | |
printf("%d\n", file[i].alloc[j]); | |
} | |
else | |
{ | |
printf("%d\t\t-\t\t-\tUnallocated\t\t-\n", (i+1)); | |
} | |
} | |
} | |
void main() | |
{ | |
int n, filled, x; | |
for(int i=0;i<MAX;i++) | |
blocks[i] = 0; | |
printf("Enter the number of blocks already occupied: "); | |
scanf("%d", &filled); | |
for(int i=0; i<filled; i++) | |
{ | |
printf("Enter the location of the occupied block: "); | |
scanf("%d", &x); | |
blocks[x] = 1; | |
} | |
printf("Enter the number of files to be allocated: "); | |
scanf("%d", &n); | |
for (int i = 0; i < n; i++) | |
{ | |
printf("\nEnter the starting location of File %d: ", (i+1)); | |
scanf("%d", &file[i].start); | |
printf("Enter the length of File %d: ", (i+1)); | |
scanf("%d", &file[i].len); | |
allocate(i); | |
if(file[i].flag == 1) | |
printf("File %d was successfully allocated in the disk!\n", (i+1)); | |
else | |
printf("Unable to allocate disk space to File %d\n", (i+1)); | |
} | |
display(n); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program to implement paging in C | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct { | |
int page_no; | |
int frame_no; | |
} page_table; | |
int pages, page_size, mem_size; | |
page_table pg_tab[100]; | |
void input() //To input the data regarding pages, memory size and the page table | |
{ | |
printf("Enter the size of the Physical memory(kB): "); | |
scanf("%d", &mem_size); | |
printf("Enter the size of one page(kB): "); | |
scanf("%d", &page_size); | |
int frames = mem_size/page_size; | |
printf("\nNumber of available frames: %d", frames); | |
printf("\nEnter the number of pages: "); | |
scanf("%d", &pages); | |
if(pages > frames) | |
{ | |
printf("Number of pages cannot exceed the number of frames. Terminating...\n"); | |
exit(0); | |
} | |
printf("\nInput the page table. Enter frame no as -1 for unallocated pages.\n"); | |
printf("Page No.\tFrame No.\n"); | |
for (int i = 0; i < pages; ++i) | |
{ | |
scanf("%d\t\t%d", &pg_tab[i].page_no, &pg_tab[i].frame_no); | |
} | |
printf("-------------------------\n"); | |
} | |
void getPhysicalAddress() //To calculate the physical address | |
{ | |
int pg, offset, phy_add, log_add, flag=0; | |
// printf("\nEnter the page number to be used for calculating the logical address: "); | |
// scanf("%d", &pg); | |
// printf("Enter the offset: "); | |
// scanf("%d", &offset); | |
printf("\nEnter the Logical Address: "); | |
scanf("%d", &log_add); | |
pg = log_add / page_size; | |
offset = log_add % page_size; | |
printf("Page No: %d\t\tOffset: %d", pg, offset); | |
if(log_add > mem_size) | |
{ | |
printf("\nLogical Address cannot exceed memory size. Terminating...\n"); | |
exit(0); | |
} | |
if(pg > pages) | |
{ | |
printf("\nPage No cannot exceed total number of pages. Terminating...\n"); | |
exit(0); | |
} | |
for (int i = 0; i < pages; ++i) | |
{ | |
if(pg_tab[i].page_no == pg) | |
{ | |
if(pg_tab[i].frame_no == -1) | |
{ | |
flag = 1; | |
break; | |
} | |
else | |
{ | |
phy_add = pg_tab[pg].frame_no*page_size + offset; | |
break; | |
} | |
} | |
} | |
if(flag == 0) | |
printf("\nThe Physical Address corresponding to the given logical address is: %d\n", phy_add); | |
else | |
printf("\nThe page hasn't yet been allocated in memory yet.\n"); | |
} | |
void main() | |
{ | |
input(); | |
getPhysicalAddress(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program to implement Sequential File allocation | |
#include <stdio.h> | |
int files[100], start[10], len[10], alloc[10]; | |
void allocate(int fno) { | |
int count = 0; | |
for(int i=start[fno]; i < (start[fno] + len[fno]); i++) | |
if(files[i] == 0) | |
count++; | |
if(count == len[fno]) | |
{ | |
for(int i=start[fno]; i < (start[fno] + len[fno]); i++) | |
files[i] = 1; | |
alloc[fno] = 1; | |
} | |
else | |
alloc[fno] = 0; | |
} | |
void display(int n) { | |
printf("\nFile No.\tStarting block\tLength\tStatus\n"); | |
for(int i=0; i < n; i++) | |
{ | |
if(alloc[i] == 1) | |
printf("%d\t\t%d\t\t%d\tAllocated\n", (i+1), start[i], len[i]); | |
else | |
printf("%d\t\t-\t\t-\tUnallocated\n", (i+1)); | |
} | |
} | |
void main() { | |
for(int i=0;i<100;i++) | |
files[i] = 0; | |
int n; | |
printf("Enter the number of files: "); | |
scanf("%d", &n); | |
for(int i=0;i<n;i++) | |
{ | |
printf("\nEnter the starting location of File %d: ", (i+1)); | |
scanf("%d", &start[i]); | |
printf("Enter the length of File %d: ", (i+1)); | |
scanf("%d", &len[i]); | |
allocate(i); | |
if(alloc[i] == 1) | |
printf("File %d was successfully allocated in the disk!\n", (i+1)); | |
else | |
printf("Unable to allocate disk space to File %d\n", (i+1)); | |
} | |
printf("\n-----------------------------\n"); | |
printf("The File allocation table is: \n"); | |
display(n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment