0% found this document useful (0 votes)
250 views

Operating System Programs

The document contains details about the Operating Systems Laboratory course conducted in the Department of Computer Science and Engineering at Sri Manakula Vinayagar Engineering College. It includes a list of 12 experiments related to Linux commands, shell programming, system calls, process scheduling, synchronization, memory management, virtual memory management, file systems and real-time operating systems. For each experiment, the document provides the title, aim and procedures to be followed in the lab.

Uploaded by

Lavitha Krishnan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
250 views

Operating System Programs

The document contains details about the Operating Systems Laboratory course conducted in the Department of Computer Science and Engineering at Sri Manakula Vinayagar Engineering College. It includes a list of 12 experiments related to Linux commands, shell programming, system calls, process scheduling, synchronization, memory management, virtual memory management, file systems and real-time operating systems. For each experiment, the document provides the title, aim and procedures to be followed in the lab.

Uploaded by

Lavitha Krishnan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 93

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

(JUNE 2019-NOV 2019)

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 P51

Prepared By :

Dr. E.KODHAI, ASSO.PROF/CSE


Mr. A.SWAMINATHAN, AP/MCA
Mr. AROKIARAJ ST.HUBERT 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. Simulations of UNIX/Linux commands like ls, grep, etc.
6. Simulation of processes scheduling algorithms.
7. Simulation of synchronization problems using Semaphore.
8. Simulation of basic memory management schemes.
9. Simulation of virtual memory management schemes.
10. Simulation of disk scheduling algorithms
11. Simulation of file systems.
12. 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 TITLE OF EXPERIMENTS
1. STUDY OF OS LINUX COMMANDS
2. SHELL PROGRAMMING
2.a. BIGGEST OF TWO NUMBERS
2.b. LEAP YEAR OR NOT
2.c. EVEN OR ODD NUMBER
2.d. SUM OF N NUMBERS
2.e. FACTORIAL OF A NUMBER
3 . SYSTEM CALLS
3.a. CREATE A CHILD PROCESS USING FORK
3.b. FORK AND WAIT SYSTEM CALL
3.c. CREATE A CHILD PROCESS USING SLEEP
3.d. DIRECTORY SYSTEM CALL
3.e. FILE SYSTEM CALL USING OPEN AND CLOSE
4 . SIMULATION OF LINUX COMMANDS
4.a. SIMULATION OF GREP
4.b. SIMULATION OF ls COMMAND
5 . PROCESSES SCHEDULING
5.a. IMPLEMENTATION OF FCFS SCHEDULING
5.b. IMPLEMENTATION OF SJF SCHEDULING ZERO ARRIVALTIME
5.c. IMPLEMENTATION OF SJF SCHEDULING NON ZERO ARRIVALTIME
5.d. IMPLEMENTATION OF PRIORITY SCHEDULING ZERO ARRIVALTIME
5.e. IMPLEMENTATION OF PRIORITY SCHEDULING NON ZERO ARRIVALTIME
5.f. IMPLEMENTATION OF RR SCHEDULING
6 . SEMAPHORE
6.a. PRODUCER AND CONSUMER PROBLEM
6.b. DINING PHILOSOPHER'S PROBLEM
6.c. READERS WRITER PROBLEM USING SEMAPHORE
7. DEADLOCK PREVENTION
7 SIMULATE ALGORITHM FOR DEADLOCK PREVENTION
8 . MEMORY MANAGEMENT
8.a. IMPLEMENTATION OF FIRST FIT
8.b. IMPLEMENTATION OF BEST FIT
8.c. IMPLEMENTATION OF WORST FIT
9 . PAGE REPLACEMENT
9.a. FIFO PAGE REPLACEMENT ALGORITHM
9.b. LRU PAGE REPLACEMENT ALGORITHM
9.c. OPTIMAL PAGE REPLACEMENT ALGORITHM
10 . DISK SCHEDULING
10.a. FCFS
10.b. SSTF
10.c. SCAN
10.d. C-SCAN

P a g e | 3 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.1 STUDY OF OS LINUX COMMANDS


DATE:

AIM:
To study the basic commands in Linux.

COMMANDS:

1. Calendar

NAME : calendar
(i) SYNTAX : cal
DESCRIPTION : Displays a simple calendar. If arguments are not
Specified, the current month is displayed.
EXAMPLE : cal
OUTPUT :

June 2014
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

(ii) SYNTAX : cal year


DESCRIPTION : Displays calendar of that year
EXAMPLE : cal 2012
OUTPUT :

January February March


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

April May June


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

P a g e | 4 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

July August September


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

October November December


Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 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 :

July August September


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

(iv) SYNTAX : cal month year


DESCRIPTION : Displays the calendar for corresponding month and year .
EXAMPLE : cal 4 2012
OUTPUT :
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 : DATE- print or set the system date and time


(i) SYNTAX : date
DESCRIPTION : Display the current time in the given format or set the system date.

P a g e | 5 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT : Mon Jul 23 12:17:50 IST 2012

(ii) SYNTAX : date +% H


DESCRIPTION : Display the current hour.
OUTPUT 12

(iii) SYNTAX : date +% h


DESCRIPTION : Display the current month name.
OUTPUT : Jul

(iv) SYNTAX : date +% m


DESCRIPTION : Display the current month number.
OUTPUT 7

(v) SYNTAX : date +% a


DESCRIPTION : Display the abbreviated weekday name.
OUTPUT : Mon

(vi) SYNTAX : date +% y


DESCRIPTION : Display the current year.
OUTPUT 12

(vii) SYNTAX : date +% S


DESCRIPTION : Display the current second.
OUTPUT 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
~
~

P a g e | 6 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

INSERT
4. ls

NAME : LIST – list directory contents


(i) SYNTAX : ls
DESCRIPTION : List information about the Files (the current directory by default).
OUTPUT :
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 emp.sh
digit.sh cse.txt case.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 emp.sh
greatest.sh mouse.txt num.sh

5. cp
NAME : cp – copy files and directories
SYNTAX : cp fi f2
DESCRIPTION : Copies f1 to f2
EXAMPLE : cp cse.txt new.txt

6. rm

NAME : rm – remove files


SYNTAX : rm filename
DESCRIPTION : This command removes each specified file.
EXAMPLE : rm cse.txt

P a g e | 7 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

7. mv

NAME : mv – move(rename) files


SYNTAX : mv f1 f2
DESCRIPTION : Renames Source to Destination
EXAMPLE : mv new.txt cse.txt

8. mkdir

NAME : mkdir – makes directory


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

9. rmdir

NAME : rmdir – removes directory


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

10. Pwd

NAME : 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

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 : cat- concatenate & open files and print on the standard output
(i) SYNTAX : cat > filename
DESCRIPTION : This command is used to open a new file.
EXAMPLE : 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 : cat filename


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

(iii) SYNTAX : cat f1 f2 > f3


DESCRIPTION : To concatenate f1 and f2 save in f3
EXAMPLE : cat a.txt b.txt > c.txt
OUTPUT :

a.txt=>
NAME : ZZZZ
ROLL NO: XX

b.txt=>
COLLEGE.SMVEC

c.txt=>
NAME : ZZZZ
ROLL NO: XX
COLLEGE.SMVEC

(iv) SYNTAX : cat –n filename


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

(v) SYNTAX : cat f1 >> f2


DESCRIPTION : To redirect the data from one file to another.
EXAMPLE : cat sample.txt new.txt
OUTPUT : cat new.txt

sample. txt=>
hai
how are u?

new. txt=>
hai
how are u?

13. Whoami
NAME : Displays the current user login and identity.

P a g e | 9 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

SYNTAX : whoami
OUTPUT : csea08

14. Man

NAME : man – help command


SYNTAX : man command
DESCRIPTION : Displays the description of a command
EXAMPLE : man rm

15. Head

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

16. Tail

NAME : tail
SYNTAX : tail filename
DESCRIPTION : Displays the last ten lines in the file.
EXAMPLE : tail fruits.txt
OUTPUT : 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 : sort
(i) SYNTAX : sort filename
DESCRIPTION : Sorts the content of the file in ascending order.
EXAMPLE : sort names.txt
OUTPUT :
Arun
Balu
Chandra
David
Edwards

(ii) SYNTAX : sort –r filename


DESCRIPTION : Sorts the content of the file in descending order.
EXAMPLE : sort –r names.txt
OUTPUT :
Edwards
David
Chandra
Balu
Arun

19. Who
NAME : who
SYNTAX : who
DESCRIPTION : Displays all the users currently logged it.
OUTPUT :
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

P a g e | 11 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DESCRIPTION : Displays the detailed information about the system users.


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 : last
SYNTAX : last
DESCRIPTION : Displays the list of last logged-in users for a month.
OUTPUT :

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 : and - &&


SYNTAX : cmd1 && cmd2
DESCRIPTION : Used to combine more than one commands.
EXAMPLE : whoami && date
OUTPUT : 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 : or - ||
SYNTAX : cmd1 || cmd2
DESCRIPTION : Displays the output for one command which is true.
EXAMPLE : whoami || date
OUTPUT : csea23

24. . Alias
NAME : alias
SYNTAX : alias name="value"
DESCRIPTION : To create simple names or abbreviations for commands
EXAMPLE : alias p="pwd"
p
OUTPUT : /home/mca1

25. Edit
NAME : edit
SYNTAX : vi filename
DESCRIPTION : Edits the content of the file. To edit press I and to save press esc:wq
EXAMPLE : 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 : touch
SYNTAX : touch filename
DESCRIPTION : Creates an empty file.
EXAMPLE : touch hello.txt
OUTPUT :

BEFORE EXECUTION : ls

Sample.txt welcome.txt

AFTER EXECUTION : ls

P a g e | 13 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

Sample.txt welcome.txt hello.txt

28. Uniq

NAME : uniq
SYNTAX : uniq option filename1 filename2
DESCRIPTION : Discard all but one of successive identical lines from filename1 to filename2
EXAMPLE : uniq –d a.txt b.txt
OUTPUT :
BEFORE EXECUTION : cat > a.txt
smvec
smvec
manakula
vinayagar
AFTER EXECUTION : cat b.txt
smvec

29. Semicolon

NAME : Semicolon (;)


SYNTAX : cmd1 ; cmd2; cmd3
DESCRIPTION : Similar to ‘and’ command which combines more than one command.
EXAMPLE : whoami && date
OUTPUT : csea23
Mon Jul 23 12:22:54 IST 2012

30. Echo

NAME : echo – displays a line of text.


SYNTAX : echo “…….”
DESCRIPTION : Displays the statement within double quotes.
EXAMPLE : echo “hai”
OUTPUT : hai

31. Word Count

NAME : wc – word count


(i) SYNTAX : wc filename
DESCRIPTION : Displays the number of lines, words and characters in files.
EXAMPLE : wc a.txt
OUTPUT : a.txt=>
hai
how are u

2 lines, 4 words, 12 characters.

(ii) SYNTAX : wc -l filename


DESCRIPTION : Displays the number of lines in files.
EXAMPLE : wc –l a.txt
OUTPUT : a.txt=>
hai

P a g e | 14 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
how are u

2 lines

(iii) SYNTAX : wc -m filename


DESCRIPTION : Displays the number of characters in files.
EXAMPLE : wc –m a.txt
OUTPUT : a.txt=>
hai
how are u

12 characters

(iv) SYNTAX : wc -w filename


DESCRIPTION : Displays the number of words in files.
EXAMPLE : wc –w a.txt
OUTPUT : a.txt=>
hai
how are u

4 words

32. Grep

NAME : grep
(i) SYNTAX : grep pattern filename
DESCRIPTION : To search for a regular expression or a pattern in a file
EXAMPLE : grep apple b.txt
OUTPUT : 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 : read – reads a value(s)
SYNTAX : read identifier
DESCRIPTION : Reads a value(s)
EXAMPLE : read a
OUTPUT 10
read a

34. Fgrep
NAME : fgrep
(i) SYNTAX : fgrep pattern f1 f2
DESCRIPTION : To search for a regular expression or a pattern in two files
EXAMPLE : fgrep 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

(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

P a g e | 16 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

DESCRIPTION : To search for a regular expression or a pattern in two files and


displays the searched content along with the line and line number, if
found.
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

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 : Pipe - |
SYNTAX : cmd1 | cmd2 | cmd3
DESCRIPTION : Makes the output of one command as input for another command.
EXAMPLE : date | wc -w
OUTPUT 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 : write
SYNTAX : write login_name
DESCRIPTION : Used to communicate with other logged in users.
EXAMPLE : write csea25

P a g e | 17 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

38. Mail

NAME : Mail
(i) SYNTAX : mail login_name
DESCRIPTION : Used to send mail to a user.
EXAMPLE : write csea25

(ii) SYNTAX : mail


DESCRIPTION : Used to view the mails in the mailbox.

39.Terminal Name

NAME : tty
SYNTAX : tty
DESCRIPTION : Used to display the terminal path name.
OUTPUT : /dev/pts/23

40. Expression

NAME : expr
SYNTAX : `expr expression`
DESCRIPTION : Used to evaluate an expression
EXAMPLE : echo `expr 10 + 10`
OUTPUT 20

41. . free

NAME : free
SYNTAX : free
DESCRIPTION : 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 : ping
(i) SYNTAX : ping Ip-address
DESCRIPTION : 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

P a g e | 18 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

PING 172.17.10.111 (172.17.10.111) 56(84) bytes of data.


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 : Password
SYNTAX : passwd
DESCRIPTION : Used to change the password.
EXAMPLE : passwd
OUTPUT : 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 : Uname
SYNTAX : uname
DESCRIPTION : Used to display the name of the system being used.
OUTPUT : Linux

46. Sequence

NAME : seq
SYNTAX : seq starting_value ending_value
DESCRIPTION : print a sequence of numbers
EXAMPLE : seq 1 4
OUTPUT :
1
2
3
4

47. Open and Fill

NAME : Open and Fill


SYNTAX : vi +filename

DESCRIPTION : Used to open a file and position the cursor at the first line.
EXAMPLE : vi +b.txt

P a g e | 19 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

OUTPUT :
hi bala

:wq
"new.txt" 7L, 42C written

48. Compare

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

49. Type

NAME : Type
SYNTAX : type filename
DESCRIPTION : To view the content of the file.
EXAMPLE : type names.txt
OUTPUT :
Arun
Balu
Chandra
David
Edwards

50. Basic Calculator

NAME : Basic Calculator


SYNTAX : bc
DESCRIPTION : To open the basic calculator.
EXAMPLE : bc
OUTPUT : 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 SHELL PROGRAMMING


DATE:

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 N NUMBERS

AIM:
To find the sum of n natural numbers of a given number.

ALGORITHM:
1. Start the program.
2. Get the value for num.
3. Initialize the variable i=1 and sum=0.
4. Check whether i is less than n, if so sum the number one by one by doing the following
steps.otherwise goto step 6.
sum=$(( $sum + $i ))
i=$(( $i + 1 ))Goto step 4.
5. Print the value of sum of digit.
6. Stop the program execution.

PROGRAM:
echo -n "Enter number : "
read n

# store single digit


i=1
# store number of digit
sum=0
# use while loop to caclulate the sum of all digits
while [ $i -le $n ]
do
sum=$(( $sum + $i )) # calculate sum of digit
i=$(( $i + 1 ))
done
echo "Sum of all digit is $sum"

OUTPUT:

Enter number : 4
Sum of all digit is 10

P a g e | 24 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

FACTORIAL NUMBER

AIM:
To write a program to calculate the factorial of a given number.

ALGORITHM:
1. Get a number
2. Use for loop or while loop to compute the factorial by using the below formula
3. fact(n) = n * n-1 * n-2 * .. 1
4. Display the result.

PROGRAM:
echo "Enter a number"
read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done

echo $fact

OUTPUT:

Enter a number
5
120

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

P a g e | 27 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 | 28 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 | 29 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 | 30 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");
}

OUTPUT:

P a g e | 31 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

[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 | 32 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. Start the program.


2. Initialize the flag variable to zero
3. Get the name of the file and pattern to be searched.
4. Open the file in which the searching is going to be performed in read mode
5. 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 | 33 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 | 34 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. Start the program.
2. Get the name of the directory.
3. Open the directory
4. Read the content of the directory
5. Display the content of the directory.
6. 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++

RESULT:

Thus a C program to simulate ls command of Unix is written and executed successfully.

P a g e | 35 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 | 36 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 | 37 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
PROCESS BRUSTTIME ARRIVALTIME WAITINGTIME TURNARROUNDTIME
p0 8 0 0 8
p1 3 0 8 11
p2 4 0 11 15
p3 2 0 15 17
p4 6 0 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 | 38 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 | 39 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 BRUSTTIME WAITINGTIME TURNARROUNDTIME


p4 3 0 3
p1 6 3 9
p3 7 9 16
p2 8 16 24

P a g e | 40 OPERATING SYSTEMS LABORATORY 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 | 41 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 | 42 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 BRUSTTIME ARRIVALTIME WAITINGTIME TURNARROUNDTIME


p1 8 0 0 8
p2 4 1 7 11
p4 5 3 9 14
p3 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 | 43 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 | 44 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 | 45 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 BRUSTTIME PRIORITY WAITINGTIME TURNARROUNDTIME


p2 1 1 0 1
p5 5 2 1 6
p1 10 3 6 16
p3 2 4 16 18
p4 1 5 18 19

AVERAGE WAITING TIME= 8.200000


AVERAGE TURNARROUND TIME=12.000000

P a g e | 46 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 | 47 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 | 48 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 | 49 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 | 50 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 | 51 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 WAITINGTIME TURNARROUNDTIME


p0 24 0 6 30
p1 3 0 4 7
p2 3 0 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 | 52 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 BRUSTTIME ARRIVAL TIME WAITINGTIME TURNARROUNDTIME


p0 24 0 6 30
p1 3 1 3 6
p2 3 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 | 53 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 | 54 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 | 55 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 | 56 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
OUTPUT:
1.producer 2.consumer 3.both 4.Exit
choice
1
producer
produced item is 7
full=1 empty=4

1.producer 2.consumer 3.both 4.Exit


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

1.producer 2.consumer 3.both 4.Exit


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

1.producer 2.consumer 3.both 4.Exit


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

1.producer 2.consumer 3.both 4.Exit


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

1.producer 2.consumer 3.both 4.Exit


choice
3
consumer
consumerd item is 3
full=0 empty=5
producer
produced item is 1
full=1 empty=4

1. producer 2.consumer 3.both 4.Exit


Choice 4

P a g e | 57 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

P a g e | 58 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");
for(i=0;i<5;i++)

P a g e | 59 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

{
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 | 60 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 Spoon
1 t s s
2 t s s
3 t s s
4 t s s
5 t s s
Enter the option :
1.Exit
2.Hungry
3.Thinking

Enter your choice :2

Enter which philosophers is Hungry :1

State of each Philosepher :


Philno: Think/Eat Status Spoon
1 e s n
2 t s s
3 t s s
4 t s s
5 t s n
Enter the option :
1.Exit
2.Hungry
3.Thinking 2

Enter which philosophers is Hungry :2

State of each Philosepher :


Philno: Think/Eat Status Spoon
1 e s n
2 h w s
3 t s s
4 t s s
5 t s n
Enter the option :
1.Exit
2.Hungry
3.Thinking 2

P a g e | 61 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 Spoon
1 e s n
2 h w s
3 t s n
4 e s n
5 t s n
Enter the option :
1.Exit
2.Hungry
3.Thinking :2

Enter which philosophers is Hungry :5

State of each Philosepher :


Philno: Think/Eat Status Spoon
1 e s n
2 h w s
3 t s n
4 e s n
5 h w n
Enter the option :
1.Exit
2.Hungry
3.Thinking :3

Enter which Philosopher is Thinking :1

State of each Philosepher :


Philno: Think/Eat Status Spoon
1 t s n
2 e s n
3 t s n
4 e s n
5 h w s
Enter the option :
1.Exit
2.Hungry
3.Thinking :3

Enter which Philosopher is Thinking :4

State of each Philosepher :


Philno: Think/Eat Status Spoon
1 t s n
2 e s n
3 t s s
4 t s n
5 e s n

P a g e | 62 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 Spoon
1 t s s
2 t s s
3 t s s
4 t s n
5 e s n
Enter the option :
1.Exit
2.Hungry
3.Thinking :3

Enter which Philosopher is Thinking :5

State of each Philosepher :


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

P a g e | 63 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 | 64 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 | 65 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 | 66 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.7 SIMULATE ALGORITHM FOR DEADLOCK PREVENTION

AIM: To write a C program to implement bankers algorithm for dead lock avoidance

ALGORITHM:

1: Start the Program


2: Get the values of resources and processes.
3: Get the avail value.
4: After allocation find the need value.
5: Check whether its possible to allocate. If possible it is safe state
6: If the new request comes then check that the system is in safety or not if we allow the request.
7: Stop the execution

SOURCE CODE:
#include
#include
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++) for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p); printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}

P a g e | 67 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

}
block[p]=0; run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++) for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
Else
{

P a g e | 68 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}

OUTPUT:
Enter the no of resources: 4
Enter the no of resource classes: 3
Enter the total existed resources in each class: 3 2 2
Enter the allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request: 2
Enter the requested resource: 1 1 2
Enter the processes which are n blocked or running: Process 1: 1 2
Process 3: 1 0
Process 4: 1 0
Deadlock will occur

REUSLT:
Thus the C program to implement algorithm for deadlock prevention has been entered and verified.

P a g e | 69 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX. NO.8 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 a g e | 70 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

{
p[j].mem=i+1;
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 MEMORYPARTISION MEMORY ALLOCATION


212 100 100
417 500 1
112 200 3
420 300 300
0 600 2

RESULT:

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

P a g e | 71 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++)

P a g e | 72 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
a[i].mem=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;
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 MEMORY PARTISION MEMORY ALLOCATION


212 100 100
417 200 3
112 300 1
420 500 2
0 600 4

RESULT:

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

P a g e | 73 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++)
{

P a g e | 74 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

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 MEMORY PARTISION MEMORY ALLOCATION


212 600 1
417 500 2
112 300 3
420 200 200
0 100 100

RESULT:

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

P a g e | 75 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.9 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 | 76 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 | 77 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 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
6 2 3 4
6 5 3 4
6 5 1 4
6 5 1 2
3 5 1 2

Number of page faults : 9

P a g e | 78 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 | 79 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 | 80 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 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 6 4
1 2 6 5
1 2 6 5
1 2 6 5
1 2 3 5

no of page faults :7

P a g e | 81 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 | 82 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 | 83 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 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 6
1 2 3 5
1 2 3 5
1 2 3 5
1 2 3 5

no of page faults:6

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

P a g e | 84 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

EX.NO.10 DISK SCHEDULING

DATE:

AIM:
To implement various disk scheduling algorithm
1. FCFS
2. SSTF
3. SCAN
4. 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 | 85 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 | 86 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 | 87 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 | 88 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 | 89 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 No of Tracks traversed

4 5 1
5 6 1
6 7 6
7 2 5
2 1 1

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

P a g e | 90 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 | 91 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 | 92 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 No of Tracks traversed

4 5 1
5 6 1
6 7 1
7 0 7
0 1 1
1 2 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 | 93 OPERATING SYSTEMS LABORATORY DEPARTMENT OF CSE

You might also like