Os Lab Manual 7 - 9 Programs
Os Lab Manual 7 - 9 Programs
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;
getch();
}
OUTPUT:-
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
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");
return 0;
}
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
} Node;
return graph;
}
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);
return 0;
}
OUTPUT:
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
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
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
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 :
}
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.