operating system lab manual

71
NCS-451/JP/ 1 Meerut Institute of Technology, Meerut Department of Computer Science & Engineering LAB MANUAL Lab Name: Operating System Lab Code: NCS-451 Jyotirmay Patel

Upload: meerut-institute-of-technology-meerut

Post on 15-Jul-2015

710 views

Category:

Education


29 download

TRANSCRIPT

NCS-451JP 1

Meerut Institute of Technology Meerut

Department of Computer Science amp Engineering

LAB MANUAL

Lab Name Operating System

Lab Code NCS-451

Jyotirmay Patel

NCS-451JP 2

Operating System (ECS-551)

List of Experiment

SNo Program Page No

1 Testing the Linux Unix Commands

5

2 Getting familiar with vi editor and environment

10

3 Write a C program to simulate FCFS CPU scheduling algorithm

14

4 Write a C program to simulate SJF CPU scheduling algorithm

20

5 Write a C program to simulate Priority CPU scheduling algorithm

27

6 Write a C program to simulate Round Robin CPU scheduling

algorithm

33

7 Write a C program to simulate Bankerrsquos algorithm for deadlock

avoidance

40

8 Write a C program to simulate FIFO Page replacement algorithm

50

9 Write a C program to simulate LRU Page replacement algorithm

55

10 Write a C program to simulate FCFS disk scheduling algorithm

61

11 Write a C program to simulate SSTF disk scheduling algorithm

66

NCS-451JP 3

HardwareSoftware Requirements

Hardware

1 700 MHz processor (about Intel Celeron or better)

2 512 MB RAM (system memory)

3 5 GB of hard-drive space (or USB stick memory card or external

drive but see LiveCD for an alternative approach)

4 VGA capable of 1024x768 screen resolution

5 Either a CDDVD drive or a USB port for the installer media

Software

Ubuntu Desktop OS latest release Windows XP7 Turbo C++ Compiler

Latest Version

NCS-451JP 4

Objective of the Lab

1 To familiarize the learners with the Operating System

2 To introduce LINUX basic commands

3 To get familiarize with vi editor

4 To make students how to make simple programs in LINUX

5 To implement different CPU scheduling algorithms in C

6 To implement different page replacement algorithms in C

7 To implement Banker algorithms in C

NCS-451JP 5

Linux Commands

These commands will work with most (if not all) distributions of Linux as well as most

implementations of UNIX These are commonly use commands

Index

1 Navigation - how to get around

o cd - changing directories

o ls - listing files

o pwd - knowing where you are

2 File Management - who needs a graphical file manager

o cp - copying files

o ln - creating symbolic links

o mv - moving and renaming files

o rm - removing files

3 Monitoring Your System - to satisfy your insatiable curiosity

o tail - follow a file as it grows

o top - a program to see how your memory and CPU are holding up

o w - look at whos logged on

4 Shutting Down and Rebooting - you better know this though you may not use it a lot

Navigation

Navigating around the files and directories of your hard drive could be a dreaded task for

you but it is necessary knowledge If you were a user of command prompt interfaces such as

MS-DOS youll have little trouble adjusting Youll only need to learn a few new commands

If youre used to navigating using a graphical file manager I dont know how itll be like but

some concepts might require a little more clarification Or maybe itll be easier for you Who

knows Everyone is different

cd

As you might already have guessed the cd command changes directories Its a very common

navigation command that youll end up using just like you might have done in MS-DOS

You must put a space between cd and the or else it wont work Linux doesnt see the two

dots as an extension to the cd command but rather a different command altogether Itll come

to make sense if it doesnt already

NCS-451JP 6

ls

The ls letters stand for list It basically works the same way as the dir command in DOS

Only being a Unix command you can do more with it -)

Typing ls will give you a listing of all the files in the current directory If youre new to

Linux chances are that the directories you are commonly in will be empty and after the ls

command is run you arent given any information and will just be returned to the command

prompt (the shell)

There are hidden files in Linux too Their file names start with a dot and doing a normal ls

wont show them in a directory Many configuration files start with a dot on their file names

because they would only get in the way of users who would like to see more commonly used

items To view hidden files use the -a flag with the ls command ie ls -a

To view more information about the files in a directory use the -l flag with ls It will show

the file permissions as well as the file size which are probably what are the most useful

things to know about files

You might occasionally want to have a listing of all the subdirectories also A simple -R flag

will do so you could look upon ls -R as a rough equivalent of the dir s command in MS-

DOS

You can put flags together so to view all the files in a directory show their permissionssize

and view all the files that way through the subdirectories you could type ls -laR

pwd

This command simply shows what directory youre in at the moment It stands for Print

Working Directory Its useful for scripting in case you might ever want to refer to your

current directory

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 2

Operating System (ECS-551)

List of Experiment

SNo Program Page No

1 Testing the Linux Unix Commands

5

2 Getting familiar with vi editor and environment

10

3 Write a C program to simulate FCFS CPU scheduling algorithm

14

4 Write a C program to simulate SJF CPU scheduling algorithm

20

5 Write a C program to simulate Priority CPU scheduling algorithm

27

6 Write a C program to simulate Round Robin CPU scheduling

algorithm

33

7 Write a C program to simulate Bankerrsquos algorithm for deadlock

avoidance

40

8 Write a C program to simulate FIFO Page replacement algorithm

50

9 Write a C program to simulate LRU Page replacement algorithm

55

10 Write a C program to simulate FCFS disk scheduling algorithm

61

11 Write a C program to simulate SSTF disk scheduling algorithm

66

NCS-451JP 3

HardwareSoftware Requirements

Hardware

1 700 MHz processor (about Intel Celeron or better)

2 512 MB RAM (system memory)

3 5 GB of hard-drive space (or USB stick memory card or external

drive but see LiveCD for an alternative approach)

4 VGA capable of 1024x768 screen resolution

5 Either a CDDVD drive or a USB port for the installer media

Software

Ubuntu Desktop OS latest release Windows XP7 Turbo C++ Compiler

Latest Version

NCS-451JP 4

Objective of the Lab

1 To familiarize the learners with the Operating System

2 To introduce LINUX basic commands

3 To get familiarize with vi editor

4 To make students how to make simple programs in LINUX

5 To implement different CPU scheduling algorithms in C

6 To implement different page replacement algorithms in C

7 To implement Banker algorithms in C

NCS-451JP 5

Linux Commands

These commands will work with most (if not all) distributions of Linux as well as most

implementations of UNIX These are commonly use commands

Index

1 Navigation - how to get around

o cd - changing directories

o ls - listing files

o pwd - knowing where you are

2 File Management - who needs a graphical file manager

o cp - copying files

o ln - creating symbolic links

o mv - moving and renaming files

o rm - removing files

3 Monitoring Your System - to satisfy your insatiable curiosity

o tail - follow a file as it grows

o top - a program to see how your memory and CPU are holding up

o w - look at whos logged on

4 Shutting Down and Rebooting - you better know this though you may not use it a lot

Navigation

Navigating around the files and directories of your hard drive could be a dreaded task for

you but it is necessary knowledge If you were a user of command prompt interfaces such as

MS-DOS youll have little trouble adjusting Youll only need to learn a few new commands

If youre used to navigating using a graphical file manager I dont know how itll be like but

some concepts might require a little more clarification Or maybe itll be easier for you Who

knows Everyone is different

cd

As you might already have guessed the cd command changes directories Its a very common

navigation command that youll end up using just like you might have done in MS-DOS

You must put a space between cd and the or else it wont work Linux doesnt see the two

dots as an extension to the cd command but rather a different command altogether Itll come

to make sense if it doesnt already

NCS-451JP 6

ls

The ls letters stand for list It basically works the same way as the dir command in DOS

Only being a Unix command you can do more with it -)

Typing ls will give you a listing of all the files in the current directory If youre new to

Linux chances are that the directories you are commonly in will be empty and after the ls

command is run you arent given any information and will just be returned to the command

prompt (the shell)

There are hidden files in Linux too Their file names start with a dot and doing a normal ls

wont show them in a directory Many configuration files start with a dot on their file names

because they would only get in the way of users who would like to see more commonly used

items To view hidden files use the -a flag with the ls command ie ls -a

To view more information about the files in a directory use the -l flag with ls It will show

the file permissions as well as the file size which are probably what are the most useful

things to know about files

You might occasionally want to have a listing of all the subdirectories also A simple -R flag

will do so you could look upon ls -R as a rough equivalent of the dir s command in MS-

DOS

You can put flags together so to view all the files in a directory show their permissionssize

and view all the files that way through the subdirectories you could type ls -laR

pwd

This command simply shows what directory youre in at the moment It stands for Print

Working Directory Its useful for scripting in case you might ever want to refer to your

current directory

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 3

HardwareSoftware Requirements

Hardware

1 700 MHz processor (about Intel Celeron or better)

2 512 MB RAM (system memory)

3 5 GB of hard-drive space (or USB stick memory card or external

drive but see LiveCD for an alternative approach)

4 VGA capable of 1024x768 screen resolution

5 Either a CDDVD drive or a USB port for the installer media

Software

Ubuntu Desktop OS latest release Windows XP7 Turbo C++ Compiler

Latest Version

NCS-451JP 4

Objective of the Lab

1 To familiarize the learners with the Operating System

2 To introduce LINUX basic commands

3 To get familiarize with vi editor

4 To make students how to make simple programs in LINUX

5 To implement different CPU scheduling algorithms in C

6 To implement different page replacement algorithms in C

7 To implement Banker algorithms in C

NCS-451JP 5

Linux Commands

These commands will work with most (if not all) distributions of Linux as well as most

implementations of UNIX These are commonly use commands

Index

1 Navigation - how to get around

o cd - changing directories

o ls - listing files

o pwd - knowing where you are

2 File Management - who needs a graphical file manager

o cp - copying files

o ln - creating symbolic links

o mv - moving and renaming files

o rm - removing files

3 Monitoring Your System - to satisfy your insatiable curiosity

o tail - follow a file as it grows

o top - a program to see how your memory and CPU are holding up

o w - look at whos logged on

4 Shutting Down and Rebooting - you better know this though you may not use it a lot

Navigation

Navigating around the files and directories of your hard drive could be a dreaded task for

you but it is necessary knowledge If you were a user of command prompt interfaces such as

MS-DOS youll have little trouble adjusting Youll only need to learn a few new commands

If youre used to navigating using a graphical file manager I dont know how itll be like but

some concepts might require a little more clarification Or maybe itll be easier for you Who

knows Everyone is different

cd

As you might already have guessed the cd command changes directories Its a very common

navigation command that youll end up using just like you might have done in MS-DOS

You must put a space between cd and the or else it wont work Linux doesnt see the two

dots as an extension to the cd command but rather a different command altogether Itll come

to make sense if it doesnt already

NCS-451JP 6

ls

The ls letters stand for list It basically works the same way as the dir command in DOS

Only being a Unix command you can do more with it -)

Typing ls will give you a listing of all the files in the current directory If youre new to

Linux chances are that the directories you are commonly in will be empty and after the ls

command is run you arent given any information and will just be returned to the command

prompt (the shell)

There are hidden files in Linux too Their file names start with a dot and doing a normal ls

wont show them in a directory Many configuration files start with a dot on their file names

because they would only get in the way of users who would like to see more commonly used

items To view hidden files use the -a flag with the ls command ie ls -a

To view more information about the files in a directory use the -l flag with ls It will show

the file permissions as well as the file size which are probably what are the most useful

things to know about files

You might occasionally want to have a listing of all the subdirectories also A simple -R flag

will do so you could look upon ls -R as a rough equivalent of the dir s command in MS-

DOS

You can put flags together so to view all the files in a directory show their permissionssize

and view all the files that way through the subdirectories you could type ls -laR

pwd

This command simply shows what directory youre in at the moment It stands for Print

Working Directory Its useful for scripting in case you might ever want to refer to your

current directory

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 4

Objective of the Lab

1 To familiarize the learners with the Operating System

2 To introduce LINUX basic commands

3 To get familiarize with vi editor

4 To make students how to make simple programs in LINUX

5 To implement different CPU scheduling algorithms in C

6 To implement different page replacement algorithms in C

7 To implement Banker algorithms in C

NCS-451JP 5

Linux Commands

These commands will work with most (if not all) distributions of Linux as well as most

implementations of UNIX These are commonly use commands

Index

1 Navigation - how to get around

o cd - changing directories

o ls - listing files

o pwd - knowing where you are

2 File Management - who needs a graphical file manager

o cp - copying files

o ln - creating symbolic links

o mv - moving and renaming files

o rm - removing files

3 Monitoring Your System - to satisfy your insatiable curiosity

o tail - follow a file as it grows

o top - a program to see how your memory and CPU are holding up

o w - look at whos logged on

4 Shutting Down and Rebooting - you better know this though you may not use it a lot

Navigation

Navigating around the files and directories of your hard drive could be a dreaded task for

you but it is necessary knowledge If you were a user of command prompt interfaces such as

MS-DOS youll have little trouble adjusting Youll only need to learn a few new commands

If youre used to navigating using a graphical file manager I dont know how itll be like but

some concepts might require a little more clarification Or maybe itll be easier for you Who

knows Everyone is different

cd

As you might already have guessed the cd command changes directories Its a very common

navigation command that youll end up using just like you might have done in MS-DOS

You must put a space between cd and the or else it wont work Linux doesnt see the two

dots as an extension to the cd command but rather a different command altogether Itll come

to make sense if it doesnt already

NCS-451JP 6

ls

The ls letters stand for list It basically works the same way as the dir command in DOS

Only being a Unix command you can do more with it -)

Typing ls will give you a listing of all the files in the current directory If youre new to

Linux chances are that the directories you are commonly in will be empty and after the ls

command is run you arent given any information and will just be returned to the command

prompt (the shell)

There are hidden files in Linux too Their file names start with a dot and doing a normal ls

wont show them in a directory Many configuration files start with a dot on their file names

because they would only get in the way of users who would like to see more commonly used

items To view hidden files use the -a flag with the ls command ie ls -a

To view more information about the files in a directory use the -l flag with ls It will show

the file permissions as well as the file size which are probably what are the most useful

things to know about files

You might occasionally want to have a listing of all the subdirectories also A simple -R flag

will do so you could look upon ls -R as a rough equivalent of the dir s command in MS-

DOS

You can put flags together so to view all the files in a directory show their permissionssize

and view all the files that way through the subdirectories you could type ls -laR

pwd

This command simply shows what directory youre in at the moment It stands for Print

Working Directory Its useful for scripting in case you might ever want to refer to your

current directory

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 5

Linux Commands

These commands will work with most (if not all) distributions of Linux as well as most

implementations of UNIX These are commonly use commands

Index

1 Navigation - how to get around

o cd - changing directories

o ls - listing files

o pwd - knowing where you are

2 File Management - who needs a graphical file manager

o cp - copying files

o ln - creating symbolic links

o mv - moving and renaming files

o rm - removing files

3 Monitoring Your System - to satisfy your insatiable curiosity

o tail - follow a file as it grows

o top - a program to see how your memory and CPU are holding up

o w - look at whos logged on

4 Shutting Down and Rebooting - you better know this though you may not use it a lot

Navigation

Navigating around the files and directories of your hard drive could be a dreaded task for

you but it is necessary knowledge If you were a user of command prompt interfaces such as

MS-DOS youll have little trouble adjusting Youll only need to learn a few new commands

If youre used to navigating using a graphical file manager I dont know how itll be like but

some concepts might require a little more clarification Or maybe itll be easier for you Who

knows Everyone is different

cd

As you might already have guessed the cd command changes directories Its a very common

navigation command that youll end up using just like you might have done in MS-DOS

You must put a space between cd and the or else it wont work Linux doesnt see the two

dots as an extension to the cd command but rather a different command altogether Itll come

to make sense if it doesnt already

NCS-451JP 6

ls

The ls letters stand for list It basically works the same way as the dir command in DOS

Only being a Unix command you can do more with it -)

Typing ls will give you a listing of all the files in the current directory If youre new to

Linux chances are that the directories you are commonly in will be empty and after the ls

command is run you arent given any information and will just be returned to the command

prompt (the shell)

There are hidden files in Linux too Their file names start with a dot and doing a normal ls

wont show them in a directory Many configuration files start with a dot on their file names

because they would only get in the way of users who would like to see more commonly used

items To view hidden files use the -a flag with the ls command ie ls -a

To view more information about the files in a directory use the -l flag with ls It will show

the file permissions as well as the file size which are probably what are the most useful

things to know about files

You might occasionally want to have a listing of all the subdirectories also A simple -R flag

will do so you could look upon ls -R as a rough equivalent of the dir s command in MS-

DOS

You can put flags together so to view all the files in a directory show their permissionssize

and view all the files that way through the subdirectories you could type ls -laR

pwd

This command simply shows what directory youre in at the moment It stands for Print

Working Directory Its useful for scripting in case you might ever want to refer to your

current directory

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 6

ls

The ls letters stand for list It basically works the same way as the dir command in DOS

Only being a Unix command you can do more with it -)

Typing ls will give you a listing of all the files in the current directory If youre new to

Linux chances are that the directories you are commonly in will be empty and after the ls

command is run you arent given any information and will just be returned to the command

prompt (the shell)

There are hidden files in Linux too Their file names start with a dot and doing a normal ls

wont show them in a directory Many configuration files start with a dot on their file names

because they would only get in the way of users who would like to see more commonly used

items To view hidden files use the -a flag with the ls command ie ls -a

To view more information about the files in a directory use the -l flag with ls It will show

the file permissions as well as the file size which are probably what are the most useful

things to know about files

You might occasionally want to have a listing of all the subdirectories also A simple -R flag

will do so you could look upon ls -R as a rough equivalent of the dir s command in MS-

DOS

You can put flags together so to view all the files in a directory show their permissionssize

and view all the files that way through the subdirectories you could type ls -laR

pwd

This command simply shows what directory youre in at the moment It stands for Print

Working Directory Its useful for scripting in case you might ever want to refer to your

current directory

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 7

File Management

A lot of people surprisingly for me prefer to use graphical file managers Fortunately for me

I wasnt spoiled like that and used commands in DOS That made it a bit easier for me to

make the transition to Linux Most of the file management Linux gurus do is through the

command line so if you learn to use the commands you can brag that youre a guru Well

almost

cp

Copying works very much the same The cp command can be used just like the MS-DOS

copy command only remember that directories are separated with slashes () instead of

backslashes () So a basic command line is just cp filename1 filename2

There are other extensions to the cp command You can use the -f command to force it You

can use the -p command to preserve the permissions (and also who owns the file but Im not

sure)

You can move an entire directory to its new destination Lets say you want to copy a

directory (and all of its contents) from where you are to be homejacknewdirectory You

would type cp -rpf olddirectory homejacknewdirectory To issue this command you

would have to be in the directory where the subdirectory olddirectory is actually located

ln

A feature of linking files is available in Linux It works by redirecting a file to the actual

file Its referred to as a symbolic link Dont confuse this term with the linking of programs

which is when binary programs are connected with libraries that they need to load in order to

run

The most simple way that Ive ever used ln to create symbolic links is ln -s existing_file link

Evidently theres a hard link and a symbolic link Ive been using a symbolic link all along

You can also use the -f flag to force the command line to overwrite anything that might have

the symbolic links file name already

To remove a symbolic link simply type rm symbolic_link It wont remove the file that its

linked to

mv

The mv command can be used both to move files and to rename them The syntax is mv

fileone filetwo where fileone is the original file name and filetwo will be the new file

name

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 8

You cant move a directory that is located in one partition to another unfortunately You can

copy it though using cp -rpf and then remove it with rm -rf later on If you have only a

single partition that makes up your filesystem then you have very little to worry about in this

area

rm

The rm command is used for removing files You use it just like the del or delete command

in MS-DOS Lets say you want to remove a file called foobar in your current directory To

do that simply type rm foobar Note that there is no Recycle Bin like in Windows 95 So

when you delete a file its gone for good

To delete something in some other directory use the full path as the file name For example

if you want to delete a file called windows thats in the directory usrlocalsrc you would

type rm usrlocalsrcwindows

To remove an entire directory and its contents type rm -rf directory where directory is

the path to the directory that you want to delete If youre wondering the rf stands for

recursive and force Be very careful with this command as it can wreak havoc easily if

misused

Monitoring Your System

An important part of system administration (especially with your own system) is being able

to know whats going on

tail

The program tail allows you to follow a file as it is growing Most often I use it to follow

varlogmessages I do that by typing tail -f varlogmessages Of course you can use

anything else including the other logs in varlog Another file you may want to keep an eye

out for is varlogsecure

If you want to leave that running all the time I recommend having some sort of terminal

program in X logged in as root through su

Another program you may want to look at is head It monitors the top of the file specified

instead of the bottom

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 9

top

This program shows a lot of stuff that goes on with your system In the program you can

type

1 M for memory usage information

2 P for CPU information

3 q to quit

Once you try it you can see that top shows you the memory usage uptime load average

CPU states and processes

w

Typing w will tell you who is logged in This can be helpful if youre the only one who uses

your computer and you see someone logged in thats not supposed to be

Another alternative is who

Shutting Down and Rebooting

To shut down your system type shutdown -h now which tells the shutdown

program to begin system halt immediately You can also tell it to halt the system at a later

time I think but youll have to consult the shutdown manual page for that (man shutdown)

To do a reboot you can either type reboot or shutdown -r You can also use the

famous Ctrl-Alt-Delete combination to reboot which you might already be familiar with

Shutting down and restarting properly (as described above) will prevent your

filesystem from being damaged Filesystem damage is the most obvious of the consequences

but there are probably other things out there that I dont know about The point is shut down

your system properly

There are (rare) cases in which the machine might lock up entirely and prevent you

from being able to access a command prompt Only then will your last resort be to do a

forced reboot (just pressing the restart button on the case)

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 10

vi Editor Commands

General Startup

To use vi vi filename

To exit vi and save changes ZZ or wq

To exit vi without saving changes q

To enter vi command mode [esc]

Counts

A number preceding any vi command tells vi to

repeat that command that many times

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the

current line

^ move cursor to first nonblank column on the

current line

w move to the beginning of the next word or

punctuation mark

W move past the next space

b move to the beginning of the previous word

or punctuation mark

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 11

B move to the beginning of the previous word

ignores punctuation

e end of next word or punctuation mark

E end of next word ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen

Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen

( does not work with VT100 type terminals )

Inserting

r replace character under cursor with next

character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 12

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code

yy (yank)copies line which may then be put by

the p(put) command Precede with a count for

multiple lines

Put Command

brings back previous deletion or yank of lines

words or characters

P bring back before cursor

p bring back after cursor

Find Commands

finds a word going backwards

finds a word going forwards

f finds a character on the line under the

cursor going forward

F finds a character on the line under the

cursor going backwards

t find a character on the current line going

forward and stop one character before it

T find a character on the current line going

backward and stop one character before it

repeat last f F t T

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 13

Miscellaneous Commands

repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after

second (swap)

J join current line with the next line

^G display current line number

if at one parenthesis will jump to its mate

mx mark current line with character x

x find line marked with character x

NOTE Marks are internal and not written to the

file

Line Editor Mode

Any commands form the line editor ex can be

issued upon entering line mode

To enter type

To exit press[return] or [esc]

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 14

PROGRAM 01

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo FIRST

COME FIRST SERVE rsquo ALGORITHM

Concept Perhaps First-Come-First-Served algorithm is the simplest scheduling algorithm is the

simplest scheduling algorithm Processes are dispatched according to their arrival time on the ready

queue Being a nonpreemptive discipline once a process has a CPU it runs to completion The FCFS

scheduling is fair in the formal sense or human sense of fairness but it is unfair in the sense that long

jobs make short jobs wait and unimportant jobs make important jobs wait

FCFS is more predictable than most of other schemes since it offers time FCFS scheme is not

useful in scheduling interactive users because it cannot guarantee good response time The code for

FCFS scheduling is simple to write and understand One of the major drawback of this scheme is that

the average time is often quite long

The First-Come-First-Served algorithm is rarely used as a master scheme in modern

operating systems but it is often embedded within other schemes

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijsum=0

int arrv[10] ser[10] start[10] finish[10]wait[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 15

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS d i+1)

scanf(ddamparrv[i]ampser[i])

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 16

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d ni arrv[i]

ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu

avgturn)

getch()

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 17

FCFS CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 0

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 1

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 0 3 0 3 0 3

P1 1 3 3 6 3 6

P2 2 2 6 8 6 8

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5666667 tu

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 18

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 1

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 4

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 2 2 4 2 4

P2 1 5 4 9 4 9

P3 4 6 9 15 9 15

AVERAGE WAITING TIME = 3750000 tu

AVERAGE TURN AROUND TIME = 7500000 tu

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 19

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 1 1

2

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 2 3

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 3 2

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 4 3

6

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 5 5

3

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 1 2 0 2 0 2

P1 3 5 2 7 2 7

P2 2 6 7 13 7 13

P3 3 6 13 19 13 19

P4 5 3 19 22 19 22

AVERAGE WAITING TIME = 8200000 tu

AVERAGE TURN AROUND TIME = 12600000 tu

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 20

PROGRAM 02

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING lsquo

SHORTEST JOB FIRST rsquo ALGORITHM

Concept Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process)

with the smallest estimated run-time-to-completion is run next In other words when CPU is

available it is assigned to the process that has smallest next CPU burst The SJF scheduling is

especially appropriate for batch jobs for which the run times are known in advance Since the SJF

scheduling algorithm gives the minimum average time for a given set of processes it is probably

optimal

The SJF algorithm favors short jobs (or processors) at the expense of longer ones The obvious

problem with SJF scheme is that it requires precise knowledge of how long a job or process will run

and this information is not usually available The best SJF algorithm can do is to rely on user

estimates of run times

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]arrv[10]ser[10]start[10]finish[10]wait[10]

turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 21

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE ARRIVAL TIME AND SERVICE TIME

OF PROCESS di+1)

scanf(ddamparrv[i]ampser[i])

pro[i]=i

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(ser[j]gtser[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 22

temp2=arrv[j]

arrv[j]=arrv[j+1]

arrv[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 23

for(i=0iltni++)

avgwait +=wait[i]

avgturn +=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS ARRIVAL SERVICE START FINISH WAIT

TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n

pro[i]arrv[i] ser[i] start[i] finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 24

SJF CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 15

9

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 35

6

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 2 4 0 4 0 4

P1 5 6 4 10 4 10

P2 5 9 10 19 10 19

AVERAGE WAITING TIME = 4666667 tu

AVERAGE TURN AROUND TIME = 11000000 tu

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 25

ENTER THE NO OF PROCESSES4

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 12

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 29

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 33

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 45

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 9 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 3 3 6 9 6 9

AVERAGE WAITING TIME = 2500000 tu

AVERAGE TURN AROUND TIME = 4750000 tu

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 26

ENTER THE NO OF PROCESSES5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 11

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 22

3

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 34

5

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 46

1

ENTER THE ARRIVAL TIME AND SERVICE TIME OF PROCESS 55

2

PROCESS ARRIVAL SERVICE START FINISH WAIT TURN

P0 6 1 0 1 0 1

P1 5 2 1 3 1 3

P2 2 3 3 6 3 6

P3 1 5 6 11 6 11

P4 4 5 11 16 11 16

AVERAGE WAITING TIME = 4200000 tu

AVERAGE TURN AROUND TIME = 7400000 tu

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 27

PROGRAM 03

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo PRIORITY SCHEDULING rsquo ALGORITHM

Concept Each process is assigned a priority and priority is allowed to run Equal-Priority

processes are scheduled in FCFS order The shortest-Job-First (SJF) algorithm is a special case of

general priority scheduling algorithm

Source code

includeltstdiohgt

includeltconiohgt

void main()

int nijtemp1temp2sum=0

int pro[10]ser[10]start[10]finish[10]wait[10]prior[10] turn[10]

float avgturn=00avgwait=00

start[0]=0

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

printf(n ENTER THE SERVICE TIME AND PRIORITY OF PROCESSdi+1)

scanf(ddampser[i]ampprior[i])

pro[i]=i

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 28

for(i=0iltni++)

for(j=0jltn-i-1j++)

if(prior[j]gtprior[j+1])

temp1=ser[j]

ser[j]=ser[j+1]

ser[j+1]=temp1

temp2=prior[j]

prior[j]=prior[j+1]

prior[j+1]=temp2

for(i=0iltni++)

sum=0

for(j=0jltij++)

sum=sum+ser[j]

start[i]=sum

for(i=0iltni++)

finish[i]=ser[i]+start[i]

wait[i]=start[i]

turn[i]=ser[i]+wait[i]

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 29

for(i=0iltni++)

avgwait+=wait[i]

avgturn+=turn[i]

avgwait=n

avgturn=n

printf(n PROCESS SERVICE PRIORITY START FINISH WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d t d t d t d t d n pro[i]ser[i] prior[i]

start[i]finish[i]wait[i]turn[i])

printf(n AVERAGE WAITING TIME = f tuavgwait)

printf(n AVERAGE TURN AROUND TIME = f tu avgturn)

getch()

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 30

Priority CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS21

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS33

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 1 5 7 8 7 8

AVERAGE WAITING TIME = 3666667 tu

AVERAGE TURN AROUND TIME = 6333333 tu

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 31

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS11

3

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS32

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS43

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 1 3 0 1 0 1

P1 3 4 1 4 1 4

P2 3 5 4 7 4 7

P3 2 6 7 9 7 9

AVERAGE WAITING TIME = 3000000 tu

AVERAGE TURN AROUND TIME = 5250000 tu

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 32

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS14

2

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS23

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS31

6

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS42

7

ENTER THE SERVICE TIME AND PRIORITY OF PROCESS53

4

PROCESS SERVICE PRIORITY START FINISH WAIT TURN

P0 4 2 0 4 0 4

P1 3 4 4 7 4 7

P2 3 6 7 10 7 10

P3 1 6 10 11 10 11

P4 2 7 11 13 11 13

AVERAGE WAITING TIME = 6400000 tu

AVERAGE TURN AROUND TIME = 9000000 tu

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 33

PROGRAM 04

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo ROUND ROBIN SCHEDULING rsquo ALGORITHM

Concept In the round robin scheduling processes are dispatched in a FIFO manner but are

given a limited amount of CPU time called a time-slice or a quantum If a process does not complete

before its CPU-time expires the CPU is preempted and given to the next process waiting in a queue

The preempted process is then placed at the back of the ready list

Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in

time-sharing environments in which the system needs to guarantee reasonable response times for

interactive users The only interesting issue with round robin scheme is the length of the quantum

Setting the quantum too short causes too many context switches and lower the CPU efficiency On

the other hand setting the quantum too long may cause poor response time and appoximates FCFS

Source code

includeltstdiohgt

includeltconiohgt

void main()

int count=0swt=0stat=0itempsq=0

int pro[10]st[10]bt[10]wt[10]tat[10]ntq

float atat=00awt=00

clrscr()

printf(n ENTER THE NO OF PROCESSES)

scanf(dampn)

for(i=0iltni++)

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 34

printf(n ENTER THE SERVICE TIME OF PROCESS di)

scanf(dampbt[i])

st[i]=bt[i]

pro[i]=i

printf(n ENTER THE TIME QUANTUM)

scanf(damptq)

while(1)

for(i=0count=0iltni++)

temp=tq

if(st[i]==0)

count++

continue

if(st[i]gttq)

st[i]=st[i]-tq

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 35

else

if(st[i]gt=0)

temp=st[i]

st[i]=0

sq=sq+temp

tat[i]=sq

if(count==n)

break

for(i=0iltni++)

wt[i]=tat[i]-bt[i]

stat=stat+tat[i]

swt=swt+wt[i]

awt=(float)swtn

atat=(float)statn

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 36

printf(n PROCESS BURST TIME WAIT TURN n)

for(i=0iltni++)

printf(ntPdtd t d tdn

pro[i]bt[i]wt[i]tat[i])

printf(n AVERAGE WAITING TIME = f tuawt)

printf(n AVERAGE TURN AROUND TIME = f tuatat)

getch()

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 37

Round Robin CPU Scheduling Output

ENTER THE NO OF PROCESSES3

ENTER THE SERVICE TIME OF PROCESS 030

ENTER THE SERVICE TIME OF PROCESS 140

ENTER THE SERVICE TIME OF PROCESS 220

ENTER THE TIME QUANTUM10

PROCESS BURST TIME WAIT TURN

P0 30 40 70

P1 40 50 90

P2 20 40 60

AVERAGE WAITING TIME = 43333332 tu

AVERAGE TURN AROUND TIME = 73333336 tu

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 38

ENTER THE NO OF PROCESSES4

ENTER THE SERVICE TIME OF PROCESS 012

ENTER THE SERVICE TIME OF PROCESS 120

ENTER THE SERVICE TIME OF PROCESS 29

ENTER THE SERVICE TIME OF PROCESS 35

ENTER THE TIME QUANTUM5

PROCESS BURST TIME WAIT TURN

P0 12 24 36

P1 20 26 46

P2 9 25 34

P3 5 15 20

AVERAGE WAITING TIME = 22500000 tu

AVERAGE TURN AROUND TIME = 34000000 tu

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 39

ENTER THE NO OF PROCESSES5

ENTER THE SERVICE TIME OF PROCESS 022

ENTER THE SERVICE TIME OF PROCESS 111

ENTER THE SERVICE TIME OF PROCESS 234

ENTER THE SERVICE TIME OF PROCESS 32

ENTER THE SERVICE TIME OF PROCESS 412

ENTER THE TIME QUANTUM6

PROCESS BURST TIME WAIT TURN

P0 22 43 65

P1 11 26 37

P2 34 47 81

P3 2 18 20

P4 12 37 49

AVERAGE WAITING TIME = 34200001 tu

AVERAGE TURN AROUND TIME = 50400002 tu

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 40

PROGRAM 05

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquoBANKERS ALGORITHMlsquo

Concept This approach to the deadlock problem anticipates deadlock before it actually occurs

This approach employs an algorithm to access the possibility that deadlock could occur and acting accordingly This method differs from deadlock prevention which guarantees that deadlock cannot occur by denying one of the necessary conditions of deadlock

If the necessary conditions for a deadlock are in place it is still possible to avoid deadlock by being careful when resources are allocated Perhaps the most famous deadlock avoidance algorithm due to Dijkstra is the Bankerrsquos algorithm So named because the process is analogous to that used by a banker in deciding if a loan can be safely made

The Bankers algorithm considers each request as it occurs and see if granting it leads to a safe state If it does the request is granted otherwise it postponed until later

SOURCE CODE

includeltstdiohgt

includeltconiohgt

void main()

int clm[7][5]req[7][5]alloc[7][5]rsrc[5]avail[5]comp[7]

int firstprijprccountt

clrscr()

count=0

for(i=1ilt=7i++)

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 41

comp[i]=0

printf(Enter the no of processes )

scanf(dampp)

printf(Enter the no of resource types )

scanf(dampr)

printf(Enter the claim for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

scanf(dampclm[i][j])

printf(Enter the allocation for each process n)

for(i=1ilt=pi++)

printf(For process d i)

for(j=1jlt=rj++)

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 42

scanf(dampalloc[i][j])

printf(Enter total no of each resource )

for(j=1jlt=rj++)

scanf(damprsrc[j])

for(j=1jlt=rj++)

int total=0

avail[j]=0

for(i=1ilt=pi++)

total+=alloc[i][j]

avail[j]=rsrc[j]-total

do

for(i=1ilt=pi++)

for(j=1jlt=rj++)

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 43

req[i][j]=clm[i][j]-alloc[i][j]

printf(nClaim matrixtAllocation matrixtRequest

matrixn)

for(i=1ilt=pi++)

printf(n)

for(j=1jlt=rj++)

printf(2dclm[i][j])

printf(tt)

for(j=1jlt=rj++)

printf(2dalloc[i][j])

printf(ttt)

for(j=1jlt=rj++)

printf(2dreq[i][j])

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 44

printf(nAvailable resources are )

for(j=1jlt=rj++)

printf(d tavail[j])

prc=0

for(i=1ilt=pi++)

if(comp[i]==0) if not completed

prc=i

for(j=1jlt=rj++)

if(avail[j]==0|| req[i][j]gtavail[j])

prc=0

break

if(prc=0)

break

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 45

if(prc=0)

printf(nProcess d runs to completionprc)

count++

for(j=1jlt=rj++)

avail[j]+=alloc[prc][j]

alloc[prc][j]=0

clm[prc][j]=0

comp[prc]=1

while(count=p+1 ampamp prc=0)

if(count==p)

printf(nThe system is in a safe state)

else

printf(nThe system is in an unsafe state)

getch()

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 46

Banker Algorithm Output

Enter the no of processes 3

Enter the no of resource types 4

Enter the claim for each process

For process 1 2

1

3

4

For process 2 5

6

7

2

For process 3 3

4

5

6

Enter the allocation for each process

For process 1 2

3

4

5

For process 2 6

1

2

3

For process 3 4

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 47

2

3

4

Enter total no of each resource 1

8

9

4

Claim matrix Allocation matrix Request matrix

2 1 3 4 2 3 4 5 0-2-1-1

5 6 7 2 6 1 2 3 -1 5 5-1

3 4 5 6 4 2 3 4 -1 2 2 2

Available resources are -11 2 0 -8

The system is in an unsafe state

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 48

Enter the no of processes 3

Enter the no of resource types 2

Enter the claim for each process

For process 1 2

3

For process 2 1

2

For process 3 2

3

Enter the allocation for each process

For process 1 1

1

For process 2 1

1

For process 3 0

1

Enter total no of each resource 6

7

Claim matrix Allocation matrix Request matrix

2 3 1 1 1 2

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 4 4

Process 1 runs to completion

Claim matrix Allocation matrix Request matrix

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 49

0 0 0 0 0 0

1 2 1 1 0 1

2 3 0 1 2 2

Available resources are 5 5

Process 2 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

2 3 0 1 2 2

Available resources are 6 6

Process 3 runs to completion

Claim matrix Allocation matrix Request matrix

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

Available resources are 6 7

The system is in a safe state

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 50

PROGRAM 06

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo FIRST IN FIRST OUT PAGE REPLACEMENT rsquo ALGORITHM

Concept The simplest page-replacement algorithm is a FIFO algorithm The first-in first-out

(FIFO) page replacement algorithm is a low-overhead algorithm that requires little book-keeping on

the part of the operating system The idea is obvious from the name ndash the operating system keeps

track of all the pages in memory in a queue with the most recent arrival at the back and the oldest

arrival in front When a page needs to be replaced the page at the front of the queue (the oldest

page) is selected While FIFO is cheap and intuitive it performs poorly in practical application Thus

it is rarely used in its unmodified form

Source code

includeltconiohgt

includeltstdiohgt

void main()

int ijna[50]frame[10]nokavailcount=0

clrscr()

printf(n ENTER THE NO OF PAGES )

scanf(dampn)

printf(n ENTER THE PAGE Reference String )

for(i=1ilt=ni++)

scanf(dampa[i])

printf(n ENTER THE NO OF FRAMES )

scanf(dampno)

for(i=0iltnoi++)

frame[i]=-1

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 51

j=0

printf(n REF STRING t PAGE FRAMESn)

for(i=1ilt=ni++)

printf(2dtta[i])

avail=0

for(k=0kltnok++)

if(frame[k]==a[i])

avail=1

if(avail==0)

frame[j]=a[i]

j=(j+1)no

count++

for(k=0kltnok++)

printf(dtframe[k])

printf(n)

printf(n NO OF PAGE FAULTS dcount)

getch()

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 52

FIFO Page Replacement Algorithm Output

ENTER THE NO OF PAGES 5

ENTER THE PAGE Reference String 1

2

3

4

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

2

NO OF PAGE FAULTS 4

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 53

ENTER THE NO OF PAGES 7

ENTER THE PAGE Reference String 1

2

3

1

2

3

4

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

1 1 -1 -1

2 1 2 -1

3 1 2 3

1

2

3

4 4 2 3

NO OF PAGE FAULTS 4

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 54

ENTER THE NO OF PAGES 6

ENTER THE PAGE Reference String 2

2

2

2

2

2

ENTER THE NO OF FRAMES 3

REF STRING PAGE FRAMES

2 2 -1 -1

2

2

2

2

2

NO OF PAGE FAULTS 1

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 55

PROGRAM 07

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

lsquo LEAST RECENTLY USED PAGE REPLACEMENT rsquo ALGORITHM

Concept The Least Recently Used replacement policy chooses to replace the page which has not

been referenced for the longest time This policy assumes the recent past will approximate the

immediate future The operating system keeps track of when each page was referenced by recording

the time of reference or by maintaining a stack of references

Source code

includeltstdiohgt

void main()

int q[20]p[50]c=0c1dfijk=0nrtb[20]c2[20]

printf(Enter no of pages)

scanf(dampn)

printf(Enter the reference string)

for(i=0iltni++)

scanf(dampp[i])

printf(Enter no of frames)

scanf(dampf)

q[k]=p[k]

printf(ntdnq[k])

c++

k++

for(i=1iltni++)

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 56

c1=0

for(j=0jltfj++)

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

c1++

if(c1==f)

c++

if(kltf)

q[k]=p[i]

k++

for(j=0jltkj++)

printf(tdq[j])

printf(n)

else

for(r=0rltfr++)

c2[r]=0

for(j=i-1jltnj--)

if(q[r]=p[j])

c2[r]++

else

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 57

break

for(r=0rltfr++)

b[r]=c2[r]

for(r=0rltfr++)

for(j=rjltfj++)

if(b[r]ltb[j])

t=b[r]

b[r]=b[j]

b[j]=t

for(r=0rltfr++)

if(c2[r]==b[0])

q[r]=p[i]

printf(tdq[r])

printf(n)

printf(nThe no of page faults is dc)

getch()

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 58

LRU Page Replacement Algorithm Output

Enter no of pages4

Enter the reference string2

3

1

2

Enter no of frames2

2

2 3

1 3

1 2

The no of page faults is 4

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 59

Enter no of pages7

Enter the reference string1

2

3

2

4

2

5

Enter no of frames3

1

1 3

1 3 2

4 3 2

4 5 2

The no of page faults is 5

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 60

Enter no of pages8

Enter the reference string1

2

1

2

3

1

2

3

Enter no of frames2

1

1 2

3 2

3 1

2 1

2 3

The no of page faults is 6

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 61

PROGRAM 8

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR

IMPLEMENTINGFIFO(FIRST IN FIRST OUT) DISK SCHEDULING

Concept All incoming requests are placed at the end of the queue Whatever number that is

next in the queue will be the next number served This algorithm doesnt provide the best results To

determine the number of head movements we would simply find the number of tracks it took to

move from one request to the next

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt

void main()

int a[20]nit=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of disk request)

scanf(dampn)

printf(nEnter request in order)

for(i=1ilt=ni++)

scanf(dampa[i])

if(a[i]lt0)

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 62

printf(nINVALID INPUT)

getch()

exit(0)

for(i=0iltni++)

if(a[i]lta[i+1])

t+=(a[i+1]-a[i])

else

t+=(a[i]-a[i+1])

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tda[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 63

FCFS Disk Scheduling Algorithm Output

Enter head pointer position500

Enter number of disk request4

Enter request in order100

200

400

300

Processing order 500 100 200 400 300

Total Head Movement800

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 64

Enter head pointer position200

Enter number of disk request6

Enter request in order300

100

400

200

250

550

Processing order 200 300 100 400 200 250 550

Total Head Movement1150

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 65

Enter head pointer position200

Enter number of disk request4

Enter request in order200

100

-300

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 66

PROGRAM 9

STATEMENT OF THE PROBLEM TO WRITE A C PROGRAM FOR IMPLEMENTING

SSTF(SHORTEST SEEK TIME FIRST) DISK SCHEDULING

Concept In this case request is serviced according to next shortest distance

from the header position

SOURCE CODE

includeltstdiohgt

includeltconiohgt

includeltstdlibhgt0

void main()

int a[20]b[20]dnijtempsk=0x=0t=0

clrscr()

printf(Enter head pointer position)

scanf(dampa[0])

printf(nEnter number of processes)

scanf(dampn)

printf(nEnter processes in request order)

for(i=1ilt=ni++)

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 67

scanf(dampa[i])

if(a[i]lt0)

printf(nINVALID INPUT)

getch()

exit(0)

b[k++]=a[0]

for(i=0iltni++)

s=1000

for(j=i+1jlt=nj++)

if(a[i]gta[j])

d=a[i]-a[j]

else

d=a[j]-a[i]

if(dlts)

s=d

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 68

x=j

t+=s

temp=a[i+1]

a[i+1]=a[x]

a[x]=temp

b[k++]=a[i+1]

printf(nProcessing order)

for(i=0ilt=ni++)

printf(tdb[i])

printf(nTotal Head Movementdt)

getch()

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 69

SSTF Disk Scheduling Algorithm Output

Enter head pointer position100

Enter number of processes4

Enter processes in request order200

100

500

200

Processing order 100 100 200 200 500

Total Head Movement400

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 70

Enter head pointer position500

Enter number of processes6

Enter processes in request order400

300

200

600

700

600

Processing order 500 400 300 200 600 600 700

Total Head Movement800

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT

NCS-451JP 71

Enter head pointer position700

Enter number of processes7

Enter processes in request order500

600

300

-700

INVALID INPUT