II-II Aids Os Lab r22
II-II Aids Os Lab r22
OPERATING SYSTEM
LAB MANUAL (R22)
Department of AI&DS
VISION
To empower female students with professional education using creative & innovative technical practices of
global competence and research aptitude to become competitive engineers with ethical values and
entrepreneurial skills.
MISSION
To impart value based professional education through creative and innovative teaching-learning process to
face the global challenges of the new era technology.
To inculcate research aptitude and to bring out creativity in students by imparting engineering knowledge
imbibing interpersonal skills to promote innovation, research and entrepreneurship.
Department Vision:
To be a leading department of Artificial Intelligence and Data Science that provides cutting-edge education,
research, and innovation in the field, and prepares graduates to become globally competitive professionals,
researchers, and entrepreneurs
Department Mission:
M1: Providing comprehensive education and training in the principles, tools, and applications of Artificial
Intelligence and Data Science, to prepare graduates for a wide range of careers and research opportunities.
M2: Conducting cutting-edge research in the field of Artificial Intelligence and Data Science, including the
development of new algorithms, models, and platforms for data analysis, machine learning, and deep
learning.
M3: Fostering collaborations and partnerships with industry, government, and academia to promote the
transfer of technology, innovation, and entrepreneurship.
..
Program Outcomes:
1 PSO1: Professional skills : the ability to understand ,analyze and develop computer programs in the
areas related to algorithms,system software
2 PSO2:Problem solving skills:the ability to apply standard practices and strategies in software
project development to deliver a quality and defect free product.
3 PSO3:Successful career and Entrepreneurship: the ability to employ modern computer languages,
techniques, in creating innovative career paths to be an entrepreneur and a zest for higher studies
To prepare students to fit into any industry associated with developing and
PEO3 implementation of software products or technologies.
PEO4 To equip the graduates with ability to analyze, design and test the novel products.
Course Structure
Course Code
Programme B.Tech II-II
Course Structure
Practical
L T P Credits
0 0 3 1.5
COURSE OBJECTIVES
SYLLABUS
Prerequisites:
Co-requisite:
A course on “Operating Systems”.
Course Objectives:
To provide an understanding of the design aspects of operating system concepts
through simulation
Introduce basic Unix commands, system call interface for process management,
Course Outcomes:
Simulate and implement operating system concepts such as scheduling,
a) Paging b) Segmentation
TEXT BOOKS:
1. Operating System Principles- Abraham Silberchatz, Peter B. Galvin, Greg Gagne 7th
Edition, John Wiley
2. Advanced programming in the Unix environment, W.R.Stevens, Pearson education
EXPERIMENT NO.1
CPU SCHEDULINGALGORITHMS
AIM: To write a c program to simulate the CPU scheduling algorithm First Come
First Serve (FCFS)
DESCRIPTION:
To calculate the average waiting time using the FCFS algorithm first the
waiting time of the first process is kept zero and the waiting time of the second
process is the burst time of the first process and the waiting time of the third
process is the sum of the burst times of the first and the second process and so on.
After calculating all the waiting times the average waiting time is calculated as
the average of all the waiting times. FCFS mainly says first come first serve the
algorithm which came first will be served first.
ALGORITHM:
#include<stdio.h>
#include<conio.h> main()
{
int bt[20], wt[20],
tat[20], i, n;float
wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d --
", i);scanf("%d", &bt[i]);
}
wt[0] = wtavg
= 0; tat[0] =
tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1]
+bt[i]; wtavg =
wtavg + wt[i];
tatavg = tatavg +
tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f",
tatavg/n);getch();
}
INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3
OUTPUT
WAITING TIME TURNAROUND
PROCESS BURST TIME
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000
• SHORTEST JOB FIRST:
DESCRIPTION:
To calculate the average waiting time in the shortest job first algorithm the
sorting of the process based on their burst time in ascending order then calculate
the waiting time of each process as the sum of the bursting times of all the process
previous or before to that process.
ALGORITHM:
SOURCE CODE :
#include<stdio.h>
#include<conio.h>main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float
wtavg,tatavg;
clrscr();
printf("\nEnter the number of processes -- ")
;scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);scanf("%d", &bt[i]);
}
for(i=0;i<n;
i++)
for(k=i+1;k
<n;k++)
if(bt[i]>bt[k
])
{
temp=bt[i];bt[i]=bt[k]; bt[k]=temp;
temp=p[i];p[i]=p[k]; p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i],
wt[i], tat[i]);printf("\nAverage Waiting Time -- %f",
wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n); getch();}
INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT
PROCESS BURST WAITING TURNARO
TIME TIME UND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000
ROUND ROBIN:
DESCRIPTION:
To aim is to calculate the average waiting time. There will be a time slice,
each process should be executed within that time-slice and if not it will go to
the waiting state so first check whether the burst time is less than the time-slice.
If it is less than it assign the waiting time to the sum of the total times. If it is
greater than the burst-time then subtract the time slot from the actual burst time
and increment it by time-slot and the loop continues until all the processes are
completed.
ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum
(or) timeslice
Step 3: For each process in the ready Q, assign the process id and accept the
CPU bursttime
Step 4: Calculate the no. of time slices for each process where No.
of timeslice for process (n) = burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
• Waiting time for process (n) = waiting time of process(n-1)+ burst
time ofprocess(n-1 ) + the time difference in getting the CPU
fromprocess(n-1)
• Turnaround time for process(n) = waiting time of process(n) + burst
time ofprocess(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
• Average waiting time = Total waiting Time / Number of process
• Average Turnaround time = Total Turnaround Time / Number
ofprocess Step8: Stop the process
SOURCE CODE
#include<stdio.h>main()
{
int
i,j,n,bu[10],wa[10],tat[10],t,ct[1
0],max;float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)
+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];bu[i]=0;
}
Else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i
<n;i++){
wa[i]=tat
[i]-
ct[i];
att+=tat[i];
awt+=wa[i]
;}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();}
INPUT:
OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2 3 4 7
3 3 7 10
The Average Turnaround time is –
15.666667 TheAverage Waiting time is5.666667
• PRIORITY:
DESCRIPTION:
To calculate the average waiting time in the priority algorithm, sort the
burst times according to their priorities and then calculate the average waiting
time of the processes. The waiting time of each process is obtained by summing
up the burst times of all the previous processes.
ALGORITHM:
SOURCE CODE:
#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float
wtavg,tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i
<n;i++){
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d
%d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++) if(pri[i] > pri[k])
{
temp=p[i]; p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0]
= 0; tatavg =
tat[0] = bt[0];
for(i=1;i<n;i++
)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg +
wt[i];tatavg =
tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING
TIME\tTURNAROUNDTIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();}
INPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Theory:
There are 5 basic system calls that Unix provides for file I/O.
1. Create: Used to Create a new empty file
Syntax :int creat(char *filename, mode_t mode)
filename : name of the file which you want to create
mode : indicates permissions of new file.
2. open: Used to Open the file for reading, writing or both.
Syntax: int open(char *path, int flags [ , int mode ] );
Path : path to file which you want to use
flags : How you like to use
O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create
file if it doesn’t exist, O_EXCL: prevent creation if it already exists
3. close: Tells the operating system you are done with a file descriptor and Close the file
which pointed by fd.
Syntax: int close(int fd);
fd :file descriptor
4. read: From the file indicated by the file descriptor fd, the read() function reads cnt
bytes
of input into the memory area indicated by buf. A successful read() updates the access time for the
file.
Syntax: int read(int fd, char *buf, int size);
fd: file descripter
buf: buffer to read data from
cnt: length of buffer
5. write: Writes cnt bytes from buf to the file or socket associated with fd. cnt should not
be
greater than INT_MAX (defined in the limits.h header file). If cnt is zero, write() simply returns 0
without attempting any other action.
Syntax: int write(int fd, char *buf, int size);
fd: file descripter
buf: buffer to write data to
cnt: length of buffer
*File descriptor is integer that uniquely identifies an open file of the process
2. Theory:
There are 5 basic system calls that Unix provides for file I/O.
O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create
file if it doesn’t exist, O_EXCL: prevent creation if it already exists
3. close: Tells the operating system you are done with a file descriptor and Close the file
which pointed by fd.
Syntax: int close(int
fd); fd :file descriptor
4. read: From the file indicated by the file descriptor fd, the read() function reads cnt
bytes of input into the memory area indicated by buf. A successful read() updates the access time
for the file.
Syntax: int read(int fd, char *buf, int
size); fd: file descripter
5. write: Writes cnt bytes from buf to the file or socket associated with fd. cnt should not
be greater than INT_MAX (defined in the limits.h header file). If cnt is zero, write() simply
returns 0 without attempting any other action.
Syntax: int write(int fd, char *buf, int
size); fd: file descripter
buf: buffer to write data
to cnt: length of buffer
*File descriptor is integer that uniquely identifies an open file of the process.
Algorithm
1. Star the program.
2. Open a file for O_RDWR for R/W,O_CREATE for creating a file ,O_TRUNC for
truncate a file.
3. Using getchar(), read the character and stored in the string[] array.
4. The string [] array is write into a file close it.
5. Then the first is opened for read only mode and read the characters and displayed it
and close the file.
6. Stop the program.
Program
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h
> int main()
{
int
n,i=0;
int
f1,f2;
char c,strin[100];
f1=open("data",O_RDWR|O_CREAT|O_TRUNC);
while((c=getchar())!='\n')
{
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY
); read(f2,strin,0);
printf("\n%s\n",strin);
close(f2);
return 0;
Output:
Hai Hai
b) Aim: C program using lseek
3. Theory:
lseek is a system call that is used to change the location of the read/write pointer of a file
descriptor. The location can be set either in absolute or relative terms.
int fildes : The file descriptor of the pointer that is going to be moved.
off_t offset : The offset of the pointer (measured in bytes).
int whence : Legal values for this variable are provided at the end which are
Algorithm:
Program:
#include<stdio.h>
#include
<unistd.h>
#include <fcntl.h>
#include
<sys/types.h>
int main()
{
int file=0;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
return 0;
4. Output:
c) Aim: C program using opendir(), closedir(), readdir()
Theory:
The following are the various operations using directories
1. Creating directories.
Syntax : int mkdir(const char *pathname, mode_t mode);
5. Removing directories.
Syntax: int rmdir(const char *pathname);
Algorithm:
1. Start the program
2. Print a menu to choose the different directory operations
3. To create and remove a directory ask the user for name and create and remove the
same respectively.
4. To open a directory check whether directory exists or not. If yes open the directory
.If it does not exists print an error message.
5. Finally close the opened directory.
6. Stop
5. Program:
#include<stdio.h
>
#include<fcntl.h
>
#include<dirent.
h> main()
{
char d[10]; int c,op; DIR
*e; struct dirent *sd;
switch(op)
{
case 1: printf("enter dir name\n"); scanf("%s",&d);
c=mkdir(d,777);
if(c==1)
printf("dir is not
created"); else
if(c==1)
printf("dir is not
removed"); else
{
printf("dir exist\n"); while((sd=readdir(e))!=NULL) printf("%s\t",sd->d_name);
}
closedir
(e);
break;
}
}
Output:
6. WEEK -3
Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention
a) Aim
Write a C program to simulate the Bankers Algorithm for Deadlock Avoidance.
7. Data structures
1. 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.
2. If Request<=Need I go to step 2. Otherwise raise an error condition.
3. If Request<=Available go to step 3. Otherwise Pi must since the resources are available.
4. Have the system pretend to have allocated the requested resources to process Pi
by modifying the state as follows;
5. Available=Available-Request I;
6. Allocation I =Allocation+Request I;
7. 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 restore.
10. Algorithm:
11. Program:
#include<stdio.h>
int main ()
{
int allocated[15][15], max[15][15], need[15][15], avail[15],
tres[15], work[15], flag[15];
//clrscr ();
}
printf ("\n Enter total numbers of each
resources:"); for (i = 1; i <= rno; i++)
{
printf ("\n for process %d:",
i); for (j = 1; j <= rno; j++)
}
printf ("\n Enter allocated resources for each
process:"); for (i = 1; i <= pno; i++)
{
printf ("\n for process %d:",
i); for (j = 1; j <= rno; j++)
}
printf ("\n available
resources:\n"); for (j = 1; j <=
rno; j++)
{
avail[j] = 0;
total = 0;
{
total += allocated[i][j];
}
avail[j] = tres[j] -
total; work[j] =
avail[j];
}
do
{
for (i = 1; i <= pno; i++)
{
for (j = 1; j <= rno; j++)
{
need[i][j] = max[i][j] - allocated[i][j];
}
}
{
printf ("\n");
{
printf ("%4d", allocated[i][j]);
}
printf ("|");
{
printf ("%4d", max[i][j]);
}
printf ("|");
{
printf ("%4d", need[i][j]);
}
}
prc = 0;
{
if (flag[i] == 0)
{
prc = i;
{
if (work[j] < need[i][j])
{
prc =
0;
break
;
}
}
}
if (prc != 0)
break;
{
printf ("\n Process %d completed", i);
count++;
max[prc][j] = 0;
flag[prc] = 1;
}
}
}
while (count != pno && prc != 0);
if (count == pno)
if (prc != 0)
12. Output:
b) Aim
Write a C program to simulate Bankers Algorithm for Deadlock Prevention
13. Algorithm:
1. Start
2. Attacking Mutex condition : never grant exclusive access. but this may not be possible
for several resources.
3. Attacking preemption: not something you want to do.
4. Attacking hold and wait condition : make a process hold at the most 1 resource at a
time.make all the requests at the beginning. All or nothing policy. If you feel,retry. eg.
2- phase locking 34
5. Attacking circular wait: Order all the resources. Make sure that the requests are issued
in the correct order so that there are no cycles present in the resource graph.
Resources numbered 1 ... n. Resources can be requested only in increasing order. ie.
you cannot request a resource whose no is less than any you may be holding.
6. Stop
14. Program:
#include<stdio.h>
int
max[10][10],alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0
; main( )
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("\n enter max
matrix");
scanf("%d",&max[i][j]);
{if(finish[i]!=1)
{
printf("\n Failing :Mutual
exclusion"); for(j=0;j<r;j++)
avail[j]=need[i][j];
}fun();
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
}
fun( );
}
}
}
fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
els
e
br
ea
k;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j]
; flag=1;
finish[i]=1;
}
}
}
15. Output:
WEEK-4
Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls.
Aim:
Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls.
16. Algorithm:
ii) If there is any item in the buffer check for mutex value, if the mutex==0,
remove item from buffer
iii) Signal the mutex value and reduce the empty value by 1.
#include<stdio.h
>
#include<stdlib.
h>
{
int n;
void producer
(); void
consumer ();
int wait (int);
printf
("\n1.Producer\n2.Consumer\n3.Exit");
while (1)
{
printf ("\nEnter your choice:");
scanf ("%d", &n);
switch (n)
{
case 1:
else
else
printf ("Buffer is
empty!!"); break;
case 3:
exit
(0);
brea
k;
}
}
return 0;
{
return (--s);
{
return (++s);
void producer ()
{
mutex = wait
(mutex); full =
signal (full); empty
= wait (empty);
x++;
void consumer ()
{
mutex = wait
(mutex); full = wait
(full);
}
Output:
Week: 5
ALGORITHM:
#include <unistd.h>
int fd[2];
char message[MSG_LEN];
char recvd_msg[MSG_LE];
if (result < 0)
perror("pipe ");
exit(1);
result=write(fd[1],message,strlen(message));
if (result< 0)
perror("write");
exit(2);
}
strncpy(message,"Understanding ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
53
{
perror("write");
exit(2);
strncpy(message,"Concepts of ",MSG_LEN);
result=write(fd[1],message,strlen(message));
perror("write");
exit(2);
result=write(fd[1],message,strlen(message));
if (result < 0)
perror("write");
exit(2);
}
result=read(fd[0],recvd_msg,MSG_LEN);
if (result < 0)
perror("read");
exit(3);
printf("%s\n",recvd_msg);
return 0;
54
}
B)FIFO
Program:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/stat.h>
int main(void)
{
FILE *fp;
char readbuf[80];
while(1)
{
fp = fopen(FIFO_FILE, "r");
fgets(readbuf, 80, fp);
return(0);
#include <stdio.h>
#include <stdlib.h>
{
FILE *fp;
if ( argc != 2 ) {
exit(1);
}
fputs(argv[1], fp);
fclose(fp);
return(0);
56
C Program for Message Queue (Writer Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
long msg_type;
char
msg_text[100];
} message;
int main()
{
key_t
key;
int
msgid;
return 0;
58
C Program for Message Queue (Reader Process)
#include <stdio.h>
#include
<sys/ipc.h>
#include
<sys/msg.h>
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t
key;
int
msgid;
message.mesg_text);
return 0;
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
long mesg_type;
char
mesg_text[100];
} message;
int main() 60
{
key_t
key;
int
msgid;
message.mesg_text);
return 0;
61
OUTPUT: Thus the Piping process using IPC program was executed and verified successfully
62
Week: 6
Aim: Write C programs to simulate the following memory management techniques
a) Paging
ALGORITHM:
Step2 : Read the base address, page size, number of pages and memory unit.
Step3 : If the memory limit is less than the base address display the memory limit is less than
limit. Step4 : Create the page table with the number of pages and page address.
Step6 : If the page number and displacement value is valid, add the displacement value with the address
corresponding to the page number and display the result.
Step7 : Display the page is not found or displacement should be less than page size.
Step8 : Stop the program.
18. Program:
#include<stdio.
h>
#include<conio
.h> main()
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset; int s[10], fno[10][20];
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
63
printf("\nEnter the page size --
"); scanf("%d",&ps);
nop = ms/ps;
scanf("%d",&np);
64
rempages = nop; for(i=1;i<=np;i++)
if(s[i] >rempages)
printf("\nMemory is Full");
break;
scanf("%d",&fno[i][j]);
pa=fno[x][y]*ps+offset;
getch();
OUTPUT:
66
a) Segmentation
Aim: To write a C program to implement memory management using segmentation
Algorithm:
Step1 : Start the program.
Step2 : Read the base address, number of segments, size of each segment,
memory limit. Step3 : If memory address is less than the base address display
“invalid memory limit”.
Step4 : Create the segment table with the segment number and segment address and
display it. Step5 : Read the segment number and displacement.
Step6 : If the segment number and displacement is valid compute the real address and display the
same. Step7 : Stop the program.
Program:
#include<stdio.
h>
#include<conio
.h> struct list
{
int
seg;
int
base;
int
limit;
} *p;
{
if(p==NU
LL)
{
p=malloc(sizeof(Struct 67
list)); p->limit=limit;
p-
>base=bas
e; p-
>seg=seg;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
Q=q-
>next;
Printf(“y
es”)
}
q->next=malloc(sizeof(Struct
list)); q->next ->limit=limit;
q->next -
>base=base; q-
>next ->seg=seg;
q->next ->next=NULL;
}
}
int find(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->limit;
} 68
{
q=q->next;
}
return q->base;
}
main()
{
p=NULL;
int
seg,offset,limit,base,c,s,physical
; printf(“Enter segment
table/n”);
{
printf(“Enter segment number”);
scanf(“%d”,&seg);
if(seg!=-1)
{
printf(“Enter base value:”);
scanf(“%d”,&base);
}
}
while(seg!=-1)
printf(“Enter offset:”);
scanf(“%d”,&offset);
69
printf(“Enter bsegmentation
number:”); scanf(“%d”,&seg);
c=find(p,seg);
s=search(p,seg);
if(offset<c)
{
physical=s+offset;
}
else
{
printf(“error”);
OUTPUT:
Enter segmentation
number:-1 Enter offset:90
Enter segmentation
number:-1 Enter offset:90
71
Week-7:write c programs to simulate pge replacement policies
a)FCFS b)LRU C)OPTIMAL
AIM
ALGORITHM
4. Check the need of replacement from old page to new page in memory
72
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
} 73
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:
74
B)IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM
AIM:
ALGORITHM :
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:"); 75
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{ 76
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
77
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
Program Description: Optimal Page Replacement refers to the removal of the page that will not be used78
in the future, for the longest period of time.
Program Code:
#include<stdio.h>
int main()
{
int n,pg[30],fr[10];
int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;
fault=0;
dist=0;
k=0;
printf("Enter the total no pages:\t");
scanf("%d",&n);
printf("Enter the sequence:");
for(i=0;i<n;i++)
scanf("%d",&pg[i]);
printf("\nEnter frame size:");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
fr[i]=-1;
}
for(i=0;i<n;i++)
{
flag=0;
temp=pg[i];
for(j=0;j<f;j++)
{
if(temp==fr[j])
{
flag=1;
break;
} 79
}
if((flag==0)&&(k<f))
{
fault++;
fr[k]=temp;
k++;
}
else if((flag==0)&&(k==f))
{
fault++;
for(cnt=0;cnt<f;cnt++)
{
current=fr[cnt];
for(c=i;c<n;c++)
{
if(current!=pg[c])
count[cnt]++;
else
break;
}
}
max=0;
for(m=0;m<f;m++)
{
if(count[m]>max)
{
max=count[m];
p=m;
}
}
fr[p]=temp;
}
printf("\npage %d frame\t",pg[i]);
for(x=0;x<f;x++)
{
printf("%d\t",fr[x]); 80
}
}
printf("\nTotal number of faults=%d",fault);
return 0;
}
Output:
81
82