Os Lab Manual Final Os-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 88

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING
(JUNE 2015-NOV 2015)

OPERATING SYSTEMS LABORATORY


V SEMESTER

P a g e | 1 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SUBJECT: OPERATING SYSTEMS LABORATORY

SUBJECT CODE: CS P53

Prepared By :
T. AHILANDESWARI, AP/CSE
V.GOMATHI, AP/CSE
P.SUBHAPRIYA, AP/CSE

Verified by:

Approved by:

LIST OF EXPRIMENTS

1. Study of basic UNIX/Linux commands.


2. Shell Programming.
3. Programs using the following system calls of UNIX/Linux operating system:
i. fork, exec, getpid, exit, wait, close, stat, opendir, readdir.
4.

Programs using the I/O system calls of UNIX operating system:


i. open, read, write, etc).

5.
6.
7.
8.
9.
10.
11.
12.

Simulations of UNIX/Linux commands like ls, grep, etc.


Simulation of processes scheduling algorithms.
Simulation of synchronization problems using Semaphore.
Simulation of basic memory management schemes.
Simulation of virtual memory management schemes.
Simulation of disk scheduling algorithms
Simulation of file systems.
Develop an application using any RTOS.

P a g e | 2 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

LIST OF EXPERIMENTS
S.NO
1.
2.a.
2.b.
2.c.
2.d.
2.e.
3.a.
3.b.
3.c.
3.d.
3.e.
4.a.
4.b.
5.a.
5.b.
5.c.
5.d.
5.e.
5.f.
6.a.
6.b.
6.c.
7.a.
7.b.
7.c.
8.a.
8.b.
8.c.
9.a.
9.b.
9.c.
9.d.

TITLE OF EXPERIMENTS
STUDY OF OS LINUX COMMANDS
2. SHELL PROGRAMMING
BIGGEST OF TWO NUMBERS
LEAP YEAR OR NOT
EVEN OR ODD NUMBER
SUM OF DIGITS
FIBONACCI SERIES
3 . SYSTEM CALLS
CREATE A CHILD PROCESS USING FORK
FORK AND WAIT SYSTEM CALL
CREATE A CHILD PROCESS USING SLEEP
DIRECTORY SYSTEM CALL
FILE SYSTEM CALL USING OPEN AND CLOSE
4 . SIMULATION OF LINUX COMMANDS
SIMULATION OF GREP
SIMULATION OF ls COMMAND
5 . PROCESSES SCHEDULING
IMPLEMENTATION OF FCFS SCHEDULING
IMPLEMENTATION OF SJF SCHEDULING ZERO ARRIVALTIME
IMPLEMENTATION OF SJF SCHEDULING NON ZERO ARRIVALTIME
IMPLEMENTATION OF PRIORITY SCHEDULING ZERO ARRIVALTIME
IMPLEMENTATION OF PRIORITY SCHEDULING NON ZERO ARRIVALTIME
IMPLEMENTATION OF RR SCHEDULING
6 . SEMAPHORE
PRODUCER AND CONSUMER PROBLEM
DINING PHILOSOPHER'S PROBLEM
READERS WRITER PROBLEM USING SEMAPHORE
7 . MEMORY MANAGEMENT
IMPLEMENTATION OF FIRST FIT
IMPLEMENTATION OF BEST FIT ALGORITHM
IMPLEMENTATION OF WORST FIT
8 . PAGE REPLACEMENT
FIFO PAGE REPLACEMENT ALGORITHM
LRU PAGE REPLACEMENT ALGORITHM
OPTIMAL PAGE REPLACEMENT ALGORITHM
9 . DISK SCHEDULING
FCFS
SSTF
SCAN
C-SCAN

P a g e | 3 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.1
DATE:

STUDY OF OS LINUX COMMANDS

AIM:
To study the basic commands in Linux.
COMMANDS:
1. Calendar
NAME
(i) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: calendar
: cal
: Displays a simple calendar. If arguments are not
Specified, the current month is displayed.
: cal
:

Su
1
8
15
22
29

(ii) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

Su Mo
1 2
8 9
15 16
22 23
29 30

Su Mo
1 2
8 9
15 16
22 23
29 30

January
Tu We Th
3 4 5
10 11 12
17 18 19
24 25 26
31

April
Tu We Th
3 4 5
10 11 12
17 18 19
24 25 26

Fr
6
13
20
27

Fr
6
13
20
27

Mo
2
9
16
23
30

June 2014
Tu We Th
3 4
5
10 11 12
17 18 19
24 25 26

Sa
7
14
21
28

: cal year
: Displays calendar of that year
: cal 2012
:

Sa Su Mo
7
14 5 6
21 12 13
28 19 20
26 27

Sa
7
14
21
28

Fr
6
13
20
27

February
Tu We Th
1
2
7 8
9
14 15 16
21 22 23
28 29

Su Mo Tu
1
6 7 8
13 14 15
20 21 22
27 28 29

May
We Th
2 3
9 10
16 17
23 24
30 31

Fr
3
10
17
24

Fr
4
11
18
25

P a g e | 4 OPERATING SYSTEMS LABORATORY

Sa
4
11
18
25

March
Su Mo Tu We Th
1
4 5 6 7 8
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29

Fr
2
9
16
23
30

Sa
3
10
17
24
31

Sa
5
12
19
26

June
Su Mo Tu We Th Fr
1
3 4
5 6 7 8
10 11 12 13 14 15
17 18 19 20 21 22
24 25 26 27 28 29

Sa
2
9
16
23
30

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

Su Mo
1 2
8 9
15 16
22 23
29 30

Tu
3
10
17
24
31

Su Mo
1
7 8
14 15
21 22
28 29

Tu
2
9
16
23
30

July
We
4
11
18
25

Th
5
12
19
26

Fr
6
13
20
27

Sa
7
14
21
28

August
Su Mo Tu We Th
1 2
5 6 7 8 9
12 13 14 15 16
19 20 21 22 23
26 27 28 29 30

Fr
3
10
17
24
31

Sa
4
11
18
25

October
We Th
3 4
10 11
17 18
24 25
31

Fr
5
12
19
26

Sa
6
13
20
27

November
Su Mo Tu We Th
1
4 5
6 7 8
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29

Fr
2
9
16
23
30

Sa
3
10
17
24

September
Su Mo Tu We Th Fr Sa
1
2 3
4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
December
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

(iii) SYNTAX : cal -3


DESCRIPTION : Displays calendar of previous, current, next months of current
year
OUTPUT
:

Su Mo
1 2
8 9
15 16
22 23
29 30

July
Tu We
3 4
10 11
17 18
24 25
31

Th
5
12
19
26

Fr
6
13
20
27

Sa
7
14
21
28

(iv)SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

Su Mo
5
12
19
26

6
13
20
27

August
Tu We Th
1 2
7 8 9
14 15 16
21 22 23
28 29 30

Fr
3
10
17
24
31

Sa
4
11
18
25

September
Su Mo Tu We Th Fr Sa
1
2 3
4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

: cal month year


: Displays the calendar for corresponding month and year .
: cal 4 2012
:
April 2012
Su Mo Tu We Th Fr Sa
1 2
3 4
5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

2. Date
NAME
(i) SYNTAX
DESCRIPTION
OUTPUT

: DATE- print or set the system date and time


: date
: Display the current time in the given format or set the system date.
: Mon Jul 23 12:17:50 IST 2012

P a g e | 5 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

(ii) SYNTAX
DESCRIPTION
OUTPUT

: date +% H
: Display the current hour.
: 12

(iii) SYNTAX
DESCRIPTION
OUTPUT

: date +% h
: Display the current month name.
: Jul

(iv) SYNTAX
DESCRIPTION
OUTPUT

: date +% m
: Display the current month number.
:7

(v) SYNTAX
DESCRIPTION
OUTPUT

: date +% a
: Display the abbreviated weekday name.
: Mon

(vi) SYNTAX
DESCRIPTION
OUTPUT

: date +% y
: Display the current year.
: 12

(vii) SYNTAX
DESCRIPTION
OUTPUT

: date +% S
: Display the current second.
: 57

3. Script
NAME
: SCRIPT makes typescript of terminal session
DESCRIPTION : Makes a typescript of everything printed on your terminal. It is useful
for students who need a hardcopy record of an interactive session as
proof of an assignment, as the typescript file can be printed out later
with lpr(1).
SYNTAX
: script scriptname
..
..
..
Exit
OPENING A SCRIPT:
SYNTAX
: vi scriptname
EXAMPLE
: vi date.txt
OUTPUT
: ~date
~ Mon Jul 23 12:17:50 IST 2012
~
~
INSERT
P a g e | 6 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

4. ls
NAME
(i) SYNTAX
DESCRIPTION
OUTPUT

: LIST list directory contents


: ls
: List information about the Files (the current directory by default).
:
greatest.sh cse.txt
mouse.txt
digit.sh
emp.sh
num.sh
case.sh

(ii) SYNTAX
: ls l
DESCRIPTION : Displays files in long listing format.
OUTPUT
:
csea08
csea08
1384962
Jul 23 12:17 [23.07.2012]
csea08
csea08
20325
Jul 23 12:30 [23.07.12]
csea08
csea08
138
Jul 23 12:33 [01;32case.sh]
csea08
csea08
13830
Jul 23 12:37 [01;34cse]
csea08
csea08
183
Jul 23 12:40 [01;32mdigit.sh]
csea08
csea08
530
Jul 23 12:50 [01;32memp.sh]
csea08
csea08
730
Jul 23 12:59 [01;32mgreatest.sh]
(iii) SYNTAX
: ls -r
DESCRIPTION : Displays the files in reverse sorted order.
OUTPUT
:
num.sh
mouse.txt
greatest.sh
digit.sh
cse.txt
case.sh

emp.sh

(iv) SYNTAX
: ls -s
DESCRIPTION : Displays the size of each files.
OUTPUT
:
8 num.sh
4 mouse.txt 8 greatest.sh 8 emp.sh
8 digit.sh
4 cse.txt
8 case.sh
(v) SYNTAX
: ls -S
DESCRIPTION : Displays the files in sorted order.
OUTPUT
:
case.sh
cse.txt
digit.sh
greatest.sh mouse.txt
num.sh
5. cp
NAME
SYNTAX
DESCRIPTION
EXAMPLE

emp.sh

: cp copy files and directories


: cp fi f2
: Copies f1 to f2
: cp cse.txt new.txt

6. rm
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: rm remove files
: rm filename
: This command removes each specified file.
: rm cse.txt

P a g e | 7 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

7. mv
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: mv move(rename) files
: mv f1 f2
: Renames Source to Destination
: mv new.txt cse.txt

8. mkdir
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: mkdir makes directory


: mkdir DirectoryName
: Creates the directory, if they do not exist already
: mkdir new

9. rmdir
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: rmdir removes directory


: rmdir DirectoryName
: Removes the directory, only it is empty.
: rm cse.txt

10. Pwd
: pwd Present Working Directory displays the name of the
current/working directory
SYNTAX
: pwd
DESCRIPTION : Displays the name of the current/working directory
OUTPUT
: \home\csea08\new
NAME

11. Cd
NAME
: cd Change Directory
(i) SYNTAX
: cd dirname
DESCRIPTION : Change the directory which we use to work with.
EXAMPLE

: cd New

(ii) SYNTAX
: cd ..
DESCRIPTION : Quits from the current directory.
(iii) SYNTAX
: cd\
DESCRIPTION : Returns to the home directory.
12. Cat
NAME
(i) SYNTAX
DESCRIPTION
EXAMPLE

: cat- concatenate & open files and print on the standard output
: cat > filename
: This command is used to open a new file.
: cat > a.txt

P a g e | 8 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT

:
NAME : ZZZZ
ROLL NO: XX

(ii) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: cat filename
: To view the contents of the file.
: cat a.txt
:
NAME : ZZZZ
ROLL NO: XX

(iii) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: cat f1 f2 > f3
: To concatenate f1 and f2 save in f3
: cat a.txt b.txt > c.txt
:
a.txt=>
NAME : ZZZZ
ROLL NO: XX
b.txt=>
COLLEGE.SMVEC
c.txt=>
NAME : ZZZZ
ROLL NO: XX
COLLEGE.SMVEC

(iv) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: cat n filename
: To display the contents of the file along with the line numbers.
: cat -n sample.txt
: 1 hai
2 how are u?

(v) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: cat f1 >> f2
: To redirect the data from one file to another.
: cat sample.txt new.txt
: cat new.txt
sample. txt=>
hai
how are u?
new. txt=>
hai
how are u?

13. Whoami
NAME
SYNTAX

: Displays the current user login and identity.


: whoami

P a g e | 9 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT

: csea08

14. Man
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: man help command


: man command
: Displays the description of a command
: man rm

15. Head
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: head
: head filename
: Displays the first ten lines in the file.
: head fruits.txt
: apple
banana
cherry
jack fruit
strawberry
orange
pineapple
mango
grape
papaya

16. Tail
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: tail
: tail filename
: Displays the last ten lines in the file.
: tail fruits.txt
: apple
banana
cherry
jack fruit
strawberry
orange
pineapple
mango
grape
papaya

17. Clear
NAME
: clear
SYNTAX
: clear
DESCRIPTION : Clears the content of the command prompt.

P a g e | 10 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

18. Sort
NAME
(i) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: sort
: sort filename
: Sorts the content of the file in ascending order.
: sort names.txt
:
Arun
Balu
Chandra
David
Edwards

(ii) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: sort r filename
: Sorts the content of the file in descending order.
: sort r names.txt
:
Edwards
David
Chandra
Balu
Arun

19. Who
NAME
SYNTAX
DESCRIPTION
OUTPUT

: who
: who
: Displays all the users currently logged it.
:
csea13 pts/1
Jul 23 10:08 (172.17.22.38)
csea06 pts/10
Jul 23 10:13 (172.17.21.11)
csea12 pts/12
Jul 23 10:13 (172.17.21.35)
csea18 pts/11
Jul 23 10:13 (172.17.20.9)
csea24 pts/8
Jul 23 10:14 (172.17.22.33)
csea05 pts/13
Jul 23 10:15 (172.17.21.36)
root :0
Jul 23 10:20
csea20 pts/16
Jul 23 10:22 (172.17.21.43)
csea11 pts/20
Jul 23 10:28 (172.17.21.27)
csea23 pts/5
Jul 23 10:37 (172.17.22.14)
csea03 pts/0
Jul 23 10:37 (172.17.222.37)
csea01 pts/4
Jul 23 11:00 (172.17.22.13)
csea21 pts/15
Jul 23 11:00 (172.17.21.34)
csea07 pts/18
Jul 23 11:00 (172.17.20.28)
csea08 pts/14
Jul 23 11:01 (172.17.22.16)
csea16 pts/7
Jul 23 11:01 (172.17.21.24)
csea02 pts/19
Jul 23 11:01 (172.17.21.45)
staff pts/26
Jul 23 12:20 (172.17.21.21)

20. Finger
NAME
: finger
SYNTAX
: finger
DESCRIPTION : Displays the detailed information about the system users.
P a g e | 11 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT

:
Login Name
Tty Idle Login Time Office Office Phone
csea01
pts/4
2 Jul 23 11:00 (172.17.22.13)
csea02 csea02 pts/19
2 Jul 23 11:01 (172.17.21.45)
csea03
pts/0
Jul 23 10:37 (172.17.222.37)
csea05
pts/13
Jul 23 10:15 (172.17.21.36)
csea06
pts/10
Jul 23 10:13 (172.17.21.11)
csea07
pts/18
Jul 23 11:00 (172.17.20.28)
csea08
pts/14
Jul 23 11:01 (172.17.22.16)
csea09
pts/3
Jul 23 12:13 (172.17.22.115)
csea10
pts/2
1 Jul 23 12:08 (172.17.22.26)
csea11
pts/20
1 Jul 23 10:28 (172.17.21.27)
csea12
pts/12
1 Jul 23 10:13 (172.17.21.35)
csea13
pts/1
1 Jul 23 10:08 (172.17.22.38)
csea14
pts/21
Jul 23 11:01 (172.17.22.29)
csea15
pts/9
Jul 23 11:25 (172.17.22.130)
root
root
*:0
Jul 23 10:20
staff
pts/26
1 Jul 23 12:20 (172.17.21.21)

21. Last
NAME
SYNTAX
DESCRIPTION
OUTPUT

: last
: last
: Displays the list of last logged-in users for a month.
:

staff pts/26
172.17.21.21 Mon Jul 23 12:20 still logged in
csea09 pts/3
172.17.22.115 Mon Jul 23 12:13 still logged in
csea04 pts/23
172.17.22.60 Mon Jul 23 12:12 - 12:18 (00:05)
csea23 pts/25
172.17.22.60 Mon Jul 23 12:11 - 12:11 (00:00)
csea10 pts/2
172.17.22.26 Mon Jul 23 12:08 still logged in
csea04 pts/23
172.17.22.60 Mon Jul 23 11:51 - 11:52 (00:00)
csea08 pts/23
172.17.22.60 Mon Jul 23 11:50 - 11:51 (00:01)
csea04 pts/23
172.17.22.60 Mon Jul 23 11:48 - 11:50 (00:01)
csea06 pts/23
172.17.22.60 Mon Jul 23 11:46 - 11:47 (00:01)
csea23 pts/6
172.17.22.14 Mon Jul 23 11:45 still logged in
csea09 pts/3
172.17.22.14 Mon Jul 23 11:44 - 11:45 (00:01)
csea09 pts/6
172.17.22.115 Mon Jul 23 11:38 - 11:42 (00:04)
reboot system boot 2.4.20-8smp
Mon Jul 23 08:57
(03:24)
wtmp begins Mon Jul 2 10:08:30 2012
22. And
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: and - &&
: cmd1 && cmd2
: Used to combine more than one commands.
: whoami && date
: csea23
Mon Jul 23 12:22:54 IST 2012

P a g e | 12 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

23. Or
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT
24. . Alias
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT
25. Edit
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: or - ||
: cmd1 || cmd2
: Displays the output for one command which is true.
: whoami || date
: csea23
: alias
: alias name="value"
: To create simple names or abbreviations for commands
: alias p="pwd"
p
: /home/mca1

: edit
: vi filename
: Edits the content of the file. To edit press I and to save press esc:wq
: vi names.txt

26. cut
NAME
: cut
(i) SYNTAX
: cut OPTION -m [FILE]...
DESCRIPTION
: extracts m characters from the beginning of each line from the specified file.
EXAMPLE
: cut c -3 test.txt
OUTPUT
:
BEFORE EXECUTION : cat > test.txt
Smvec
manakula
vinayagar
AFTER EXECUTION : smv
man
vin

27. Touch
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: touch
: touch filename
: Creates an empty file.
: touch hello.txt
:

BEFORE EXECUTION : ls
Sample.txt
AFTER EXECUTION

welcome.txt

: ls
Sample.txt

welcome.txt

P a g e | 13 OPERATING SYSTEMS LABORATORY

hello.txt
DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

28. Uniq
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: uniq
: uniq option filename1 filename2
: Discard all but one of successive identical lines from filename1 to filename2
: uniq d a.txt b.txt

OUTPUT

BEFORE EXECUTION

AFTER EXECUTION

: cat > a.txt


smvec
smvec
manakula
vinayagar
: cat b.txt
smvec

29. Semicolon
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: Semicolon (;)
: cmd1 ; cmd2; cmd3
: Similar to and command which combines more than one command.
: whoami && date
: csea23
Mon Jul 23 12:22:54 IST 2012

30. Echo
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: echo displays a line of text.


: echo .
: Displays the statement within double quotes.
: echo hai
: hai

31. Word Count


NAME
(i) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: wc word count
: wc filename
: Displays the number of lines, words and characters in files.
: wc a.txt
: a.txt=>
hai
how are u
2 lines, 4 words, 12 characters.

(ii) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: wc -l filename
: Displays the number of lines in files.
: wc l a.txt
: a.txt=>
hai
how are u

P a g e | 14 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

2 lines
(iii) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: wc -m filename
: Displays the number of characters in files.
: wc m a.txt
: a.txt=>
hai
how are u
12 characters

(iv) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: wc -w filename
: Displays the number of words in files.
: wc w a.txt
: a.txt=>
hai
how are u
4 words

32. Grep
NAME
(i) SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: grep
: grep pattern filename
: To search for a regular expression or a pattern in a file
: grep apple b.txt
: b.txt=>
This is an apple
This is a fruit
apple

(ii) SYNTAX
: grep c pattern filename
DESCRIPTION : To search for a regular expression or a pattern in a file and displays
how many times that pattern is repeated in the file.
EXAMPLE
: grep c apple b.txt
OUTPUT
: b.txt=>
This is an apple
This is a fruit
1
(iii) SYNTAX
: grep n pattern filename
DESCRIPTION : To search for a regular expression or a pattern in a file and displays
the searched content along with the line and line number, if found.
EXAMPLE
: grep n apple b.txt
OUTPUT
: b.txt=>
This is an apple
This is a fruit
1: apple

P a g e | 15 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

(iv) SYNTAX
: grep i pattern filename
DESCRIPTION : To search for a regular expression or a pattern in a file irrespective of
the case.
EXAMPLE
: grep n APPLE a.txt
OUTPUT
: b.txt=>
This is an apple
This is a fruit
apple
33. Read
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

34. Fgrep
NAME
(i)SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: read reads a value(s)


: read identifier
: Reads a value(s)
: read a
: 10
read a

: fgrep
: fgrep pattern f1 f2
: To search for a regular expression or a pattern in two files
: fgrep hai a.txt d.txt
: a.txt=>
hai
how are u
d.txt=>
this is a file
the type of the file is text
a.txt: hai

(ii) SYNTAX
: fgrep c pattern f1 f2
DESCRIPTION : To search for a regular expression or a pattern in two files and
displays how many times that pattern is repeated in the files.
EXAMPLE
: fgrep c file a.txt d.txt
OUTPUT
: a.txt=>
hai
how are u
d.txt=>
this is a file
the type of the file is text
a.txt : 0
d.txt : 2
(iii) SYNTAX
: fgrep n pattern f1 f2
DESCRIPTION : To search for a regular expression or a pattern in two files and
P a g e | 16 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EXAMPLE
OUTPUT

displays the searched content along with the line and line number, if
found.
: fgrep c file a.txt d.txt
: a.txt=>
hai
how are u
d.txt=>
this is a file
the type of the file is text
d.txt : 1 : this a file
d.txt : 2 : the type of the file is text

(iv) SYNTAX
: fgrep i pattern f1 f2
DESCRIPTION : To search for a regular expression or a pattern in two files
irrespective of the case.
EXAMPLE
: fgrep c HaI a.txt d.txt
OUTPUT
: a.txt=>
hai
how are u
d.txt=>
this is a file
the type of the file is text
a.txt : hai
35. Pipe
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: Pipe - |
: cmd1 | cmd2 | cmd3
: Makes the output of one command as input for another command.
: date | wc -w
:6

36. Tee
NAME
: Tee
SYNTAX
: cmd1 | tee filename
DESCRIPTION : Used to read the standard input and then write to standard output or
file.
EXAMPLE
: date | tee f.txt | wc -w
OUTPUT
:6

37. Write
NAME
SYNTAX
DESCRIPTION
EXAMPLE

: write
: write login_name
: Used to communicate with other logged in users.
: write csea25

P a g e | 17 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

38. Mail
NAME
(i) SYNTAX
DESCRIPTION
EXAMPLE

: Mail
: mail login_name
: Used to send mail to a user.
: write csea25

(ii) SYNTAX
: mail
DESCRIPTION : Used to view the mails in the mailbox.
39. Terminal Name
NAME
SYNTAX
DESCRIPTION
OUTPUT

: tty
: tty
: Used to display the terminal path name.
: /dev/pts/23

40. Expression
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: expr
: `expr expression`
: Used to evaluate an expression
: echo `expr 10 + 10`
: 20

41. . free
NAME
SYNTAX
DESCRIPTION

: free
: free
: Displays the total amount of free and used physical and swap memory in the
system
EXAMPLE
: free
OUTPUT
:
[mca1@redhat ~]$ free
total
used
free
shared buffers cached
Mem:
1016820 647688 369132
0
59916
414316
-/+ buffers/cache: 173456 843364
Swap:
2064380
0 2064380
42. Find
ping
NAME
(i) SYNTAX
DESCRIPTION

: ping
: ping Ip-address
: Ping is used diagnostically to ensure that a host computer the user is trying to
reach is actually operating.
EXAMPLE
: ping 172.17.10.111
OUTPUT
:
[mca1@redhat ~]$ ping 172.17.10.111
PING 172.17.10.111 (172.17.10.111) 56(84) bytes of data.
P a g e | 18 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

64 bytes from 172.17.10.111: icmp_req=1 ttl=64 time=0.032 ms


64 bytes from 172.17.10.111: icmp_req=2 ttl=64 time=0.017 ms
64 bytes from 172.17.10.111: icmp_req=3 ttl=64 time=0.013 ms
^Z
[1]+ Stopped
ping 172.17.10.111

43. Password
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: Password
: passwd
: Used to change the password.
: passwd
: Changing password for user csea09.
Changing password for csea09
(current) UNIX password:
New password:

44.Exit
NAME
: Logout
SYNTAX
: logout
DESCRIPTION : Used to logout.
45.Uname
NAME
SYNTAX
DESCRIPTION
OUTPUT

: Uname
: uname
: Used to display the name of the system being used.
: Linux

46. Sequence
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT
1
2
3
4

: seq
: seq starting_value ending_value
: print a sequence of numbers
: seq 1 4
:

47. Open and Fill


NAME
SYNTAX

: Open and Fill


: vi +filename

DESCRIPTION : Used to open a file and position the cursor at the first line.
EXAMPLE
: vi +b.txt
OUTPUT
:
P a g e | 19 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

hi bala
:wq
"new.txt" 7L, 42C written
48. Compare
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: Compare
: comm filename1 filename2
: Compares the sorted files line by line.
: comm a.txt b.txt
: OS is a program that acts as an interface.

49. Type
NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: Type
: type filename
: To view the content of the file.
: type names.txt
:
Arun
Balu
Chandra
David
Edwards

50. Basic Calculator


NAME
SYNTAX
DESCRIPTION
EXAMPLE
OUTPUT

: Basic Calculator
: bc
: To open the basic calculator.
: bc
: 10 + 10
20

RESULT:
Thus all the Linux commands are executed.

P a g e | 20 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.2
DATE:

SHELL PROGRAMMING

AIM:
To write various shell programming.
BIGGEST OF TWO NUMBERS
ALGORITHM:
1. Start the program.
2. Enter the value for a & b.
3. If a is greater than b then goto step 3 else goto step 4.
4. Print a is greate goto step 6.
5. Print b is greate goto step 6.
6. Stop the program execution.
PROGRAM:
clear
echo "enter the value of a & b"
read a
read b
if [$a -gt $b]
then
echo "A is Greater"
else
echo "B is Greater "
fi

OUTPUT:
Enter the value of a & b
8
9
B is big

P a g e | 21 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

LEAP YEAR OR NOT

AIM:
To find whether the given year is a leap year or not.
ALGORITHM:
1. Start the program.
2. Enter the value for year.
3. Divide year by 4.Check the remainder.If the remainder is equal to zero goto step 4 else goto step 5.
4. Print the given year is leap year goto step 6.
5. Print the given year is not a leap year goto step 6.
6. Stop the program execution.
PROGRAM:
clear
echo "enter year"
read year
if [ ` expr $year % 4` -eq 0 ]
then
echo "$year is leap year"
else
echo "$year is not leap year"
fi

OUTPUT:
Enter year
2004
2004 is leap year
Enter year
2005
2005 is not leap year

P a g e | 22 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EVEN OR ODD NUMBER


AIM:
To find whether the given number is odd or even .
ALGORITHM:
1. Start the program.
2. Get the value for variable n.
3. Initialize the variable i=0.
4. Check whether i is lessthan n ,if so divide the value of i by 2.If the remainder is equal to zero then
goto step 5 else goto step 6.
5. Print the given number is even number goto step 7.
6. Print the given number is odd number goto step 7.
7. Increment the value of i by 1, goto step 4.
8. Stop the program execution.
PROGRAM:
clear
echo "enter the number"
read n
i=0
while [ $i -lt $n ]
do
if [ ` expr $i % 2` -eq 0 ]
then
echo " $i is a even number"
else
echo " $i is a odd number"
fi
i=` expr $i + 1`
done

OUTPUT:
Enter the number
5
0 is a even number
1 is a odd number
2 is a even number
3 is a odd number
4 is a even number

P a g e | 23 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

SUM OF DIGITS
AIM:
To find the sum of digits of a given number.
ALGORITHM:
1. Start the program.
2. Get the value for num.
3. Initialize the variable s=0.
4. Check whether n is not equal to zero, if so extract the digit one by one by doing the following
steps.otherwise goto step 6.
n1=` expr $num % 10`
s=` expr $s + $n1`
num=` expr $num / 10`
5. Goto step 4.
6. Print the value of sum of digit.
7. Stop the program execution.
PROGRAM:
clear
echo "Enter the value for num"
read num
s=0
while [ $num -ne 0 ]
do
n1=` expr $num % 10`
s=` expr $s + $n1`
num=` expr $num / 10`
done
echo "sum of digits:$s"

OUTPUT:
Enter digits
123
Sum of digits:6

P a g e | 24 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

FIBONACCI SERIES
AIM:
To write a program to generate a fibonnacci series.
ALGORITHM:
1. Start the program.
2. Get the value of num to generate the fibonnacci series.
3. Initialize a=-1,b=1, and c=0.
4. Check if the num is greater than 0, Add a and b value to store value into c variable goto step 5
else goto step 8.
5. Store the value of b to a, and c to b.
6. Decrement the value n by 1.
7. Print the value of c goto step 4.
8. Stop the program.
PROGRAM:
clear
echo "Enter the number"
read num
a=-1
b=1
c=0
echo "Fibnoci series"
while [ $num -gt 0 ]
do
c=` expr $a + $b`
a=$b
b=$c
num=` expr $num - 1`
echo $c
done

OUTPUT:
Enter the number
5
Fibonacci series
0
1
1
2
3
RESULT:
Thus the various shell programs has been entered and verified

P a g e | 25 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.3

SYSTEM CALLS OF UNIX/LINUX OPERATING SYSTEM

DATE:
AIM:
To write various system calls.
CREATE A CHILD PROCESS USING FORK
ALGORITHM:
1. Start the program.
2. Cerate a child process by assigning Pid=fork ().
3. If Pid =0 ,the child process successfully created ,print the child process id goto step 4
4. If Pid >0, then the parent process executed and print the parent process id goto step 6 else goto
step 5.
5. Print fork () process is not executed goto step 6.
6. Stop the process.
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("\n HELLO \n");
if(pid==0)
{
printf("\n CHILD PROCESS IS EXECUTED \n");
printf("\n THE ID NUMBER OF THE CHILD PROCESS IS %d \n",getpid());
}
else if(pid>0)
{
printf("\n PARENT PROCESS IS EXECUTED");
printf("\n THE ID NUMBER OF PARENT PROCESS IS %d\n",getppid());
}
else
printf("\n FORK PROCESS IS NOT EXECUTED");
}
OUTPUT:
[iiiit01@localhost cpro]$ cc pro2.c
[iiiit01@localhost cpro]$ ./a.out
HELLO
CHILD PROCESS IS EXECUTED
THE ID NUMBER OF THE CHILD PROCESS IS 3128
HELLO
PARENT PROCESS IS EXECUTED
THE ID NUMBER OF PARENT PROCESS IS 2964
P a g e | 26 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

FORK AND WAIT SYSTEM CALL


ALGORITHM:
1. Start the program.
2. Cerate a child process by assigning Pid=fork ().
3. If Pid =0 , the child process successfully created ,then execute wait(20) then print the child
process id goto step 4.
4. If Pid >0, then execute wait(50) ,the parent process executed and print the parent process id
goto step 5 .
5. Stop the process.
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("\n HELLO \n");
if(pid==0)
{
wait(20);
printf("\n CHILD PROCESS IS EXECUTED \n");
printf("\n THE ID NUMBER OF THE CHILD PROCESS IS %d \n",getpid());
}
else if(pid>0)
{
wait(50);
printf("\n PARENT PROCESS IS EXECUTED");
printf("\n THE ID NUMBER OF PARENT PROCESS is %d",getpid());
}
}

OUTPUT:
[iiiit01@localhost cpro]$ cc pro3.c
[iiiit01@localhost cpro]$ ./a.out
HELLO
CHILD PROCESS IS EXECUTED
THE ID NUMBER OF THE CHILD PROCESS IS 3150
HELLO
PARENT PROCESS IS EXECUTED
THE ID NUMBER OF PARENT PROCESS is 3149

P a g e | 27 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

CREATE A CHILD PROCESS USING SLEEP


ALGORITHM:
1. Start the program.
2. Cerate a child process by assigning Pid=fork ().
3. If Pid =0 , the child process successfully created , then print the child process id then execute
sleep(30) goto step 4.
4. If Pid >0, then execute sleep (20) ,then print the parent process running, print the parent
process id goto step 5 .
5. Stop the process.
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("\n HELLO \n");
if(pid==0)
{
printf("\n CHILD PROCESS IS CREATED \n");
printf("\n THE ID NUMBER OF THE CHILD PROCESS IS %d \n",getpid());
sleep(30);
}
else if(pid>0)
{
sleep(20);
printf("\n PARENT PROCESS IS RUNNING");
printf("\n THE ID NUMBER OF PARENT PROCESS IS %d\n",getppid());
}
}

OUTPUT:
[iiiit01@localhost cpro]$ cc pro4.c
[iiiit01@localhost cpro]$ ./a.out
HELLO
CHILD PROCESS IS CREATED
THE ID NUMBER OF THE CHILD PROCESS IS 3206
HELLO
PARENT PROCESS IS RUNNING
THE ID NUMBER OF PARENT PROCESS IS 2964

P a g e | 28 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DIRECTORY SYSTEM CALL


ALGORITHM:
1. Start the program.
2. Get the name of the directory through the command line argument.
3. Open directory by executing dirname=opendir(argv[1]).
4. Read the content of the directoryand assigned to the variable preaddr.
5. Check preaddr=null,if so close the directory goto step 6 else print the content of the directory goto
step 4.
6. Stop the execution.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
main(int argc,char *argv[])
{
DIR *dirname;
struct dirent *preaddr;
dirname=opendir(argv[1]);
while(1)
{
preaddr=readdir(dirname);
if(preaddr==NULL)
{
closedir(dirname);
exit(0);
}
printf("\n\nFOUND ENTRY %s:%s:",argv[1],preaddr->d_name);
}
}

OUTPUT:
[iiiit01@localhost cpro]$ cc pro1.c
[iiiit01@localhost cpro]$ mkdir hiram
[iiiit01@localhost cpro]$ ./a.out hiram

FOUND ENTRY hiram:.:


FOUND ENTRY hiram:..:

P a g e | 29 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

FILE SYSTEM CALL USING OPEN AND CLOSE


ALGORITHM:
1. Start the program.
2. Get the name of the file through the command line argument.
3. Open the file by executing fd=open(argr[1],0),if fd is equal to -1 then print error occur
else create a new file by using the statement cr=creat(argr[2],9999).
4. If cr=-1 then print file is not created else a file is successfully created .
5. Read the content of the file by using the statement rd=read(fd,s,size) and write the read
content to another file using wd=write(cr,s,size).
6. Stop the execution.
PROGRAM:
#include<stdio.h>
#define size 10
main(int argc,char *argr[])
{
int i,n,rd,wd,cr,fd;
char s[size];
if(argc<3)
{
printf("illegal input");
exit(1);
}
fd=open(argr[1],0);
if(fd==-1)
{
printf("error occured");
exit(1);
}
cr=creat(argr[2],9999);
if(cr==-1)
{
printf("file not created");
exit(1);
}
rd=read(fd,s,size);
while(rd>0)
{
wd=write(cr,s,size);
rd=read(fd,s,size);
}
close(fd);
close(cr);
printf("file completed");
}

P a g e | 30 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT:
[iiiit01@localhost cpro]$ cc pro5.c
[iiiit01@localhost cpro]$ vi output
Hi how are u
[iiiit01@localhost cpro]$. /a.out output peruout
File completed
[iiiit01@localhost cpro]$ Cat peruout
Hi how are u

RESULT:
Thus the various system calls has been entered and verified

P a g e | 31 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.4

SIMULATION OF GREP

DATE:
AIM:
To write a c program to simulate the Linux command grep.
ALGORITHM:
1.
2.
3.
4.
5.

Start the program.


Initialize the flag variable to zero
Get the name of the file and pattern to be searched.
Open the file in which the searching is going to be performed in read mode
Read the contednt of the file word by word and compare it with the string to be searched . If
a match occurs go to step 6 else goto step 7.
6. Print the pattern is found .go to step 8
7. Print the pattern is not found .go to step 8
8. Stop the execution.
PROGRAM :
#include<stdio.h>
main()
{
FILE *f;
char str[10],strf[10],c[10];
int flag=0;
printf("\nENTER THE PATTERN:");
scanf("%s",str);
f=fopen("cse.txt","r");
while(!feof(f))
{
fscanf(f,"%s",strf);
if(strcmp(str,strf)==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("\nTHE PATTERN IS FOUND\n");
else
printf("\nTHE PATTERN IS NOT FOUND\n");
return 0;
}

P a g e | 32 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT:
ENTER THE PATTERN: PCET
THE PATTERN IS FOUND
ENTER THE PATTERN: PARK
THE PATTERN IS NOT FOUND

RESULT:
Thus a C program to simulate grep command of Unix is written and executed successfully.

P a g e | 33 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

SIMULATION OF ls COMMAND
AIM:
To write a C program to simulate ls command used in Unix.
Algorithm:
1.
2.
3.
4.
5.
6.

Start the program.


Get the name of the directory.
Open the directory
Read the content of the directory
Display the content of the directory.
Stop the execution.

PROGRAM:
#include<dirent.h>
#include<sys/stat.h>
main()
{
DIR *dp;
struct dirent *dir;
char d[10];
printf("enter the directory name");
scanf("%s" ,d);
dp=opendir(d);
if(dp!=NULL)
{
while((dir=readdir(dp))!=NULL)
printf(" %s \n ",dir->d_name);
}
else
printf("\n no such directory found");
closedir(dp);
exit(0);
}
OUTPUT:
Enter the directory name:s05cse15
F1
F2
Vc++
C
RESULT:
Thus a C program to simulate ls command of Unix is written and executed successfully.
P a g e | 34 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.5

PROCESS SCHEDULING

DATE:
IMPLEMENTATION OF FCFS SCHEDULING
AIM:
To write a c program to implement FCFS scheduling.
ALGORITHM:
1: start the program.
2: with burst time, execution time ,arrival Time, waiting time and turnaround time and create object
for the structure
3: In main function get the number of processes and burst time arrival time for each processes.
4: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
5: waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME-ARRIVAL TIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
6: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/ No of processes.
7: Print processes Burst time Arrival time Waiting time Turnaround time for each processes and
Average waiting and Turnaround time.
8 : Terminate the program.
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu;
}p[10];
main()
{
int pro,i;
float awt=0,atat=0;
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&pro);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<pro;i++)
{
scanf("%d",&p[i].bst);
}
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS\n");
for(i=0;i<pro;i++)
{
scanf("%d",&p[i].ar);
P a g e | 35 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

}
p[i].exu=0;
for(i=0;i<pro;i++)
{
p[i+1].exu=(p[i].exu+p[i].bst);
}
for(i=0;i<pro;i++)
{
p[i].wt=p[i].exu-p[i].ar;
p[i].tat=p[i].wt+p[i].bst;
}
for(i=0;i<pro;i++)
{
awt=awt+p[i].wt;
atat=atat+p[i].tat;
}
printf("\nPROCESS BRUSTTIME ARRIVALTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<pro;i++)
{
printf("\np %d
%d
%d
%d
%d ",i+1,p[i].bst,p[i].ar,p[i].wt,p[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/pro);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/pro);
return 0;
}
OUTPUT:
ENTER NUMBER OF PROCESS
5
ENTER BURST TIME FOR EACH PROCESS
8
3
4
2
6
ENTER ARRAIVAL TIME FOR EACH PROCESS
0
0
0
0
0

P a g e | 36 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

PROCESS
p0
p1
p2
p3
p4

BRUSTTIME
8
3
4
2
6

ARRIVALTIME
0
0
0
0
0

WAITINGTIME
0
8
11
15
17

TURNARROUNDTIME
8
11
15
17
23

AVERAGE WAIRING TIME= 10.200000


AVERAGE TURNARROUND TIME=14.800000
ENTER NUMBER OF PROCESS
4
ENTER BURST TIME FOR EACH PROCESS
8
4
9
5
ENTER ARRAIVAL TIME FOR EACH PROCESS
0
1
2
3
PROCESS
BRUSTTIME ARRIVALTIME WAITINGTIME TURNARROUNDTIME
p0
8
0
0
8
p1
4
1
7
11
p2
9
2
10
19
p3
5
3
18
23
AVERAGE WAITING TIME= 8.750000
AVERAGE TURNARROUND TIME=15.250000[

RESULT:
The the C program to implement FCFS scheduling has been executed successfully and the output has
been verified.

P a g e | 37 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

IMPLEMENTATION OF SJF SCHEDULING


AIM:
To write c programs to implement non preemptive sjf scheduling with zero arrival time and non zero
arrival time.
ALGORITHM:
ZERO ARRIVALTIME
1: start the program.
2: Declare a structure with burst time ,execution time,waiting time, and turnaround time ,processes
no,variables and create objects for the structure
3: In main function get the number of processes and burst timeArrival time for each process.
4: Sort the burst time and processes number according to burst time .
5: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
6 : waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
7: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/ No of processes.
8: Print processes Burst time Waiting time Turnaround time for each processes and Average waiting
and Turnaround time.
9 : Terminate the program.
ZERO ARRIVALTIME
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,o,k;
float awt=0,atat=0;
printf(" ************ARRIVAL TIME IS ZERO**************");
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
for(i=0;i<=n;i++)
{ a[i].pro=i+1;
a[i].bst=p[i].bst;
}
for(i=0;i<n;i++)
{
P a g e | 38 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

for(j=i+1;j<n;j++)
{
if(a[i].bst>a[j].bst)
{t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
k=a[i].pro;
a[i].pro=a[j].pro;
a[j].pro=k;
}}}
a[0].exu=0;
for(i=0;i<n;i++)
a[i+1].exu=a[i].exu+a[i].bst;
for(i=0;i<n;i++)
{
a[i].wt=a[i].exu;
a[i].tat=a[i].wt+a[i].bst;
}
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\nPROCESS BRUSTTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d
%d
%d
%d ",a[i].pro,a[i].bst,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}
OUTPUT:
************ARRIVAL TIME IS ZERO**************
ENTER NUMBER OF PROCESS
4
ENTER BURST TIME FOR EACH PROCESS
6
8
7
3
PROCESS
p4
p1
p3
p2

BRUSTTIME
3
6
7
8

WAITINGTIME
0
3
9
16

P a g e | 39 OPERATING SYSTEMS LABORATORY

TURNARROUNDTIME
3
9
16
24

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

AVERAGE WAITING TIME= 7.000000


AVERAGE TURNARROUND TIME=13.000000[
NON ZERO ARRIVAL TIME
ALGORITHM:
1: start the program.
2: Declare a structure with burst time ,execution time, arrival time ,waiting time, and turnaround time
,processes no, variables and create objects for the structure
3: In main function get the number of processes and burst time Arrival time for each process.
4: Sort the burst time ,processes number and arrival time according to burst time.
5: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
6 : waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME-ARRIVAL TIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
7: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/ No of processes.
8: Print processes Burst time waiting time
Turnaround time for each processes and Average waiting and Turnaround time.
9 : Terminate the program.
NON ZERO ARRIVALTIME
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,k,z;
float awt=0,atat=0;
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i].ar);
}
for(i=0;i<n;i++)
{
P a g e | 40 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

a[i].pro=i+1;
a[i].bst=p[i].bst;
a[i].ar=p[i].ar;
}
for(i=1;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].bst>a[j].bst)
{
t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
k=a[i].pro;
a[i].pro=a[j].pro;
a[j].pro=k;
z= a[i].ar ;
a[i].ar=a[j].ar;
a[j].ar=z;
}
}
}
a[0].exu=0;
for(i=1;i<n;i++)
a[i].exu=a[i-1].exu+a[i-1].bst;
for(i=0;i<n;i++)
{
a[i].wt=a[i].exu-a[i].ar;
a[i].tat=a[i].wt+a[i].bst;
}
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\nPROCESS BRUSTTIME ARRIVALTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d
%d
%d
%d
%d ",a[i].pro,a[i].bst,a[i].ar,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}

P a g e | 41 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT:
ENTER NUMBER OF PROCESS:
4
ENTER BURST TIME FOR EACH PROCESS
8
4
9
5
ENTER ARRAIVAL TIME FOR EACH PROCESS:
0
1
2
3
PROCESS
p1
p2
p4
p3

BRUSTTIME ARRIVALTIME WAITINGTIME TURNARROUNDTIME


8
0
0
8
4
1
7
11
5
3
9
14
9
2
15
24

AVERAGE WAITING TIME= 7.750000


AVERAGE TURNARROUND TIME=14.250000

RESULT:
The C programs to implement non preemptive SJF scheduling with zero arrival time and non zero
arrival time has been executed successfully and the output has been verified.

P a g e | 42 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

IMPLEMENTATION OF PRIORITY SCHEDULING


AIM:
To write C programs to implement non preemptive priority scheduling with zero arrival time and
non zero arrival time.
ALGORITHM:
ZERO ARRIVALTIME
1: start the program.
2: Declare a structure with burst time, execution time, waiting time, and turnaround time ,processes no,
variables and create objects for the structure
3: In main function get the number of processes and burst time Arrival time for each process.
4: Sort the burst time and process numbers according to priority.
5: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
6 : waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
7: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/ No of processes.
8: Print processes Burst time Waiting time Turnaround time for each processes and Average waiting
and Turnaround time.
9: Terminate the program.

NON ZERO ARRIVAL TIME


ALGORITHM:
1: start the program.
2: Declare a structure with burst time ,execution time, arrival time ,waiting time, and turnaround time
,processes no, variables and create Objects for the structure
3: In main function get the number of processes and burst time Arrival time for each process.
4: Sort the burst time ,processes number and arrival time according to priority .
5: calculate the execution time by using the following loop.
Initially first process execution is zero.
Next processes execution =previous processes execution
time + previous process burst time.
6 : waiting time and turn around time can be calculated by
WAITING TIME=EXECUTIONTIME - ARRIVAL TIME.
TURNARROUND TIME=WAITING TIME+BURST TIME.
7: Average waiting time and average turn around can be Calculated by

Average waiting time=sum of waiting time /No of process.


Average Turnaround time= sum of Turnaround time/ No of processes.
8: Print processes Burst time waiting time
Turnaround time for each processes and Average waiting and Turnaround time.
9 : Terminate the program.
P a g e | 43 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

ZERO ARRIVALTIME
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro,pr;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,k,z;
float awt=0,atat=0;
printf(" ************ARRIVAL TIME IS ZERO**************");
printf("\n*******LOW NUMBERS HAVE HIGH PRIORITY********");
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
printf("\nENTER PRIORITY FOR EACH PROCESS\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pr);
for(i=0;i<n;i++)
{
a[i].pro=i+1;
a[i].bst=p[i].bst;
a[i].pr=p[i].pr;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].pr>a[j].pr)
{
z=a[i].pr;
a[i].pr=a[j].pr;
a[j].pr=z;
t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
k=a[i].pro;
a[i].pro=a[j].pro;
a[j].pro=k;
} } }
a[0].exu=0;
for(i=0;i<n;i++)
a[i+1].exu=a[i].exu+a[i].bst;
for(i=0;i<n;i++)
{
P a g e | 44 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

a[i].wt=a[i].exu;
a[i].tat=a[i].wt+a[i].bst;
}
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\n PROCESSES BRUSTTIME PRIORITY
WAITINGTIME TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d
%d
%d
%d
%d ",a[i].pro,a[i].bst,a[i].pr,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}
OUTPUT:
**************ARRIVAL TIME IS ZERO*****************
*******LOW NUMBERS HAVE HIGH PRIORITY********
ENTER NUMBER OF PROCESS
5
ENTER BURST TIME FOR EACH PROCESS
10
1
2
1
5
ENTER PRIORITY FOR EACH PROCESS
3
1
4
5
2
PROCESS
p2
p5
p1
p3
p4

BRUSTTIME
1
5
10
2
1

PRIORITY
1
2
3
4
5

WAITINGTIME
0
1
6
16
18

TURNARROUNDTIME
1
6
16
18
19

AVERAGE WAITING TIME= 8.200000


AVERAGE TURNARROUND TIME=12.000000

P a g e | 45 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

NON ZERO ARRIVALTIME


PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,ar,exu,pro,pr;
}p[10],a[10],b[10];
main()
{
int n,i,j,t,k,z,x;
float awt=0,atat=0;
printf("\n*******LOW NUMBERS HAVE HIGH PRIORITY********");
printf("\nENTER NUMBER OF PROCESS:\n");
scanf("%d",&n);
printf("\nENTER BURST TIME FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].bst);
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i].ar);
}
printf("\nENTER PRIORITY FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
{
scanf("%d",&p[i].pr);
}
for(i=0;i<n;i++)
{
a[i].pro=i+1;
a[i].bst=p[i].bst;
a[i].ar=p[i].ar;
a[i].pr=p[i].pr;
}
for(i=1;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].pr>a[j].pr)
{
x=a[i].pr;
a[i].pr=a[j].pr;
a[j].pr=x;
t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
k=a[i].pro;
a[i].pro=a[j].pro;
a[j].pro=k;
P a g e | 46 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

z= a[i].ar ;
a[i].ar=a[j].ar;
a[j].ar=z;
} } }
a[0].exu=0;
for(i=1;i<n;i++)
a[i].exu=a[i-1].exu+a[i-1].bst;
for(i=0;i<n;i++)
{
a[i].wt=a[i].exu-a[i].ar;
a[i].tat=a[i].wt+a[i].bst;
}
for(i=0;i<n;i++)
{
awt=awt+a[i].wt;
atat=atat+a[i].tat;
}
printf("\nPROCESS BRUSTTIME ARRIVALTIME PRIORITY
WAITINGTIME TURNARROUNDTIME");
for(i=0;i<n;i++)
{
printf("\np %d
%d %d %d %d %d",
a[i].pro,a[i].bst,a[i].ar,a[i].pr,a[i].wt,a[i].tat);
}
printf("\nAVERAGE WAITING TIME= %f",awt/n);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/n);
return 0;
}
OUTPUT:
*******LOW NUMBERS HAVE HIGH PRIORITY********
ENTER NUMBER OF PROCESS:
4
ENTER BURST TIME FOR EACH PROCESS:
6
5
3
5
ENTER ARRAIVAL TIME FOR EACH PROCESS:
0
1
2
3
ENTER PRIORITY FOR EACH PROCESS:
4
2
6
3

P a g e | 47 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

PROCESS BRUSTTIME ARRIVALTIME PRIORITY WAITINGTIME TURNARROUNDTIME


p1
6
0
4
0
6
p2
5
1
2
5
10
p4
5
3
3
8
13
p3
3
2
6
14
17
AVERAGE WAITING TIME= 6.750000
AVERAGE TURNARROUND TIME=11.500000

RESULT:
The c programs to implement non preemptive priority scheduling with zero arrival time and non zero
arrival time has been executed successfully and the output has been verified.

P a g e | 48 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

IMPLEMENTATION OF RR SCHEDULING
AIM:
To write a c program to implement Round Robin scheduling.
ALGORITHM:
1: start the program.
2: Declare a structure with burst time, execution time, arrival time ,waiting time, and turnaround time ,
variables and create Objects for that structure.
3: In main function get the number of processes , burst time,Arrival time for each process and time
slice.
4:store the burst time in temporary arrays.
5: Sort the burst time arrival time according to burst time and calculate maximum no of execution .
6: Execution time can be calculated by.
In two loops first loop for max no of execution second loop for noof process.
If burst time for each process greater than time slice do:
Temp var=Temp var+ time slice.
Execution time=Temp var.
Burst time=burst time time slice.
Else burst time less than time slice and greater than zero
Temp var =Temp var + burst time.
Execution time=Temp var.
Burst time=0.
7:Waiting time and turnaround time can be calculated by
WATING TIME=EXECUTION TIME-ARRIVAL TIME BRUST TIME
TURNARROUND TIME=WAITING TIME+BRUST TIME.
8: Average waiting time and average turn around can be Calculated by
Average waiting time=sum of waiting time /No of process.
Average Turnaround time= sum of Turnaround time/No of processes.
9: Print processes Burst time Arrival time waiting time
Turnaround time for each processes and Average waiting and Turnaround time.
10: Terminate the program.
PROGRAM:
#include<stdio.h>
struct fc
{
int bst,wt,tat,exu,n,ar;
}p[10],a[10],b[10];
main()
{
int pn,i,j,ts,t,x,y=0;
float awt=0,atat=0;
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&pn);
printf("\nENTER BURST TIME FOR EACH PROCESS\n");
for(i=0;i<pn;i++)
{
scanf("%d",&p[i].bst);
P a g e | 49 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

}
printf("\nENTER ARRAIVAL TIME FOR EACH PROCESS\n");
for(i=0;i<pn;i++)
{
scanf("%d",&p[i].ar);
}
printf("\nENTER THE TIME SLICE :");
scanf("%d",&ts);
for(i=0;i<pn;i++)
{
a[i].bst=p[i].bst;
b[i].bst=p[i].bst;
}
for(i=0;i<pn;i++)
{
for(j=i+1;j<pn;j++)
{
if(a[i].bst>a[j].bst)
{
t=a[i].bst;
a[i].bst=a[j].bst;
a[j].bst=t;
} } }
x=a[pn-1].bst/ts;
for(j=0;j<x+5;j++)
{
for(i=0;i<pn;i++)
{
if(p[i].bst>ts)
{
y=y+ts;
p[i].exu=y;
p[i].bst=p[i].bst-ts;
p[i].n=p[i].n+1;
}
else if(p[i].bst>0)
{
y=y+p[i].bst;
p[i].exu=y;
p[i].bst=0;
p[i].n=p[i].n+1;
} } }
for(i=0;i<pn;i++)
{
p[i].wt=p[i].exu-b[i].bst-p[i].ar;
p[i].tat=p[i].wt+b[i].bst;
}
for(i=0;i<pn;i++)
P a g e | 50 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

{
awt=awt+p[i].wt;
atat=atat+p[i].tat;
}
printf("\nPROCESS BRUSTTIME WAITINGTIME
TURNARROUNDTIME");
for(i=0;i<pn;i++)
{
printf("\np %d
%d
%d
%d ",i,b[i].bst,p[i].wt,p[i].tat);
}
printf("\nAVERAGE WAIRING TIME= %f",awt/pn);
printf("\nAVERAGE TURNARROUND TIME=%f",atat/pn);
return 0;
}

OUTPUT:
ENTER NUMBER OF PROCESS
3
ENTER BURST TIME FOR EACH PROCESS
24
3
3
ENTER ARRAIVAL TIME FOR EACH PROCESS
0
0
0
ENTER THE TIME SLICE :4
PROCESS BRUSTTIME ARRIVALTIME
p0
24
0
p1
3
0
p2
3
0

WAITINGTIME
6
4
7

TURNARROUNDTIME
30
7
10

AVERAGE WAITING TIME= 5.666667


AVERAGE TURNARROUND TIME=15.666667[s06cse34@fileserver today]$ ./a.out
ENTER NUMBER OF PROCESS
3
ENTER BURST TIME FOR EACH PROCESS
24
3
3

P a g e | 51 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

ENTER ARRAIVAL TIME FOR EACH PROCESS


0
1
2
ENTER THE TIME SLICE :4
PROCESS
p0
p1
p2

BRUSTTIME
24
3
3

ARRIVAL TIME WAITINGTIME TURNARROUNDTIME


0
6
30
1
3
6
2
5
8

AVERAGE WAITING TIME= 4.666667


AVERAGE TURNARROUND TIME=14.666667

RESULT:
The C program to implement RR scheduling has been executed successfully and the output has been
verified.

P a g e | 52 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.6

SEMAPHORE

DATE:
AIM:
To write a c program to implement the following using semaphore
1. Producer and consumer problem
2. Dining philosopher's problem
3. Readers writer problem using semaphore

PRODUCER AND CONSUMER PROBLEM


ALGORITHM:
1.Start the program.
2. Initialize the following variables
mutex=1;
empty=5;
full=0;
3. Display the menu and get the choice.
4. If choice is 1,the producer process is executed,goto step 8.
5. If choice is 2 ,the consumer process is executed goto step 9.
6. If choice is 3, then both producer and consumer processes are executed goto step 10.
7. If choice is 4,go to step 11.
8. Check whether empty equal to zero, if so producer has to wait else allow the producer to produce the
item. Go to step 3
9. Check whether full equal to zero, if so consumer has to wait else allow the consumer to consume the
item. Go to step 3
10. Check whether empty buffer is available, if available invoke producer else invoke consumer. Go to step
3.
11. Stop the program.
PROGRAM
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
static int full,empty,mutex;
int buffer[5],in=0,out=0;
void wait(int *a);
void signal(int *b);
void producer()
{
int nextp;
printf("producer\n");
wait(&empty);
wait(&mutex);
nextp=rand()%10+1;
buffer[in]=nextp;
printf("produced item is %d\n",nextp);
in=(in+1)%5;
signal(&mutex);
signal(&full);
P a g e | 53 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

printf("full=%d\t empty=%d\n",full,empty);
}
void consumer()
{
int nextc;
printf("consumer\n");
wait(&full);
wait(&mutex);
nextc=buffer[out];
printf("consumerd item is %d\n",nextc);
out=(out+1)%5;
signal(&mutex);
signal(&empty);
printf("full=%d\t empty=%d\n",full,empty);
}
void wait(int *a)
{
while(*a<=0);
*a=*a-1;
}
void signal(int *b)
{
*b=*b+1;
}
main()
{
int c;
mutex=1;
empty=5;
full=0;
clrscr();
while(1)
{
printf("1.producer\t 2.consumer\t 3.both\t 4.Exit\n");
printf("choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
if(empty==0)
printf("producer has to wait\n");
else
{
producer();
}
break;
case 2:
P a g e | 54 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

if(full==0)
printf("consumer has to wait");
else
{
consumer();
} break;
case 3:
if(!empty)
{
printf("producer has to wait\n");
consumer();
}
else if(!full)
{
printf("consumer has to wait\n");
producer();
}
else
{
consumer();
producer();
}
break;
case 4:
exit(0);
break;
}
}
getch();
return 0;
}

P a g e | 55 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT:
1.producer
2.consumer
choice
1
producer
produced item is 7
full=1 empty=4

3.both 4.Exit

1.producer
2.consumer
choice
1
producer
produced item is 1
full=2 empty=3

3.both 4.Exit

1.producer
2.consumer
choice
2
consumer
consumerd item is 7
full=1 empty=4

3.both 4.Exit

1.producer
2.consumer
choice
2
consumer
consumerd item is 1
full=0 empty=5

3.both 4.Exit

1.producer
2.consumer
3.both 4.Exit
choice
2
consumer has to wait1.producer 2.consumer
choice
1
producer
produced item is 3
full=1 empty=4
1.producer
2.consumer
choice
3
consumer
consumerd item is 3
full=0 empty=5
producer
produced item is 1
full=1 empty=4

3.both 4.Exit

1.producer
Choice

3.both 4.Exit

2.consumer
4

3.both 4.Exit

P a g e | 56 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DINING PHILOSOPHER'S PROBLEM


ALGORITHM:
1. Start the program.
2. Display the menu and get the choice.
3. If choice is 1,go to step 9.
4. If choice is 2 , Enter which philosophers is Hungry, call the pickup() function Go to step 7.
5. If choice is 3, then Enter which Philosopher is Thinking . Call the putdown() function. Go to step 8
6. Print State of each Philosepher.Go to step 2.
7. Change the state of the philosopher to hungry, and test whether two spoons are available. If so
make the status of philosopher as eating else make state as waiting. Go to step 6.
8. Change the state of the philosopher to thinking, and put the two spoons back in the table . Go to
step 6.
9. Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
char state[10],self[10],spoon[10];
void test(int k)
{
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
{
state[k]='e';
self[k]='s';
spoon[k]='n';
spoon[(k+4)%5]='n';
}
}
void pickup(int i)
{
state[i]='h';
test(i);
if(state[i]!='e')
self[i]='w';
}
void putdown(int i)
{
state[i]='t';
spoon[i]='s';
spoon[(i+4)%5]='s';
test((i+4)%5);
test((i+1)%5);
}
main()
{
int ch,a,n,i;
clrscr();
printf("\n Dining Philosopher's Problem:");
printf("\n...........................................\n");
P a g e | 57 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\n Initial state of each philososphers:");
printf("\n Phil No : \t Think/Eat \t Status \t Spoon");
for(i=0;i<5;i++)
printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]);
printf("\n Enter the option : \n1.Exit \n 2.Hungry \n 3.Thinking \n");
printf("\n Enter your choice :");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 1:
return 0;
case 2:
printf("\n Enter which philosophers is Hungry :");
scanf("%d",&n);
pickup(n-1);
break;
case 3:
printf("\n Enter which Philosopher is Thinking :");
scanf("%d",&n);
putdown(n-1);
break;
}
printf("\n State of each Philosepher :");
printf("\n Philno: \t Think/Eat \t Status \t Spoon");
for(i=0;i<5;i++)
printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]);
printf("\n Enter the option : \n1.Exit \n 2.Hungry \n 3.Thinking \t :");
scanf("%d",&ch);
}
}

P a g e | 58 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

INPUT/OUTPUT
Dining Philosopher's Problem:
...........................................
Initial state of each philososphers:
Phil No :
Think/Eat
Status
1
t
s
2
t
s
3
t
s
4
t
s
5
t
s
Enter the option :
1.Exit
2.Hungry
3.Thinking

Spoon
s
s
s
s
s

Enter your choice :2


Enter which philosophers is Hungry :1
State of each Philosepher :
Philno:
Think/Eat
Status
1
e
s
2
t
s
3
t
s
4
t
s
5
t
s
Enter the option :
1.Exit
2.Hungry
3.Thinking : 2

Spoon
n
s
s
s
n

Enter which philosophers is Hungry :2


State of each Philosepher :
Philno:
Think/Eat
Status
1
e
s
2
h
w
3
t
s
4
t
s
5
t
s
Enter the option :
1.Exit
2.Hungry
3.Thinking :2

Spoon
n
s
s
s
n

P a g e | 59 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

Enter which philosophers is Hungry :4


State of each Philosepher :
Philno:
Think/Eat
Status
1
e
s
2
h
w
3
t
s
4
e
s
5
t
s
Enter the option :
1.Exit
2.Hungry
3.Thinking :2

Spoon
n
s
n
n
n

Enter which philosophers is Hungry :5


State of each Philosepher :
Philno:
Think/Eat
Status
1
e
s
2
h
w
3
t
s
4
e
s
5
h
w
Enter the option :
1.Exit
2.Hungry
3.Thinking :3

Spoon
n
s
n
n
n

Enter which Philosopher is Thinking :1


State of each Philosepher :
Philno:
Think/Eat
Status
1
t
s
2
e
s
3
t
s
4
e
s
5
h
w
Enter the option :
1.Exit
2.Hungry
3.Thinking :3

Spoon
n
n
n
n
s

Enter which Philosopher is Thinking :4


State of each Philosepher :
Philno:
Think/Eat
Status
1
t
s
2
e
s
3
t
s
4
t
s
5
e
s

Spoon
n
n
s
n
n

P a g e | 60 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

Enter the option :


1.Exit
2.Hungry
3.Thinking :3
Enter which Philosopher is Thinking :2
State of each Philosepher :
Philno:
Think/Eat
Status
1
t
s
2
t
s
3
t
s
4
t
s
5
e
s
Enter the option :
1.Exit
2.Hungry
3.Thinking :3

Spoon
s
s
s
n
n

Enter which Philosopher is Thinking :5


State of each Philosepher :
Philno:
Think/Eat
Status
1
t
s
2
t
s
3
t
s
4
t
s
5
t
s
Enter the option :
1.Exit
2.Hungry
3.Thinking :1

Spoon
s
s
s
s
s

P a g e | 61 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

READERS WRITER PROBLEM USING SEMAPHORE


Algorithm
1. Start the program.
2. Initialize the following variables
mutex=1;
wrt=1;
readcount=0;
3. Display the menu and get the choice.
4. If choice is 1, the reader process is executed. Go to step 9.
5. If choice is 2, the writer process is executed. Go to step 10.
6. If choice is 3,then first reader ,second writer processes are executed i.e go to step 9 and then
10.Finally go to step 3.
7. If choice is 4 ,then first writer ,second reader processes are executed. .i.e go to step 10 and then
9.Finally go to step 3.
8. If choice is 5,go to step 11.
9. Execute mutex variable ,increment read count by 1.Check whether read count equals 1,then execute
wait(&wrt). If read count is equal to 0 then execute signal(&wrt);Go to step 3.
10. Execute wait(&wrt),get the string to write, then the writer writes. Perform signal(&wrt).Go to step
3.
11. Stop the program.
PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
static int mutex;
int wrt,readcount,ch;
char buffer[50]="hello";
void wait(int*);
void signal(int*);
void print();
void reader()
{
wait(&mutex);
readcount++;
if(readcount==1)
wait(&wrt);
signal(&mutex);
cout<<"\nREADER READS:"<<buffer;
wait(&mutex);
readcount--;
if(readcount==0)
signal(&wrt);
signal(&mutex);
}
void writer()
P a g e | 62 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

{
wait(&wrt);
cout<<"\nEnter the string to write:";
gets(buffer);
cout<<"\nWriter writes:"<<buffer;
signal(&wrt);
}
void wait(int *x)
{
while(*x<=0);
*x=*x-1;
}
void signal(int *y)
{
*y=*y+1;
}
main()
{
mutex=1;
wrt=1;
readcount=0;
do
{
cout<<"\n\nREADER-WRITER PROBLEM\n\n1.READER\n2.WRITER\n3.READAND
WRITE\n4.WRITE AND READS\n5.EXIT\n\nENTER THE CHOCIE:";
cin>>ch;
switch(ch)
{
case 1:
reader();
break;
case 2:
writer();
break;
case 3:
reader();
writer();
break;
case 4:
writer();
reader();
break;
case 5:
exit(0);
}
}
while(ch<6);
getch();
}
P a g e | 63 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT
READER-WRITER PROBLEM
1.READER
2.WRITER
3.READAND WRITE
4.WRITE AND READS
5.EXIT
ENTER THE CHOCIE:1
READER READS:hello
READER-WRITER PROBLEM
1.READER
2.WRITER
3.READAND WRITE
4.WRITE AND READS
5.EXIT
ENTER THE CHOCIE:2
Enter the string to write:hi

REUSLT:
Thus the C program to implement semaphore has been entered and verified

P a g e | 64 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.7

MEMORY MANAGEMENT

DATE:
IMPLEMENTATION OF FIRST FIT
AIM:
To write a C program to implement first fit algorithm.
ALGORITHM:
1: start the program.
2: Declare a structure with memory size & process size as variables and create object for that
structure.
3: In main get the number and size of process and memory Partition.
4:compare process memory with memory partition if Process memory is less than memory partition
put the process into memory partition move to next process .
5:Continue the loop until all processes compare with memory partition.
6: Print process memory memory partition and memory allocation.
7: Terminate the program.
PROGRAM:
#include<stdio.h>
struct fc
{
int mem,po,pm;
}p[10];
main()
{
int n,i,j,x,t[10];
printf("\nENTER NUMBER OF MEMORY PARTISION:\n");
scanf("%d",&x);
printf("\nENTER THE SIZE OF MEMORY PARTISION:\n");
for(i=0;i<x;i++)
scanf("%d",&p[i].mem);
printf("\nENTER NUMBER OF PROCESS:\n");
scanf("%d",&n);
printf("\nENTER THE MEMORY SIZE FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pm);
for(i=0;i<x;i++)
t[i]=p[i].mem;
for(i=0;i<n;i++)
{
for(j=0;j<x;j++)
{
if(p[i].pm<=p[j].mem)
{
p[j].mem=i+1;
P a g e | 65 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

break;
}
}
}
printf("PROCESS MEMORY MEMORYPARTISION MEMORY ALLOCATION");
for(i=0;i<x;i++)
printf("\n %d
%d
%d ",p[i].pm,t[i],p[i].mem);
return 0;
}
OUTPUT:
ENTER NUMBER OF MEMORY PARTISION:
5
ENTER THE SIZE OF MEMORY PARTISION:
100
500
200
300
600
ENTER NUMBER OF PROCESS:
4
ENTER THE MEMORY SIZE FOR EACH PROCESS:
212
417
112
420
PROCESS MEMORY
212
417
112
420
0

MEMORYPARTISION
100
500
200
300
600

MEMORY ALLOCATION
100
1
3
300
2

RESULT:
The C program to implement first fit algorithm has been executed successfully and the output has
been verified.

P a g e | 66 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

IMPLEMENTATION OF BEST FIT ALGORITHM


AIM:
To write a C program to implement best fit algorithm.
ALGORITHM:
1: start the program.
2: Declare a structure with memory size & process size as variables and create object for that
structure.
3: In main function get the number and size of process and Memory Partition.
4: Sort the memory partition in ascending order.
5: compare the size of process with size of memory partition if it is less than memory partition put
the process into memory partition then move to next process .
6: Continue the loop until all processes compare with memory partition.
7: Print size of process and memory partition and memory allocation.
8: Terminate the program.
PROGRAM:
#include<stdio.h>
struct fc
{
int mem,po,pm;
}p[10],a[10];
main()
{
int n,i,j,x,t;
printf("\nENTER NUMBER OF MEMORY PARTISION:\n");
scanf("%d",&x);
printf("\nENTER SIZE OF MEMORY PARTISION:\n");
for(i=0;i<x;i++)
scanf("%d",&p[i].mem);
printf("\nENTER NUMBER OF PROCESS:\n");
scanf("%d",&n);
printf("\nENTER THE MEMORY SIZE FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pm);
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(p[i].mem>p[j].mem)
{
t=p[i].mem;
p[i].mem=p[j].mem;
p[j].mem=t;
} } }
for(i=0;i<x;i++)
a[i].mem=p[i].mem;
P a g e | 67 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

for(i=0;i<n;i++)
{
for(j=0;j<x;j++)
{
if(p[i].pm<=p[j].mem)
{
p[j].mem=i+1;
break;
}
}}
printf("\n PROCESS MEMORY MEMORY PARTISION
MEMORY ALLOCATION");
for(i=0;i<x;i++)
printf("\n %d %d %d ",p[i].pm,a[i].mem,p[i].mem);
return 0;
}
OUTPUT:
ENTER NUMBER OF MEMORY PARTISION:
5
ENTER SIZE OF MEMORY PARTISION:
100
500
200
300
600
ENTER NUMBER OF PROCESS:
4
ENTER THE MEMORY SIZE FOR EACH PROCESS:
212
417
112
420
PROCESS MEMORY
212
417
112
420
0

MEMORY PARTISION
100
200
300
500
600

MEMORY ALLOCATION
100
3
1
2
4

RESULT:
The C program to implement best fit algorithm has been executed successfully and the
output has been verified.

P a g e | 68 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

IMPLEMENTATION OF WORST FIT


AIM:
To write a C program to implement worst fit algorithm.
ALGORITHM:
1: start the program.
2: Declare a structure with memory size & process size as variables and create object for that structure.
3: In main function get the number and size of process and memory Partition.
4:sort the memory partition in descending order.
5 : compare the size of process with size of memory partition if it is less than memory partition put the
process into memory partition then move to next process .
6:Continue the loop until all processes compare with memory partition.
7: Print size of process and memory partition and memory allocation.
8: Terminate the program.
PROGRAM:
#include<stdio.h>
struct fc
{
int mem,po,pm;
}p[10],a[10];
main()
{
int n,i,j,x,t;
printf("\nENTER NUMBER OF MEMORY PARTISION\n");
scanf("%d",&x);
printf("\nENTER SIZE OF MEMORY PARTISION\n");
for(i=0;i<x;i++)
scanf("%d",&p[i].mem);
printf("\nENTER NUMBER OF PROCESS\n");
scanf("%d",&n);
printf("\nENTER THE MEMORY SIZE FOR EACH PROCESS:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i].pm);
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(p[i].mem<p[j].mem)
{
t=p[i].mem;
p[i].mem=p[j].mem;
p[j].mem=t;
} } }
for(i=0;i<x;i++)
a[i].mem=p[i].mem;
for(i=0;i<n;i++)
{
for(j=0;j<x;j++)
P a g e | 69 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

{
if(p[i].pm<=p[j].mem)
{
p[j].mem=i+1;
break;
} }}
printf("\n PROCESS MEMORY MEMORY PARTISION
MEMORY ALLOCATION");
for(i=0;i<x;i++)
printf("\n %d
%d
%d ",p[i].pm,a[i].mem,p[i].mem);
return 0;
}
OUTPUT:
ENTER NUMBER OF MEMORY PARTISION
5
ENTER SIZE OF MEMORY PARTISION
100
500
200
300
600
ENTER NUMBER OF PROCESS
4
ENTER THE MEMORY SIZE FOR EACH PROCESS:
212
417
112
420
PROCESS MEMORY
212
417
112
420
0

MEMORY PARTISION
600
500
300
200
100

MEMORY ALLOCATION
1
2
3
200
100

RESULT:
The C program to implement worst fit algorithm has been executed successfully and the output has been
verified.

P a g e | 70 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.8

PAGE REPLACEMENT

DATE:
AIM :
To write a C program to implement the following page replacement algorithms
1)FIFO
2)LRU
3)OPTIMAL
FIFO PAGE REPLACEMENT ALGORITHM
ALGORITHM:
1: Read functions fifo outside main class
2: Inside main function
3: Read number of pages in reference string
4: Read number of frames
5: Read number of page faults if all the condition satisfies the following statements are displayed
6: replacement stratergies are efficient
7: fifo page replacement stratergy is efficient
8: End processing
PROGRAM
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int i,j, page[100],p,pages;
int flag1=0,flag2=0,pf=0,top=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the toal number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0;
flag2=0;
P a g e | 71 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

//

for(i=0;i<pages;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
j=0;
if(flag1==0)
{
for(i=0;i<n;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
pf++;
flag2=1;
break;
}
}
}
if(flag2==0)
{
fr[top]=page[j];
top++;
pf++;
if(top>=n)
top=0;
}
display();
}
printf("\n Number of page faults : %d ",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("%d\t",fr[i]);
}

OUTPUT
Enter total number of frames :
4
Enter the toal number of page reference string :
11
P a g e | 72 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

Enter the page reference string :1 2 3 4 2 1 6 5 1 2 3


1
1
1
1
1
1
6
6
6
6
3

-1
2
2
2
2
2
2
5
5
5
5

-1
-1
3
3
3
3
3
3
1
1
1

-1
-1
-1
4
4
4
4
4
4
2
2

Number of page faults : 9

P a g e | 73 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

LRU PAGE REPLACEMENT ALGORITHM


ALGORITHM:
1: Read the number of pages in the reference string
2: Get the reference string
3: Get the number of frames
4: Get the page reference
5: Perform LRU operation and display
6: Display efficient algorithm
7: End processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int page[100],i,j,fs[3];
int index,k,l,p,pages,flag1=0,flag2=0,pf=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the toal number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0,flag2=0;
for(i=0;i<n;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<n;i++)
{
P a g e | 74 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
pf++;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<n;i++)
fs[i]=0;
for(k=j-1,l=1;l<=n-1;l++,k--)
{
for(i=0;i<n;i++)
{
if(fr[i]==page[k])
fs[i]=1;
}
}
for(i=0;i<n;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=page[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("\t%d",fr[i]);
}
Enter total number of frames :
4
Enter the toal number of page reference string :
11

P a g e | 75 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

Enter the page reference string :1 2 3 4 2 1 6 5 1 2 3


1
1
1
1
1
1
1
1
1
1
1

-1
2
2
2
2
2
2
2
2
2
2

-1
-1
3
3
3
3
6
6
6
6
3

-1
-1
-1
4
4
4
4
5
5
5
5

no of page faults :7

P a g e | 76 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OPTIMAL PAGE REPLACEMENT ALGORITHM


ALGORITHM:
1: Read functions optimal outside main class
2: Inside main function
3: Read number of pages in reference string
4: Read number of frames
5: Read number of page faults if all the condition satisfies the following statements are displayed
6: replacement stratergies are efficient
7: optimal page replacement stratergy is efficient
8: End processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int page[100],pages,p,i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the toal number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0;
flag2=0;
for(i=0;i<n;i++)
{
if(fr[i]==page[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
P a g e | 77 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

for(i=0;i<n;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
pf++;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<n;i++)
{ lg[i]=0;
}
for(i=0;i<n;i++)
{
for(k=j+1;k<pages;k++)
{
if(fr[i]==page[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<n;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<n;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=page[j];
P a g e | 78 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

pf++;
}
display();
}
printf("\n no of page faults:%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("\t%d",fr[i]);
}
Enter total number of frames :
4
Enter the toal number of page reference string :
11
Enter the page reference string :1 2 3 4 2 1 6 5 1 2 3
1
1
1
1
1
1
1
1
1
1
1

-1
2
2
2
2
2
2
2
2
2
2

-1
-1
3
3
3
3
3
3
3
3
3

-1
-1
-1
4
4
4
6
5
5
5
5

no of page faults:6

RESULT:
Thus the C programs for page replacement algorithms are entered and verified.

P a g e | 79 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.9

DISK SCHEDULING

DATE:
AIM:
1.
2.
3.
4.

To implement various disk scheduling algorithm


FCFS
SSTF
SCAN
C-SCAN
FCFS

ALGORITHM:
1: read the number of cylinders
2: Enter the values of cylinders
3: read the starting head position
4: enter the starting head position
5:
enter the order of visiting the cylinder
6:
b+=abs(h-a[i]);
h=a[i];
7: finally calculate the seek time
8:end processing
PROGRAM:
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
//#include<process.h>
int h,a[20],b=0,i,j,n;
main( )
{
// clrscr( );
printf("FIRST COME FIRST SERVE DISK SCHEDULING\n");
printf("\nEnter the Number of Requests : ");
scanf("%d",&n);
printf("\nEnter the requests : \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nEnter the Starting Head Position : ");
scanf("%d",&h);
printf("\nThe Order of Visiting the Cylinders:\n");
for(i=1;i<=n;i++)
printf("\t%d",a[i]);
for(i=1;i<=n;i++)
{
b+=abs(h-a[i]);
h=a[i];
}
printf("\nThe Seek Time is %d\n",b);
P a g e | 80 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

// getch( );
}
[staff@localhost diskpgm]$ cc -o p fcfs.c
[staff@localhost diskpgm]$ ./p
Output:
Enter the Number of Requests : 4
Enter the requests :
5
1
6
2
Enter the Starting Head Position : 4
The Order of Visiting the Cylinders:
5
1
6
2
The Seek Time is 14

P a g e | 81 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

SSTF
ALGORITHM:
1: read the number of cylinders
2: Enter the values of cylinders
3: read the starting head position
4: enter the starting head position
5:
enter the order of visiting the cylinder
6:
seek=seek+abs(hr-d[i]);
hr=d[i];
7: then calculate the seek time
8:
then declare the sstf function
9:
h=a[c];
d[z]=a[c];
z++;
a[c]=9999;
10:
return(0)
11:
end processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int sstf();
int seek=0,t1,t2,n,a[20]={{0}},z=1,d[20]={{0}},h=0;
void main()
{
int i,j,hr;
clrscr();
printf("\t\t SHORTEST SEEK TIME FIRST SCHEDULING \n\n");
printf("Enter the Number of requests : ");
scanf("%d",&n);
printf("\nEnter the requests : \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nEnter the Starting Head Position : ");
scanf("%d",&h);
hr=h;
printf("\nThe Order of Visiting the Cylinders : \n");
for(i=1;i<=n;i++)
sstf();
printf("\n");
for(i=1;i<=n;i++)
{
seek=seek+abs(hr-d[i]);
hr=d[i];
}
printf("\nThe Seek Time is %d",seek);
getch();
}
int sstf()
P a g e | 82 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

{
int b[20]={{0}},c=1,i,j;
for(i=1;i<=n;i++)
b[i]=abs(h-a[i]);
for(i=1;i<=n;i++)
if(b[c]>b[i])
c=i;
printf("\t%d",a[c]);
h=a[c];
d[z]=a[c];
z++;
a[c]=9999;
return(0);
}

OUTPUT:
Enter the Number of requests : 4
Enter the requests :
5
1
6
2
Enter the Starting Head Position : 4
The Order of Visiting the Cylinders :
5
6
2
1
The Seek Time is 7

P a g e | 83 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

SCAN
ALGORITHM:
1: read the ending position of the disc
2: Enter the number of cylinders
3: enter the starting head position
4: enter the order of visiting the cylinder
5:
right=right+abs(h-a[i]);
h=a[i];
6:
right=right+abs(h-end);
h=end;
7: finally calculate the seek time
8:
end processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,req[30],head,i,t,j,move,n,ch,g[30],c,h,cy,s[15];
clrscr();
printf("Disk Scheduling - SCAN");
printf("\nTotal number of cylinders: ");
scanf("%d",&cy);
printf("Enter the current head position: ");
scanf("%d",&head);
h=head;
printf("Enter the number of requests:");
scanf("%d",&n);
move=0;
printf("Enter the requests (1 to %d):\n",cy);
req:
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
s[i]=req[i];
if(req[i]>cy||req[i]<1)
{
printf("Enter requests only between 1-%d..",cy);
goto req;
}
}
printf("\nCurrent\tNext\tNo of Tracks traversed\n");
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(s[i]>s[j])
{
t=s[i];
P a g e | 84 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

s[i]=s[j];
s[j]=t;
}
for(i=0;i<n;i++)
if(s[i]>h)
break;
x=i;
for(c=0;i<n;i++)
{
printf("\n%d\t%d\t%d",head,s[i],abs(head-s[i]));
move+=abs(head-s[i]);
head=s[i];
g[c++]=s[i];
}
move+=abs(cy-head);
printf("\n%d\t%d\t%d",head,cy,abs(head-s[i]));
head=cy;
for(i=x-1;i>=0;i--)
{
printf("\n%d\t%d\t%d",head,s[i],abs(head-s[i]));
move+=abs(head-s[i]);
head=s[i];
g[c++]=s[i];
}
printf("\n\nOrder of servicing:%d ",h);
for(i=0;i<n;i++)
printf("%d ",g[i]);
printf("\n\nTotal Head Movements: %d",move);
getch();
}
OUTPUT:
Total number of cylinders: 7
Enter the current head position: 4
Enter the number of requests:4
Enter the requests (1 to 7):
5
1
6
2
Current Next
4
5
6
7
2

5
6
7
2
1

No of Tracks traversed

1
1
6
5
1

Order of servicing:4 5 6 2 1
Total Head Movements: 9
P a g e | 85 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

C-SCAN
ALGORITHM:
1: read the ending position of the disc
2: Enter the number of cylinders
3: enter the starting head position
4: enter the order of visiting the cylinder
5:
right=right+abs(h-a[i]);
h=a[i];
6:
right=right+abs(h-end);
h=end;
7: finally calculate the seek time
8:end processing
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,req[30],head,i,t,j,move,n,ch,g[30],c,h,cy,s[15];
clrscr();
printf("Disk Scheduling - CSCAN");
printf("\nTotal number of cylinders: ");
scanf("%d",&cy);
printf("Enter the current head position: ");
scanf("%d",&head);
h=head;
printf("Enter the number of requests:");
scanf("%d",&n);
move=0;
printf("Enter the requests (1 to %d):\n",cy);
req:
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
s[i]=req[i];
if(req[i]>cy||req[i]<1)
{
printf("Enter requests only between 1-%d..",cy);
goto req;
}
}
printf("\nCurrent\tNext\tNo of Tracks traversed\n");
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(s[i]>s[j])
{
t=s[i];
P a g e | 86 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

s[i]=s[j];
s[j]=t;
}
for(i=0;i<n;i++)
if(s[i]>h)
break;
x=i;
for(c=0;i<n;i++)
{
printf("\n%d\t%d\t%d",head,s[i],abs(head-s[i]));
move+=abs(head-s[i]);
head=s[i];
g[c++]=s[i];
}
move+=abs(cy-head);
printf("\n%d\t%d\t%d",head,cy,cy-head);
move+=cy;
printf("\n%d\t%d\t%d",cy,0,cy);
head=0;
for(i=0;i<x;i++)
{
printf("\n%d\t%d\t%d",head,s[i],abs(head-s[i]));
move+=abs(head-s[i]);
head=s[i];
g[c++]=s[i];
}
printf("\n\nOrder of servicing:%d ",h);
for(i=0;i<n;i++)
printf("%d ",g[i]);
printf("\n\nTotal Head Movements: %d",move);
getch();
}

P a g e | 87 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT:
Total number of cylinders: 7
Enter the current head position: 4
Enter the number of requests:4
Enter the requests (1 to 7):
5
1
6
2
Current Next
4
5
6
7
0
1

5
6
7
0
1
2

No of Tracks traversed

1
1
1
7
1
1

Order of servicing:4 5 6 1 2
Total Head Movements: 12

RESULT:
Thus the various disk scheduling algorithm has been entered and verified.

P a g e | 88 OPERATING SYSTEMS LABORATORY

DEPARTMENT OF CSE

You might also like