OS Lab - Record-4
OS Lab - Record-4
DATE: 28/4/25
EX.NO: 11
AIM:
To write C programs to implement the various File Organization Techniques.
ALGORITHM:
Step 1 - Initialize memory as an array of fixed size (e.g., 100 blocks), setting all elements to
0 (free).
Step 2 - Prompt the user to choose a file allocation strategy.
Step 3 - If Sequential Allocation is chosen, input starting block and length.
Step 4 - Check if all required blocks are free.
Step 5 - If yes, mark blocks as allocated and store file info; otherwise, retry.
Step 6 - If Indexed Allocation is chosen, input an index block and number of blocks to
allocate.
Step 7 - Check if index block and all data blocks are free.
Step 8 - If yes, mark them allocated and link them to the index block.
Step 9 - If Linked Allocation is chosen, input starting block and total number of blocks.
Step 10 - Iteratively input next blocks, checking each for availability.
Step 11 - If all are available, create a linked list to represent the block sequence.
Step 12 - Display allocation result for the chosen method.
Step 13 - Allow the user to repeat with a different strategy or exit.CODE:
Code:-
#include <stdio.h>
#include <stdlib.h>
struct File {
int startBlock;
int length;
struct Node {
int block;
struct Node* next;
};
int memory[100] = {0};
void sequentialAllocation() {
int i, j, n, block, len;
struct File files[10];
printf("\n[Sequential Allocation]\n");
printf("Enter number of files: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter starting block and length of file %d: ", i + 1);
scanf("%d%d", &block, &len);
int flag = 1;
for (j = block; j < block + len; j++) {
if (memory[j] == 1) {
flag = 0;
break;
}
}
if (flag) {
for (j = block; j < block + len; j++)
memory[j] = 1;
files[i].startBlock = block;
files[i].length = len;
} else {
printf("Memory already allocated for File %d. Try again.\n", i + 1);
i--;
}
ROLL NO:2127230501126 Page | 67
}
Result:
Thus, the C program to implement the various File Organization Techniques has been
implemented and the output has been verified successfully
AIM:
To implement the Memory Allocation Methods in the C programming language.
ALGORITHM:
Step 1 - Input the number of memory blocks and processes along with their sizes.
Step 2 - Initialize an allocation array to -1 for all processes.
Step 3 - For each process, scan all memory blocks to find a suitable one based on the
allocation strategy.
Step 4 - If a suitable block is found, allocate it to the process and reduce the block size by
the process size.
Step 5 - If no suitable block is found, mark the process as Not Allocated.
Step 6 - Display the process number, its size, and the corresponding allocated block or Not
Allocated.
CODE:
#include <stdio.h>
#include <string.h>
#define MAX 10
void printAllocation(const char *method, int processSize[], int allocation[], int np) {
printf("\n%s:\n", method);
printf("Process\tSize\tBlock Allocated\n");
for (int i = 0; i < np; i++) {
printf("%d\t%d\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
ROLL NO:2127230501126 Page | 74
printf("Not Allocated\n");}}
Result:
Thus, the C program to implement the various Memory Allocation Methods has been
implemented and the output has been verified successfully.
ALGORITHM:
Step 1 - Initialize separate frame arrays and counters for FCFS, LRU, and Optimal algorithms.
Step 2 - Input the number of pages, reference string, and number of frames.
Step 3 - Loop through each page in the reference string.
Step 4 - For each algorithm, check for a hit; if it's a miss, replace the page based on the respective
strategy.
Step 5 - Increment the fault counter on a miss for each algorithm.
Step 6 - After processing all pages, print the total page faults for FCFS, LRU, and Optimal.
Code:
#include <stdio.h>
#define MAX 50
int predict(int pages[], int frame[], int n, int index, int f) {
int res = -1, farthest = index, i, j;
for (i = 0; i < f; i++) {
for (j = index; j < n; j++) {
if (frame[i] == pages[j]) {
if (j > farthest) {
farthest = j;
res = i;}
break;}}
if (j == n)
return i;}
return (res == -1) ? 0 : res;}
int main() {
Page Faults:
FCFS: 5
LRU: 6
Optimal: 5
Result:
Thus, the C program to implement the various Page Replacement Algorithms has been
implemented and the output has been verified successfully.
CODE:
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
OUTPUT:
Enter name of directory -- CSE
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- A
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- B
1. Create File 2. Delete File 3. Search File
ROLL NO:2127230501126 Page | 84
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- C
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 4
The Files are -- A B C
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 3
Enter the name of the file – ABC
File ABC not found
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 2
Enter the name of the file – B
File B is deleted
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 5
ALGORITHM:
Step 1 – Initialize the directory system with a structure array to store up to 10 directories, each having
a name, a list of filenames, and a file count.
Step 2 – Set the initial directory count to zero.
Step 3 – Display a menu in a continuous loop with options to create a directory, create a file, delete a
file, search a file, display directories/files, or exit.
Step 4 – Use a switch statement to handle the user's menu selection.
Step 5 – If creating a directory, prompt for the name, store it, initialize its file count to zero, and
increment the directory count.
Step 6 – If creating a file, prompt for the target directory name, search for it, and if found, add the
filename to that directory and increment its file count.
Step 7 – If deleting or searching for a file, prompt for the directory and file names, locate the directory,
then search for the file.
Step 8 – If deleting, replace the file with the last one in the list and decrement the file count.
Step 9 – If searching, display whether the file exists or not.
Step 10 – If displaying, loop through all directories and print their names along with their files.
Step 11 – If no directories exist, show an appropriate message.
CODE:
#include<stdio.h>
struct
{char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
ALGORITHM:
Step 1 – Initialize the graphics and declare the root pointer as NULL.
Step 2 – Clear the screen and call the create() function to initialize the root directory node
with default screen coordinates and hierarchy level.
Step 3 – In create(), if the current node is NULL, dynamically allocate memory for it.
Step 4 – Prompt the user to enter the node name and type (directory or file).
Step 5 – Set the position (x, y) and bounds (lx, rx) for graphical layout.
Step 6 – If the node is a directory, ask for the number of child elements.
Step 7 – Calculate the horizontal gap for child nodes based on available width.
Step 8 – Recursively call create() for each child node, adjusting the layout bounds and
center position.
Step 9 – If the node is a file, set the child count to zero (no recursion).
Step 10 – Initialize graphics mode using initgraph() and call display() to render the tree.
Step 11 – In display(), draw lines connecting nodes to children, use bar3d() for directories,
fillellipse() for files, and outtextxy() to show names.
Step 12 – Recursively traverse and call display() for child nodes.
Step 13 – After rendering, close graphics mode using closegraph().
OUTPUT:
Enter Name of dir/file (under root): ROOT
Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under ROOT):USER 1
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1):1
Enter Name of dir/file (under USER 1):SUBDIR
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR):2
Enter Name of dir/file (under USER 1):
JAVA Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA): 0
Enter Name of dir/file (under SUBDIR):VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 0
Enter Name of dir/file (under ROOT):USER2
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER2):2
Enter Name of dir/file (under ROOT):A
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under USER2):SUBDIR 2
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR 2):2
Enter Name of dir/file (under SUBDIR2):PPL
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for PPL):2
Enter Name of dir/file (under PPL):B
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under PPL):C
ROLL NO:2127230501126 Page | 93
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under SUBDIR):AI
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for AI): 2
Enter Name of dir/file (under AI):D
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under AI):E
Enter 1 for Dir /2 for file:2
RESULT:
Thus we have implemented C programs for various File Organization Techniques and the
output has been verified successfully.