0% found this document useful (0 votes)
10 views29 pages

Os Edited Manual

Uploaded by

Aanya Jain
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)
10 views29 pages

Os Edited Manual

Uploaded by

Aanya Jain
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/ 29

Name RA2311003030118

Experiment-6

AIM: Write a program of Dining Philosopher problem

Algorithm – monitor
ForkMonitor: integer
array[0..4]
fork ← [2,2,2,2,2]
condition array[0..4]OKtoEat

operation takeForks(integer i)
if(fork[i]!=2)
waitC(OKtoEat[i])

fork[i+1]<- fork[i+1]-1 fork[i-1]


<- fork[i-1]-1

operation releaseForks(integer i)
fork[i+1] <- fork[i+1]+1 fork[i-
1] <- fork[i-1]

if(fork[i+1]==2) signalC(OKtoEat[i+1])

if(fork[i-1]==2) signalC(OKtoEat[i-1])
For each Philosopher – loop
forever :
p1 : think
p2 : takeForks(i)
p3 : eat
p4 : releaseForks(i)
Here Monitor will ensure all such needs mentioned above.

Implementation in C++ :
Name RA2311003030118

Header file include


#include <bits/stdc++.h>
#include <pthread.h> #include
<unistd.h>
using namespace std;

#define N 10
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N

Philosopher index int


phil[N];
int times = 200;

class monitor {

state of the philosopher


int state[N];

Philosopher condition variable


pthread_cond_t phcond[N];

mutex variable for synchronization


pthread_mutex_t condLock;

public:
Test for the desired condition
i.e. Left and Right philosopher are not reading
void test(int phnum)
{

if (state[(phnum + 1) % 5] != EATING
and state[(phnum + 4) % 5] != EATING
and state[phnum] == HUNGRY) {
state[phnum] = EATING;

pthread_cond_signal(&phcond[phnum]);
}
}

Take Fork function


void take_fork(int phnum)
{

pthread_mutex_lock(&condLock);
Name RA2311003030118
Indicates it is hungry
state[phnum] = HUNGRY;

test for condition


test(phnum);

If unable to eat.. wait for the signal


if (state[phnum] != EATING) {
pthread_cond_wait(&phcond[phnum], &condLock);
}
cout << "Philosopher " << phnum << " is Eating"
<< endl;

pthread_mutex_unlock(&condLock);
}

Put Fork function


void put_fork(int phnum)
{

pthread_mutex_lock(&condLock);

Indicates that I am thinking


state[phnum] = THINKING;

test(RIGHT);
test(LEFT);

pthread_mutex_unlock(&condLock);
}

constructor monitor()
{

for (int i = 0; i < N; i++) {


state[i] = THINKING;
}

for (int i = 0; i < N; i++) {


pthread_cond_init(&phcond[i], NULL);
}

pthread_mutex_init(&condLock, NULL);
}

destructor
~monitor()
{

for (int i = 0; i < N; i++) {


pthread_cond_destroy(&phcond[i]);
Name RA2311003030118
}

pthread_mutex_destroy(&condLock);
}
}

// Global Object of the monitor phil_object;

void* philosopher(void* arg)


{
int c = 0;
while (c < times) {
int i = *(int*)arg;
sleep(1);
phil_object.take_fork(i);
sleep(0.5);
phil_object.put_fork(i);
c++; }
}

int main()
{

Declaration... pthread_t
thread_id[N];
pthread_attr_t attr;

Initialization...
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,
PTHREAD_CREATE_JOINABLE);

for (int i = 0; i < N; i++) {


phil[i] = i;
}

Creating...
for (int i = 0; i < N; i++) {
pthread_create(&thread_id[i], &attr, philosopher,
&phil[i]);
cout << "Philosopher " << i + 1 << " is thinking..."
<< endl;
}

Joining....
for (int i = 0; i < N; i++) {
pthread_join(thread_id[i], NULL);
}

Destroying
pthread_attr_destroy(&attr);
Name RA2311003030118
pthread_exit(NULL);

return 0;
}

Output :

Result: Program successfully executed.

Experiment-7
Name RA2311003030118
AIM : To implement Bankers Algorithm for Deadlock avoidance

BANKER'S ALGORITHM
The banker's algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating
the allocation for predetermined maximum possible amounts of all resources, then makes an 's-state' check to test for
possible activities, before deciding whether allocation should be allowed to continue.
The Banker's algorithm got its name because it could be used in a banking system to ensure that the bank never
allocated its available cash in such a way that it could no longer satisfy the needs of all its customers.

Properties -
Multiple in
stances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite amount of time

PROGRAM-
#include<bits/stdc++.h>
using namespace std;

int allocation[10][3],need[10][3],Max[10]
[3],available[10][3]; int p,current[3]; bool
executed[10],come; void IMP(){
come=false;
for (int i = 0; i < 10; ++i)
{
executed[i]=false;
}
} void
Calculate(){
IMP();
int i,j;
for (i = 0; i < p; ++i)
{
for (j = 0; j < p; ++j)
{
while(executed[j] && j<p-1){
j++;
}
if (need[j][0]<=current[0]&&need[j][1]<=current[1]&&need[j][2]<=current[2])
{
if (!executed[j])
{
executed[j]=true;

current[0]+=allocation[j][0];current[1]+=allocation[j][1];current[2]+=allocation[j][2];
cout<<"\nProcess P"<<j+1;
cout<<"\nCurrent: "<<current[0]<<" "<<current[1]<<"

"<<current[2]<<"\n";
cout<<"\nProcess executed without deadlock";
Name RA2311003030118
come=true;
break;
}
}
}
if (!come)

{
cout<<"\n Dead lock\n\n";
break;
}else{
come=false;
}
}
}

int main (){


int keepon = 1; cout<<"Enter No.
of processes: ";
cin>>p;
cout<<"\n";
cout<<"Enter the current resources: ";
cin>>current[0]>>current[1]>>current[2];
for (int i = 0; i < p; ++i)
{
cout<<"\n\n Process P"<<i+1<<" Details\n"; cout<<"Enter Allocation : ";
cin>>allocation[i][0]>>allocation[i][1]>>allocation[i][2];
cout<<"Enter Max :"; cin>>Max[i][0]>>Max[i]
[1]>>Max[i][2];
need[i][0]=Max[i][0]-allocation[i][0];need[i][1]=Max[i][1]-allocation[i][1];need[i][2]=Max[i]
[2]allocation[i][2];
}
cout<<"\n\n Table for Bankers Algo\n\n";
cout<<"Initial Resources: "<<current[0]<<" "<<current[1]<<" "<<current[2]<<"\n\n";
cout<<"Process Max Allocation Need\n"; for (int i = 0; i < p; ++i)
{
cout<<" P"<<i+1<<" ";
cout<<" "<<Max[i][0]<<" "<<Max[i][1]<<" "<<Max[i][2]<<" "; cout<<" "<<allocation[i]
[0]<<" "<<allocation[i][1]<<" "<<allocation[i][2]<<" ";
cout<<" "<<need[i][0]<<" "<<need[i][1]<<" "<<need[i][2];
cout<<"\n";
}
cout<<"\n\n"; Calculate();
while(keepon){
int val,pro;
cout<<"\n\nSelect Below oprations:\n\n";
cout<<"1.Change Max of process: \n";
cout<<"2.Change Allocation of process\n";
cout<<"3.Change Initial Resources\n";
cout<<"4.Exit\n\n";
cin>>val;
if (val==1)
{
cout<<"\n\nEnter Process No: ";
Name RA2311003030118
cin>>pro;
cout<<"\nEnter New Max: ";
cin>>Max[pro-1][0]>>Max[pro-1][1]>>Max[pro-1][2];
}
else if (val==2)
{
cout<<"\n\nEnter Process No: ";
cin>>pro;
cout<<"\nEnter New Allocation: ";
cin>>allocation[pro-1][0]>>allocation[pro-1][1]>>allocation[pro-1][2];
}
else if (val==3)
{
cout<<"\nEnter Initial Resources: ";
cin>>current[0]>>current[1]>>current[2];

}
else{
break;
}
Calculate();
}
return 0;
}
Output:

Result: Program successfully executed.

Experiment-8
Name RA2311003030118

AIM: Write a program of FCFS and SJF Scheduling

ALGO FCFS
1- Input the processes along with their burst time (bt).
2- Find waiting time (wt) for all processes. 3- As first process that
comes need not to wait so waiting time for process 1 will be 0 i.e.
wt[0] = 0.
4- Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time
for all processes.
6- Find average waiting time =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.
PROGRAM:
#include<iostream>
using namespace std;

void findWaitingTime(int processes[], int n,


int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time for
(int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

void findTurnAroundTime( int processes[], int n,


int bt[], int wt[], int tat[])
{

//TAT=bt[i] + wt[i] for (int


i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

void findavgTime( int processes[], int n, int bt[])


{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
Name RA2311003030118
Function to find waiting time of all processes findWaitingTime(processes, n, bt,
wt);

Function to find turn around time for all processes findTurnAroundTime(processes, n,


bt, wt, tat);

Display processes along with all details cout


<< "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";

Calculate total waiting time and total turn


around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i]; total_tat =
total_tat + tat[i]; cout << " " << i+1 << "\t\t" << bt[i]
<<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n; cout << "\
nAverage turn around time = "
<< (float)total_tat / (float)n;
}

Driver code int


main()
{
process id's
int processes[] = { 1, 2, 3}; int n = sizeof
processes / sizeof processes[0];

Burst time of all processes


int burst_time[] = {10, 5, 8};

findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT
Name RA2311003030118

ALGO SJF:

The steps to perform the SJF scheduling program in c.

● Firstly, we will begin the procedure.

● After that, we will Consider the number of elements to be inserted.

● Then, we will choose the process with the shortest burst time and will execute the first process.

● We will check that if both processes have the same burst time length then the FCFS scheduling algorithm is used.

● We will make the average waiting time for the next process.

● We will begin with the first process, make the above selection, and place the other processes in a queue.

● We will calculate burst time.

● We will display the values that are related.

● In the end, we will stop the process.

● Likewise, we will run all the steps until all the processes are executed.

● PROGRAM:-
#include<stdio.h>

void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]); p[i]=i+1;
//contains process number
}
Name RA2311003030118
//sorting burst time in ascending order using selection sort for(i=0;i<n;i+
+)

{ pos
=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; // calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
Output:
Name RA2311003030118

Result: Program successfully executed.


Name RA2311003030118

Experiment-9
AIM: Write a program of Priority and Round robin scheduling

Program:
#include<iostream>
using namespace std; int
main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:"; cin>>n;
cout<<"\nEnter Burst Time and Priority\n";
for(i=0;i<n;i++)
{
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; contains process number
}
sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)

{ pos
=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
calculate waiting time
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"; for(i=0;i<n;i+
+)
{ tat[i]=bt[i]+wt[i]; //calculate turnaround
time total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
Name RA2311003030118
}
avg_tat=total/n; //average turnaround time cout<<"\n\nAverage
Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;
return 0;

Result: Program successfully executed.


Name RA2311003030118
Experiment -10

AIM: Write a program of FIFO Page Replacement Algorithm

Algorithm for FIFO Page Replacement

● Step 1. Start to traverse the pages.


● Step 2. If the memory holds fewer pages, then the capacity else goes to step 5.
● Step 3. Push pages in the queue one at a time until the queue reaches its maximum capacity or all page requests are
fulfilled.
● Step 4. If the current page is present in the memory, do nothing.
● Step 5. Else, pop the topmost page from the queue as it was inserted first.
● Step 6. Replace the topmost page with the current page from the string.
● Step 7. Increment the page faults.
● Step 8. Stop

PROGRAM
C program for FIFO page replacement 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];
}
Name RA2311003030118
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;
}
Output –

Result: Program successfully executed.

Experiment-11
Name RA2311003030118
AIM: LRU and LFU Page Replacement Algorithm

Program:
#include<stdio.h>
#include<limits.h>

int checkHit(int incomingPage, int queue[], int occupied){

for(int i = 0; i < occupied; i++){


if(incomingPage == queue[i])
return 1;
}

return 0;
}

void printFrame(int queue[], int occupied)


{ for(int i = 0; i < occupied; i+
+) printf("%d\t\t\
t",queue[i]);
}

int main()
{

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


incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3}; int
incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3};

int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3; int queue[n]; int distance[n]; int
occupied = 0;
int pagefault = 0;

printf("Page\t Frame1 \t Frame2 \t Frame3\n");

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


{
printf("%d: \t\
t",incomingStream[i]); what if
currently in frame 7 next item that
appears also 7
didnt write condition for HIT

if(checkHit(incomingStream[i], queue, occupied)){


printFrame(queue, occupied);
}

filling when frame(s) is/are empty


else if(occupied < frames)
{ queue[occupied] =
incomingStream[i];
pagefault++;
occupied++;

printFrame(queue, occupied);
}
else{
Name RA2311003030118

int max = INT_MIN;

int index; get LRU distance for each


item in frame
for (int j = 0; j < frames; j++)
{ distance[j] = 0;
traverse in reverse direction to find at
what distance frame item occurred last
for(int k = i - 1; k >= 0; k--)
{
++distance[j];

if(queue[j] == incomingStream[k])
break;
}

find frame item with max distance for LRU


also notes the index of frame item in queue
which appears furthest(max distance)
if(distance[j] > max){ max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied); pagefault+
+;
}

printf("\n");
}

printf("Page Fault: %d",pagefault);

return 0;
}

OUTPUT
Page Frame1 Frame2 Frame3
1: 1
2: 1 2
3: 1 2 3
2: 1 2 3
1: 1 2 3
5: 1 2 5
2: 1 2 5
1: 1 2 5
6: 1 2 6
2: 1 2 6
5: 5 2 6
6: 5 2 6
3: 5 3 6
1: 1 3 6
3: 1 3 6
Page Fault: 8

Result: Program successfully executed.


Name RA2311003030118

Experiment -12

AIM : Write a program of Best fit and Worst fit memory management policies

BEST FIT PROGRAM:


#include <stdio.h>

void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses)


{
This will store the block id of the allocated block to a process
int allocation[proccesses];
int occupied[blocks];
Name RA2311003030118
initially assigning -1 to all allocation
indexes means nothing is allocated
currently for(int i = 0; i < proccesses; i+
+){
allocation[i] = -1;
}

for(int i = 0; i < blocks; i++)


{ occupied[i] = 0;
}

pick each process and find suitable blocks


according to its size ad assign to it
for (int i = 0; i < proccesses; i++)
{
int indexPlaced = -1; for
(int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i] && !occupied[j])
{
place it at the first block fit to accomodate process
if (indexPlaced == -1)
indexPlaced = j;

if any future block is smalller than the current block where


process is placed, change the block and thus indexPlaced
// this reduces the wastage thus best fit
else if (blockSize[j] < blockSize[indexPlaced])
indexPlaced = j;
}
}

If we were successfully able to find block for the


process if (indexPlaced != -1)
{
allocate this block j to process p[i]
allocation[i] = indexPlaced;

make the status of the block as occupied


occupied[indexPlaced] = 1;
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < proccesses; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1); else
printf("Not Allocated\n");
}
}

Driver code int main() { int blockSize[] = {100,


50, 30, 120, 35}; int processSize[] = {40, 10, 30,
60}; int blocks =
sizeof(blockSize)/sizeof(blockSize[0]);
Name RA2311003030118
int proccesses = sizeof(processSize)/sizeof(processSize[0]);

implimentBestFit(blockSize, blocks, processSize, proccesses);

return 0 ;

OUTPUT:
Process No. Process Size Block no.
1 10 2
2 30 1
3 60 4
4 30 4

WORST FIT PROGRAM:


#include <stdio.h>

void implimentWorstFit(int blockSize[], int blocks, int processSize[], int


processes) {
This will store the block id of the allocated block to a process
int allocation[processes];
int occupied[blocks];

initially assigning -1 to all allocation


indexes means nothing is allocated
currently for(int i = 0; i < processes; i++){
allocation[i] = -1;
}

for(int i = 0; i < blocks; i++)


{ occupied[i] = 0;
}

pick each process and find suitable blocks


according to its size ad assign to it
for (int i=0; i < processes; i++)
{
int indexPlaced = -1;
for(int j = 0; j < blocks; j++)
{
if not occupied and block size is large enough
if(blockSize[j] >= processSize[i] && !occupied[j])
{
place it at the first block fit to accomodate process
if (indexPlaced == -1)
indexPlaced = j;

if any future block is larger than the current block


where process is placed, change the block and thus
indexPlaced else if (blockSize[indexPlaced] <
blockSize[j])
indexPlaced = j;
}
}
Name RA2311003030118

If we were successfully able to find block for the process


if (indexPlaced != -1)
{
allocate this block j to process p[i]
allocation[i] = indexPlaced;

make the status of the block as occupied


occupied[indexPlaced] = 1;

Reduce available memory for the block


blockSize[indexPlaced] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < processes; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

Driver code int main() { int blockSize[] = {100,


50, 30, 120, 35}; int processSize[] = {40, 10, 30,
60}; int blocks =
sizeof(blockSize)/sizeof(blockSize[0]);
int processes = sizeof(processSize)/sizeof(processSize[0]);

implimentWorstFit(blockSize, blocks, processSize, processes);

return
0; }
Output
Process No. Process Size Block no.
1 40 4
2 10 1
3 30 2
4 60 Not Allocated

Result: Program successfully executed.


Name RA2311003030118

Experiment-13

AIM: Write a program of Disk Scheduling algorithm


first come first serve disk scheduling:-
FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed in the order they
arrive in the disk queue. Example: Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Readwrite
head initially at the track 50 and the tail track being at 199.
#include<stdio.h> #include<stdlib.h> int
main() { int
RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n");
scanf("%d",&n); printf("Enter the Requests
sequence\n"); for(i=0;i<n;i++)
scanf("%d",&RQ[i]); printf("Enter initial
head position\n"); scanf("%d",&initial);

logic for FCFS disk scheduling

for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

printf("Total head moment is %d",TotalHeadMoment);


return 0;
Name RA2311003030118

}
OUTPUT

Enter the number of Request


8
Enter the Requests Sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Total head movement is 644

Scan/ Elevator disk scheduling:-

It is also called as Elevator Algorithm. In this algorithm, the disk arm moves into a particular direction till the end,
satisfying all the requests coming in its path, and then it turns backend moves in the reverse direction satisfying
requests coming in its path.

It works in the way an elevator works, elevator moves in a direction completely till the last floor of that direction and
then turns back.

Example:- Given the following queue -- 95, 180, 34, 119, 11, 123, 62, 64 with the Read-write head initially at the
track 50 and the tail track being at 199. head movement is towards low value.

#include<stdio.h>
#include<stdlib.h>
int main() {
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\
n"); for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\
n"); scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);

logic for Scan disk scheduling


Name RA2311003030118

/*logic for sort the request array */


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

}
}

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

{ if(initial<RQ
[i])

{ index
=i;
break;
}
}

if movement is towards high value


if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}
if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
last movement for min size
Name RA2311003030118
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-
0); initial =0; for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}

printf("Total head movement is %d",TotalHeadMoment);


return
0; }

Output:-
Enter the number of Request
8
Enter the Requests Sequence 95 180 34 119 11 123 62 64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0
1
Total head movement is 337

Result: Program successfully executed.

Experiment-14

AIM : Write a program of Sequential and Indexed file Allocation

Algorithm for Indexed File Allocation:


Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer. If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer Step 9: If the
buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required Step
11: Terminate the process.

/* Program to simulate indexed file allocation strategy */ Program Code:


#include<stdio.h>
#include<conio.h>
#include<stdlib.h> void
main()
{ int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
Name RA2311003030118
x:printf("Enter the index block: ");
scanf("%d",&ind); if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind); scanf("%d",&n);
} else
{
printf("%d index is already allocated \n",ind); goto x;
}
y: count=0; for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0) count++;
}
if(count==n)
{ for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
} else
{ prin
tf("Fi
le in
the
index
is
alrea
dy
alloca
ted \
n");

printf("Enter another file indexed");


goto y; }
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else exit(0); getch();
}

Program Output:
Name RA2311003030118

Algorithm for Sequential File Allocation:


Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is big enough is encountered. It
allocates that memory block for the requesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can be allocated to requesting
process and allocates it.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and allocates it to the process. Step 7:
Analyses all the three memory management techniques and display the best algorithm which utilizes the memory
resources effectively and efficiently.
Step 8: Stop the program.

/* Program to simulate sequential file allocation strategy */ Program Code:


#include <stdio.h>
#include <stdlib.h>
#include<conio.h> void
main()
{
int f[50], i, st, len, j, c, k, count = 0;
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");

You might also like