Documente Academic
Documente Profesional
Documente Cultură
SRM University
Ramapuram part,
Vadapalani
(City Campus)
LAB MANUAL
SRM/CSE/LM/CSCS1035
NAME OF THE
ACTIVITY DESIGNATION SIGNATURE
FACULTY
1
2
Associate Professor
APPROVED BY Dr. S.PRASANNA DEVI
& Head
CS1035 OPERATING SYSTEMS LABORATORY LTPC
0022
OBJECTIVES:
LIST OF EXPERIMENTS:
TOTAL: 30 PERIODS
2
3
INDEX
EXPT. NO TOPIC PAGE NO
3
4
EX.NO : 1
AIM :
ALGORITHM :
PROGRAM:
#include<stdio.h>
struct process
{
char PName[10];
int ex_time,wt_time,st_time,end_time,turn_time;
int rem_time,completed_time;
}p[10],temp;
int quant_time;
main()
{
int n,i,j,k;
float avgWaitTime=0.0,avgTurnAroundTime=0.0;
float totalWaitTime=0.0;
int totalExecTime=0,totalTurnAroundTime=0;
int fs,fe;
4
5
5
6
printf("\n%s\t%d\t%d\t%d\t%d\t%d",p[j].PName,p[j].st_time,p[j].end_time,p[j].ex_time,p[j].rem
_time,p[j].completed_time);
}
else if(p[j].rem_time>0)
{
p[j].end_time=p[j].st_time+p[j].rem_time;
p[j].wt_time=fe-p[j].completed_time;
p[j].completed_time+=p[j].rem_time;
p[j].turn_time=p[j].wt_time+p[j].ex_time;
fe+=p[j].rem_time;
p[j].rem_time=0;
printf("\n%s\t%d\t%d\t%d\t%d\t%d",p[j].PName,p[j].st_time,p[j].end_time,p[j].ex_time,p[j].rem
_time,p[j].completed_time);
}
j++;
if(j==n)
{
for(k=0;k<n;k++)
{
if(p[k].rem_time>0) /*break for the inner for loop */
{ j=k;
break; }
}
if(k>n) /* breaks from the outer while loop */
break;
}
}
printf("\n Process Name Waiting Time Turn Around Time");
for(i=0;i<n;i++)
{ printf("\n%s\t\t%d\t\t%d",p[i].PName,p[i].wt_time,p[i].turn_time);
totalWaitTime+=p[i].wt_time;
totalTurnAroundTime+=p[i].turn_time;
}
avgWaitTime=(float)totalWaitTime/n;
avgTurnAroundTime=(float)totalTurnAroundTime/n;
printf("\nAverage waiting time %f",avgWaitTime);
printf("\nAverage turn around time %f",avgTurnAroundTime);
}
6
7
OUTPUT:
enter number of process4
RESULT :
Thus C program was written and verified simulating round robin scheduling algorithm.
7
8
EX.NO :
DATE :
AIM :
To write a C program simulating the First Come First Served scheduling algorithm.
ALGORITHM
PROGRAM
#include<stdio.h>
struct process
{
char pName[10];
int ex_time,wt_time,st_time,end_time;
}p[10];
main()
{
int n,i,j,k;
float avgwaittime=0.0,avgTurnAroundTime=0.0;
float totalWaitTime=0.0;
int totalExecTime=0,totalTurnAroundTime=0;
printf("\n enter number of process");
8
9
scanf("%d",&n);
p[0].st_time=0;
p[0].wt_time=0;
for(i=0;i<n;i++)
{
printf("\n enter process name");
scanf("%s",p[i].pName);
printf("enter Burst time");
scanf("%d",&p[i].ex_time);
if(i==0) p[i].end_time=p[i].ex_time;
else
{
p[i].wt_time=p[i-1].end_time;
p[i].st_time=p[i-1].end_time;
p[i].end_time=p[i].st_time+p[i].ex_time;
}
}
for(j=0;j<n;j++)
{
totalExecTime+=p[j].ex_time;
totalWaitTime+=p[j].wt_time;
}
totalTurnAroundTime=totalExecTime+totalWaitTime;
avgwaittime=(float)totalWaitTime/n;
avgTurnAroundTime=(float)totalTurnAroundTime/n;
printf("\n\n Name Burst Start End Wait Time\n");
for(k=0;k<n;k++)
printf("\n%s \t%d \t%d \t%d \t%d",p[k].pName,p[k].ex_time,p[k].st_time,p[k
].end_time,p[k].wt_time);
printf("\nAverage Waiting Time %f",avgwaittime);
printf("\n Average Turn Around Time %f",avgTurnAroundTime);
}
9
10
p1 3 0 3 0
p2 2 3 5 3
p3 1 5 6 5
p4 4 6 10 6
p5 2 10 12 10
Average Waiting Time 4.800000
Average Turn Around Time 7.200000
RESULT :
Thus C program was written and verified simulating first come first served scheduling algorithm.
10
11
EX.NO :
DATE :
AIM :
ALGORITHM:
PROGRAM
#include<stdio.h>
struct process
{
char pname[10];
int ex_time,wt_time,st_time,end_time;
}p[10],temp;
main()
{
int n,i,j,k;
float avgwaittime=0.0,avgturnaroundtime=0.0;
11
12
float totalwaittime=0.0;
int totalexectime=0,totalturnaroundtime=0;
printf("\n enter the number of process");
scanf("%d",&n);
p[0].st_time=0;
p[0].wt_time=0;
for(i=0;i<n;i++)
{
printf("\n enter process name");
scanf("%s",p[i].pname);
printf("enter burst time");
scanf("%d",&p[i].ex_time);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
temp=p[i];p[i]=p[j];p[j]=temp;
}
}
p[0].wt_time=0;
p[0].st_time=0;
p[0].end_time=p[0].ex_time;
for(j=0;j<n;j++)
{
if(j>0)
{
p[j].wt_time=p[j-1].end_time;
p[j].st_time=p[j-1].end_time;
p[j].end_time=p[j].st_time+p[j].ex_time;
}
totalexectime+=p[j].ex_time;
totalwaittime+=p[j].wt_time;
}
avgwaittime=(float)totalwaittime/n;
totalturnaroundtime=totalexectime+totalwaittime;
avgturnaroundtime=(float)totalturnaroundtime/n;
printf("\n\nname brust start endwaittime\n");
for(k=0;k<n;k++)
printf("\n%s\t%d\t%d\t%d\t%d",p[k].pname,p[k].ex_time,p[k].st_time,p[k].end_time
,p[k].wt_time);
printf("\naverage waiting time %f",avgwaittime);
printf("\n average turnaroundtime %f",avgturnaroundtime);
}
12
13
RESULT :
Thus C program was written and verified simulating shortest job first cheduling algorithm.
13
14
EX.NO :
DATE :
AIM :
ALGORITHM:
PROGRAM :
#include<stdio.h>
struct process
{
char pname[10];
int ex_time,wt_time,st_time,end_time,turn_time,priority;
}p[10],temp;
main()
{
int n,i,j,k;
float avgwaittime=0.0,avgturnaroundtime=0.0;
float totalwaittime=0.0;
int totalexectime=0,totalturnaroundtime=0;
printf("\nenter number of process");
scanf("%d",&n);
p[0].st_time=0;
p[0].wt_time=0;
14
15
for(i=0;i<n;i++)
{
printf("\nenter process name");
scanf("%s",p[i].pname);
printf("\nenter process priority 0 with highest priority");
scanf("%d",&p[i].priority);
printf("enter burst time");
scanf("%d",&p[i].ex_time);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i].priority<p[j].priority)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
if(j==0)
{
p[j].wt_time=0;
p[j].st_time=0;
p[j].end_time=p[j].ex_time;
p[j].turn_time=p[j].ex_time+p[j].wt_time;
}
if(j>0)
{
p[j].wt_time=p[j-1].end_time;
p[j].st_time=p[j-1].end_time;
p[j].end_time=p[j].st_time+p[j].ex_time;
p[j].turn_time=p[j].ex_time+p[j].wt_time;
}
totalexectime+=p[j].ex_time;
totalwaittime+=p[j].wt_time;
totalturnaroundtime+=p[j].turn_time;
}
avgwaittime=(float)totalwaittime/n;
avgturnaroundtime=(float)totalturnaroundtime/n;
printf("\n\nname burst start end waittime\n");
for(k=0;k<n;k++)
printf("\n%s\t%d\t%d\t%d\t%d\t%d",p[k].pname,p[k].ex_time,p[k].st_time,p[k].end_
15
16
OUTPUT:
p3 4 0 4 0
p1 3 4 7 4
p4 2 7 9 7
p2 6 9 15 9
RESULT :
Thus C program was written and verified priority based scheduling algorithm.
16
17
DATE:
EXPT NO:
AIM:
ALGORITHM:
1. Declare and initialize the required variables.
2. Get the input for the number of files .
3. Get the number of blocks and starting block form the user
4. Allocate the blocks for files sequentially.
5. Print the filename, start block and length.
6. Repeat the step from 3 to 5 for n number of files
PROGRAM
#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if you want to enter more files?(y-1/n-0)");
17
18
scanf("%d",&c);
if(c==1)
goto X;
else
return(0);
getch();
}
18
19
OUTPUT
enter the starting block & length of file 4 3
4->1
5->1
6->1
the file is allocated to disk
if you want to enter more files?(y-1/n-0)1
RESULT
Thus a C program was written and verified simulating the sequential file allocation stratergy.
19
20
DATE:
EXPT NO:
AIM:
ALGORITHM:
1. Start the program by including the required header files.
2. Declare the required variables.
3. Get the input from user
4. Enter the file name and index block
5. Enter the child blocks
6. Print the file, index, length
PROGRAM
#include<stdio.h>
void main()
{
char a[10];
int i,ib,cib[10],k[50],cont;
for(i=0;i<50;i++)
k[i]=-1;
printf("\n enter the file name:");
scanf("%s",a);
printf("\n index block:");
scanf("%d",&ib);
k[ib]=ib;
for(i=1;i<=5;i++)
{
printf("\n enter the child index block %d:",i);
scanf("%d",&cib[i]);
if(k[cib[i]]!=-1)
{
printf("\n block already allocated");
i--;
continue;
}
k[cib[i]]!=cib[i];
20
21
}
printf("\n the list of files\t index block\n");
printf("%s\t\t %d",a,ib);
printf("\n");
for(i=1;i<=5;i++)
{
printf("%d\t\t",cib[i]);
}
printf("\n");
}
21
22
OUTPUT
RESULT
Thus a C program was written and verified simulating the indexed file allocation stratergy.
22
23
DATE:
EXPT NO:
AIM:
ALGORITHM:
1. Start the program by including the required header files.
2. Declare the required variables.
3. Get the input from user
4. Enter the file name and index block
5. Enter the child blocks
6. Print the file, index, length
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
23
24
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
24
25
OUTPUT:
RESULT
Thus a C program was written and verified simulating the linked file allocation stratergy.
25
26
DATE:
EXP NO:
AIM:
To show the working of MVT using C program.
ALGORITHM:
1.Start the program.
2.Include the header files.
3.Get input for the memory capacity and no of processes.
4.Caluculate the memory allocated to each process and remaining memory.
5.Display the value for external fragmentation.
6.Terminate the program.
PROGRAM:
#include<stdio.h>
void main()
{
int m=0,m1=0,m2=0,p,count=0,i;
printf("Enter the memory capacity:");
scanf("%d",&m);
printf("Enter the no of processes:");
scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("\nEnter memory req for process%d: ",i+1);
scanf("%d",&m1);
count=count+m1;
if(m1<=m)
{
if(count==m)
{
printf("There is no further memory remaining:");
}
else
{
printf("The memory allocated for process%d is: %d ",i+1,m);
m2=m-m1;
printf("\nRemaining memory is: %d",m2);
26
27
m=m2;
}
}
else
{
printf("Memory is not allocated for process%d",i+1);
}
printf("\nExternal fragmentation for this process is:%d",m2);
}
}
OUTPUT:
Enter the memory capacity:100
Enter the no of processes:4
RESULT
The program was written for MVT and executed successfully.
27
28
DATE:
EXP NO:
AIM :
To write a program to implement MFT.
ALGORITHM:
1. Start the program
2. Include header files
3. Get input for memory and no of partitions
4. Enter the memory required for all the process
5. Calculate internal or external fragmentation for the process
6. Calculate total no of fragmentation
7. Display the value
8. Terminate the program
PROGRAM:
#include<stdio.h>
void main()
{
int a[10],b[10],c[10],i,j,p,s,n,t=0,q[10];
clrscr();
printf("Enter the no. of process : ");
scanf("%d",&n);
printf("\nEnter the size of each process : ");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); q[i]=0;}
printf("Enter the size of memory: ");
scanf("%d",&s);
printf("Enter no. of partitions: ");
scanf("%d",&p);
for(i=0;i<p;i++)
b[i]=s/p;
for(i=0;i<n;i++)
{ if(b[i]>=a[i])
{ c[i]=b[i]-a[i]; q[i]=1; }
}
for(i=0;i<p;i++)
28
29
t+=c[i];
printf("\nProcess memory size process size fragmentation\n ");
for(i=0;i<p;i++)
{if(q[i]==1)
printf("\n%d \t %d\t %d\t %d\t\n",i,b[i],a[i],c[i]);
}
}
29
30
OUTPUT
Enter the no. of process : 4
0 20 5 15
1 20 10 10
2 20 20 0
RESULT:
The program for MFT was written and executed successfully..
30
31
DATE:
EXP NO:
Aim:
To write a C program to implement single level directory structure
Algorithm
1. Read the main directory name and the file names from the user
2. For the each set of main directory create a files under the specified structure
3. Print the directory name and the files under the directory
Program
#include<stdio.h>
#include<conio.h>
main()
{
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
clrscr();
printf("enter number of directorios:");
scanf("%d",&master);
printf("enter names of directories and size:");
for(i=0;i<master;i++)
{ scanf("%s",d[i]);
scanf("%d",&s[i]);
}
printf("enter the file names :");
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\n");
printf("*************************************************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
31
32
printf("\n");
}
printf("\t\n");
getch();
}
32
33
OUTPUT
enter number of directorios:
2
enter names of directories and size:d1
2
d2
3
enter the file names :a
b
c
d
e
d2 3 c
d
e
RESULT
Thus program was written implementing the single level directory
33
34
EXP NO:
Aim:
To write a C program to implement two level directory structure
Algorithm
1. Read the set of main directory names and the file names from the user
2. For the each set of main directory create a files under the specified structure
3. Print the directory name and the files under the directory
Program
#include<stdio.h>
#include<conio.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;
clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
34
35
35
36
OUTPUT
D2 1 SD3 2 F G
RESULT
Thus program was written implementing the two level directory structures
36
37
DATE:
EXP NO:
Aim:
To write a C program to implement tree structured directory
Algorithm
1. Read the root directory name from the user
2. Read the set of directory names and the file names from the user
3. Iterate the step 2 for n levels
4. Print the directory name and the files under the directory
Program
#include <stdio.h>
#include <malloc.h>
struct Directory{
struct Directory *parent, *child[5];
char name;
int n, level;
}*root;
printf("\t");
printf("%c",ptr->name);
if(ptr->n!=0)
printf(" ->");
for(i=0;i<ptr->n;i++){
disp(ptr->child[i]);
}
}
}
int main(){
root=init(root,NULL,0);
disp(root);
printf("\n");
return 1;
}
38
39
OUTPUT
Enter name: A
Enter number of files: 1
Enter name: B
Enter number of files: 2
Enter name: C
Enter number of files: 0
Enter name: D
Enter number of files: 2
Enter name: E
Enter number of files: 0
Enter name: F
Enter number of files: 0
A ->
B ->
C
D ->
E
F
RESULT
Thus program was written implementing the tree structured directory organization
39
40
Aim :
To create a graph using DAG format
Description
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.
Sample 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 d
ir/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 subdirectorie
s /files (for HTML):0
How many links:2
File/Dir: B
User Name: USER 2
File/Dir: HTML
40
41
DATE:
EXP NO:
AIM:
To implement deadlock avoidance using Bankers Algorithm.
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.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
printf("\n ENTER THE NO. OF PROCESSES:");
scanf("%d",&n);
printf("\n ENTER THE NO. OF RESOURCES:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d \n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCE %d:",j+1);
scanf("%d",&p[i].max[j]);
41
42
} for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCE %d:",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCE %d:",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n\t RESOURCES ALLOCATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\n P%d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
42
43
}
printf("\n\n\t AVAIL BEFORE\T AVAIL AFTER ");
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j] >av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\n P %d \t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;
}
else
{
cn=0;cz=0;
}
}
}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else printf("\n DEADLOCK OCCURED");
}
43
44
OUTPUT:
//TEST CASE 1:
ENTER THE NO. OF PROCESSES:4
ENTER THE NO. OF RESOURCES:3
PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:0
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:2
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6
44
45
//TEST CASE:2
ENTER THE NO. OF PROCESSES:4
ENTER THE NO. OF RESOURCES:3
PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:1
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:2
PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:2
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6
45
46
RESULT
Thus the C program was written for implementing deadlock avoidance using Bankers algorithm
46
47
DATE:
EXP NO:
AIM:
PROGRAM
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
47
48
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
48
49
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i);
if(finish[i]==1)
{
i=n;
}
}
49
50
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}
50
51
OUTPUT
Enter the number of the processes : 3
Enter the resource instances : 3
Enter the Max matrix :
3 6 8
4 3 3
3 4 4
Enter the allocation matrix
3 3 3
2 0 3
1 2 4
Enter the available resource
1 2 0
Process Allocation Max Available
P1 3 3 3 3 6 8 1 2 0
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4
System is in deadlock and the deadlock process are
P0 P1 P2
RESULT
Thus the C program was written for detecting deadlocks in the system
51
52
DATE :
EXP. NO
AIM:
To implement FIFO (First In First Out) page replacement algorithms
ALGORITHM:
FIFO:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n Enter no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
52
53
{
if(frm[j]==ref[i])
{
flag=1;
break;
}}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
getch();
}
53
54
OUTPUT:
RESULT
Thus the program has been implemented similating FIFO page replacement algorithm
54
55
DATE :
EXP. NO
AIM: To implement LRU (Least Recently Used) page replacement algorithm
ALGORITHM:
PROGRAM
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
56
57
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
57
58
OUTPUT:
Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1
RESULT
Thus C program was written and verified defining LRU page replacement algorithm.
58
59
DATE :
EXP.NO
ALGORITHM:
PROGRAM
#include<stdio.h>
int main()
{
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no/reference string: \n");
59
60
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
60
61
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}
61
62
OUTPUT:
Enter no of frames:3
Enter no of pages:10
Enter page no/reference string:
2
3
4
2
1
3
7
5
4
3
2 -1 -1
2 3 -1
2 3 4
2 3 4
2 1 4
2 1 3
2 7 3
2 1 5
2 7 5
2 4 5
2 4 3
RESULT
Thus C program was written for implementing LFU page replacement algorithm
62
63
DATE :
EX.NO
AIM:
To write a C program to implement memory management using paging technique.
ALGORITHM:
PROGRAM
#include <stdio.h>
#include <conio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\nMEMORY MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter the partition size: ");
63
64
scanf("%d",&psize);
frame = (int) pmsize/psize;
page = (int) lmsize/psize;
printf("\nThe physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for (i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit= -1;
}
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
{
printf("\n\nEnter the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
printf("\n\nPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
getch();
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
64
65
scanf("%d",&baddr);
printf("\nEnter theLogical Address: ");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
info();
assign();
cphyaddr();
}
OUTPUT:
PAGE TABLE
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
65
66
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
RESULT:
Thus C program was written implementing the paging techinique.
66
67
DATE :
EX.NO
AIM
To write a C program to implement inter-process communication using shared memory
ALGORITHM
1. Create a Child process using fork system call, and supporting two process acces for
sharing the memory
2. Create a shared memory segment form shmget system call with necessary priviliages.
3. Attach the shared memeory to the process when required using shmat system call.
4. Write the string into the shared memory segment from parent process.
5. Read the string from the child from the same shared region.
6. Detach the shared memory and the process.
PROGRAM
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
main()
{
int child, shmid,i;
char *shmptr;
child=fork();
if(!child)
{
shmid=shmget(2041,32,0666|IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf("\nParent writing.........\n");
for(i=0;i<10;i++)
{
shmptr[i]='a'+i;
putchar(shmptr[i]);
}
printf("\n%s",shmptr);
wait(NULL);
}
67
68
else
{
shmid=shmget(2041,32,0666|IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf("\nReading from child.........\n");
for(i=0;i<10;i++)
putchar(shmptr[i]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
return 0;
}
68
69
OUTPUT:
parent writing...
reading from child...
abcdefghijabcdefghij
abcdefghij
RESULT :
Thus C program was program written to implement IPC using shared the sy memory.
69
70
DATE :
EX.NO
AIM
To implement multithreading with synchronization in Java
PROGRAM
import java.io.*;
import java.lang.*;
class PrintDemo{
int i;
void print(String threadName){
try{
for(i=5;i>0;i--){
System.out.println("Counter " + i + " - Thread - " + threadName);
}
}catch(Exception e){
System.out.println("Exception\n");
}
}
}
class simplethread extends Thread{
private Thread t;
private String name;
PrintDemo pd;
sth1.start();
sth2.start();
try{
sth1.join();
sth2.join();
}catch(Exception e){
System.out.println("exception\n");
}
}
}
71
72
OUTPUT
RESULT:
72
73
73