0% found this document useful (0 votes)
15 views

Os Lab

Uploaded by

mohits.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Os Lab

Uploaded by

mohits.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

INDEX

PAGE
EXP NO EXPERIMENT NAME MARKS SIGN
NO

FINAL VERIFICATION BY THE FACULTY

TOTAL MARKS:

INTERNAL EXAMINER EXTERNAL EXAMINER

OPERATING SYSTEMS LAB


EXPERIMENT – 1

GETTING USED TO LINUX BASIC COMMANDS, DIRECTORY STRUCTURE,

AIM:

To familiarize with Linux basic commands, directory structure, file and directory operations.

DESCRIPTION

Linux basic commands:

(a) cd: - It is used to change directory.

- The format of this command is:


cd <space> <directory_name> Eg: cd
bin
- Here “/” indicates root directory and “~” indicates home directory.

-To move step by step backwards we use:


cd<space>..
(b) ls: - It is used to list all the files in the current directory.
-The hidden files in the directory can be listed using the command:
ls<space>-a

(c) clear: - It is used to clear the terminal screen.

(d) pwd: - It is used to get the full path to current directory.

(e) cal: - It is used to get the calendar of the current month.

-The whole calendar can be retrieved using: cal<space>-y

OPERATING SYSTEMS LAB


(f) date: - It is used to get the current time of the machine.

(g) mkdir: - It is used to make directory in the current location.

- The format is given by:


mkdir<space><directory_name>
Eg: mkdir newfile

(h) rmdir: - It is used to remove directory from the current location.

- The format is given by:


rmdir<space><directory name>
Eg: rmdir newfile

- If the directory is not present then it displays error as:


rmdir: cannot remove <directoryname>:No such file or directory

- If the directory is not empty, it displays the error as:


rmdir: failed to remove<directoryname>: Directory not empty

(i) touch: -It is used to create a file.


-The format is given by:
touch<space><filename>
Eg: touch hi
(j) rm: -It is used to remove a file.
-The format is given by:
rm<space><filename>
Eg: rm hi
(k) mv: -It is used to rename a file.
-The format is given by: mv<space><oldname><newname>
Eg: mv hi hello
(l) cp: -It is used to copy files between directories.
-The format is given by:
cp<space><source><destination>

OPERATING SYSTEMS LAB


Eg: cp hello /usr

(m) man: -It is used to get the manual of each of the commands.

-The format is given by:


man<space><command>
(n) whoami: -It is used to get the current user

(o)history: -It is used to display the history details of the terminal.

Directory structure:

The Unix operating system consists of a single file system. All the controlling directories is present in the
main file system termed as the root directory of the operating system. The different directories in the root
are as follows:

(1) bin: -This is used to store binary files.


-All the executable files will be present here.

(2) boot: -This is used to store all booting related files.

-The initial program to be loaded on to the ram and the kernel is situated in this
directory.

(3) dev: -This is used to store all device descriptions.

-All the known devices will have a description and the newly installed
devices will have an entry in this directory.

(4) etc: -This is used to store the configuration files of the system.

-It includes details like user login, IP address, hostname, etc.

(5) home: -Each user will have a directory here.

-User login is directed to this and all home directory files are here.

(6)lib/lib64/lib32: -This consists of all the system libraries.

OPERATING SYSTEMS LAB


(7) media: - Contents of external drives will be stored here.

(8) mnt: -All the mounted drives details will be stored here.

(9) opt: -This is an optional folder for third part programs.

(10) proc: -This is a special folder that resides in RAM.

-All the processes are here and process ids are displayed as sub-
directories which contains its resources.

(11) lost+found: -This is an administrative level directory that contains recovery bits for
the deleted files or applications.

(12) root: -This is another administrative level directory to store system


administrative files.

(13) sbin: -This also contains system administrative files but can be
accessed by all users.

(14) srv: -This contains server related files.

(15) sys: -This contains system related files like power related files.

(16) tmp: -This is used to store temporary files.

(17) usr: -This is used for extra programs like compilers.

(18) var: -This is used to store variable files like system logs.

OPERATING SYSTEMS LAB


EXPERIMENT – 2

SEMAPHORES IMPLEMENTATION
AIM:
Write a C program to implement the Producer consumer problem using semaphores

Semaphores in operating system

Semaphore is a simply a variable. This variable is used to solve critical section problem and to achieve
process synchronization in the multi-processing environment.
The two most common kinds of semaphores are counting semaphores and binary semaphores.
Semaphores are of two types:

1. Binary Semaphore – This is also known as mutex lock. It can have only two values – 0 and
1. Its value is initialized to 1. It is used to implement solution of critical section problem with
multiple processes.
2. Counting Semaphore – Its value can range over an unrestricted domain. It is used to
control access to a resource that has multiple instances.

P and V are the two operations which can be used to access and change the value of semaphore variable
1. P operation is also called wait, sleep or down operation and V operation is also called
signal, wake-up or up operation.
2. Both operations are atomic and semaphore(s) is always initialized to one.
3. The wait() operation reduces the value of semaphore by 1 and the signal() operation
increases its value by 1.
4. A critical section is surrounded by both operations to implement process synchronization. See
below image. Critical section of Process P is in between P and V operation

OPERATING SYSTEMS LAB


PROGRAM:
#include<stdio.h>
void main()
{
int buffer[10],bufsize = 5, in, out, pro, cons, choice;
in=out=0;

do{

printf("\n1 --- Produce\t2 --- Consume\t3 ---


Exit\n"); printf("Choice ?[1/2/3] : ");
scanf("%d",&choic

e); switch(choice){

case 1: if((in+1)%bufsize == out)


printf("Buffer is
full.\n");
else{
printf("Enter production value : ");
scanf("%d",&pro);
buffer[in] = pro;
in = (in + 1) % bufsize;
}
break;
case 2: if(in == out)
printf("Buffer is
empty.\n"); else{
cons = buffer[out];
printf("\nConsumed Product :
%d\n",cons); out = (out+1) % bufsize;
}
}

}while(choice!=3);
}

OPERATING SYSTEMS LAB


OUTPUT

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 1
Enter production value : 100

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 1
Enter production value : 200

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 1
Enter production value : 300

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 1
Enter production value : 400

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 1
Buffer is full.

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 500

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 1
Buffer is full.

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 2

Consumed Product : 100

1 --- Produce 2 --- Consume 3 --- Exit


Choice ?[1/2/3] : 3

OPERATING SYSTEMS LAB


EXPERIMENT – 3 IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS

Implementation of CPU scheduling algorithms.

a) Round Robin
b) SJF
c) FCFS
d)Priority
FCFS: First Come First Serve Scheduling

It is the simplest algorithm to implement.

The process with the minimal arrival time will get the CPU first.

The lesser the arrival time, the sooner will the process gets the CPU.

It is the non-pre-emptive type of scheduling.

The Turnaround time and the waiting time are calculated by using the following formula.

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turnaround time - Burst Time
Process ID Arrival Burst Time Completion Turn Waiting
Time Time Around Time
Time

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 8 4

3 3 9 21 18 9

4 4 12 33 29 17

Avg Waiting Time=31/5

OPERATING SYSTEMS LAB


Shortest-Job-First Scheduling
This algorithm associates with each process the length of the process’s next CPU burst. When the CPU is
available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two
processes are the same, FCFS scheduling is used to break the tie. Note that a more appropriate term for this
scheduling method would be the shortest-next- CPU-burst algorithm, because scheduling depends on the
length of the next CPU burst of a process, rather than its total length.We use the term SJF because most
people and textbooks use this term to refer to this type of scheduling. As an example of SJF scheduling,
consider the following set of processes, with the length of the CPU burst given in milliseconds:

The waiting time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9 milliseconds for
process P3, and 0 milliseconds for process P4. Thus, the average waiting time is (3 + 16 + 9 + 0)/4 = 7
milliseconds. By comparison, if we were using the FCFS scheduling scheme, the average waiting time
would be 10.25 milliseconds.

Priority Scheduling

The SJF algorithm is a special case of the general priority-scheduling algorithm. Apriority is associated
with each process, and the CPUis allocated to the process with the highest priority. Equal- priority processes
are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where thepriority (p) is the
inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa.
Note that we discuss scheduling in terms of high priority and low priority.

OPERATING SYSTEMS LAB


Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to 4,095. However,
there is no general agreement on whether 0 is the highest or lowest priority. Some systems use low numbers
to represent low priority; others use low numbers for high priority. This difference can lead to confusion. In
this text, we assume that low numbers represent high priority. As an example, consider the following set of
processes, assumed to have arrived at time 0 in the order P1, P2, · · ·, P5, with the length of the CPU burst
given in milliseconds:

Round-Robin Scheduling

The round-robin (RR) scheduling algorithm is designed especially for timesharing systems. It is similar to
FCFS scheduling, but preemption is added to enable the system to switch between processes. A small unit
of time, called a time quantum or time slice, is defined. A time quantum is generally from 10 to 100
milliseconds in length. The ready queue is treated as a circular queue. The CPU scheduler goes around the
ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum.

To implement RR scheduling, we again treat the ready queue as a FIFO queue of processes. New processes
are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue,
sets a timer to interrupt after 1 time quantum, and dispatches the process. One of two things will then
happen. The process may have a CPU burst of less than 1 time quantum. In this case, the process itself will
release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. If the
CPU burst of the currently running process is longer than 1 time quantum, the timer will go off and will
cause an interrupt to the operating system. A context switch will be executed, and the process will be put at
the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue.

OPERATING SYSTEMS LAB


The average waiting time under the RR policy is often long. Consider the following set of processes that
arrive at time 0, with the length of the CPU burst given in milliseconds:

Let’s calculate the average waiting time for this schedule. P1 waits for 6 milliseconds (10 - 4), P2 waits for
4 milliseconds, and P3 waits for 7 milliseconds. Thus, the average waiting time is 17/3 = 5.66

OPERATING SYSTEMS LAB


PROGRAM

1. FIRST COME FIRST SERVE (FCFS)

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

int i=0,j=0,b[i],g[20],p[20],w[20],t[20],a[20],n=0,m;

float avgw=0,avgt=0;

printf("Enter the number of process : ");


scanf("%d",&n);
for(i=0;i<n;i++)

printf("Process ID : ");
scanf("%d",&p[i]);
printf("Burst Time : ");
scanf("%d",&b[i]);
printf("Arrival Time: ");
scanf("%d",&a[i]);
}

int temp=0;
for(i=0;i<n-1;i++)
{

for(j=0;j<n-1;j++)

if(a[j]>a[j+1])

temp=a[j];

OPERATING SYSTEMS LAB


a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}

}
g[0]=0;
for(i=0;i<=n;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{

t[i]=g[i+1]-a[i];

w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i];
}

avgw=avgw/n;
avgt=avgt/n;
printf("pid\tarrivalT\tBrustT\tCompletionT\tWaitingtime\tTurnaroundTi\n");
for(i=0;i<n;i++)
{

OPERATING SYSTEMS LAB


printf("%d\t%d\t%d\t%d\t\t%d\t\t\t%d\n",p[i],a[i],b[i],g[i+1],w[i],t[i]);

printf("\nAverage waiting time %f",avgw); printf("\nAverage


turnarround time %f",avgt);
}

Output

Enter the number of process : 5


Process ID : 1
Burst Time : 4
Arrival Time: 0
Process ID : 2
Burst Time : 3
Arrival Time: 1
Process ID : 3
Burst Time : 1
Arrival Time: 2
Process ID : 4
Burst Time : 2
Arrival Time: 3
Process ID : 5
Burst Time : 5
Arrival Time: 4

Pid arrival BrustT CompletionT Waitingtime TurnaroundTi

1 0 4 4 0 4
2 1 3 7 3 6
3 2 1 8 5 6
4 3 2 10 5 7
5 4 5 15 6 11

OPERATING SYSTEMS LAB


2.SHORTEST JOB FIRST

#include<stdio.h>
void main()
{
int i=0,j=0,p[i],b[i],g[20],w[20],t[20],a[20],n=0,m;
int k=1,min=0,btime=0;
float avgw=0,avgt=0;
printf("Enter the number of process : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nProcess id : ");
scanf("%d",&p[i]);

printf("Burst Time : ");


scanf("%d",&b[i]);

printf("Arrival Time: ");


scanf("%d",&a[i]);
}

//sort the jobs based on burst time. int


temp=0;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{

OPERATING SYSTEMS LAB


temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;

temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}

for(i=0;i<n;i++)
{
btime=btime+b[i];
min=b[k];
for(j=k;j<n;j++)
{
if(btime >= a[j] && b[j]<min)
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;

temp=b[j];
b[j]=b[j-1];
b[j-1]=temp;
temp=p[j]

}
} k++;

OPERATING SYSTEMS LAB


}

g[0]=a[0];
for(i=0;i<n;i++)
{
g[i+1]=g[i]+b[i];
if(g[i]<a[i])
g[i]=a[i];
}

for(i=0;i<n;i++)
{

t[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i];

}
avgw=avgw/n;
avgt=avgt/n;
printf("pid\tBrustTime\tGantChart\tWaiting time\t\tTurnarround Time\n"); for(i=0;i<n;i+
+)
{
printf(" %d\t %d\t\t%d-%d\t\t%d\t\t\t%d\n",p[i],b[i],g[i],g[i+1],w[i],t[i]);
}
printf("\nAverage waiting time %f",avgw);
printf("\nAverage turnarround time %f\n",avgt);

OPERATING SYSTEMS LAB


OUTPUT

Enter the number of process : 5

Process id : 1 Burst
Time : 7 Arrival
Time: 0

Process id : 2 Burst
Time : 5

Arrival Time: 1

Process id : 3 Burst
Time : 1 Arrival Time:
2

Process id : 4 Burst
Time : 2 Arrival Time:
3

Process id : 5 Burst
Time : 8 Arrival Time:

pid Brust Time GantChart Waiting time Turnarround Time


8 7 0-7 0 7
3 1 7-8 5 6
4 2 8-10 5 7
2 5 10-15 9 14
5 8 15-23 11 19
Average waiting time 6.000000 Average turnaround time 10.600000

OPERATING SYSTEMS LAB


3. PRIORITY SCHEDULING

#include<stdio.h>
int main()
{
int burst_time[20], process[20], waiting_time[20], turnaround_time[20], priority[20]; int i, j,
limit, sum = 0, position, temp;
float average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Burst Time and Priority For %d Processes\n", limit); for(i =
0; i < limit; i++)
{
printf("\nProcess[%d]\n", i + 1);
printf("Process Burst Time:\t");
scanf("%d", &burst_time[i]);
printf("Process Priority:\t");
scanf("%d", &priority[i]);
process[i] = i + 1;
}
for(i = 0; i < limit; i++)
{
position = i;
for(j = i + 1; j < limit; j++)
{
if(priority[j] < priority[position])
{
position = j;
}
}
temp = priority[i];

OPERATING SYSTEMS LAB


priority[i] = priority[position];
priority[position] = temp; temp
= burst_time[i];
burst_time[i] = burst_time[position];
burst_time[position] = temp;
temp = process[i];
process[i] = process[position];
process[position] = temp;
}
waiting_time[0] = 0;
for(i = 1; i < limit; i++)
{
waiting_time[i] = 0;
for(j = 0; j < i; j++)
{
waiting_time[i] = waiting_time[i] + burst_time[j];
}
sum = sum + waiting_time[i];
}
average_wait_time = sum / limit;
sum = 0;
printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n"); for(i = 0; i
< limit; i++)
{
turnaround_time[i] = burst_time[i] + waiting_time[i]; sum
= sum + turnaround_time[i];
printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i], waiting_time[i],
turnaround_time[i]);
}
average_turnaround_time = sum / limit;
printf("\nAverage Waiting Time:\t%f", average_wait_time); printf("\nAverage
Turnaround Time:\t%f\n", average_turnaround_time); return 0;}

OPERATING SYSTEMS LAB


OUTPUT

Enter the number of process: 3

Process id : 1
Burst Time : 15
Priority: 3

Process id : 2

Burst Time : 10
Priority: 2

Process id : 3 Burst
Time : 90
Priority: 1

pid Burst Time Waiting time Turnaround Time


3 90 0 90
2 10 90 100
1 15 100 115

Average waiting time 63.000000


Average turnaround time 101.000000

4. Round Robin (pre-emptive)

#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10]; float
average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)

OPERATING SYSTEMS LAB


{
printf("\nEnter Details of Process[%d]\n", i + 1); printf("Arrival
Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
printf("\nEnter Time Quantum:\t");
scanf("%d", &time_quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n"); for(total =
0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)

{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum; total
= total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total -
arrival_time[i] - burst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;

OPERATING SYSTEMS LAB


}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
average_wait_time = wait_time * 1.0 / limit; average_turnaround_time =
turnaround_time * 1.0 / limit; printf("\n\nAverage Waiting Time:\t%f",
average_wait_time); printf("\nAvg Turnaround Time:\t%f\n",
average_turnaround_time); return 0;
}

OPERATING SYSTEMS LAB


OUTPUT

Enter the number of process : 3

Process id : 2 Burst
Time : 3 Arrival
Time:2

Process id : 3 Burst
Time : 2 Arrival
Time:3

pid Burst Time Waiting time Turnarround Time


3 2 1 3
2 3 9 6

OPERATING SYSTEMS LAB


EXPERIMENT – 4

IMPLEMENTATION OF THE MEMORY ALLOCATION METHODS FOR FIXED


PARTITION

AIM:

Implementation of the Memory Allocation Methods for fixed partition

a) First Fit

b) Worst Fit

c) Best Fit

Contiguous Memory Allocation Techniques

First Fit

In the first fit approach is to allocate the first free partition or hole large enough which can accommodate the
process. It finishes after finding the first suitable free partition.

Best Fit

The best fit deals with allocating the smallest free partition which meets the requirement of the requesting process.
This algorithm first searches the entire list of free partitions and considers the smallest hole that is adequate. It then
tries to find a hole which is close to actual process size needed.

Worst fit

In worst fit approach is to locate largest available free portion so that the portion left will be big enough to be
useful. It is the reverse of best fit.

PROGRAM

1. FIRST FIT

#include<stdio.h>
struct process
{
int ps;
int flag;
} p[50];
struct sizes
{

OPERATING SYSTEMS LAB


int size;
int alloc;
}
s[5];
int main()
{
int i=0,np=0,n=0,j=0;
printf("\n first fit");
printf("\n");
printf("enter the number of blocks \t");
scanf("%d",&n);
printf("\t\t\n enter the size for %d blocks\n",n);
for(i=0;i<n;i++)
{
printf("enter the size for %d block \t",i);
scanf("%d",&s[i].size);
}
printf("\n\t\t enter the number of process\t",i);
scanf("%d",&np);
printf("\n enter the size of %d processors !\t",np);
printf("/n");
for(i=0;i<np;i++)
{
printf("enter the size of process %d\t",i);
scanf("\n%d",&p[i].ps);
}
printf("\n\t\t Allocation of blocks using first fit is as follows\n");
printf("\n\t\t process \t process size\t blocks\n"); for(i=0;i<np;i++)
{
for(j=0;j<n;j++)
{
if(p[i].flag!=1)
{
if(p[i].flag!=1)

OPERATING SYSTEMS LAB


{
if(p[i].ps<=s[j].size)
{
if(!s[j].alloc)
{
p[i].flag=1;
s[j].alloc=1;
printf("\n\t\t %d\t\t\t%d\t%d\t",i,p[i].ps,s[j].size);
}
}
}
}
}
}
for(i=0;i<np;i++)
{
if(p[i].flag!=1)
printf("sorry !!!!!!!process %d must wait as there is no sufficient memory");
}
}

OUTPUT

First fit

Enter the number of blocks :5

Enter the size for 5 bocks

Enter the size for 0 block : 100

Enter the size for 1 block : 250

Enter the size for 2 block : 300

Enter the size for 3 block : 50

Enter the size for 4 block : 120

Enter the number of process: 3

Enter the size of process

OPERATING SYSTEMS LAB


Enter the size of process 1 40

Enter the size of process 2 200

Enter the size of process 3 300

Allocation of block using first fit is as follows

Process process size block

1 40 100

2 200 250

3 300 300

2. WORST FIT

#include<stdio.h>
int main()
{

int fragments[10], blocks[10], files[10];

int m, n, number_of_blocks, number_of_files, temp, top = 0;


static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files); printf("\nEnter
the Size of the Blocks:\n"); for(m = 0; m <
number_of_blocks; m++)
{

printf("Block No.[%d]:\t", m + 1);

scanf("%d", &blocks[m]);

OPERATING SYSTEMS LAB


printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{

printf("File No.[%d]:\t", m + 1);

scanf("%d", &files[m]);

for(m = 0; m < number_of_files; m++)

for(n = 0; n < number_of_blocks; n++)

if(block_arr[n] != 1)

temp = blocks[n] - files[m];


if(temp >= 0)
{

if(top < temp)

file_arr[m] = n;
top = temp;
}

fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;

OPERATING SYSTEMS LAB


}

printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment"); for(m


= 0; m < number_of_files; m++)
{

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]],


fragments[m]);
}

printf("\n");
return 0;
}

OUTPUT

Enter total number of blocks : 4

Enter total number of files: 3

Enter the size of blocks

Block No.[1] : 50

Block No.[2] : 75

Block No.[3] : 100

Block No.[4] : 300

Enter the size of the files

File No.[1] : 20

File No.[2] : 100

File No.[3] : 40

File number File size Block Number Block Size Fragment

0 20 3 300 280

1 100 0 50 0

2 40 0 50 10

OPERATING SYSTEMS LAB


3.BEST FIT

#include<stdio.h>
#define MAX 20
int main()
{
int bsize[MAX],fsize[MAX],nb,nf;
int temp,low=10000;
static int bflag[MAX],fflag[MAX]; int
i,j;
printf("\n enter the number of blocks");
scanf("%d",&nb);
for(i=1;i<=nb;i++)
{
printf("Enter the size of memory block % d",i);
scanf("%d", &bsize[i]);
}
printf("\n enter the number of files");
scanf("%d",&nf);
for(i=1;i<=nf;i++)
{
printf("\n enetr the size of file %d",i);
scanf("%d",&fsize[i]);
}
for(i=1;i<=nf;i++)

{
for(j=1;j<=nb;j++)
{
if(bflag[j]!=1)
{
temp=bsize[j]-fsize[i];
if(temp>=0)
{
if(low>temp)
{

OPERATING SYSTEMS LAB


fflag[i]=j;
low=temp;
}
}
}}
bflag[fflag[i]]=1;
low=10000;
}
printf("\n file no \t file.size\t block no \t block size");
for(i=1;i<=nf;i++)
printf("\n \n %d \t\t%d\t\t%d\t\t%d",i,fsize[i],fflag[i],bsize[fflag[i]]);
}

OUTPUT

Enter the number of blocks : 4

Enter the size of memory block 1: 40

Enter the size of memory block 1: 100

Enter the size of memory block 1: 250

Enter the size of memory block 1: 50

Enter the number of files : 3

Enter the size of file 1: 50

Enter the size of file 2: 20

Enter the size of file 3: 225

File no File size Block No Block Size

1 50 4 50

2 20 1 40

23 225 3 250

OPERATING SYSTEMS LAB


EXPERIMENT – 5

IMPLEMENT PAGE REPLACEMENT ALGORITHMS

AIM:

Implement l page replacement algorithms a) FIFO b) LRU c) LFU

Page Replacement Algorithms :

1. First In First Out (FIFO) –


This is the simplest page replacement algorithm. In this algorithm, the operating system keeps track of all
pages in the memory in a queue, the oldest page is in the front of the queue. When a page needs to be replaced
page in the front of the queue is selected for removal.
Example-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.Find number of page faults

Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1 Page Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault.
Finally when 3 come it is not available so it replaces 0 1 page fault

2.Least Recently Used – (LRU)


In this algorithm page will be replaced which is least recently used.
Example-3Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page frames.Find number
of page faults.

OPERATING SYSTEMS LAB


Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in the
memory.

3. LEAST FREQUENTLY USED – (LFU)


In this algorithm, pages are replaced which would not be used for the longest duration of time in the future.
Example-2:Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame. Find number
of page fault.

OPERATING SYSTEMS LAB


PROGRAM

FIFO (FIRST IN FIRST OUT)

#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:\n"); scanf("%d",&no); for(i=0;i<no;i+


+) frame[i]=-1;
j=0;

printf("ref 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++)

OPERATING SYSTEMS LAB


printf("%d\t",frame[k]);

printf("\n");

printf("page fault is %d",count); getch(); return 0;


}

OUTPUT

Enter the number of frames : 3

Enter number of pages : 20


Enter page reference string
7 0 1 2
0 3 0 4 2 3 0 3 2 1

0 1 7 0 1

7 -1 -1
7 0 -1
7 0 1
2 0 1

2 3 1
2 3 0
2 0 3
4 3 0
4 2 0
4 2 3
0 2 3

0 1 3
0 1 2

7 1 2
7 0 2
7 0 1

Total page fault = 15

OPERATING SYSTEMS LAB


2.LEAST RECENTLY USED

#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i){


if(time[i] < minimum){
minimum = time[i]; pos
= i;
}
}
return pos;
}

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults =
0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}

for(i = 0; i < no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){


flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){

OPERATING SYSTEMS LAB


if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){ counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}

printf("\n");

for(j = 0; j < no_of_frames; ++j){


printf("%d\t", frames[j]);
}
}

OPERATING SYSTEMS LAB


printf("\n\nTotal Page Faults = %d", faults);

return 0;
}

OUTPUT

Enter the number of frames : 3

Enter number of pages : 20


Enter page reference string
7 0 1 2
0 3 0 4 2 3 0 3 2 1

0 1 7 0 1

7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 0 3
2 0 3
4 0 3
4 0 2
4 3 3
0 3 2
0 3 2
0 3 2
1 3 2
1 3 2
1 0 2
1 0 2
1 0 7
1 0 7
1 0 7

Total page fault = 12

OPERATING SYSTEMS LAB


3. LEAST FREQUENTLY USED

#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max,
faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

for(i = 0; i < no_of_pages; ++i){


scanf("%d", &pages[i]);
}

for(i = 0; i < no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){


flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){

OPERATING SYSTEMS LAB


faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

for(j = 0; j < no_of_frames; ++j){


temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){


if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}

for(j = 0; j < no_of_frames; ++j){


if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp[0];
pos = 0;

for(j = 1; j < no_of_frames; ++j){

OPERATING SYSTEMS LAB


if(temp[j] > max){
max = temp[j]; pos
= j;
}
}
}
frames[pos] = pages[i];
faults++;
}

printf("\n");

for(j = 0; j < no_of_frames; ++j){


printf("%d\t", frames[j]);
}
}

printf("\n\nTotal Page Faults = %d", faults);

return 0;

OUTPUT

Enter the number of frames : 3

Enter number of pages : 20


Enter page reference string
7 0 1 2
0 3 0 4 2 3 0 3 2 1

0 1 7 0 1

7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 0 3
2 4 3
2 4 3

OPERATING SYSTEMS LAB


2 4 3
2 0 3
2 0 3
2 0 3
2 0 1
2 0 1
2 0 1
2 0 1
7 0 1
7 0 1
7 0 1

Total page fault = 9

OPERATING SYSTEMS LAB

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy