0% found this document useful (0 votes)
9 views29 pages

OS Lab - Record-4

The document outlines the implementation of various file organization techniques, memory allocation methods, and page replacement algorithms using C programming. It includes detailed algorithms and code for sequential, indexed, and linked file allocation, as well as first fit, best fit, and worst fit memory allocation strategies. Additionally, it describes the implementation of page replacement algorithms such as FCFS, LRU, and Optimal, with sample outputs demonstrating successful execution.

Uploaded by

astroethirkalam
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)
9 views29 pages

OS Lab - Record-4

The document outlines the implementation of various file organization techniques, memory allocation methods, and page replacement algorithms using C programming. It includes detailed algorithms and code for sequential, indexed, and linked file allocation, as well as first fit, best fit, and worst fit memory allocation strategies. Additionally, it describes the implementation of page replacement algorithms such as FCFS, LRU, and Optimal, with sample outputs demonstrating successful execution.

Uploaded by

astroethirkalam
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/ 29

CS22411-OPERATING SYSTEMS LABORATORY

DATE: 28/4/25
EX.NO: 11

Implement the various File Organization Techniques

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;

ROLL NO:2127230501126 Page | 66


};

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
}

printf("\nFile Allocation Table:\n");


printf("File\tStart\tLength\n");
for (i = 0; i < n; i++)
printf("%d\t%d\t%d\n", i + 1, files[i].startBlock, files[i].length);
}
void indexedAllocation() {
int indexBlock, n, i, block;
printf("\n[Indexed Allocation]\n");
printf("Enter the index block: ");
scanf("%d", &indexBlock);
if (memory[indexBlock] == 1) {
printf("Block already allocated. Exiting...\n");
return;
}
memory[indexBlock] = 1;
printf("Enter number of blocks to allocate: ");
scanf("%d", &n);
int blocks[n];
printf("Enter the blocks:\n");
for (i = 0; i < n; i++) {
scanf("%d", &block);
if (memory[block] == 1) {
printf("Block %d already allocated. Try again.\n", block);
i--;
} else {
memory[block] = 1;
blocks[i] = block;
}
}
printf("\nIndexed Allocation Table:\n");
ROLL NO:2127230501126 Page | 68
printf("Index Block: %d\n", indexBlock);
printf("Allocated Blocks: ");
for (i = 0; i < n; i++)
printf("%d ", blocks[i]);
printf("\n");
}
void linkedAllocation() {
int start, len, i, block;
struct Node* head = NULL, * temp, * ptr;
printf("\n[Linked Allocation]\n");
printf("Enter starting block and number of blocks: ");
scanf("%d%d", &start, &len);
if (memory[start] == 1) {
printf("Start block already allocated. Exiting...\n");
return;
}
memory[start] = 1;
head = (struct Node*)malloc(sizeof(struct Node));
head->block = start;
head->next = NULL;
ptr = head;
for (i = 1; i < len; i++) {
printf("Enter next block: ");
scanf("%d", &block);
if (memory[block] == 1) {
printf("Block %d already allocated. Try another.\n", block);
i--;
continue;
}
memory[block] = 1;
temp = (struct Node*)malloc(sizeof(struct Node));
temp->block = block;
ROLL NO:2127230501126 Page | 69
temp->next = NULL;
ptr->next = temp;
ptr = temp;
}
printf("\nLinked Allocation Sequence:\n");
ptr = head;
while (ptr != NULL) {
printf("%d -> ", ptr->block);
ptr = ptr->next;
}
printf("NULL\n");
}
int main() {
int choice;
do {
printf("\n==== File Allocation Strategies ====\n");
printf("1. Sequential Allocation\n");
printf("2. Indexed Allocation\n");
printf("3. Linked Allocation\n");
printf("4. Exit\n");
printf("Choose an option: ");
scanf("%d", &choice);
switch (choice) {
case 1:
sequentialAllocation();
break;
case 2:
indexedAllocation();
break;
case 3:
linkedAllocation();
break;
ROLL NO:2127230501126 Page | 70
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 4);
return 0;
}
Output:
==== File Allocation Strategies ====
1. Sequential Allocation
2. Indexed Allocation
3. Linked Allocation
4. Exit
Choose an option: 1
[Sequential Allocation]
Enter number of files: 3
Enter starting block and length of file 1: 1
2
Enter starting block and length of file 2: 4
2
Enter starting block and length of file 3: 7
2
File Allocation Table:
File Start Length
1 1 2
2 4 2
3 7 2
==== File Allocation Strategies ====
1. Sequential Allocation
2. Indexed Allocation
ROLL NO:2127230501126 Page | 71
3. Linked Allocation
4. Exit
Choose an option: 2
[Indexed Allocation]
Enter the index block: 1
Block already allocated. Exiting...
==== File Allocation Strategies ====
1. Sequential Allocation
2. Indexed Allocation
3. Linked Allocation
4. Exit
Choose an option: 2
[Indexed Allocation]
Enter the index block: 11
Enter number of blocks to allocate: 3
Enter the blocks:
12
13
14
Indexed Allocation Table:
Index Block: 11
Allocated Blocks: 12 13 14
==== File Allocation Strategies ====
1. Sequential Allocation
2. Indexed Allocation
3. Linked Allocation
4. Exit
Choose an option: 3
[Linked Allocation]
Enter starting block and number of blocks: 15
3
Enter next block: 18
ROLL NO:2127230501126 Page | 72
Enter next block: 20

Linked Allocation Sequence:


15 -> 18 -> 20 -> NULL
==== File Allocation Strategies ====
1. Sequential Allocation
2. Indexed Allocation
3. Linked Allocation
4. Exit
Choose an option: 4
Exiting...

Result:
Thus, the C program to implement the various File Organization Techniques has been
implemented and the output has been verified successfully

ROLL NO:2127230501126 Page | 73


CS22411-OPERATING SYSTEMS LABORATORY
DATE: 28/4/25
EX.NO: 12

Implement the Memory Allocation Methods using C

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

void firstFit(int blockSize[], int processSize[], int nb, int np) {


int allocation[MAX];
int blocks[MAX];
memcpy(blocks, blockSize, sizeof(int) * nb);
for (int i = 0; i < np; i++) allocation[i] = -1;
for (int i = 0; i < np; i++) {
for (int j = 0; j < nb; j++) {
if (blocks[j] >= processSize[i]) {
allocation[i] = j;
blocks[j] -= processSize[i];
break;}}}
printAllocation("First Fit", processSize, allocation, np);}
void bestFit(int blockSize[], int processSize[], int nb, int np) {
int allocation[MAX];
int blocks[MAX];
memcpy(blocks, blockSize, sizeof(int) * nb);
for (int i = 0; i < np; i++) allocation[i] = -1;
for (int i = 0; i < np; i++) {
int bestIdx = -1;
for (int j = 0; j < nb; j++) {
if (blocks[j] >= processSize[i]) {
if (bestIdx == -1 || blocks[j] < blocks[bestIdx])
bestIdx = j;}}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blocks[bestIdx] -= processSize[i];}}
printAllocation("Best Fit", processSize, allocation, np);}
void worstFit(int blockSize[], int processSize[], int nb, int np) {
int allocation[MAX];
int blocks[MAX];
ROLL NO:2127230501126 Page | 75
memcpy(blocks, blockSize, sizeof(int) * nb);
for (int i = 0; i < np; i++) allocation[i] = -1;
for (int i = 0; i < np; i++) {
int worstIdx = -1;
for (int j = 0; j < nb; j++) {
if (blocks[j] >= processSize[i]) {
if (worstIdx == -1 || blocks[j] > blocks[worstIdx])
worstIdx = j;}}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blocks[worstIdx] -= processSize[i];
}}
printAllocation("Worst Fit", processSize, allocation, np);}
int main() {
int blockSize[MAX], processSize[MAX];
int nb, np;
printf("Enter number of blocks: ");
scanf("%d", &nb);
printf("Enter sizes of blocks:\n");
for (int i = 0; i < nb; i++)
scanf("%d", &blockSize[i]);
printf("Enter number of processes: ");
scanf("%d", &np);
printf("Enter sizes of processes:\n");
for (int i = 0; i < np; i++)
scanf("%d", &processSize[i]);
firstFit(blockSize, processSize, nb, np);
bestFit(blockSize, processSize, nb, np);
worstFit(blockSize, processSize, nb, np);
return 0;
}

ROLL NO:2127230501126 Page | 76


OUTPUT:
Enter number of blocks: 4
Enter sizes of blocks:
10
20
30
40
Enter number of processes: 3
Enter sizes of processes:
1
11
21
First Fit:
Process Size Block Allocated
1 1 1
2 11 2
3 21 3
Best Fit:
Process Size Block Allocated
1 1 1
2 11 2
3 21 3
Worst Fit:
Process Size Block Allocated
1 1 4
2 11 4
3 21 3

Result:
Thus, the C program to implement the various Memory Allocation Methods has been
implemented and the output has been verified successfully.

ROLL NO:2127230501126 Page | 77


CS22411-OPERATING SYSTEMS LABORATORY
DATE: 28/4/25
EX.NO: 13

Implement the various Page Replacement Algorithms

AIM: To implement C programs to implement the various Page Replacement Algorithms

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() {

ROLL NO:2127230501126 Page | 78


int pages[MAX], n, f, i, j;
int fcfs_frame[10], lru_frame[10], opt_frame[10];
int lru_count[10];
int fcfs_k = 0, fcfs_faults = 0, lru_faults = 0, opt_faults = 0;
printf("Enter number of pages: ");
scanf("%d", &n);
printf("Enter the reference string:\n");
for (i = 0; i < n; i++)
scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &f);
for (i = 0; i < f; i++) {
fcfs_frame[i] = -1;
lru_frame[i] = -1;
opt_frame[i] = -1;
lru_count[i] = 0;}
for (i = 0; i < n; i++) {
int fcfs_hit = 0;
for (j = 0; j < f; j++) {
if (fcfs_frame[j] == pages[i]) {
fcfs_hit = 1;
break;}}
if (!fcfs_hit) {
fcfs_frame[fcfs_k] = pages[i];
fcfs_k = (fcfs_k + 1) % f;
fcfs_faults++;
}
int lru_hit = 0;
for (j = 0; j < f; j++) {
if (lru_frame[j] == pages[i]) {
lru_hit = 1;
lru_count[j] = i;
ROLL NO:2127230501126 Page | 79
break;}}
if (!lru_hit) {
int lru_pos = 0, min = lru_count[0];
for (j = 1; j < f; j++) {
if (lru_count[j] < min) {
min = lru_count[j];
lru_pos = j;}}
lru_frame[lru_pos] = pages[i];
lru_count[lru_pos] = i;
lru_faults++;}
int opt_hit = 0;
for (j = 0; j < f; j++) {
if (opt_frame[j] == pages[i]) {
opt_hit = 1;
break;}}
if (!opt_hit) {
int pos = predict(pages, opt_frame, n, i + 1, f);
opt_frame[pos] = pages[i];
opt_faults++;}}
printf("\nPage Faults:\n");
printf("FCFS: %d\n", fcfs_faults);
printf("LRU: %d\n", lru_faults);
printf("Optimal: %d\n", opt_faults);
return 0;
}
OUTPUT:-
Enter number of pages: 10
Enter the reference string:
12
10
11
12
ROLL NO:2127230501126 Page | 80
10
11
13
11
10
9
Enter number of frames: 3

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.

ROLL NO:2127230501126 Page | 81


CS22411-OPERATING SYSTEMS LABORATORY
DATE: 28/4/25
EX.NO: 14

File Organization Techniques


AIM:
To implement C programs to implement the various File Organization Techniques

SINGLE LEVEL DIRECTORY ORGANIZATION


ALGORITHM:
Step 1 - Initialize the directory by defining a structure with fields for the directory name, a
2D array for filenames, and a file count.
Step 2 - Prompt the user to enter the directory name and set the file count to zero.
Step 3 - Display a menu in a loop with options to create, delete, search, display files, or exit.
Step 4 - Use a loop and switch-case to handle the user's menu choice.
Step 5 - If creating a file, prompt for the filename, store it, and increment the file count.
Step 6 - If deleting a file, prompt for the filename, search for it, and if found, replace it with
the last file and decrement the count; otherwise, show not found.
Step 7 - If searching for a file, prompt for the name, search the array, and display whether it
is found or not.
Step 8 - If displaying files, list all filenames if available, or show a message if the directory
is empty.
Step 9 - If exit is chosen, terminate the program using exit(0).

CODE:
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;

ROLL NO:2127230501126 Page | 82


void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter 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--;
ROLL NO:2127230501126 Page | 83
break;
case 3: printf("\n Enter 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("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);}
break;
default: exit(0);}}
getch();}

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

ROLL NO:2127230501126 Page | 85


TWO LEVEL DIRECTORY ORGANIZATION

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)

ROLL NO:2127230501126 Page | 86


{printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{if(strcmp(d,dir[i].dname)==0)
{printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{if(strcmp(f, dir[i].fname[k])==0)
{printf("File %s is deleted ",f);
dir[i].fcnt--;
ROLL NO:2127230501126 Page | 87
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;}}
printf("File %s not found",f);
goto jmp;}}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{if(strcmp(d,dir[i].dname)==0)
{printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{if(strcmp(f, dir[i].fname[k])==0)
{printf("File %s is found ",f);
goto jmp1;}}
printf("File %s not found",f);
goto jmp1;}}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);}}
break;
default:exit(0);}}
getch();}

ROLL NO:2127230501126 Page | 88


OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Enter name of directory -- DIR1
Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Enter name of directory -- DIR2
Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 2
Enter name of the directory – DIR1
Enter name of the file -- A1
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 2
Enter name of the directory – DIR1
Enter name of the file -- A2
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 2
Enter name of the directory – DIR2
Enter name of the file -- B1
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 5
Directory Files
DIR1 A1 A2
DIR2 B1
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 4
Enter name of the directory – DIR
ROLL NO:2127230501126 Page | 89
Directory not found
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 3
Enter name of the directory – DIR1
Enter name of the file -- A2
File A2 is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice – 6

HIERARCHICAL DIRECTORY ORGANIZATION

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().

ROLL NO:2127230501126 Page | 90


CODE:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element
node; void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
display(root);
getch();
closegraph();}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) :",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 forfile :");
ROLL NO:2127230501126 Page | 91
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name); scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);}
else (*root)->nc=0;}}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14); if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++)
{
ROLL NO:2127230501126 Page | 92
display(root->link[i]);}}}

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.

ROLL NO:2127230501126 Page | 94

You might also like