0% found this document useful (0 votes)
28 views24 pages

Os Programs

The document discusses different page replacement algorithms like FIFO, LRU, OPR and FCS. It provides code snippets in C language to implement these algorithms. The code includes functions for checking page hits and misses, finding the frame to be replaced and calculating the number of hits and misses.
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)
28 views24 pages

Os Programs

The document discusses different page replacement algorithms like FIFO, LRU, OPR and FCS. It provides code snippets in C language to implement these algorithms. The code includes functions for checking page hits and misses, finding the frame to be replaced and calculating the number of hits and misses.
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/ 24

Write a c program for fifo algorithm.

#include < stdio.h >


int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
Write a program in c for LRU algorithm.
#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:");
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--)
{
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);
}
Write a program in c for OPR algorithm

#include <bits/stdc++.h>

using namespace std;

// Function to check whether a page exists

// in a frame or not

bool search(int key, vector<int>& fr)

for (int i = 0; i < fr.size(); i++)

if (fr[i] == key)

return true;

return false;

// Function to find the frame that will not be used

// recently in future after given index in pg[0..pn-1]

int predict(int pg[], vector<int>& fr, int pn, int index)

// Store the index of pages which are going

// to be used recently in future

int res = -1, farthest = index;

for (int i = 0; i < fr.size(); i++) {

int j;

for (j = index; j < pn; j++) {

if (fr[i] == pg[j]) {

if (j > farthest) {

farthest = j;
res = i;

break;

// If a page is never referenced in future,

// return it.

if (j == pn)

return i;

// If all of the frames were not in future,

// return any of them, we return 0. Otherwise

// we return res.

return (res == -1) ? 0 : res;

void optimalPage(int pg[], int pn, int fn)

// Create an array for given number of

// frames and initialize it as empty.

vector<int> fr;

// Traverse through page reference array

// and check for miss and hit.

int hit = 0;
for (int i = 0; i < pn; i++) {

// Page found in a frame : HIT

if (search(pg[i], fr)) {

hit++;

continue;

// Page not found in a frame : MISS

// If there is space available in frames.

if (fr.size() < fn)

fr.push_back(pg[i]);

// Find the page to be replaced.

else {

int j = predict(pg, fr, pn, i + 1);

fr[j] = pg[i];

cout << "No. of hits = " << hit << endl;

cout << "No. of misses = " << pn - hit << endl;

// Driver Function

int main()

{
int pg[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 };

int pn = sizeof(pg) / sizeof(pg[0]);

int fn = 4;

optimalPage(pg, pn, fn);

return 0;

}
Write a program in for FCS .

#include <bits/stdc++.h>

using namespace std;

// Function to check whether a page exists

// in a frame or not

bool search(int key, vector<int>& fr)

for (int i = 0; i < fr.size(); i++)

if (fr[i] == key)

return true;

return false;

// Function to find the frame that will not be used

// recently in future after given index in pg[0..pn-1]

int predict(int pg[], vector<int>& fr, int pn, int index)

// Store the index of pages which are going

// to be used recently in future

int res = -1, farthest = index;

for (int i = 0; i < fr.size(); i++) {

int j;

for (j = index; j < pn; j++) {

if (fr[i] == pg[j]) {

if (j > farthest) {

farthest = j;
res = i;

break;

// If a page is never referenced in future,

// return it.

if (j == pn)

return i;

// If all of the frames were not in future,

// return any of them, we return 0. Otherwise

// we return res.

return (res == -1) ? 0 : res;

void optimalPage(int pg[], int pn, int fn)

// Create an array for given number of

// frames and initialize it as empty.

vector<int> fr;

// Traverse through page reference array

// and check for miss and hit.

int hit = 0;
for (int i = 0; i < pn; i++) {

// Page found in a frame : HIT

if (search(pg[i], fr)) {

hit++;

continue;

// Page not found in a frame : MISS

// If there is space available in frames.

if (fr.size() < fn)

fr.push_back(pg[i]);

// Find the page to be replaced.

else {

int j = predict(pg, fr, pn, i + 1);

fr[j] = pg[i];

cout << "No. of hits = " << hit << endl;

cout << "No. of misses = " << pn - hit << endl;

// Driver Function

int main()

{
int pg[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 };

int pn = sizeof(pg) / sizeof(pg[0]);

int fn = 4;

optimalPage(pg, pn, fn);

return 0;

}
#include<stdio.h>

#include<conio.h>

void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y
12.
// Use for loop to enter the details of the process like Arrival time and the Burst Time

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


{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sumat[i]-
bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}

Output
Experiment no – 6

program for C-SCAN disk scheduling algorithm

#include<stdio.h>

int main()

int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],

temp1=0,temp2=0;

float avg; printf("Enter the max range of disk\

n"); scanf("%d",&max); printf("Enter the

initial head position\n"); scanf("%d",&head);

printf("Enter the size of queue request\n");

scanf("%d",&n); printf("Enter the queue of disk

positions to be read\n"); for(i=1;i<=n;i++)

scanf("%d",&temp);

if(temp>=head)

queue1[temp1]=temp;

temp1++;

e
l

queue2[temp2]=temp;

temp2++;

for(i=0;i<temp1-1;i++)

for(j=i+1;j<temp1;j++)

if(queue1[i]>queue1[j])

temp=queue1[i];

queue1[i]=queue1[j];

queue1[j]=temp;

for(i=0;i<temp2-1;i++)

for(j=i+1;j<temp2;j++)

if(queue2[i]>queue2[j])
{

temp=queue2[i];

queue2[i]=queue2[j];

queue2[j]=temp;

for(i=1,j=0;j<temp1;i++,j++)

queue[i]=queue1[j];

queue[i]=max;

queue[i+1]=0;

for(i=temp1+3,j=0;j<temp2;i++,j+

+) queue[i]=queue2[j];

queue[0]=head;

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

diff=abs(queue[j+1]-

queue[j]);

seek+=diff;

printf("Disk head moves from %d to %d with seek


%d\n",queue[j],queue[j+1],diff);

printf("Total seek time is %d\n",seek);

avg=seek/(float)n;

printf("Average seek time is %f\

n",avg);

return 0;
}

Output
Experiment no- 7

Program for process synchronization using semaphores

#include<pthread.h>

#include<stdio.h>

#include<semaphore.h>

#include<unistd.h> void

*fun1(); void *fun2(); int

shared=1; //shared variable

sem_t s; //semaphore variable

int main()

sem_init(&s,0,1); //initialize semaphore variable - 1st argument is address of variable, 2nd is

number of processes sharing semaphore, 3rd argument is the initial value of semaphore variable

pthread_t thread1, thread2; pthread_create(&thread1, NULL, fun1, NULL);

pthread_create(&thread2, NULL, fun2, NULL); pthread_join(thread1, NULL);

pthread_join(thread2,NULL); printf("Final value of shared is %d\n",shared); //prints the last

updated value of shared variable

void *fun1()

{ int

x;

sem_wait(&s); //executes wait operation on s

x=shared;//thread1 reads value of shared variable


printf("Thread1 reads the value as %d\n",x); x+

+; //thread1 increments its value

printf("Local updation by Thread1: %d\n",x); sleep(1); //thread1 is

preempted by thread 2 shared=x; //thread one updates the value of

shared variable printf("Value of shared variable updated by Thread1

is: %d\n",shared); sem_post(&s);

void *fun2()

{ int

y;

sem_wait(&s); y=shared;//thread2 reads value of shared variable

printf("Thread2 reads the value as %d\n",y); y--; //thread2 increments

its value printf("Local updation by Thread2: %d\n",y); sleep(1);

//thread2 is preempted by thread 1 shared=y; //thread2 updates the

value of shared variable printf("Value of shared variable updated by

Thread2 is: %d\n",shared); sem_post(&s);

Output
Experiment no-8 Linux

commands

ls - The most frequently used command in Linux to list directories

pwd - Print working directory command in Linux cd - Linux

command to navigate through directories mkdir - Command used

to create directories in Linux mv - Move or rename files in Linux

cp - Similar usage as mv but for copying files in Linux rm - Delete

files or directories touch - Create blank/empty files ln - Create

symbolic links (shortcuts) to other files cat - Display file contents

on the terminal clear - Clear the terminal display echo - Print any

text that follows the command less - Linux command to display

paged outputs in the terminal man - Access manual pages for all

Linux commands uname - Linux command to get basic

information about the OS whoami - Get the active username tar -

Command to extract and compress files in Linux grep - Search for

a string within an output head - Return the specified number of

lines from the top tail - Return the specified number of lines from

the bottom diff - Find the difference between two files cmp -

Allows you to check if two files are identical comm - Combines the

functionality of diff and cmp sort - Linux command to sort the

content of a file while outputting export - Export environment

variables in Linux

zip - Zip files in Linux unzip - Unzip files in Linux ssh - Secure Shell

command in Linux service - Linux command to start and stop services


ps - Display active processes kill and killall - Kill active processes by

process ID or name df - Display disk filesystem information mount -

Mount file systems in Linux chmod - Command to change file

permissions chown - Command for granting ownership of files or

folders ifconfig - Display network interfaces and IP addresses

traceroute - Trace all the network hops to reach the destination wget

- Direct download files from the internet ufw - Firewall command

iptables - Base firewall for all other firewall utilities to interface with

apt, pacman, yum, rpm - Package managers depending on the distro

sudo - Command to escalate privileges in Linux cal - View a command-

line calendar alias - Create custom shortcuts for your regularly used

commands dd - Majorly used for creating bootable USB sticks whereis

- Locate the binary, source, and manual pages for a command whatis -

Find what a command is used for top - View active processes live with

their system usage useradd and usermod - Add new user or change

existing users data passwd - Create or update passwords for existing

users

You might also like