You are on page 1of 52

UNIVERSITY

VISION

To be a Center of Excellence of International Repute in Education and Research.

MISSION

To Produce Technically Competent, Socially Committed Technocrats and Administrators


through Quality Education and Research.

DEPARTMENT
VISION

To become a Centre of Excellence in Teaching and Research in the field of Computer


Science and Engineering.

MISSION
To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge.

To carry out research in cutting edge technologies in computer engineering to meet the
requirement of the industry and society.

CSE 284 – Operating Systems Lab


1
List of Programs

1. Study of basic Commands in Linux Operating System


2. Shell programming using control statements
3. Shell programming using loops, patterns, expansions and substitutions
4. Write programs using the following system calls (fork, exec, getpid, exit, wait, close,
stat,
opendir, readdir).
5. Write programs using the I/O system calls (open, read, write, etc).
6. Simulation of Linux commands.
7. Implementation of CPU Scheduling Algorithms(FCFS, SJF, RR, Priorty).
8. Implementation of Page Replacement Algorithms (LRU, OPT, FIFO).
9. Implementation of memory allocation algorithms (First Fit, Best Fit, Worst Fit)
10. Implement the Producer – Consumer problem using semaphores.
11. Simulation of Shared Memory Concept.
12. Implementation of bankers Algorithm.
13. Implementation Disk Scheduling Algorithms

BASIC LINUX COMMANDS


2
EX. NO. 1a WORKING WITH FILES AND DIRECTORIES
AIM:
To study the UNIX commands for accessing files and directories.

COMMANDS:
cat --- for creating and displaying short files
chmod --- change permissions
cd --- change directory
cp --- for copying files
date --- display date
echo --- echo argument
ftp --- connect to a remote machine to download or upload files
grep --- search file
head --- display first part of file
ls --- see what files you have
lpr --- standard print command
more --- use to read files
mkdir --- create directory
mv --- for moving and renaming files
ncftp --- especially good for downloading files via anonymous ftp.
print --- custom print command
pwd --- find out what directory you are in
rm --- remove a file
rmdir --- remove directory
rsh --- remote shell
setenv --- set an environment variable
sort --- sort file
tail --- display last part of file
tar --- create an archive, add or extract files
telnet --- log in to another machine
wc --- count characters, words, lines

1. To create a file.

3
Syntax: $ cat>filename
Example: $ cat>ex1

2. To view the content of the file.


Syntax: $ cat filename
Example: $ cat ex1

3. To append some details with the existing details in the file


Syntax: $ cat>>filename
Example: $ cat>>ex1

4. To concatenate multiple files


Syntax: $ cat file1 file2 > file3
Example: $ cat computer compiler>world

5. To know the list of all files in directory


Syntax: $ ls

6. To copy the file to another file


Syntax: $ cp source destination
Example: $ cp ex1 ex2

7. To rename the file


Syntax: $ mv oldfile newfile
Example: $ mv ex1 ex3

8. To delete a file
Syntax: $ rm filename
Example: $ rm ex1

9. To delete all files


Syntax: $ rm *

10. To create a directory


Syntax: $ mkdir dirname
11. To change the name of the directory
Syntax: $ cd dirname

12. To remove the directory


Syntax: $ rmdir dirname
Example: $ rmdir flower

13. Echo
i. To display the filename starting with single letter
Syntax: $ echo?

ii. To display the filename starting with two letters


Syntax: $ echo??
4
iii. To display the filename starting with the letter f
Syntax: $ echo f*

iv. To display the filename ending with letter f.


Syntax: $ echo *f

14. Present Working Directory


i. To display the present working directory
Syntax: $ pwd

ii. To clear the screen


Syntax: $ tput clear

iii. To calculate the values


Syntax: $ bc

iv. Uname: To know your machine name


-n: Tells machine name in network
Syntax: $ uname –n

v. To display the version number of the OS


Syntax: $ uname –r

15. Head
i. To display first 10 lines
Syntax: $ head filename

ii. To display first 6 characters


Syntax: $ head -6c filename

iii. To display 5 lines from 2 files


Syntax: $ head -5 file1 file2

16. To display last 10 lines


Syntax: $ tail filename
Example: $ tail ex3

17. Word Count


i. To display the number of words in a file
Syntax: $ wc filename
Example: $ wc ex1

ii. To display the number of characters in a file


Syntax: $ wc –c filename
Example: $ wc –c ex1

iii. To display the number of lines


5
Syntax: $ wc –l filename
Example: $ wc –l ex3

18. Line Number


i. To display number of lines with numbers
Syntax: $ nl filename
Example: $ nl ex1

ii. To increment the line number by 5


Syntax: $ nl –i5 filename
Example: $ nl –i5 ex3

19. Sort
i. To reverse and sort the content of file
Syntax: $ sort –r filename
Example: $ sort –r ex1

ii. To sort the content of the file


Syntax: $ sort filename
Example: $ sort ex1

iii. To sort and remove the duplicate


Syntax: $ sort –u filename
Example: $ sort –u ex1

20. VI Editor Commands


i. To compile and run shell program
Syntax: $ sh filename
Example: $ sh odd.sh

ii. To compile a C program


Syntax: $ cc –o filename filename.c

iii. To run a C program


Syntax: $ ./filename

21. To paste the contents of file


Syntax: $ paste filename1, filename2
Example: $ paste ex1, ex2

22. To display file contents page by page


Syntax: $ more filename

Ex. No. 1b General Purpose Utility Commands

AIM:
To work with some of the general purpose utility commands in UNIX.
6
COMMANDS:

1. Calendar:
i. To display the calendar.
Syntax: $ cal

ii. To display the previous, current and next month.


Syntax: $ cal -3

iii. To display the current month starting from Sunday.


Syntax: $ cal –s

iv. To display the current month starting from Monday.


Syntax: $ cal –m

2. Date:
i. To display system date.
Syntax: $ date
Output: Tue Jan 20 10:54:25 IST 2009

ii. To display month only.


Syntax: $ date+%m
Output: 01
iii. To display month name and month
Syntax: $date +%h%m
Output: Jan01
iv. To display month name
Syntax: $ date+%h
Output: Jan

v. To display the time in hours


Syntax: $ date+%H
Output: 10
vi. To display the time in minutes
Syntax: $ date+%M
Output: 53

vii. To display the time in AM or PM


Syntax: $ date+%r
Output: 10: 53:24 AM

viii. To display date of month


Syntax: $ date+%d
Output: 20

3. WHO
i. To display the login details
7
Syntax: $ who
Output: root :0 Jan 20 10:51
cs1010 pts/0 Jan 20 10:51 (172.16.1.72)

ii. To display the login user details


Syntax: $ who am i
Output: cs1010

iii. To display my login id


Syntax: $ logname
Output: cs1010

4. MAN
i. It is used to view more details of all the commands
Syntax: $ man command_name
Example: $ man date

Sample Viva-Voce questions:


1. What is UNIX?
2. How is Unix differ from Linux
3. What is operating system?
4. What are the various components of a computer system?
5. What are the different operating systems?
6. Write a command to create a directory.
7. What command can you use to display the first 3 lines of text from a file and how does it
work?
8. How do you reverse the string?
9. How can you find out what a command does?
10. Write a script that prints out date information in this order: time, day of week, day number,
month, year.
11. Is there a way to erase all files in the current directory, including all its sub-directories, using
only one command?
Experiments addressing COs:
The experiments mentioned above address CO1

SHELL PROGRAMMING

Ex. No. 2a Sum and Average of N Numbers

AIM:
8
To find the sum and average of N numbers using shell programming.

ALGORITHM:

Step 1: Initialize the variables


Set s=0, i=1
Step 2: Read the input
Get the value of n
Step 3: Calculate the sum of n numbers
Get the value of a
Set s = s + a
Step 4: Increment the value of i.
Set i = i+1
Step 5: Go to step 3 if i is less than or equal to n
Step 5: Calculate the average
Set a=s/n
Step 6: Display the sum and average

SAMPLE OUTPUT:

[meera@localhost meera]$ sh sum01.sh


sum of N Numbers
Enter the number:
5
6
3
1
2
4
sum=16
Average=3

Ex. No. 2b Largest and Smallest of Three Numbers

AIM:
To find the largest and smallest of three numbers using shell programming.

9
ALGORITHM:

Step 1: Read the values


Get the value of a, b, c, n
Step 2: If n is equal to 1 then check the largest of three numbers.
Step 2a: If a is greater than b and a is greater than c then print a is greater than b and c.
Step 2b: Else if b is greater than c then print b is greater than a and c.
Step 2c: Else print c is greater than a and b.
Step 3: If n is equal to 2 then check the smallest of three numbers.
Step 3a: If a is smaller than b and a is smaller than c then print a is smaller than b and c.
Step 3b: Else if b is smaller than c then print b is smaller than a and c.
Step 3c: Else print c is smaller than a and b.

SAMPLE OUTPUT:

[meera@localhost meera]$ sh largest.sh


Largest and Smallest of three numbers
Enter the three numbers
638
Choose 1. Largest and 2. Smallest
Enter the choice
1
8 is larger than 6 and 3
[meera@localhost meera]$ sh largest.sh
Largest and Smallest of three numbers
Enter the three numbers
543
Choose 1. Largest and 2. Smallest
Enter the choice
2
3 is smaller than 5 and 4

Ex. No. 2c Leap Year or Not

AIM:
To find the given year is a leap year or not using shell programming.

ALGORITHM:

Step 1: Read the input


10
Get the value of a
Step 2: If a%4 is equal to 0
then print a is a leap year.
Step 3: Else print a is not a leap year.

OUTPUT:
[meera@localhost meera]$ sh leap.sh
Leap Year or Not
Enter the Year
1920
1920 is a leap year
[meera@localhost meera]$ sh leap.sh
Leap Year or Not
Enter the Year
1991
1991 is not a leap year

Ex. No. 2d Odd or Even

AIM:
To find whether the given number is odd or even using shell programming.

ALGORITHM:

Step 1: Read the input.


Get the value of n.
Step 2: If the value of n%2 is equal to 0
then print the value n is a even number.
Step 3: Else print the value n is an odd number.

SAMPLE OUTPUT:

[meera@localhost meera]$ sh oddeven.sh


Odd or Even
Enter the value of n
6
6 is a even Number

Ex. No. 2e Positive or Negative

AIM:
To write a program to identify whether the given number is positive or negative using shell
programming.

ALGORITHM:
11
Step 1: Read the input
Get the value of n
Step 2: If n is greater than 0
then print n is a positive number.
Step 3: Else if n is less than 0
then print n is a negative number.
Step 4: Else print n is equal to zero.

SAMPLE OUTPUT:

[meera@localhost meera]$ sh positivenegative.sh


Positive or negative
Enter the number
5
5 is a positive number

Ex. No. 2f Sum of digits

AIM:
To calculate the sum of digits using shell programming.

ALGORITHM:

Step 1: Initialize the variables


Set i=0, sum=0
Step 2: Read the input
Get the value of l, n
Step 3: Calculate the sum of digits
Set a=n%10
Set sum=a+sum
Set n=n/10
Set i=i+1
Step 4: Go to step 3 until i is less than l else go to step 5.
Step 5: Print sum

OUTPUT:

[meera@localhost meera]$ sh sumofdigits.sh


Sum of digits
Enter the number of digits
3
Enter the number
265
Sum of the digits: 13

12
Ex. No. 2g Fibonacci Series

AIM:
To write a shell program to find the Fibonacci series.

ALGORITHM:

Step 1: Read the input.


Get the value of n.
Step 2: Initialize the variables.
Set a= -1, b=1, i=0
Step 3: Calculate the series
Set c=a+b
Print c
Set a=b
Set b=c
Set i=i+1
Step 4: If i is less than n go to step 3 else go to break

OUTPUT:

[meera@localhost meera]$ sh fibonacci.sh


Fibonacci Series
Enter the value of n:
5
Fibonacci series upto 5:
0
1
1
2
3

Ex. No. 2h Factorial of a Number


AIM:
To find the factorial of the given number using shell programming.

ALGORITHM:

Step 1: Read the input


Get the value of n
Step 2: Initialize the variables
Set f=1, i=1
Step 3: If i is less than or equal to n
13
Set f=f*i
Set i=i+1
Step 4: Repeat the step 3 until the condition comes false.
Step 5: Print the value of f.

SAMPLE OUTPUT:

[meera@localhost meera]$ sh factorial.sh


Factorial of a number
Enter a number
5
Factorial of 5 = 120

Ex. No. 2i Student’s Mark List

AIM:
To calculate the mark list of a student using shell programming.

ALGORITHM:

Step 1: Read the input


Get the value of name, roll, a, b, c, d, e
Step 2: Calculate the sum and average
Set sum=a+b+c+d+e
Set avg=sum/5
Step 3: Print name, roll, a, b, c, d, e, sum, avg.
Step 4: If avg is less than 100 and avg is greater than 85 then
Print “Grade A”
Step 5: Else If avg is less than 85 and avg is greater than 75 then
Print “Grade B”
Step 6: Else If avg is less than 75 and avg is greater than 55 then
Print “Grade C”
Step 7: Else print “Fail”

OUTPUT:

[meera@localhost meera]$ sh marklist.sh


Student Marklist
Enter the student name
aaa
Enter the roll number
111
Enter the marks
89 78 56 84 53
Name: aaa
14
Roll Number: 111
Marks
Mark1: 89
Mark2: 78
Mark3: 56
Mark4: 84
Mark5: 53
Total = 360
Average= 72
Grade C

Ex. No. 2j Sum of Series


AIM:
To write a program to find the sum of series using shell programming.
S=12+22+32+…+n2
ALGORITHM:

Step 1: Read the input.


Get the value for n
Step 2: Initialize the variables.
Set i=1, s=0
Step 3: check if i is less than or equal to n
Step 4: If yes, perform the following process
Set a=i*i
Set s=s+a
Set i=i+1
Step 5: Repeat step 4 until I is less than or equal to n. Else go to step 6.
Step 6: Print sum.

OUTPUT:
[meera@localhost meera]$ sh sumofseries.sh
Sum of Series
Enter the Number:
5
Sum= 55

Ex. No. 2k Sequence of Odd Numbers

AIM:
To write a program to find the sequence of odd numbers present up to given n number.

ALGORITHM:

Step 1: Read the input.


Get the value of n.
Step 2: Initialize the variables.
Set i=1, j=1
15
Step 3: check if j is less than of equal to n or not.
Step 4: If yes, perform the following steps
Step 4a: If i%2 is not equal to zero, then print i
Step 4b: Increment the value of i, j.
Set i=i+1
Set j=j+1
Step 5: Repeat the step 4 until j is less than or equal to n.

OUTPUT:

[meera@localhost meera]$ sh sequenceofoddnumbers.sh


Sequence of Odd Numbers
Enter the value of n:
6
Odd number upto 6:
1
3
5

Ex. No. 2l Prime or Composite

AIM:
To write a shell program to find whether the number is prime or composite.

ALGORITHM:

Step 1: Read the input.


Get the value of n.
Step 2: If n is equal to 0 then
Print “Enter another number”
Step 3: Else if n is equal to 1 then
Print n is neither prime nor composite
Step 4: Else if n is equal to 2 then
Print n is even prime.
Step 5: Else, perform the following process.
Step 5a: Initialize the variables.
Set K=0, i=2
Step 5b: Check if i is less than n.
Step 5c: If yes, perform the following step.
i. If n%i is equal to 0 then set k=1 and break the loop.
ii. Increment the value of i.
Set i=i+1
Step 5d: Repeat the step 5c until i is less than n.
Step 5e: If k is equal to 0 then print n is prime.
Step 5f: Else print n is composite.

OUTPUT:

16
[meera@localhost meera]$ sh primecomposite.sh
Prime or composite
Enter the value of n:
6
6 is composite
[meera@localhost meera]$ sh primecomposite.sh
Prime or composite
Enter the value of n:
3
3 is prime

Ex. No. 2m Multiple Choice using Switch Case-Arithmetic operation

AIM:
To write a shell program to perform the arithmetic operation using switch case.

ALGORITHM:

Step 1: Initialize the variables.


Set n=1
Step 2: Check if n is less than 6
Step 3: If yes, perform the following steps
Step 4: Read the input
Get the value of m, a, b.
Step 5: Using switch case, read the choice m.
Step 5a: If the choice is 1 then perform addition
Set c=a+b
Print c
Step 5b: If the choice is 2 then perform subtraction
Set c=a-b
Print c
Step 5c: If the choice is 3 then perform multiplication
Set c=a*b
Print c
Step 5d: If the choice is 4 then perform division
Set c=a/b
Print c
Step 5e: If the choice is 5 then perform modulo
Set c=a%b
Print c
Step 6: Increment the value of n
Step 7: Repeat the process from step 4 until n is less than 6.

OUTPUT:

Enter ur choice
1----Addition
17
2----Subtraction
3----Multiplication
4----Division
5----Modulo
1
Enter ur number
56
23
The sum of 56 and 23 is 79

Ex. No. 2n Length of the String

AIM:
To write a shell program to find the length of the string.

ALGORITHM:

Step 1: Read the input.


Get the string for str.
Step 2: Count the length of the given string using the command wc.
Step 3: Store the result in the variable len.
Step 4: Print len.

OUTPUT:

[meera@localhost meera]$ sh lengthofstring.sh


Length of the string
Enter the string
have
length of the given string have is 5
Ex. No. 2o Pattern matching

AIM:
To write a shell program to perform various pattern search using file.

ALGORITHM:

Step 1: Initialize the variable.


Set a=0
Step 2: If a is equal to 0 then perform the following process.
Step 3: Read the input.
Get the value of choice
Step 4: If the choice is 1, create a file and search for a pattern.
Step 4a: Create and enter the content in a file using cat command.
Step 4b: Get the pattern to search.
Step 4c: Using grep command, search the pattern in the given file and store the result in a new file.
18
Step 4d: Print “Pattern found” and the result file and then count the no. of lines in the result file using wc
command.
Step 5: If the choice is 2, search for a pattern in a particular file.
Step 5a:Read the file to be searched.
Step 5b: Get the pattern to search.
Step 5c: Using grep command, search the pattern in the given file and store the result in a new file.
Step 5d: Print “Pattern found” and the result file and then count the no. of lines in the result file using wc
command.
Step 6: If the choice is 3, search for a pattern in all files.
Step 6a: Get the pattern to search.
Step 6b: Using grep command, search the pattern in all the file and store the result in a new file.
Step 6c: Print “Pattern found” and the result file and then count the no. of lines in the result file using wc
command.
Step 7: If the choice is 4, set a=1 and then continue.
Step 8: Repeat from step 3 until a is equal to 0.

Ex. No. 2p Multiple Choice using Switch Case-Sum of series

AIM:
To write a shell program to perform sum of series using switch case.

ALGORITHM:

Step 1: Read the input.


Get the value for n.
Step 2: Initialize the variables.
Set q=0
Step 3: If q is less than 3, perform the following steps.
Step 4: Increment the value of q.
Set q=q+1
Step 5: If q is equal to 1, then
Step 5a: Initialize the variables
Set s=0, i=1
Step 5b: Read the input.
Get the value of n.
Step 5c: If i is less than or equal to n, then
Set s=s+i
Set i=i+1
Step 5d: Repeat the step 5c until the condition becomes false.
Step 5e: Print s.
Step 6: If q is equal to 2, then
Step 6a: Initialize the variables.
Set s=0, i=1
Step 6b: Read the input
Get the value of n
Step 6c: If i is less than or equal to n then
Set s=s+i
19
Set i=i+2
Step 6d: Repeat the step 6c until the condition becomes false.
Step 6e: Print s.
Step 7: If q is equal to 3, then
Step 7a: Initialize the variables.
Set s=0, i=2
Step 7b: Read the input
Get the value of n
Step 7c: If i is less than or equal to n then
Set s=s+i
Set i=i+2
Step 7d: Repeat the step 6c until the condition becomes false.
Step 7e: Print s.
Step 8: Repeat the process from Step 3 until q is less than 3.

OUTPUT:

[meera@localhost meera]$ sh sumofseries01.sh


Enter the number
6
Enter the choice
1
Sum of series --> 1+2+3+...+n
Sum of series: 21

[meera@localhost meera]$ sh sumofseries01.sh


Enter the number
6
Enter the choice
2
Sum of series --> 1+3+5+...+n
Sum of series: 9

[meera@localhost meera]$ sh sumofseries01.sh


Enter the number
6
Enter the choice
3
Sum of series --> 2+4+...+n
Sum of series: 12

[meera@localhost meera]$ sh sumofseries01.sh


Enter the number
6
Enter the choice
4

20
Ex. No. 2q Palindrome – Reversing a Number

AIM:
To write a shell program to find the palindrome of a given number.
ALGORITHM:

Step 1: Read the input.


Get the value of n.
Step 2: Initialize the variables.
Set i=0, a=n
Step 3: If n is greater than 0, then
Set m=n%10
Set i=i*10+m
Set n=n/10
Step 4: Repeat step 3 until the condition becomes false.
Step 5: Print i.
Step 6: If a is equal to i, then print the number is a palindrome.
Step 7: Else print the number is not a palindrome.
OUTPUT:

[meera@localhost meera]$ sh palindrome.sh


Palindrome-Reversing a Number
Enter the number
123
Reversed Number: 321
The number 123 doesnt form a palindrome

[meera@localhost meera]$ sh palindrome.sh


Palindrome-Reversing a Number
Enter the number
121
Reversed Number: 121
The number 121 form a palindrome

Ex. No. 2r Login Search

AIM:
To write a program to check whether a login is connected or not.

ALGORITHM:

Step 1: Read the input.


Get the value of n where n is the user login name.
Step 2: If the login name is present in the logined list, then print the login name is logged in.
Step 3: Else print the login name is not logged in.

21
OUTPUT:

[meera@localhost meera]$ sh loginsearch.sh


Enter the login number:
CS10141
No the number hasnt logged in

[meera@localhost meera]$ sh loginsearch.sh


Enter the login number:
meera
meera pts/0 Jan 27 10:13 (172.16.1.77)
Yes the number is logged in

Sample Viva-Voce questions:


1. What needs to be done before you can run a shell script from the command line prompt?
2. What is a shell?
3. How do you terminate a shell script if statement?
4. What UNIX operating system command would you use to display the shell's environment
variables?
5. What code would you use in a shell script to determine if a directory exists?
6. How do you access command line arguments from within a shell script?
7. Within a UNIX shell scripting loop construct, what is the difference between the break and
continue?
8. What are some ways to debug a shell script problem?

Experiments addressing COs:

The experiments mentioned above address CO1

SYSTEM CALLS

Ex. No. 3a Fork System Call

AIM:
To write a program to create a child process using the system calls –fork.

ALGORITHM:

Step 1: Include the necessary header files.


Step 2: Get the ID of the current main processor using the function getpid().
22
Step 3: Create a child using fork system call
Set a=fork()
Step 4: The fork() return 0 to a, if a child is created or any other value is returned.
Step 5: If a is equal to 0, then print the child ID using getpid().
Step 6: Else if a is not equal to 0, then print the child ID and the parent ID using getpid() .
Step 7: Else print error message.

OUTPUT:

[meera@localhost meera]$ cc -o fork.out fork.c


[meera@localhost meera]$ ./fork.out

Parent is 10968
HELLO FROM CHILD PROCESS

CHILD ID is 10968
Parent is 10967
HELLO FROM PARENT PROCESS 10967
HELLO FROM CHILD PROCESS 10968

Ex. No. 3b EXEC System Call

AIM:
To write a program to display time and date using exec system call.

ALGORITHM:

Step 1: Include the necessary header file.


Step 2: Using fork () system call, create child process.
Step 3: Display the id of the child process and parent process using getpid() and getppid().
Step 4: Using exec system call, display the time and date
execl(“/bin/date”, “date”, 0)
The first field is directory, 2nd field is syntax, 3rd field is set to null.
Step 5: The exec system call will terminate the child process.

OUTPUT:
23
[meera@localhost meera]$ cc -o exec.out exec.c
[meera@localhost meera]$ ./exec.out
Child Id is 10983
Parent Id is 10982
Tue Jan 27 11:17:10 IST 2009
End of file

Ex. No. 3c STAT System Call

AIM:
To write a program to implement STAT system call.

ALGORITHM:
Step 1: Include the necessary header file.
Step 2: Check the status of the given file.
Step 3: Store the status as integer value in the variable flag.
Step 4: If the argc is not equal to 2 then print filename is not given.
Step 5: Else if flag is equal to -1 then print file does not exist.
Step 6: Else print the file link, devices, inodes, protection, size, ownership, block size and time.

OUTPUT:

[meera@localhost meera]$ cc -o stat.out stat.c


[meera@localhost meera]$ ./stat.out exec.c
File Exists and Filename is givenThe information about the file exec.c

exec.c has 1 link


exec.c has 770 devices
exec.c has 3908662 inodes
exec.c has 33204 protection
exec.c has 0 inode devices
exec.c has 248 size
exec.c has 501 owner
exec.c has 4096 block size
exec.c has 1233035222 time
exec.c has 1233035220 time

Ex. No. 3d Wait System Call

AIM:
To write a program using wait system call.

24
ALGORITHM:
Step 1: Create child process using fork system call.
Step 2: Display the child id and status of the child process.
Step 3: Using exec() system call display the time and date.
Step 4: Return the child ID as the waiting ID using wait() system call.
cid=wait(&status)
Step 5: Display waiting ID and status

OUTPUT:
[meera@localhost meera]$ cc -o wait.out wait.c
[meera@localhost meera]$ ./wait.out

Parent Id is 11004
Status Id is 134513794
Tue Jan 27 11:27:27 IST 2009
Id is 11004 Status is 0

Sample Viva-Voce questions:


1. What is System call?
2. List the System call used for process management.
3. Explain fork() system call.
4. Explain exec() system call.
5. Explain stat() system call.
6. What are the differences among a system call, a library function, and a UNIX command?
7. What is pid?
8. What command is used to execute system calls from exe?

Experiments addressing COs:

The experiments mentioned above address CO1

Ex. No. 4 Input Output system Call

AIM:
To write a program to implement the concept of I/O call.

ALGORITHM:

Step 1: Include the necessary header files.

25
Step 2: Declare the variables needed.
Step 3: check if argc!=2 then
print “File name is not given”
Step 4: Open the file specified in the command prompt.
fd=open(argv[1],0)
Step 5: Check if fd!=-1 then write the value of n into the file.
Step 6: Else print no such file exist and perform the following process.
Step 6a: Create a new file by entering the choice Y.
Step 6b: Create the file using the built in function “ch mode+rw”
Step 6c: Concatenate the arguments and the attributes using strcat.
Step 6d: Get the value of n one by one and store the value n into the new file using write ().

OUTPUT:

[meera@localhost meera]$ cc -o iocall.out iocall.c


[meera@localhost meera]$ ./iocall.out fork.c
#include<stdio.h>
main()
{
int a;
a=fork();
printf("\n\n\nParent is %d",getpid());
if(a==0)
{
printf("\n\tHELLO FROM CHILD PROCESS \n\t");
printf("\nCHILD ID is %d",getpid());
}
else if(a!=0)
{
printf("\n\tHELLO FROM PARENT PROCESS %d",getpid());
printf("\n\tHELLO FROM CHILD PROCESS %d\n",a);
}
else
{
printf("ERROR IN CREATION");
}
}

Sample Viva-Voce questions:


1. What is a System call?
2. What are the two different processor modes?
3. Define interrupt driven operation.

26
4. Define open() system call
5. Define close() system call
6. What is the use of fork() system call?

Experiments addressing COs:

The experiments mentioned above address CO1

SIMULATE UNIX COMMANDS

Ex. No. 5a Simulation of Copy Command

AIM:
To write a program for the simulation of copy command.

ALGORITHM:

Step 1: Include the necessary header files.


Step 2: Declare the variables needed.
Step 3: Check if argc<=2 then exit from the loop.
Step 4: Else copy the filename in argv[1] to src and argv[2] to dest.
Step 5: If the src file and dest file is not equal then perform the following process.
Step 5a: Check if link between src and dest is not equal to -1 then
Print “The has been copied”
Step 5b: Else print “Error while coping”
Step 6: Else print the src and dest files are same.

OUTPUT:

[meera@localhost meera]$ cat filename2


27
have a nice day
Good Morning

[meera@localhost meera]$ cc -o copy.out copy.c

[meera@localhost meera]$ ./copy.out filename2 file1

The file has been copied

[meera@localhost meera]$ cat file1


have a nice day
Good Morning

Ex. No.5b Simulation of Move Command

AIM:
To write a program to implement the simulation of move command.

ALGORITHM:

Step 1: Include the necessary header files.


Step 2: Declare the file pointer to open the specified file.
Step 3: Declare the variables needed.
Step 4: Check if argc<=2 then exit from the loop.
Step 5: Else copy the file from argv[1] to src and argv[2] to dest.
Step 6: If src and dest are not same then copy the content from one file to another.
Step 7: Else print source and destination are same.

OUTPUT:

[meera@localhost meera]$ cat filename2


have a nice day
Good Morning

[meera@localhost meera]$ cc -o move.out move.c


[meera@localhost meera]$ ./move.out

28
Moving a file
move[.src][.dest]
[meera@localhost meera]$ ./move.out filename2 file2

The file has been moved

[meera@localhost meera]$ cat filename2


cat: filename2: No such file or directory
[meera@localhost meera]$ cat file2
have a nice day
Good Morning

Ex. No. 5c Simulation of Cat Command

AIM:
To write a program for the simulation of CAT command.

ALGORITHM:

Step 1: Include the necessary header files.


Step 2: Declare the variables needed.
Step 3: Check if argc!=2 then exit from the loop.
Step 4: Else perform the following process.
Step 4a: Open the file in the read mode.
Step 4b: Until the end of the file, get the character from file and print the character.
Step 4c: Close the file after finishing the access.

OUTPUT:

[meera@localhost meera]$ cc -o cat.out cat.c


[meera@localhost meera]$ ./cat.out move.c

FILE CONTENTS ARE:


29
-----------------------------
#include<stdio.h>
#include<string.h>
int main(int argc,char *argv[])
{
char src[50],dest[50];
if(argc<=2)
{
printf("\nMoving a file\nmove[.src][.dest]\n");
exit(0);
}
else
{
strcpy(src,argv[1]);
strcpy(dest,argv[2]);
}
if(strcmp(src,dest)!=0)
{
if((link(src,dest)!=-1) && (unlink(src)!=-1))
{
printf("\nThe file has been moved\n");
}
else
{
printf("\nError in moving a file\n");
}
}
else
{
printf("\nSource and destination are same\n");
}
}

30
Ex. No. 5d Simulation of GREP Command

AIM:
To write a program to implement the simulation of grep command.

ALGORITHM:

Step 1: Include the necessary header files.


Step 2: Declare the variables needed.
Step 3: Declare a file pointer, use to open a specified file during run time.
Step 4: Check if argc!=2 then print error.
Step 5: Else copy the pattern and filename from argv[1] and argv[2] respectively.
Step 6: Open a file in a read mode.
Step 7: Read the content from file one by one and store it in an array.
Step 8: Compare the pattern with the array elements, if it match print the result.
Step 9: Else print file not found.

OUTPUT:

[meera@localhost meera]$ cc -o grep.out grep.c

[meera@localhost meera]$ ./grep.out printf cat.c


8 printf("\nArgument not matched\n");
14 printf("\n\nFILE CONTENTS ARE: ");
31
15 printf("\n-----------------------------\n");
18 printf("%c",c);
20 printf("\n");

printf has occurred 5time(s) infilecat.c

Ex. No. 5e Simulation of LS Command

AIM:
To write a program for simulation of LS command.

ALGORITHM:

Step 1: Include the necessary header files.


Step 2: Declare the variables needed and the file pointer to access a file.
Step 3: If argc<=1 then search for the sub directories.
Step 4: If the value of n is less than zero then print the file doesn’t exist.
Step 5: Else check if i<n then print the list of files present in that directory.
Step 6: Print the total number of files.

OUTPUT:

[meera@localhost meera]$ cc -o ls.out ls.c


[meera@localhost meera]$ ./ls.out 3,1
.
..

bash_history .bash_logout .bash_profile


.bashrc .emacs .gtkrc
.kde .viminfo
a aaa arithmetic.sh
cat.c cat.out copy.c
copy.out ex1 exec.c
32
exec.out f1 factorial.sh
ff fff fibonacci.sh
file1 file2 filename3
fork.c fork.out ggg
grep.c grep.out hello
hhh iocall.c iocall.out
junk junk1 largest.sh
leap.sh lengthofstring.sh loginsearch.sh
lotus ls.c ls.out
marklist.sh move.c move.out
ne new new.sh
newfile oddeven.sh overall1
palindrome.sh pat1 pattern.sh
positivenegative.sh primecomposite.sh shell
sequenceofoddnumbers.sh stat.c stat.out
sum01.sh sum01.sh. sumofdigits.sh
sumofseries.sh sumofseries01.sh wait.c
wait.out

Total files: 71

Sample Viva-Voce questions:

1. Explain the importance of directory in a UNIX system


2. What is the use of ls command?
3. What is the use of Copy command?
4. What does the cat command refers to?
5.

Experiments addressing COs:

The experiments mentioned above address CO1

33
First Come First Serve Scheduling

AIM:
To write a program using C in Unix environment to implement the first come first serve
scheduling.

ALGORITHM:

Step 1: Include the header files and declare the array size and variable.
Step 2: Get the total number of jobs in array by using for loop.
Step 3: Get the waiting time and turn around time.
Step 4: Print the values of waiting time and turn around time.
Step 5: Compute the average waiting time and turn around time and print their values.

EXAMPLE:
Suppose there are 3 processes in the ready list. Further assume that they had entered into the
ready list in the order P0, P1, P2.

i T(Pi)
0 24
1 3
2 3

Gantt (Gantl Chart):-


P0 P1 P2
0 24 27 30
Turn Around Time:-
TRND(P0)=T(P0)=24
TRND(P1)=T(P1)+ TRND(P0)
34
=3+24=27
TRND(P2)=T(P2)+ TRND(P1)
=3+27=30

Average Turn Around Time:-


TRND =(24+27+30)/3=27

Waiting Time:-
W(P0)=0
W(P1)= TRND(P0)=24 or W(P1)= W(P0)+burst time of P0 = 0+24=24
W(P2)= TRND(P1)=27

Average Waiting Time:-


(0+24+27)/3=17

OUTPUT:

FIRST COME FIRST SERVE SCHEDULING PROCESS


Enter the no. of jobs: 3

Service Time or Burst Time for a process 0: 24

Service Time or Burst Time for a process 1: 3

Service Time or Burst Time for a process 2: 3

Process Burst Time Waiting Time Turn Around Time

P0 24 0 24
P1 3 24 27
P2 3 27 30

Average Waiting Time=17

Average Turn Around Time=27

35
Ex. No. 6b Shortest Job First Scheduling

AIM:
To write a program using C in UNIX environment to implement the shortest job next concept.

ALGORITHM:

Step 1: Include the necessary header files needed.


Step 2: Declare the variables.
Step 3: Get the number of jobs in array using for loop.
Step 4: Get the service time for the process.
Step 5: Find out the ascending order of the service time for the process.
Step 6: Compute the waiting time and turn around time for each process.
Step 7: Calculate the average waiting time and turn around time.
Step 8: Print the result.

EXAMPLE:
In the following example assume no other jobs arrive during the servicing of all jobs in the ready
list. Assume there are 4 jobs.
i T(Pi)
0 5
1 10
2 8
3 3
Since the service time of P3 is 3, which is the shortest, P3 is scheduled first and then P0 is
scheduled next and so on.

Gantt Chart:-

0 3 8 16 26

P3 P0 P2 P1

Turn Around Time:-


36
TRND(P0)=T(P0)+T(P3)=5+3=8
TRND(P1)=T(P1)+ T(P0)+T(P3)+T(P2)=10+5+3+5=26
TRND(P2)=T(P2)+ T(P0)+T(P3)=8+5+3=16
TRND(P3)= T(P3)= 3

Average Turn Around Time:-


TRND =(8+26+16+3)/4=13

Waiting Time:-
W(P0)=3
W(P1)=16
W(P2)= 8
W(P3)= 0

Average Waiting Time:-


W=(3+16+8+0)/4=6.75

OUTPUT:

SHORTEST JOB NEXT SCHEDULING PROCESS


Enter the no. of jobs: 4

Service Time or Burst Time for a process 0: 5

Service Time or Burst Time for a process 1: 10

Service Time or Burst Time for a process 2: 8

Service Time or Burst Time for a process 3: 3

Process Burst Time Waiting Time Turn Around Time

P0 3 0 3
P1 5 3 8
P2 8 8 16
P3 10 16 26

Average Waiting Time=6

Average Turn Around Time=13

37
Ex. No. 6c Round Robin Scheduling

AIM:
To write a program using C in UNIX environment to implement Round-robin scheduling
concept.

ALGORITHM:

Step 1: Include the necessary header files needed.


Step 2: Declare the variables.
Step 3: Get the number of jobs in array using for loop.
Step 4: Get the quantum time and service time for the process.
Step 5: Find the order of the process execution by using the quantum time.
Step 6: Compute the waiting time and turn around time for each process.
Step 7: Calculate the average waiting time and turn around time.
Step 8: Print the result.

EXAMPLE:
Suppose the ready list contains the processes as shown in table and time quantum is 4 with a
negligible amount of time for context switching.
i T(Pi)
0 24
1 3
2 3

Gantt Chart:-

0 4 7 10 14 18 22 26 30

P0 P1 P2 P0 P0 P0 P0 P0

Turn Around Time:-


TRND(P0)=30
TRND(P1)=7
TRND(P2)=10

Average Turn Around Time:-


TRND =(30+7+10)/3=15.66

Waiting Time:-
38
W(P0)=26-(3+3+4+4+4+4) = 26-20 =6
W(P1)=4
W(P2)=7

Average Waiting Time:-


W=(6+4+7)/3=5.66667

OUTPUT:
ROUND ROBIN SCHEDULING ALGORITHM

Enter the no. of process: 4

Enter Burst time for the process 0: 8

Enter Burst time for the process 1: 4

Enter Burst time for the process 2: 6

Enter Burst time for the process 3: 2


Process Name Burst Time
P0 8
P1 4
P2 6
P3 2

Press 1. Round Robin 2. Exit


1
Enter the Time Slicing: 2

Process Name Remaining Time Total Time


P0 6 2
P1 2 4
P2 4 6
P3 0 8
P0 4 10
P1 0 12
P2 2 14
P0 2 16
P2 0 18
P0 0 20

Ex. No. 6d Priority Scheduling

AIM:
39
To implement the priority scheduling using C in UNIX environment.

ALGORITHM:

Step 1: Include the necessary header files needed.


Step 2: Declare the variables.
Step 3: Get the number of jobs in array using for loop.
Step 4: Get the service time for the process.
Step 5: Compute the waiting time and turn around time with respect to priorities.
Step 6: Calculate the average waiting time and turn around time.
Step 7: Print the result.

EXAMPLE:
Suppose the ready list contains the processes as shown in table.

i T(Pi) Priority
0 24 2
1 3 1
2 3 3

Gantt Chart:-
0 3 27 30

P1 P0 P2

Turn Around Time:-


TRND(P0)=27
TRND(P1)=3
TRND(P2)=30

Average Turn Around Time:-


TRND =(30+3+27)/3=20

Waiting Time:-
W(P0)=3
W(P1)=0
W(P2)=27

Average Waiting Time:-


W=(3+0+27)/3=10
OUTPUT:

PRIORITY SCHEDULING PROCESS


40
Enter the no. of jobs: 3

Service Time or Burst Time for a process 0: 24

Enter the Priority 0: 2

Service Time or Burst Time for a process 1: 3

Enter the Priority 1: 1

Service Time or Burst Time for a process 2: 3

Enter the Priority 2: 3

Process Burst Time Priority Waiting Time Turn Around Time


P0 3 1 0 3
P1 24 2 3 27
P2 3 3 27 30

Average Waiting Time=10

Average Turn Around Time=20

Sample Viva-Voce questions:


1. What is meant by FCFS scheduling?
2. What is meant by SJF scheduling?
3. Which scheduling algorithm allocates the CPU first to the process that requests the CPU first?
4. What does the time quantum mean?
5. What is the state of the processor, when a process is waiting for some event to occur?
6. Define time slicing.
7. What is CPU scheduler?
Experiments addressing COs:
The experiments mentioned above address CO2

Ex. No. 7 Producer – Consumer Problem

AIM:

41
To write a program to implement producer consumer problem concept.

ALGORITHM:

Step 1: Include the necessary header files needed.


Step 2: Declare the variables and semaphores needed.
Step 3: Use switch case to implement the producer consumer problem.
Step 4: If the choice is 1, then perform the producer process.
Step 4a: When the mutex is equal to one, then insert the new element or product from the producer.
Step 5: If the choice is 2, then perform the consumer process.
Step 5a: When the mutex is equal to one, then allow the consumer to consume the product.
Step 6: If the choice is 3, then display the content in the buffer.
Step 6a: Until the value of full is not equal to zero, print the value in the buffer one by one.
Step 7: If the choice is 4, come out of the loop.

OUTPUT:

PRODUCER – CONSUMER PROBLEM


Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1

Enter the item to be added: p1


Item inserted successfully

Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1

Enter the item to be added: p2


Item inserted successfully

Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 2
42
Item p1 is successfully consumed

Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1

Enter the item to be added: p3


Item inserted successfully

Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 3
p2
p3

Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 4
Sample Viva-Voce questions:

1. What is a semaphore?
2. What kind of operations is possible on a semaphore?
3. What is a critical section?
4. When does the deadlock occurs?
5. Define IPC?
6. Tell us something about mutex.
7. What is a Safe State and what is its use in deadlock avoidance?

Experiments addressing COs:

The experiments mentioned above address CO3

MEMORY MANAGEMENT SCHEME


43
Ex. No. 8a Memory Management Scheme using Best Fit.

AIM:
To write a C program to implement the memory management scheme using best fit.

ALGORITHM:

Step 1: Include the necessary header files required.


Step 2: Declare the variables needed.
Step 3: Read the number of blocks and the size of each blocks.
Step 4: Read the number of process and the size of each process.
Step 5: Arrange both the process and block size in an order.
Step 6: Check if the process size is less than or equal to block size.
Step 7: If yes, assign the corresponding block to the current process.
Step 8: Else print the current process is not allocated.

OUTPUT:

MEMORY MANAGEMENT SCHEME - BEST FIT


Enter No. of Blocks: 5
Enter the 0st block size: 500
Enter the 1st block size: 100
Enter the 2st block size: 250
Enter the 3st block size: 650
Enter the 4st block size: 850
Enter No. of Process: 5
Enter the size of 0st process: 450
Enter the size of 1st process: 605
Enter the size of 2st process: 820
Enter the size of 3st process: 110
Enter the size of 4st process: 230

Process Block Size


820 850
605 650
450 500
230 250
110 100

The process 0 [size 820] allocated to block 850

The process 1 [size 605] allocated to block 650

The process 2 [size 450] allocated to block 500

The process 3 [size 230] allocated to block 250


44
The process 110 is not allocated

Ex. No. 8b Memory Management Scheme using First Fit.

AIM:
To write a C program to implement the memory management scheme using first fit
ALGORITHM:

Step 1: Include the necessary header files required.


Step 2: Declare the variables needed.
Step 3: Read the number of blocks and the size of each blocks.
Step 4: Read the number of process and the size of each process.
Step 5: Check if the process size is less than or equal to block size.
Step 6: If yes, assign the corresponding block to the current process.
Step 7: Else print the current process is not allocated.

OUTPUT:

MEMORY MANAGEMENT SCHEME - FIRST FIT


Enter No. of Blocks: 5
Enter the 0st block size: 120
Enter the 1st block size: 230
Enter the 2st block size: 340
Enter the 3st block size: 450
Enter the 4st block size: 560
Enter No. of Process: 5
Enter the size of 0st process: 530
Enter the size of 1st process: 430
Enter the size of 2st process: 630
Enter the size of 3st process: 203
Enter the size of 4st process: 130

Process Block Size


530 120
430 230
630 340
203 450
130 560

The process 3 [size 203]allocated to block 230


The process 4 [size 130]allocated to block 340
The process 1 [size 430]allocated to block 450
The process 0 [size 530]allocated to block 560
45
The process 2 is not allocated

Ex. No. 8c Memory Management Scheme using Worst Fit.

AIM:
To write a C program to implement the memory management scheme using worst fit.

ALGORITHM:

Step 1: Include the necessary header files required.


Step 2: Declare the variables needed.
Step 3: Read the number of blocks and the size of each blocks.
Step 4: Read the number of process and the size of each process.
Step 5: Arrange both the process and block size in an opposite order.
Step 6: Check if the process size is less than or equal to block size.
Step 7: If yes, assign the corresponding block to the current process.
Step 8: Else print the current process is not allocated.

OUTPUT:

MEMORY MANAGEMENT SCHEME - WORST FIT


Enter No. of Blocks: 5
Enter the 0st block size: 520
Enter the 1st block size: 147
Enter the 2st block size: 236
Enter the 3st block size: 289
Enter the 4st block size: 600
Enter No. of Process: 5
Enter the size of 0st process: 196
Enter the size of 1st process: 245
Enter the size of 2st process: 570
Enter the size of 3st process: 145
Enter the size of 4st process: 600

Process Block Size


145 600
196 520
245 289
570 236
600 147

46
The process 0 [size 145] allocated to block 600

The process 1 [size 196] allocated to block 520

The process 2 [size 245] allocated to block 289

The process 570 is not allocated

The process 600 is not allocated

Sample Viva-Voce questions:


1. What is a process?
2. Differentiate between first fit, best fit and worst fit.
3. How does swapping result in better memory management?
4. What is first fit?
5. What is the amount of memory required for storing the page tables of the process if a process has
only following pages in its virtual address space?
6.

Experiments addressing COs:

The experiments mentioned above address CO4

Ex No: 9 IMPLEMENTATION OF DISK SCHEDULING ALGORITHMS

AIM:
            To write a ‘C’ program to implement the Disk Scheduling algorithm for First Come First Served
(FCFS), Shortest Seek Time First (SSTF), and SCAN.

PROBLEM DESCRIPTION:

47
              Disk Scheduling is the process of deciding which of the cylinder request is in the ready queue is
to be accessed next.
              The access time and the bandwidth can be improved by scheduling the servicing of disk I/O
requests in good order.
Access Time:
               The access time has two major components:  Seek time and Rotational Latency.           
Seek Time:
               Seek time is the time for disk arm to move the heads to the cylinder containing the desired
sector.
Rotational Latency:
              Rotational latency is the additional time waiting for the disk to rotate the desired sector to the
disk head.
Bandwidth:
              The disk bandwidth is the total number of bytes transferred, divided by the total time between
the first request for service and the completion of the last transfer.
ALGORITHM:

1. Input the maximum number of cylinders and work queue and its head starting position.
2. First Come First Serve Scheduling (FCFS) algorithm – The operations are performed in order
requested
3. There is no reordering of work queue.
4. Every request is serviced, so there is no starvation
5. The seek time is calculated.
6. Shortest Seek Time First Scheduling (SSTF) algorithm – This algorithm selects the request
with the minimum seek time from the current head position.
7. Since seek time increases with the number of cylinders traversed by the head, SSTF chooses the
pending request closest to the current head position.
8. The seek time is calculated.
9. SCAN Scheduling algorithm –The disk arm starts at one end of the disk, and moves toward the
other end, servicing requests as it reaches each cylinder, until it gets to the other end of the disk.
10. At the other end, the direction of head movement is reversed and servicing continues.
11. The head continuously scans back and forth across the disk.

48
12. The seek time is calculated.
13. Display the seek time and terminate the program.
Output

"disksche.c" 122L, 2076C written


[anandh@localhost ~]$ cc disksche.c
[anandh@localhost ~]$ ./a.out

 Enter the maximum number of cylinders : 200


enter number of queue elements5

 Enter the work queue23


89
132
42
187

 Enter the disk head starting posision:


100
                
                  MENU

                 1. FCFS

                 2. SSTF

                 3. SCAN

                 4. EXIT

Enter your choice: 1

 FCFS

*****

 Total seek time : 421

                 MENU

                 1. FCFS

                 2. SSTF

                 3. SCAN

                 4. EXIT
49
Enter your choice: 2

 SSTF

*****

 Total seek time is: 273

                 MENU

                 1. FCFS

                 2. SSTF

                 3. SCAN

                 4. EXIT

Enter your choice: 3

 SCAN

*****

 Total seek time : 287

                 MENU

                 1. FCFS

                 2. SSTF

                 3. SCAN

                 4. EXIT

Enter your choice: 4

 Sample Viva-Voce questions:


1. Give an example of a process state.
2. When does trashing occurs?
3. What is FCFS?
4. Define SJF
5.

50
Experiments addressing COs:

The experiments mentioned above address CO5


Ex. No.:10 MEMORY MANAGEMENT SCHEME II
  -    LRU PAGE  REPLACEMENT ALGORITHMS

AIM:

         To write a program in C to implement page replacement algorithm FIFO

PROBLEM DESCRIPTION:

          Page replacement algorithms are used to decide what pages to page out when a page needs to be
allocated. This happens when a page fault occurs and free page cannot be used to satisfy allocation

LRU:       

        “Replace the page that had not been used for a longer sequence of time”. The frames are empty in
the beginning and initially no page fault occurs so it is set to zero. When a page fault occurs the page
reference sting is brought into the memory. The operating system keeps track of all pages in the
memory, thereby keeping track of the page that had not been used for longer sequence of time. If the
page in the page reference string is not in memory, the page fault is incremented and the page that had
not been used for a longer sequence of time is replaced. If the page in the page reference string is in the
memory take the next page without calculating the next page. Take the next page in the page reference
string and check if the page is already present in the memory or not. Repeat the process until all pages
are referred and calculate the page fault for all those pages in the page references string for the number
of available frames.

Algorithm

Step1: Start the program


Step2: Declare the required variables and initialize it.
Step3; Get the frame size and reference string from the user
Step4: Read the reference string character by character.
Step5: Compare the character with the frames. If the character is already present move to the next page
number. If not replace the frame that has not be used for the longer time.
Step6: Display the frames
Step7: Display the page fault
Step7: Terminate the program

OUTPUT
[anandh@localhost ~]$ ./a.out

 LRU page replacement algorithm     


  -----------------------------------------
51
Total No.of frames:3                                                                                                                        
reference
string:125624512412                                                                                                                                      
    

page faults for the given reference string are


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

The allotted frames are 


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

 5     5                4    4     4     2     2                                                                                               


 No.of page faults:9                                                                                                                   
Sample Viva-Voce questions:

1. Define LRU Page replacement


2. What is page frame?
3. Why paging is used?
4. What is meant by page fault?
5. When does page fault error occurs?
6. Define FIFO
7. Which is the best replacement algorithm and why?
                                                                                                                                                                         
                                                                                                                                                                        
                            Experiments addressing COs:

The experiments mentioned above address CO5


                                                                                                                                                                         
                                                                                                                                                                         
                                                                                                                                                                         
                                     

52

You might also like