Sunteți pe pagina 1din 73

1

SRM University
Ramapuram part,
Vadapalani
(City Campus)

LAB MANUAL

CS1035 OPERATING SYSTEMS LAB

SRM/CSE/LM/CSCS1035

NAME OF THE
ACTIVITY DESIGNATION SIGNATURE
FACULTY

PREPARED BY Assistant Professor

VERIFIED BY Assistant Professor

1
2

Associate Professor
APPROVED BY Dr. S.PRASANNA DEVI
& Head
CS1035 OPERATING SYSTEMS LABORATORY LTPC
0022
OBJECTIVES:

To implement Scheduling algorithms


To implement deadlock algorithms and page replacement algorithms
To simulate memory management schemes, Threads and synchronization

LIST OF EXPERIMENTS:

1.Simulate the following CPU scheduling algorithms


a.Round Robin b) SJF c) FCFS d) Priority
2.Simulate all file allocation strategies
a.Sequential b) Indexed c) Linked
3.Simulate MVT and MFT
4.Simulate all File Organization Techniques
a.Single level directory b) Two level c) Hierarchica
5.Simulate Bankers Algorithm for Dead Lock Avoidance
6.Simulate an Algorithm for Dead Lock Detection
7.Simulate all page replacement algorithms
a.FIFO b) LRU c) LFU
8.Simulate Shared memory and IPC
9.Simulate Paging Technique of memory management.
10.Implement Threading & Synchronization Application

TOTAL: 30 PERIODS

2
3

INDEX
EXPT. NO TOPIC PAGE NO

1.A CPU SCHEDULING ROUND ROBIN


1.B CPU SCHEDULING - FCFS
1.C CPU SCHEDULING SHORTEST JOB FIRST
1.D CPU SCHEDULING PRIORITY
2.A SEQUENTIAL FILE ALLOCATION
2.B INDEXED FILE ALLOCATION
2.C LINKED FILE ALLOCATION
3.A MULTI PROGRAMMING USING VARIABLE
NUMBER OF TASKS(MVT)
3.B MULTI-PROGRAMMING USING FIXED
NUMBER OF TASKS
4.A FILE ORGANISATION USING SINGLE
LEVEL
4.B FILE ORGANIZATION USING TWO
LEVELS
4.C TREE STRUCTURED DIRECTORY
STRUCTURE
4.D DIRECTED ACYCLIC GRAPH
5 BANKERS ALGORITHM FOR DEADLOCK
AVOIDANCE
6 BANKERS ALOGRITHM FOR DEADLOCK
PREVENTION
7.A PAGE REPLACEMENT METHODS: FIFO
7.B PAGE REPLACEMENT METHOD: LRU
7.C PAGE REPLACEMENT METHOD: LFU
8 PAGING TECHNIQUE IN MEMORY
MANAGEMENT
9 SHARED MEMORY AND IPC
10 MULTI THREADING

3
4

1. A. PROCESS SCHEDULING ROUND ROBIN

EX.NO : 1

AIM :

To write a C program simulating the working of Round Robin scheduling algorithm.

ALGORITHM :

1: Initialize all the structure elements


2: Receive inputs from the user for process id, and burst time
3: Calculate the waiting time for all the process id.
a. Waiting time for the current process is calculated by the difference in start
time of that instance to the quantum time
b. Remaining time provides the time for which the processes still remaining
execution time over completion
4. Calculate the turnaround time which is execution time added to the
total waiting time for the process
5. Calculate the average waiting time and average turnaround time
6. Print the results of the step 4.

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

printf("\nEnter number of process");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter process name");
scanf("%s",p[i].PName);
printf("Enter Burst time ");
scanf("%d",&p[i].ex_time);
p[i].rem_time=p[i].ex_time;
p[i].completed_time=0;
}
printf("\nEnter quantum time ");
scanf("%d",&quant_time);
printf("\n name start end busrt rem compl");
/* for first process start and the wait time to be 0 */
j=0;
fs=p[j].st_time=0;
p[j].wt_time=0;
if(p[j].ex_time>quant_time)
{ p[j].end_time=quant_time;
p[j].completed_time=quant_time;
}
else
{p[j].end_time=p[j].ex_time;
p[j].completed_time=p[j].ex_time;
p[j].wt_time=p[j].end_time-p[j].rem_time;
p[j].turn_time=p[j].wt_time+p[j].ex_time;
}
p[j].rem_time=p[j].ex_time-p[j].completed_time;
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);
fe=p[j].end_time;
j++;
while(j<n)
{
p[j].st_time=fe;
if(p[j].rem_time>quant_time)
{
p[j].end_time=p[j].st_time+quant_time;
p[j].rem_time-=quant_time;
p[j].completed_time+=quant_time;
fe+=quant_time;

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

enter process name p1


enter burst time 3

enter process namep2


enter burst time6

enter process namep3


enter burst time4

enter process namep4


enter burst time2

enter quantum time2

name start end burst rem comp


p1 0 2 3 1 2
p2 2 4 6 4 2
p3 4 6 4 2 2
p4 6 8 2 0 2
p1 8 9 3 0 3
p2 9 11 6 2 4
p3 11 13 4 0 4
p2 13 15 6 0 6
processname waitingtime turnaroundtime
p1 6 9
p2 9 15
p3 9 13
p4 6 8
average waiting time7.500000
average turn around time 11.250000

RESULT :
Thus C program was written and verified simulating round robin scheduling algorithm.

7
8

1.B. FCFS SCHEDULING

EX.NO :

DATE :

AIM :

To write a C program simulating the First Come First Served scheduling algorithm.

ALGORITHM

1. Create the number of process.


2. Get the ID and Service time for each process.
3. Initially, Waiting time of first process is zero and Total time for the first process is
the starting time of that process.
4. Calculate the Total time and Processing time for the remaining processes.
5. Waiting time of one process is the Total time of the previous process.
6. Total time of process is calculated by adding Waiting time and Service time.
7. Total waiting time is calculated by adding the waiting time for lack process.
8. Total turn around time is calculated by adding all total time of each process.
9. Calculate Average waiting time by dividing the total waiting time by total number of
process.
10. Calculate Average turn around time by dividing the total time by the number of process.
11. Display the result.

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

SAMPLE INPUT/ OUTPUT:


enter number of process 5

enter process name p1


enter Burst time3
enter process name p2
enter Burst time2
enter process name p3
enter Burst time1
enter process name p4
enter Burst time4
enter process namep5
enter Burst time2

Name Burst Start End Wait Time

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

1.C. SHORTEST JOB FIRST SCHEDULING

EX.NO :

DATE :

AIM :

To write a C program simulating the Shortest Job First scheduling algorithm.

ALGORITHM:

1. Get the number of process.


2. Get the id and service time for each process.
3. Initially the waiting time of first short process as 0 and total time of first short is process
the service time of that process.
4. Calculate the total time and waiting time of remaining process.
5. Waiting time of one process is the total time of the previous process.
6. Total time of process is calculated by adding the waiting time and service time of each
process.
7. Total waiting time calculated by adding the waiting time of each process.
8. Total turnaround time calculated by adding all total time of each process.
9. Calculate average waiting time by dividing the total waiting time by total number
of process.
10. Calculate average turnaround time by dividing the total waiting time by total
number of process
11. Display the result.

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

SAMPLE INPUT / OUTPUT:

enter the number of process4


enter process namep1
enter burst time3
enter process namep2
enter burst time6
enter process namep3
enter burst time4
enter process namep4
enter burst time2
name burst start end waittime
p4 2 0 2 0
p1 3 2 5 2
p3 4 5 9 5
p2 6 9 15 9
average waiting time 4.000000
average turnaroundtime 7.750000

RESULT :
Thus C program was written and verified simulating shortest job first cheduling algorithm.

13
14

1.D. PRIORITY SCHEDULING

EX.NO :

DATE :

AIM :

To write a C program simulating the Priority scheduling algorithm.

ALGORITHM:

1. Get the number of process,burst time and priority.


2. Arrange the process based on the priority
3. Repeat the steps for all process
a. Calculate the waiting time for the process
b. Calculate the turn around time for the process
4. Total waiting time is calculated by adding the waiting time for lack process.
5. Total turn around time is calculated by adding all total time of each process.
6. Calculate Average waiting time by dividing the total waiting time by total numberof process.
7Calculate Average turn around time by dividing the total time by the number of process.
8. Display the result.

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

time,p[k].wt_time); 37,1 87%


printf("\naveragewaitingtime%f",avgwaittime);
printf("\naverageturnaroundtime%f",avgturnaroundtime);
}

OUTPUT:

Enter the number of processes 4

Enter process name p1


Enter process priority 0 with highest priority 2
Enter burst time 3

Enter process name p2

Enter process priority 0 with highest priority 4


Enter burst time 6

Enter process name p3


Enter process priority 0 with highest priority 1
Enter burst time4

Enter process name p4


Enter process priority 0 with highest priority3
Enter burst time2

name burst start end waittime

p3 4 0 4 0
p1 3 4 7 4
p4 2 7 9 7
p2 6 9 15 9

average waiting time 5.000000


average turnaroundtime 8.750000

RESULT :
Thus C program was written and verified priority based scheduling algorithm.

16
17

2.A SEQUENTIAL FILE ALLOCATION

DATE:

EXPT NO:

AIM:

To write a program for sequential file allocation using c programming.

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

enter the starting block & length of file5 2


block already allocated
if you want to enter more files?(y-1/n-0)0

RESULT
Thus a C program was written and verified simulating the sequential file allocation stratergy.

19
20

2. B INDEXED FILE ALLOCATION

DATE:

EXPT NO:

AIM:

To write a program for indexed file allocation stratergy using c programming.

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

enter the file name:A


index block:4
enter the child index block 1:2
enter the child index block 2:12
enter the child index block 3:19
enter the child index block 4:5
enter the child index block 5:6

the list of files index block


A 4
2 12 19 5 6

RESULT
Thus a C program was written and verified simulating the indexed file allocation stratergy.

22
23

2. C LINKED FILE ALLOCATION

DATE:

EXPT NO:

AIM:

To write a program for indexed file allocation stratergy using c programming.

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:

Enter no. of files:2


Enter file name:abcd
Enter starting block:20
Enter no.of blocks:6
Enter block numbers: 4
12
15
45
32
25
Enter file name:efgh
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6
5
4
3
2
File start size block
abcd 20 6 4--->12--->15--->45--->32--->25
efgh 12 5 6--->5--->4--->3--->2

RESULT
Thus a C program was written and verified simulating the linked file allocation stratergy.

25
26

3. A. MULTI PROGRAMMING USING VARIABLE NUMBER OF TASKS(MVT)

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

Enter memory req for process1: 50


The memory allocated for process1 is: 100
Remaining memory is: 50
External fragmentation for this process is:50
Enter memory req for process2: 10
The memory allocated for process2 is: 50
Remaining memory is: 40
External fragmentation for this process is:40
Enter memory req for process3: 8
The memory allocated for process3 is: 40
Remaining memory is: 32
External fragmentation for this process is:32
Enter memory req for process4: 34
Memory is not allocated for process4
External fragmentation for this process is:32

RESULT
The program was written for MVT and executed successfully.

27
28

3. B MULTI-PROGRAMMING USING FIXED NUMBER OF TASKS

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

Enter the size of each process : 5 10 20 30


Enter the size of memory: 100
Enter no. of partitions: 5

Process memory size process size fragmentation

0 20 5 15

1 20 10 10

2 20 20 0

RESULT:
The program for MFT was written and executed successfully..

30
31

4.A SINGLE LEVEL DIRECTORY STRUCTURE

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

directory size filenames


*************************************************
d1 2 a
b

d2 3 c
d
e

RESULT
Thus program was written implementing the single level directory

33
34

B. TWO LEVEL DIRECTORY STRUCTURE


DATE:

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

printf("enter file name:");


scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n"); }
getch(); }

35
36

OUTPUT

enter number of directories:2


enter directory 1 names:D1
enter size of directories:2
enter subdirectory name and size:SD1 2
enter file name:A
enter file name:B
enter subdirectory name and size:SD2
3
enter file name:C
enter file name:D
enter file name:E
enter directory 2 names:D2
enter size of directories:1
enter subdirectory name and size:SD3 2
enter file name:F
enter file name:G

dirname size subdirname size files


******************************************************
D1 2 SD1 2 A B
SD2 3 C D E

D2 1 SD3 2 F G

RESULT
Thus program was written implementing the two level directory structures

36
37

C. TREE STRUCTURED DIRECTORY

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;

struct Directory* init(struct Directory *ptr, struct Directory *parent,int level){


int i;
struct Directory* node=(struct Directory*)malloc(sizeof(struct Directory));
node->parent=parent;
node->level=level;
printf(" Enter name: ");
scanf(" %s",&node->name);
printf(" Enter number of files: ");
scanf(" %d",&node->n);
ptr=node;
for(i=0; i<ptr->n; i++)
node->child[i]=init(node->child[i],node,node->level+1);
return node;
}
void disp(struct Directory *ptr){
int i;
if(ptr){
printf("\n");
for(i=0;i<ptr->level;i++)
37
38

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

4.D DIRECTED ACYCLIC GRPAH

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

5. DEADLOCK AVOIDANCE - BANKERS ALGORITHM:

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

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 100 222 936 112
P2 613 511 102
P3 314 211 103
P4 422 002 420

44
45

AVAIL BEFORE AVAIL AFTER


P2 010 623
P1 401 723
P3 620 934
P4 514 936

THE ABOVE SEQUENCE IS A SAFE SEQUENCE

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

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 101 221 936 110
P2 613 511 102
P3 314 212 102
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


DEADLOCK OCCURRED

RESULT
Thus the C program was written for implementing deadlock avoidance using Bankers algorithm

46
47

6. DEADLOCK DETECTION ALGORITHM

DATE:

EXP NO:

AIM:

To implement a c program for detecting deadlock

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

7(a) FIFO PAGE REPLACEMENT ALGORITHM

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

Program - /* FIFO Page Replacement Algorithm */

#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:

Enter no.of frames....4


Enter number of reference string..
6

Enter the reference string..


564123

The given reference string:


...................................... 5 6 4 1 2 3
Reference np5-> 5 -1 -1 -1
Reference np6-> 5 6 -1 -1
Reference np4-> 5 6 4 -1
Reference np1-> 5 6 4 1
Reference np2-> 2 6 4 1
Reference np3-> 2 3 4 1

No.of pages faults...6

RESULT
Thus the program has been implemented similating FIFO page replacement algorithm

54
55

7(b) LRU REPLACEMENT ALGORITHM

DATE :

EXP. NO
AIM: To implement LRU (Least Recently Used) page replacement algorithm

ALGORITHM:

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
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack

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

printf(" Enter no.of reference string..");


scanf("%d",&nor);

printf("\n Enter reference string..");


for(i=0;i<nor;i++)
scanf("%d",&ref[i]);

printf("\n\t The given reference string:");


printf("\n..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
55
56

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:

Enter no.of Frames....3


Enter no.of reference string..6

Enter reference string..


654231
The given reference string:
. 6 5 4 2 3 1

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

No.of page faults...6

RESULT
Thus C program was written and verified defining LRU page replacement algorithm.

58
59

7(c) LFU PAGE REPLACEMENT ALGORITHM

DATE :

EXP.NO

AIM: To implement least frequently used (LFU) page replacement algorithms

ALGORITHM:

Step 1: Include the necassry header files


Step 2: Read the number of frames
Step 3: Read the reference string from the user.
Step 4: Create a array defining time and count
Step 5: When the page fault occurs replace page that has with the smallest count for a
period of time

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;

printf("Enter no of frames : ");


scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);

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

8 MEMORY MANAGEMENT SCHEME- PAGING

DATE :

EX.NO

AIM:
To write a C program to implement memory management using paging technique.

ALGORITHM:

Step 1: Read all the necessary input from the keyboard.


Step 2: Pages - Logical memory is broken into fixed - sized blocks.
Step 3: Frames Physical memory is broken into fixed sized blocks.
Step 4: Calculate the physical address using the following
Physical address = ( Frame number * Frame size ) + offset
Step 5: Display the physical address.
Step 6: Stop the process.

PROGRAM

/* Memory Allocation with Paging Technique */

#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:

MEMORY MANAGEMENT USING PAGING


Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2

PAGE TABLE

PageAddress FrameNo. PresenceBit

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

The Physical Address where the instruction present: 1013

RESULT:
Thus C program was written implementing the paging techinique.

66
67

9. IPC USING SHARED MEMEORY

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

10. MULTITHREADING PRODUCER CONSUMER

DATE :

EX.NO

AIM
To implement multithreading with synchronization in Java

PROGRAM

1. Create a thread extending the Thread Class


2. Define a run method with synchronized option .
3. Defining a class for printing the counter values
4. Create two thread objects.
5. Start the thread and make them to join with the core class.
6. Display the values of the counter.

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;

simplethread(String nm, PrintDemo p){


name = nm;
pd = p;
}
70
71

public void run(){


synchronized(pd){
pd.print(name);
System.out.println("Thread " + name + " Exiting");
}
}
public void start(){
System.out.println("Starting Thread " + name);
if(t==null){
t = new Thread(this, name);
t.start();
}
}
}
class simplemultithreading{
public static void main(String args[]){

PrintDemo pd = new PrintDemo();

simplethread sth1 = new simplethread("Producer ",pd);


simplethread sth2 = new simplethread("Consumer ",pd);

sth1.start();
sth2.start();
try{
sth1.join();
sth2.join();
}catch(Exception e){
System.out.println("exception\n");
}
}
}

71
72

OUTPUT

Starting Thread Producer


Starting Thread Consumer
Counter 5 Thread Producer
Counter 4 Thread Producer
Counter 3 - Thread Producer
Counter 2 Thread Producer
Counter 1 Thread Producer
Thread Producer Exiting
Counter 5 Thread Consumer
Counter 4 Thread Consumer
Counter 3 - Thread Consumer
Counter 2 Thread Consumer
Counter 1 Thread Consumer
Thread Consumer Exiting

RESULT:

Thus a program was written and verified simulating the multithreading.

72
73

73

S-ar putea să vă placă și