Oprating System lab Practical

                        Operating System Lab Manual File 

 

 

S.No. 

List of Experiments 

Page no.  

Date 

Remarks 

 

   1 

 

Write a Program to add three numbers in C language 

 

         3 

 

 

      9/9/22 

 

 

2 

 

Write a program to multiply three numbers in C language. 

 

4 

 

 

16/9/22 

 

 

3 

Case Study on Windows, UNIX, Linux, MAC, Android and iOS. 

 

5-8 

 

30/9/22 

 

 

4 

Write a program for First Come First Serve CPU Scheduling Algorithm. 

 

 

      9-11 

 

 

     7/10/22 

 

 

5 

 

Write a C program to stimulate the CPU scheduling algorithm shortest job first (non-Preemption). 

 

     12-15 

 

 

14/10/22 

 

 

6 

Write a C program to simulate the CPU scheduling algorithm round robin. 

 

   16-19 

 

 

28/10/22 

 

 

7 

Write a C program to simulate the CPU scheduling priority algorithm. 

 

   20-23 

 

 

11/11/22 

 

8 

To Implement FIFO page replacement technique. 

24-27 

2/12/22 

 

 

9 

 

To Implement Least Recently Used (LRU) page replacement technique. 

 

      28-31 

 

      9/12/22 

 

   10 

 

To implement LFU page replacement technique. 

 

32-37 

 

23/12/22 

 

EXPERIMENT-1 

 

Aim: Write a Program to add three numbers in C language. 

 

Program:  

#include <stdio.h> 

#include <conio.h> 

int sum (int x,int y,int z){ 

int temp; 

temp=x+y+z; 

return temp; 

} 

void main () { 

int a,b,c,d; 

printf("\nACCEPT VALUE FOR a,b,c:\n"); 

scanf("%d %d %d",&a,&b,&c); 

d=sum(a,b,c); 

printf("\nSUM OF %d,%d and %d IS %d",a,b,c,d); 

getch(); 

} 

Output:  

ACCEPT VALUE FOR a, b, c:13 23 4556 

SUM OF 13, 23 and 4556 IS 4592 

 

 

EXPERIMENT-2 

 

Aim: Write a program to multiply three numbers in C language. 

Program: 

#include <stdio.h> 

void main () { 

   int a,b,c,p; 

   printf("Enter first number: "); 

   scanf("%d",&a); 

   printf("Enter second number: "); 

   scanf("%d",&b); 

   printf("Enter third number: "); 

   scanf("%d",&c); 

   p=a*b*c; 

   printf("Product of the numbers is: %d",p); 

  getch(); 

} 

Ouput: 

Enter first number: 123 

Enter second number: 234 

Enter third number: 345 

Product of the numbers is: 9929790 

 

 

EXPERIMENT-3 

 

Aim: Case Study on Windows, UNIX, Linux, MAC, Android and iOS. 

 

Operating systems define how a computer stores files, switches between different applications, manages memory, keeps itself secure, and interacts with peripherals like printers and cameras. Different operating systems take different approaches to all of these, which is why you normally can't run a Windows program on a Macintosh computer and why permissions look different on an Android phone than on an iPhone. 

 

Some operating systems are designed by groups of people around the world, like the open source, freely available operating system Linux, while others are commercial products made by one company, such as Microsoft's Windows and Apple's macOS. 

 

Different operating systems run on different types of hardware and are designed for different types of applications. For example, iOS is designed for iPhones and iPad tablets, while Mac desktops and laptops use macOS. Your computer or smartphone comes equipped with an OS, but you can install another one in some cases. 

 

Microsoft Windows 

Microsoft Windows has existed in one form or another since 1985, and it remains the most popular operating system for home and office computers. Its latest versions, including Windows 10, are also used on some tablets, and the OS is used on some web and number-crunching server computers as well. Computers from a wide variety of manufacturers can use Windows. 

 

Initial versions of Windows worked with an earlier Microsoft operating system called MS-DOS, providing a modern graphical interface on top of DOS's traditional text-based commands. Signature features of Microsoft Windows's user interface include windows themselves – rectangle-shaped, on-panel screens that represent individual applications. The Windows Start menu has helped generations of users find programs and files on their devices. 

 

Efforts to use versions of the Windows OS for smartphones have been less successful. 

 

Apple iOS 

Apple's iOS is one of the most popular smartphone operating systems, second only to Android. It runs on Apple hardware, including iPhones, iPad tablets and iPod Touch media players. 

 

Signature features of iOS include the App Store where users buy apps and download free software, an emphasis on security including strong encryption to limit what unauthorized users can extract from the phone, and a simple, streamlined interface with minimal hardware buttons. 

 

Google's Android OS 

Android is the most popular operating system in the world judging by the number of devices installed. Largely developed by Google, it's chiefly used on smartphones and tablets. Unlike iOS, it can be used on devices made by a variety of different manufacturers, and those makers can tweak parts of its interface to suit their own needs. 

 

Users can download custom versions of the operating system because large portions of it are open source, meaning anyone can legally modify it and publish their own. However, most people prefer to stick with the version that comes on their devices. 

 

Android, like iOS, comes with an application and media store called the Play Store built by Google. Some phone manufacturers and other organizations also offer their own stores to install software and media. 

 

Apple macOS 

Apple's macOS, successor to the popular OS X operating system, runs on Apple laptops and desktops. Based in part on the historic family of Unix operating systems dating back to research in the 1960s at AT&T's Bell Labs, macOS shares some features with other Unix-related operating systems including Linux. While the graphical interfaces are different, many of the underlying programming interfaces and command line features are the same. 

 

Signature elements of macOS include the dock used to find programs and frequently used files, unique keyboard keys including the Command key, and the stoplight-colored buttons used to resize open program windows. MacOS is known for its user-friendly features, which include Siri, a natural-voice personal assistant, and FaceTime, Apple's video-calling application. 

 

Linux Operating System 

Unlike many other operating systems, development on Linux isn't led by any one company. The operating system was created by Finnish programmer Linus Torvalds in 1991. Nowadays, programmers from all over the world collaborate on its open source code and submit tweaks to the central kernel software and other programs. 

 

A wide assortment of commercial and open source software is available for Linux, and various Linux distributions provide custom user interfaces and tools for installing software onto machines running the operating system. A favorite of many programmers, Linux is widely used on corporate and scientific servers, including cloud computing environments. Linux can be run on a wide variety of hardware and is available free of charge over the internet. 

 

 

Windows 

Linux 

Mac 

UNIX 

Android 

iOS 

Manufacturer 

Microsoft Inc. 

Linux is developed as open source OS under the GNU project by the Originator, Linus Torvalds and many others. 

Apple Inc. for their Macintosh line of computer systems. 

Three biggest distributions are Solaris running (Oracle), AIXon (IBM) & HP- 

UX Hewlett Packard. And Apple Makes OSX, an Unix based OS 

Open source OS designed &developed by Android Inc. Google is now the current owner 

 

Apple Inc. closed, with components that are source openly 

Development and Distribution 

Developed and distributed by Microsoft. 

Linux is Open Sourced and distributed by various vendors. 

Mac OS was designed only to be deployed by Apple Computers. 

Unix system has various flavors, most of which are developed by AT&T with other commercial vendors and non-profit orgs. 

OHA (Open Handset Alliance) 

Apple Inc. developed and distributed iOS 

Computer Architecture Supported 

x86, x86-64 

x86, x86-64, PowerPC, SPARC, 

Alpha, Others 

68k, 

PowerPC 

Available on PA-RISC and Itanium machines. Solaris also available for x86/x64 based systems. OSX is PowerPC(10.0- 10.5)/x86(10.4)/x64 (10.5-10.8) 

Android-x86 powered by AMD and Intelx86 processors. 

ARM 

Target System Type 

Workstation, Personal Computer, Media Centre, Tablet PC, Embedded. 

Desktop/Server Depends on Distribution 

Workstation, Personal Computer, embedded 

8086 UNIX system, PDP-11/70 system 

Consumer, Enterprise, education 

 

 

Integrated Firewall 

Windows Firewall 

Chroot capability- based security, [s 5] seccomp, SELinux 

Application Firewall 

IPFilter 

iptables 

Firewall-IP for iOS 

Security Threats 

Huge 

Negligible 

Negligible 

Mild 

Negligible 

Negligible 

Shell Terminal 

CMD 

Bash shell powerful shell with many features 

BASH 

Originally the Bourne Shell. Now it's compatible with many others including BASH, Korn & C. 

Mosh 

Blink Shell 

Kernel Type 

Hybrid 

Monolithic with modules 

Monolithic with modules 

Monolithic with modules 

Linux kernel 

XNU kernel of Darwin 

Reliability 

Great 

Great 

Greatest 

Greater 

Could be unstable 

More than Android 

Compatibility 

Can coexist on local networks with Windows, BSD, Macs, and other Unix-like systems. More compatible. 

Linux has few programs and games like Windows. But is more compatible and scalable than Unix 

Only few programs will run on Mac 

Unix does not have as many programs and games as Windows 

Better than iOS 

Compatibility is fair 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EXPERIMENT-4 

 

Aim: Write a program for First Come First Serve CPU Scheduling Algorithm. 

 

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: 

Step1: Start the process. 

Step2: Accept the number of processes in the ready Queue. 

Step 3: For each process in the ready Q, assign the process name and the burst time. 

Step 4: Set the waiting of the first process as ‗0 ‘and its burst time as its turnaround time. 

Step 5: for each process in the Ready Q. calculate 

 a).Waiting time (n) = waiting time (n-1) + Burst time(n-1) 

 b).Turnaround time(n)= waiting time(n)+Burst time(n) 

Step6: Calculate 

  1. Average waiting time=Total waiting Time/Number of process 

  1. Average Turnaround time=Total Turnaround Time/Number of process. 

Step7: Stop the process. 

 

Source code: 

 

// First Come First Serve (FCFS Program in C) 

#include <stdio.h> 

#include <conio.h> 

int 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 ("\nEnterBurstTimeforProcess%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 ("\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n"); 

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

    printf ("\n\tP%d\t\t%d\t\t%d \t\t%d", i, bt[i], wt[i], tat[i]); 

    printf ("\nAverageWaitingTime--%f", wtavg / n); 

    printf ("\nAverageTurnaroundTime--%f", tatavg / n); 

    getch (); 

    return 0; } 

 

Input: 

 

 

 

 

Enter the number of processes--3 

Enter Burst Time for Process0--24 

Enter Burst Time for Process1--3 

Enter Burst Time for Process2--3 

 

Output: 

 

 

 

 

 

 

BURSTTIME 

WAITINGTIME 

TURNAROUNDTIME 

24 

0 

24 

3 

24 

27 

3 

27 

30 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

EXPERIMENT-5 

 

Aim: To write a program to stimulate the CPU scheduling algorithm shortest job first (non-Preemption). 

 

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: 

Step1: Start the process 

Step2: Accept the number of processes in the ready Queue 

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time 

Step4: Start the Ready Q according the shortest burst time by sorting according to lowest to highest burst time. 

Step 5: Set the waiting time of the first process as ‗0 and its turnaround time as its burst time. 

Step6: Sort the processes names based on their burst time 

Step7: For each process in the ready queue, calculate 

  1. Waiting time(n)=waiting time(n-1) +Burst time(n-1) 

  1. Turnaround time(n)=waiting time(n)+Burst time(n) 

Step8: Calculate 

  1. Average waiting time=Total waiting Time/Number of process 

  1. Average Turnaround time = Total Turnaround Time/ Number of process. 

Step 9:  Stop the process 

  

Source code: 

 

 #include <stdio.h> 

 #include <conio.h> 

 int 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 ("EnterBurstTimeforProcess%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\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\n"); 

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

    printf ("\n\tP%d\t\t%d\t\t%d\t\t%d", p[i], bt[i], wt[i], tat[i]); 

    printf ("\nAverageWaitingTime--%f", wtavg / n); 

    printf ("\nAverageTurnaroundTime--%f", tatavg / n); 

    getch (); 

    return 0; 

} 

 

Input: 

 

Enter the number of processes 4 

Enter Burst Time for Process0 6 

Enter Burst Time for Process1 8 

Enter Burst Time for Process2 7 Enter Burst Time for Process3 3 

 

Output: 

 

 

 

 

PROCESS 

BURSTTIME 

WAITINGTIME 

TURNAROUNDTIME 

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 

 

 

  

EXPERIMENT-6 

 

Aim: To simulate the CPU scheduling algorithm round-robin. 

 

Description: 

 

To aim is to calculate the average waiting time. There 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: 

Step1: Start the process 

Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice 

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time 

Step 4: Calculate the no. of time slices for each process where No. of time slice for process            (n) = burst time process (n)/time slice 

Step5: If the burst time is less than the time slice then of time slices=1. 

Step6: Consider the ready queue is a circular Q, calculate 

  1. Waiting time for process(n)=waiting time of process(n-1) +burst time of process (n-1) + the time difference in getting the CPU from process(n-1) 

  1. Turnaround time for process (n) = waiting time of process (n) + burst time of process (n) + the time difference in getting CPU from process(n). 

Step7: Calculate 

  1. Average waiting time=Total waiting Time/Number of process 

  1. Average Turnaround time = Total Turnaround Time / Number of process  

Step8:  Stop the process 

  

 

Source code: 

 

#include<stdio.h> 

int main(){ 

int i,j,n,bu[10],wa[10],tat[10],t,ct[10],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("\nEnterthesizeoftimeslice--"); 

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("\nTheAverageTurnaroundtimeis--%f",att/n); 

printf("\nTheAverageWaitingtimeis --%f",awt/n); 

printf("\n\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\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(); 

return 0; 

} 

  

Input: 

Enter the no of processes–3 

Enter Burst Time for process 1 – 24 

Enter Burst Time for process 2 – 3 

Enter Burst Time for process 3 – 3 

Enter the size of time slice –3 

 

 

Output: 

      The Average Turnaround time is--15.000000 

      The Average Waiting time is --5.000000 

        PROCESS     BURSTTIME       WAITINGTIME     TURNAROUNDTIME 

1        24             6              30  

2       3              3               

3        3              6              9 

 

 

  

EXPERIMENT-7 

 

Aim: To write a c program to simulate the CPU scheduling priority algorithm. 

 

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: 

Step1: Start the process 

Step2: Accept the number of processes in the ready Queue 

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time  

Step 4: Sort the ready queue according to the priority number. 

Step 5: Set the waiting of the first process as ‗0 and its burst time as its turnaround time. 

 Step6: Arrange the processes based on process priority 

Step7: For each process in the Ready Q calculate 

  1. Waiting time(n)=waiting time(n-1) +Burst time(n-1) 

  1. Turnaround time(n)=waiting time(n)+Burst time(n) 

Step8: Calculate 

  1. Average waiting time=Total waiting Time/Number of process 

  1. Average Turnaround time =Total Turnaround Time/Number of process.   

Step 9: Stop. 

 

Sourcecode: 

 

#include<stdio.h> 

int main () { 

        int i,j,n,bu[10],wa[10],tat[10],t,ct[10],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("\nEnterthesizeoftimeslice--"); 

    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("\nTheAverageTurnaroundtimeis--%f",att/n); 

    printf("\nTheAverageWaitingtimeis --%f",awt/n); 

    printf("\n\tPROCESS\tBURSTTIME\tWAITINGTIME\tTURNAROUNDTIME\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(); 

   return 0; 

} 

 

Input: 

Entertheno. ofprocesses–3 

 

Enter Burst Time for process 1 – 24 

Enter Burst Time for process 2 – 3 

Enter Burst Time for process 3 – 3 

Enter the size of time slice – 3 

 

 

 

 

Output: 

Text Box 

 

 

 

The Average Turnaround time is – 15.666667 

The Average Waiting time is ---5.666667 

 

  

EXPERIMENT-8 

 

Aim: To implement FIFO page replacement technique. 

 

Description: 

The FIFO page-replacement algorithm is easy to understand and program. However, its performance is not always good. 

On the one hand, the page replaced may be an initialization module that was used a long time ago and is no longer needed. 

On the other hand, it could contain a heavily used variable that was initialized early and is in constant use. 

 

Algorithm: 

 

  1. Start the process 

  1. Read number of pages  

  1. Read number of pages no. 

  1. Read page numbers into an array a[i] 

  1. Initialize avail[i]=0.to check page hit 

  1. Replace the page with circular queue, while replacing check page availability in the frame Place avail[i]=1 if page is placed in the frame Count page faults. 

  1. Print the results. 

  1. Stop the process 

. 

Source code: 

 

#include<stdio.h> 

#include<conio.h> 

int fr[3]; 

void main () 

{  

    void display (); 

    int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2}; 

    int flag1=0, flag2=0, pf=0, frsize=3, top=0; 

    clrscr(); 

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

        fr[i]=-1; 

    } 

    for (j=0; j<12; j++) 

    { 

        flag1=0; 

       flag2=0; 

        for (i=0; i<12; i++) 

        { 

            if(fr[i]==page[j]) 

            { 

                flag1=1; 

                flag2=1; 

                break; 

            } 

        } 

        if(flag1==0) 

        { 

            for (i=0; i<frsize;i++) 

            {    

                if(fr[i]==-1) 

                { 

                    fr[i]=page[j]; 

                    flag2=1; 

                    break; 

                } 

            } 

        } 

        if(flag2==0) 

        { 

            fr[top]=page[j]; 

            top++; 

            pf++; 

            if(top>=frsize) top=0; 

        } 

            display (); 

    } 

    printf("Numberofpagefaults:%d",pf); 

    getch(); 

} 

void display () 

{ 

    int i 

    printf("\n");  

    for (i=0; i<3; i++) 

    printf("%d\t”, fr[i]); 

}    

 

OUTPUT: 

2-1-1 

23-1 

23-1 

231 

531 

521 

524 

524 

324 

324 

354 

352 

Number of page faults: 6 

 

  

EXPERIMENT-9 

 

Aim: To implement Least Recently Used (LRU) page replacement technique. 

 

Algorithm: 

 

  1. Start the process. 

  1. Declare the size. 

  1. Get the number of pages to be inserted. 

  1. Get the value. 

  1. Declare counter and stack. 

  1. Select the least recently used page by counter value. 

  1. Stack them according the selection. 

  1. Display the values. 

  1. Stop the process. 

 

 

Sourcecode: 

 

#include<stdio.h> 

#include<conio.h> 

int fr[3]; 

void main() 

{ 

    void display(); 

    int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3]; 

    int index,k,l,flag1=0,flag2=0,pf=0,frsize=3; 

    clrscr(); 

    for(i=0;i<3;i++) 

    { 

        fr[i]=-1; 

    } 

    for (j=0; j<12; j++) 

    { 

        flag1=0, flag2=0; 

        for (i=0; i<3; i++) 

        { 

            if(fr[i]==p[j]) 

            { 

                flag1=1; 

                flag2=1; 

                break; 

            } 

        } 

        if(flag1==0) 

        { 

            for (i=0; i<3; i++) 

            { 

                if(fr[i]==-1) 

                { 

                    fr[i]=p[j]; 

                    flag2=1; 

                    break; 

                } 

            } 

        } 

        if(flag2==0) 

        { 

            for (i=0; i<3; i++) fs[i]=0; 

                for (k=j-1, l=1; l<=frsize-1; l++, k--) 

                { 

                    for (i=0; i<3; i++) 

                    { 

                        if(fr[i]==p[k]) fs[i]=1; 

                    } 

                } 

            for (i=0; i<3; i++) 

            { 

            if(fs[i]==0) 

                index=i; 

            } 

        fr[index]=p[j]; 

        pf++; 

    } 

    display (); 

    } 

    printf("\n no of page faults:%d",pf); 

    getch(); 

} 

void display () 

{ 

    int i; 

    printf("\n"); 

    for (i=0; i<3; i++) 

    printf("\t%d”, fr[i]); 

} 

 

OUTPUT: 

2-1-1 

23-1 

23-1 

231 

251 

251 

254 

254 

354 

352 

352 

352 

no. of page faults: 4 

 

 

  

EXPERIMENT-10 

 

Aim: To implement LFU page replacement technique. 

 

Algorithm: 

  1. Start Program. 

  1. Read Number of Pages and Frames. 

  1. Read Each Page Value. 

  1. Search For Page in The Frames. 

  1. If Not Available Allocate Free Frame. 

  1. If No Frames Is Free Replace the Page with The Page That Is Leastly Used. 

  1. Print Page Number of Page Faults. 

  1. Stop process. 

 

Source code: 

 

/* Program to simulate optimal page replacement */ 

#include<stdio.h> 

#include<conio.h> 

int fr[3],n,m; 

void display (); 

void main () 

{  

    int i,j,page[20],fs[10]; 

    int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;  

    float pr; 

    clrscr(); 

    printf("Enter length of the reference string: "); 

    scanf("%d",&n); 

    printf("Enter the reference string: "); 

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

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

    printf("Enter no of frames: "); 

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

    fr[i]=-1; 

    pf=m; 

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

    { 

        flag1=0; 

        flag2=0; 

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

        { 

            if(fr[i]==page[j]) 

            { 

                flag1=1; 

                flag2=1; 

                break; 

            } 

        } 

        if(flag1==0) 

        { 

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

 

 

            { 

                if(fr[i]==-1) 

                { 

                    fr[i]=page[j]; 

                    flag2=1; 

                    break; 

                } 

            }    

        } 

        if(flag2==0) 

        { 

            for (i=0; i<m;i++)lg[i]=0; 

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

            { 

                for (k=j+1; k<=n;k++) 

                { 

                    if(fr[i]==page[k]) 

                    { 

                        lg[i]=k-j; 

                        break; 

                    } 

                } 

            } 

        found=0; 

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

        { 

            if(lg[i]==0) 

            { 

                index=i; 

                found =1;    

                break; 

            } 

        } 

        if(found==0) 

        { 

            max=lg [0]; 

            index=0; 

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

            { 

                if(max<lg[i]) 

                { 

                    max=lg[i]; 

                    index=i; 

                } 

            } 

        } 

        fr[index]=page[j]; 

        pf++; 

        } 

    display (); 

    } 

    printf("Number of page faults : %d\n", pf); 

    pr=(float)pf/n*100; 

    printf("Page fault rate = %f \n", pr); 

    getch(); 

} 

void display () 

{ 

    int i 

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

    printf("%d\t”, fr[i]); 

    printf("\n"); 

} 

 

OUTPUT: 

Enter length of the reference string: 12 

Enter the reference string: 12 34 12 51 23 45 

 Enter no of frames: 3 

1-1-1 

12 -1 

1 23 

1 24 

1 24 

1 24 

1 25 

1 25 

1 25 

3 25 

4 25 

4 25 

Number of page faults: 7  

Page fault rate=58.333332 

  

Comments

Popular Posts