0% found this document useful (0 votes)
49 views17 pages

Os Lab Manual 7 - 9 Programs

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)
49 views17 pages

Os Lab Manual 7 - 9 Programs

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

FILE ORGANIZATION TECHNIQUES

A) SINGLE LEVEL DIRECTORY:

AIM: Program to simulate Single level directory file organization technique.

DESCRIPTION:

The directory structure is the organization of files into a hierarchy of folders. In a single-level
directory system, all the files are placed in one directory. There is a root directory which has all
files. It has a simple architecture and there are no sub directories. Advantage of single level
directory system is that it is easy to find a file in the directory. In the two-level directory system,
each user has own user file directory (UFD). The system maintains a master block that has one
entry for each user. This master block contains the addresses of the directory of the users.
When a user job starts or a user logs in, the system's master file directory (MFD) is searched.
When a user refers to a particular file, only his own UFD is searched.
This effectively solves the name collision problem and isolates users from one another.
Hierarchical directory structure allows users to create their own subdirectories and to organize
their files accordingly. A tree is the most common directory structure. The tree has a root
directory, and every file in the system has a unique path name. A directory (or subdirectory)
contains a set of files or subdirectories.

SOURCE CODE :-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. 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("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;

case 2: printf("\nEnter 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--;
break;
case 3: printf("\nEnter 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("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default:
exit(0);
}
}

getch();
}
OUTPUT:-

B) TWO LEVEL DIRECTORY

AIM: Program to simulate two level file organization technique


ALGORITHM:
Step 1:Start
Step 2: Initialize structure elements struct tree_ element char
name[20]; Initialize integer variables x, y, ftype, lx, rx, nc, level; struct
tree_element
*link[5];}
typedef structure tree_element node;
Step 3:start main function
Step 4: Step variables gd=DETECT,gm; node *root; root=NULL;
Step 5:create structure using create(&root,0,"null",0,639,320);
Step 6:initgraph(&gd, &gm,"c:\tc\bgi"); display(root); closegraph();
Step 7: end main function
Step 8: Initialize variables i,gap;
Step 9:if check *root==NULL (*root)=(node*)malloc(sizeof(node));
enter name of ir file namein dname; fflush(stdin); gets((*root)-
>name);
Step 10 if check lev==0||lev==1 (*root)->ftype=1; else(*root)->ftype=2; (*root)-
>level=lev; (*root)->y=50+lev*5; (*root)->x=x; (*root)->lx=lx; (*root)->rx=rx; Step 11:for
i=0&&i<5 increment i
(*root)->link[i]=NULL;
if check (*root)->ftype==1
Step 12: if check (lev==0||lev==1)
if check(*root)->level==0
print "how many users"
else print"how many files"
print (*root)->name
read (*root)->nc
Step 13:Then (*root)->nc=0; if check(*root)->nc==0 gap=rx-lx; else gap=(rx-lx)/(*root)->nc;
Step 14:for i=0&&i<(*root)->nc increment i;
create(&((*root)->link[i]),lev+1,(*root)->name, lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
then
(*root)->nc=0;
Step 15: Initialize e display function Initialize i set textstyle(2,0,4);
set textjustify(1,1); set fillstyle(1,BLUE);
setcolor(14); step 13:if check root!=NULL Step 16:for
i=0&&i<root->nc increment i line(root->x,root->y,root-
>link[i]->x,root->link[i]->y); Step 17: if check root-
>ftype==1
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else fill ellipse(root->x,root->y,20,20);
out textxy(root->x,root->y,root->name);

Step 18:for i=0&&i<root->nc increment i display(root->link[i]);


Step 19:End
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t 5.Display\t6. Exit\tEnter your choice --");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;

case 2: 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",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++)
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--;
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 ot 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();
}

OUTPUT:-
HIERARCHICAL DIRECTORY:

The two level directories eliminate name conflicts among users but it is not
satisfactory for users but it is not satisfactory for users with a large no of files. To
avoid this, create the subdirectory and load the same type of the files into the
subdirectory. So, in this method each can have as many directories are needed. This
directory structure looks like tree, that’s why it is also said to be tree-level directory
structure

SOURCE CODE :-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_FILES 10
#define MAX_DIRS 5

// Structure for a file


typedef struct {
char name[20];
} File;

// Structure for a directory, which can contain files and subdirectories


typedef struct Directory {
char name[20];
struct Directory* subdirs[MAX_DIRS];
File files[MAX_FILES];
int file_count;
int subdir_count;
} Directory;

// Function to create a new directory


Directory* createDirectory(char name[]) {
Directory* newDir = (Directory*)malloc(sizeof(Directory));
strcpy(newDir->name, name);
newDir->file_count = 0;
newDir->subdir_count = 0;
return newDir;
}

// Function to add a file to a directory


void addFile(Directory* dir, char name[]) {
if (dir->file_count < MAX_FILES) {
strcpy(dir->files[dir->file_count].name, name);
dir->file_count++;
} else {
printf("File limit reached in directory %s\n", dir->name);
}
}

// Function to add a subdirectory to a directory


void addSubDirectory(Directory* parent, Directory* child) {
if (parent->subdir_count < MAX_DIRS) {
parent->subdirs[parent->subdir_count] = child;
parent->subdir_count++;
} else {
printf("Subdirectory limit reached in directory %s\n", parent->name);
}
}

// Function to display the contents of a directory recursively


void displayDirectory(Directory* dir, int depth) {
int i=0, j=0;
for ( i = 0; i < depth; i++) {
printf(" "); // Indentation for hierarchy
}
printf("|-- %s\n", dir->name);

// Display files in the current directory


for ( i = 0; i < dir->file_count; i++) {
for ( j = 0; j < depth + 1; j++) {
printf(" ");
}
printf("|-- %s\n", dir->files[i].name);
}

// Display subdirectories recursively


for (i = 0; i < dir->subdir_count; i++) {
displayDirectory(dir->subdirs[i], depth + 1);
}
}

int main() {
// Create root directory
int i,j;
Directory* root = createDirectory("root");
// Create some subdirectories
Directory* home = createDirectory("home");
Directory* user = createDirectory("user");
Directory* docs = createDirectory("docs");

// Add files to directories


addFile(home, "file1.txt");
addFile(home, "file2.txt");
addFile(user, "profile.jpg");
addFile(docs, "doc1.pdf");

// Build the directory structure


addSubDirectory(root, home);
addSubDirectory(home, user);
addSubDirectory(user, docs);

// Display the directory structure


printf("Hierarchical File Organization:\n");
displayDirectory(root, 0);

// Free allocated memory (optional for this simulation)


free(root);
free(home);
free(user);
free(docs);

return 0;
}

GENERAL GRAPH DIRECTORY:

When we add links to an existing tree structured directory, the tree


structure is destroyed, resulting in a simple graph structure. This structure is
used to traversing is easy and file sharing also possible.

SOURCE CODE
#include <stdio.h>
#include <stdlib.h>

// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;

// Adjacency list structure


typedef struct Graph {
int numVertices;
Node** adjList;
} Graph;

// Create a new graph


Graph* createGraph(int numVertices)
{
int i;
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numVertices = numVertices;
graph->adjList = (Node**)malloc(numVertices * sizeof(Node*));

for ( i = 0; i < numVertices; i++)


{
graph->adjList[i] = NULL;
}

return graph;
}

// Add a directed edge to the graph


void addEdge(Graph* graph, int src, int dest) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = dest;
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;
}

int main() {
Graph* graph = createGraph(6);

addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 5);
addEdge(graph, 4, 5);

// Perform topological sort or other DAG operations

return 0;
}

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): 2
Enter Name of dir/file (under USER1): VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 2
Enter Name of dir/file (under VB): A
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under VB): B
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under USER1): C
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under ROOT): USER2
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER2): 1
Enter Name of dir/file (under USER2):JAVA
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA):2
Enter Name of dir/file (under JAVA):D
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under JAVA):HTML
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for HTML):0
How many links:2
File/Dir: B
User Name: USER 2
File/Dir: HTML
User Name: USER1
EXPERIMENT NO 8:

DEAD LOCK AVOIDANCE


AIM: Simulate bankers algorithm for Dead Lock Avoidance (Banker‘s Algorithm)

DESCRIPTION:
Deadlock is a situation where in two or more competing actions are waiting f or the other to
finish, and thus neither ever does. When a new process enters a system, it must declare the
maximum number of instances of each resource type it needed. This number may exceed the
total number of resources in the system. When the user request a set of resources, the system
must determine whether the allocation of each resources will leave the system in safe state. If it
will the resources are allocation; otherwise the process must wait until some other process
release the resources.
Data structures

n-Number of process, m-number of resource types.

Available: Available[j]=k, k – instance of resource type Rj is available. Max: If max[i, j]=k, Pi


may request at most k instances resource Rj.
Allocation: If Allocation [i, j]=k, Pi allocated to k instances of resource Rj Need: If Need[I,
j]=k, Pi may need k more instances of resource type Rj, Need[I, j]=Max[I, j]- Allocation[I, j];
Safety Algorithm

1. Work and Finish be the vector of length m and n respectively, Work=Available


and Finish[i] =False.
2. Find an i such that both
3. Finish[i] =False Need<=Work If no such I exists go to step 4.
4. work= work + Allocation, Finish[i] =True;

5. if Finish[1]=True for all I, then the system is in safe state. Resource request
algorithm
Let Request i be request vector for the process Pi, If request i=[j]=k, then process Pi wants
k instances of resource type Rj.
1. if Request<=Need I go to step 2. Otherwise raise an error condition.
2. if Request<=Available go to step 3. Otherwise Pi must since the resources are available.
3. Have the system pretend to have allocated the requested resources to process Pi
by modifying the state as follows;
Available=Available-Request I; Allocation I

=Allocation +Request I; Need i=Need i- Request I;


If the resulting resource allocation state is safe, the transaction is completed and process Pi is
allocated its resources. However if the state is unsafe, the Pi must wait for Request i and the
old resource-allocation state is restored.
Operating Systems LAB 2019-
2020

ALGORITHM:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state.
8. If the new request comes then check that the system is in safety.
9. or not if we allow the request.
10. stop the program.
11. End

SOURCE CODE :

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int alloc[10][10],max[10][10]; int
avail[10],work[10],total[10]; int i,j,k,n,need[10][10];
int m;
int count=0,c=0; char finish[10]; clrscr();
printf("Enter the no. of processes and resources:");
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++) finish[i]='n';
printf("Enter the claim matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
printf("Enter the allocation matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]);
printf("Resource vector:");
for(i=0;i<m;i++)
scanf("%d",&total[i]);
for(i=0;i<m;i++)
avail[i]=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
avail[j]+=alloc[i][j];
Operating Systems LAB 2019-
2020

for(i=0;i<m;i++)
work[i]=avail[i];
for(j=0;j<m;j++)
work[j]=total[j]-work[j];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
A:for(i=0;i<n;i++)
{ c=0; for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n'
)) c++;
if(c==m)
{
printf("All the resources can be allocated to Process
%d", i+1); printf("\n\nAvailable resources are:");
for(k=0;k<m;k++)
{
work[k]+=alloc[i][k];
printf("%4d",work[k]);
}
printf("\n");
finish[i]='y';
printf("\nProcess %d executed?:%c \n",i+1,finish[i]); count++;
}}
if(count!=n) goto A; else
printf("\n System is in safe mode"); printf("\n The given state is safe state");
getch();
}

OUTPUT
Enter the no. of processes and resources: 4 3 Enter the claim matrix:
322
613
314
422
Enter the allocation matrix:
100
612
211
002
Resource vector:9 3 6
All the resources can be allocated to Process 2 Available
resources are: 6 2 3 Process 2 executed?:y
All the resources can be allocated to Process 3 Avaiable
resources are: 8 3 4 Prcess 3 executed?:y
All the resources can be allocated to Process 4 Avilable
resources are: 8 3 6 Prcess 4 executed?:y
All the resources can be allocated to Process 1 Aailable resources are: 9 3 6
Prcess 1 executed?:y System is in safe mode
The given state is safe state
Operating Systems LAB 2019-
2020
EXPERIMENT NO 9:

DEAD LOCKPREVENTION

AIM: To implement deadlock prevention technique

Banker‘s Algorithm:
When a new process enters a system, it must declare the maximum
number of instances of each resource type it needed. This number may
exceed the total number of resources in the system. When the user
request a set of resources, the system must determine whether the
allocation of each resources will leave the system in safe state. If it will
the resources are allocation; otherwise the process must wait until
some other process release the resources.
DESCRIPTION:
Data structures
n-Number of process, m-number of resource types.
Available: Available[j]=k, k – instance of resource type Rj is available.
Max: If max[i, j]=k, Pi may request at most k instances resource Rj.
Allocation: If Allocation [i, j]=k, Pi allocated to k instances of
resource Rj Need: If Need[I, j]=k, Pi may need k more instances of
resource type Rj, Need[I, j]=Max[I, j]-Allocation[I, j];
Safety Algorithm
Work and Finish be the vector of length m and n respectively,
Work=Available and
Finish[i] =False. Find an i such that both
Finish[i] =False Need<=Work
If no such I exists go to step 4.
work=work+Allocation, Finish[i] =True;
if Finish[1]=True for all I, then the system is in safe state
ALGORITHM:
1. Start the program.
2. Get the values of resources and processes.
3. Get the avail value.
4. After allocation find the need value.
5. Check whether its possible to allocate.
6. If it is possible then the system is in safe state.
7. Else system is not in safety state
8. Stop the process.

SOURCE CODE :

#include<stdio.h> #include<conio.h> void main()


{
char job[10][10];
int time[10],avail,tem[10],temp[10]; int safe[10];
int ind=1,i,j,q,n,t; clrscr();
Oper
printf("Enter no of jobs: "); scanf("%d",&n); for(i=0;i<n;i++)
{
printf("Enter name and time: "); scanf("%s%d",&job[i],&time[i]);
}
printf("Enter the available resources:"); scanf("%d",&avail);
for(i=0;i<n;i++)
{
temp[i]=time[i]; tem[i]=i;
}
for(i=0;i<n;i++) for(j=i+1;j<n;j++)
{
if(temp[i]>temp[j])
{
t=temp[i]; temp[i]=temp[j]; temp[j]=t; t=tem[i]; tem[i]=tem[j]; tem[j]=t;
}
}
for(i=0;i<n;i++)
{
q=tem[i]; if(time[q]<=avail)
{
safe[ind]=tem[i]; avail=avail-tem[q];
//printf("%s",job[safe[ind]]);
ind++;

}
else
{
printf("No safe sequence\n");
}
}
printf("Safe sequence is:");
for(i=1;i<ind; i++) printf(" %s
%d\n",job[safe[i]],time[safe[i]]);
getch(); }

OUTPUT:
Enter no of jobs:4
Enter name and time: A 1 Enter name and time: B 4 Enter name and
time: C 2 Enter name and
time: D 3
Enter the available resources: 20 Safe sequence is: A 1, C 2, D 3, B 4.

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