67
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. 1

OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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.

DEPARTMENTVISION

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

MISSIONTo 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.

1

Page 2: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

CSE 284 – Operating Systems Lab

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

2

Page 3: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

BASIC LINUX COMMANDS

EX. NO. 1a WORKING WITH FILES AND DIRECTORIESAIM:

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

3

Page 4: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

1. To create a file.Syntax: $ cat>filenameExample: $ cat>ex1

2. To view the content of the file.Syntax: $ cat filenameExample: $ cat ex1

3. To append some details with the existing details in the fileSyntax: $ cat>>filenameExample: $ cat>>ex1

4. To concatenate multiple filesSyntax: $ cat file1 file2 > file3Example: $ cat computer compiler>world

5. To know the list of all files in directorySyntax: $ ls

6. To copy the file to another fileSyntax: $ cp source destinationExample: $ cp ex1 ex2

7. To rename the file Syntax: $ mv oldfile newfileExample: $ mv ex1 ex3

8. To delete a fileSyntax: $ rm filenameExample: $ rm ex1

9. To delete all filesSyntax: $ rm *

10. To create a directorySyntax: $ mkdir dirname

11. To change the name of the directorySyntax: $ cd dirname

12. To remove the directorySyntax: $ rmdir dirnameExample: $ rmdir flower

13. Echo i. To display the filename starting with single letter

Syntax: $ echo?4

Page 5: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

ii. To display the filename starting with two lettersSyntax: $ echo??

iii. To display the filename starting with the letter fSyntax: $ echo f*

iv. To display the filename ending with letter f.Syntax: $ echo *f

14. Present Working Directoryi. To display the present working directory

Syntax: $ pwd

ii. To clear the screenSyntax: $ tput clear

iii. To calculate the valuesSyntax: $ 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 OSSyntax: $ uname –r

15. Head i. To display first 10 lines

Syntax: $ head filename

ii. To display first 6 charactersSyntax: $ head -6c filename

iii. To display 5 lines from 2 filesSyntax: $ head -5 file1 file2

16. To display last 10 linesSyntax: $ tail filenameExample: $ tail ex3

17. Word Counti. To display the number of words in a file

Syntax: $ wc filenameExample: $ wc ex1

ii. To display the number of characters in a file

5

Page 6: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Syntax: $ wc –c filenameExample: $ wc –c ex1

iii. To display the number of linesSyntax: $ wc –l filenameExample: $ wc –l ex3

18. Line Numberi. To display number of lines with numbers

Syntax: $ nl filenameExample: $ nl ex1

ii. To increment the line number by 5Syntax: $ nl –i5 filenameExample: $ nl –i5 ex3

19. Sorti. To reverse and sort the content of file

Syntax: $ sort –r filenameExample: $ sort –r ex1

ii. To sort the content of the fileSyntax: $ sort filenameExample: $ sort ex1

iii. To sort and remove the duplicateSyntax: $ sort –u filenameExample: $ sort –u ex1

20. VI Editor Commandsi. To compile and run shell program

Syntax: $ sh filenameExample: $ sh odd.sh

ii. To compile a C program Syntax: $ cc –o filename filename.c

iii. To run a C programSyntax: $ ./filename

21. To paste the contents of file

Syntax: $ paste filename1, filename2Example: $ paste ex1, ex2

22. To display file contents page by pageSyntax: $ more filename

6

Page 7: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Ex. No. 1b General Purpose Utility Commands

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

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: $ dateOutput: Tue Jan 20 10:54:25 IST 2009

ii. To display month only. Syntax: $ date+%mOutput: 01

iii. To display month name and monthSyntax: $date +%h%mOutput: Jan01

iv. To display month nameSyntax: $ date+%hOutput: Jan

v. To display the time in hoursSyntax: $ date+%HOutput: 10

vi. To display the time in minutesSyntax: $ date+%MOutput: 53

vii. To display the time in AM or PMSyntax: $ date+%rOutput: 10: 53:24 AM

7

Page 8: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

viii. To display date of monthSyntax: $ date+%dOutput: 20

3. WHOi. To display the login details

Syntax: $ whoOutput: root :0 Jan 20 10:51

cs1010 pts/0 Jan 20 10:51 (172.16.1.72)

ii. To display the login user detailsSyntax: $ who am iOutput: cs1010

iii. To display my login idSyntax: $ lognameOutput: cs1010

4. MANi. It is used to view more details of all the commands

Syntax: $ man command_nameExample: $ 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

8

Page 9: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

SHELL PROGRAMMING

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

AIM: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 inputGet the value of n

Step 3: Calculate the sum of n numbersGet the value of aSet 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 nStep 5: Calculate the average

Set a=s/nStep 6: Display the sum and average

SAMPLE OUTPUT:

[meera@localhost meera]$ sh sum01.shsum of N NumbersEnter the number:563124sum=16Average=3

9

Page 10: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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

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

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.shLargest and Smallest of three numbersEnter the three numbers6 3 8Choose 1. Largest and 2. SmallestEnter the choice18 is larger than 6 and 3[meera@localhost meera]$ sh largest.shLargest and Smallest of three numbersEnter the three numbers5 4 3Choose 1. Largest and 2. SmallestEnter the choice23 is smaller than 5 and 4

10

Page 11: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 inputGet 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.shLeap Year or NotEnter the Year19201920 is a leap year[meera@localhost meera]$ sh leap.shLeap Year or NotEnter the Year19911991 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.shOdd or EvenEnter the value of n66 is a even Number

11

Page 12: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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:

Step 1: Read the inputGet the value of n

Step 2: If n is greater than 0then print n is a positive number.

Step 3: Else if n is less than 0then print n is a negative number.

Step 4: Else print n is equal to zero.

SAMPLE OUTPUT:

[meera@localhost meera]$ sh positivenegative.shPositive or negativeEnter the number55 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 variablesSet i=0, sum=0

Step 2: Read the inputGet the value of l, n

Step 3: Calculate the sum of digitsSet a=n%10Set sum=a+sumSet n=n/10Set i=i+1

Step 4: Go to step 3 until i is less than l else go to step 5.Step 5: Print sum

12

Page 13: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

OUTPUT:

[meera@localhost meera]$ sh sumofdigits.shSum of digitsEnter the number of digits3Enter the number265Sum of the digits: 13

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 seriesSet c=a+bPrint cSet a=bSet b=cSet 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.shFibonacci SeriesEnter the value of n:5Fibonacci series upto 5:01123

13

Page 14: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Ex. No. 2h Factorial of a NumberAIM:

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

ALGORITHM:

Step 1: Read the inputGet the value of n

Step 2: Initialize the variablesSet f=1, i=1

Step 3: If i is less than or equal to nSet f=f*iSet 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.shFactorial of a numberEnter a number5Factorial 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 inputGet the value of name, roll, a, b, c, d, e

Step 2: Calculate the sum and averageSet sum=a+b+c+d+eSet 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”

14

Page 15: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

OUTPUT:

[meera@localhost meera]$ sh marklist.shStudent MarklistEnter the student nameaaaEnter the roll number111Enter the marks89 78 56 84 53Name: aaaRoll Number: 111MarksMark1: 89Mark2: 78Mark3: 56Mark4: 84Mark5: 53Total = 360Average= 72Grade C

Ex. No. 2j Sum of SeriesAIM:

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 nStep 4: If yes, perform the following process

Set a=i*iSet s=s+aSet 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.shSum of SeriesEnter the Number:5Sum= 55

15

Page 16: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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

Step 3: check if j is less than of equal to n or not.Step 4: If yes, perform the following stepsStep 4a: If i%2 is not equal to zero, then print iStep 4b: Increment the value of i, j.

Set i=i+1Set j=j+1

Step 5: Repeat the step 4 until j is less than or equal to n.

OUTPUT:

[meera@localhost meera]$ sh sequenceofoddnumbers.shSequence of Odd NumbersEnter the value of n:6Odd number upto 6:135

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 thenPrint “Enter another number”

Step 3: Else if n is equal to 1 thenPrint n is neither prime nor composite

Step 4: Else if n is equal to 2 thenPrint n is even prime.

16

Page 17: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Step 5: Else, perform the following process.Step 5a: Initialize the variables.

Set K=0, i=2Step 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+1Step 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:

[meera@localhost meera]$ sh primecomposite.shPrime or compositeEnter the value of n:66 is composite[meera@localhost meera]$ sh primecomposite.shPrime or compositeEnter the value of n:33 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 6Step 3: If yes, perform the following stepsStep 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+bPrint c

Step 5b: If the choice is 2 then perform subtractionSet c=a-bPrint c

Step 5c: If the choice is 3 then perform multiplication

17

Page 18: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Set c=a*bPrint c

Step 5d: If the choice is 4 then perform divisionSet c=a/bPrint c

Step 5e: If the choice is 5 then perform moduloSet c=a%bPrint c

Step 6: Increment the value of nStep 7: Repeat the process from step 4 until n is less than 6.

OUTPUT:

Enter ur choice1----Addition2----Subtraction3----Multiplication4----Division5----Modulo1Enter ur number5623The 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.shLength of the stringEnter the stringhavelength of the given string have is 5

18

Page 19: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 choiceStep 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.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

19

Page 20: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Step 5: If q is equal to 1, thenStep 5a: Initialize the variables

Set s=0, i=1Step 5b: Read the input.

Get the value of n.Step 5c: If i is less than or equal to n, then

Set s=s+iSet 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, thenStep 6a: Initialize the variables.

Set s=0, i=1Step 6b: Read the input

Get the value of nStep 6c: If i is less than or equal to n then

Set s=s+iSet 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, thenStep 7a: Initialize the variables.

Set s=0, i=2Step 7b: Read the input

Get the value of nStep 7c: If i is less than or equal to n then

Set s=s+iSet 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.shEnter the number6Enter the choice1Sum of series --> 1+2+3+...+nSum of series: 21

[meera@localhost meera]$ sh sumofseries01.shEnter the number6Enter the choice2

20

Page 21: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Sum of series --> 1+3+5+...+nSum of series: 9

[meera@localhost meera]$ sh sumofseries01.shEnter the number6Enter the choice3Sum of series --> 2+4+...+nSum of series: 12

[meera@localhost meera]$ sh sumofseries01.shEnter the number6Enter the choice4

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, thenSet m=n%10Set i=i*10+mSet 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.shPalindrome-Reversing a NumberEnter the number123Reversed Number: 321The number 123 doesnt form a palindrome

[meera@localhost meera]$ sh palindrome.shPalindrome-Reversing a Number

21

Page 22: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Enter the number121Reversed Number: 121The 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.

OUTPUT:

[meera@localhost meera]$ sh loginsearch.shEnter the login number:CS10141No the number hasnt logged in

[meera@localhost meera]$ sh loginsearch.shEnter the login number:meerameera 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?

22

Page 23: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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().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 10968HELLO FROM CHILD PROCESS

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

23

Page 24: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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:

[meera@localhost meera]$ cc -o exec.out exec.c[meera@localhost meera]$ ./exec.outChild Id is 10983Parent Id is 10982Tue Jan 27 11:17:10 IST 2009End 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:

24

Page 25: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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

exec.c has 1 linkexec.c has 770 devicesexec.c has 3908662 inodesexec.c has 33204 protectionexec.c has 0 inode devicesexec.c has 248 sizeexec.c has 501 ownerexec.c has 4096 block sizeexec.c has 1233035222 timeexec.c has 1233035220 time

Ex. No. 3d Wait System Call

AIM:To write a program using wait system call.

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 11004Status Id is 134513794Tue Jan 27 11:27:27 IST 2009Id 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.

25

Page 26: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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.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());

26

Page 27: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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.

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

27

Page 28: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 filename2have a nice dayGood 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 file1have a nice dayGood Morning

28

Page 29: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 filename2have a nice dayGood Morning

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

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

The file has been moved

[meera@localhost meera]$ cat filename2cat: filename2: No such file or directory[meera@localhost meera]$ cat file2have a nice dayGood Morning

29

Page 30: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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:-----------------------------#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) {

30

Page 31: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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"); }}

31

Page 32: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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.c8 printf("\nArgument not matched\n");14 printf("\n\nFILE CONTENTS ARE: ");15 printf("\n-----------------------------\n");18 printf("%c",c);20 printf("\n");

printf has occurred 5time(s) infilecat.c

32

Page 33: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 .viminfoa aaa arithmetic.shcat.c cat.out copy.ccopy.out ex1 exec.cexec.out f1 factorial.shff fff fibonacci.shfile1 file2 filename3fork.c fork.out ggggrep.c grep.out hellohhh iocall.c iocall.outjunk junk1 largest.shleap.sh lengthofstring.sh loginsearch.shlotus ls.c ls.outmarklist.sh move.c move.outne new new.shnewfile oddeven.sh overall1palindrome.sh pat1 pattern.shpositivenegative.sh primecomposite.sh shellsequenceofoddnumbers.sh stat.c stat.out

33

Page 34: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

sum01.sh sum01.sh. sumofdigits.shsumofseries.sh sumofseries01.sh wait.cwait.out

Total files: 71

Sample Viva-Voce questions:

1. Explain the importance of directory in a UNIX system2. 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

34

Page 35: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

SCHEDULING ALGORITHMS

Ex. No. 6a 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 241 32 3

Gantt (Gantl Chart):-P0 P1 P2

0 24 27 30 Turn Around Time:-

TRND(P0)=T(P0)=24TRND(P1)=T(P1)+ TRND(P0)

=3+24=27TRND(P2)=T(P2)+ TRND(P1)

=3+27=30

Average Turn Around Time:-TRND =(24+27+30)/3=27

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

35

Page 36: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Average Waiting Time:-(0+24+27)/3=17

OUTPUT:

FIRST COME FIRST SERVE SCHEDULING PROCESSEnter 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

36

Page 37: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 51 102 83 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:-TRND(P0)=T(P0)+T(P3)=5+3=8TRND(P1)=T(P1)+ T(P0)+T(P3)+T(P2)=10+5+3+5=26TRND(P2)=T(P2)+ T(P0)+T(P3)=8+5+3=16TRND(P3)= T(P3)= 3

Average Turn Around Time:-TRND =(8+26+16+3)/4=13

37

Page 38: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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

Average Waiting Time:-W=(3+16+8+0)/4=6.75

OUTPUT:

SHORTEST JOB NEXT SCHEDULING PROCESSEnter 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

38

Page 39: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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 241 32 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)=30TRND(P1)=7TRND(P2)=10

Average Turn Around Time:-TRND =(30+7+10)/3=15.66

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

39

Page 40: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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: 2Process Name Burst Time

P0 8

P1 4

P2 6

P3 2

Press 1. Round Robin 2. Exit1Enter 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

40

Page 41: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Ex. No. 6d Priority Scheduling

AIM: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) Priority0 24 21 3 12 3 3

Gantt Chart:-0 3 27 30

P1 P0 P2

Turn Around Time:-TRND(P0)=27TRND(P1)=3TRND(P2)=30

Average Turn Around Time:-TRND =(30+3+27)/3=20

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

41

Page 42: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Average Waiting Time:-W=(3+0+27)/3=10

OUTPUT:

PRIORITY SCHEDULING PROCESSEnter 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

P1

P2

3

24

3

1

2

3

0

3

27

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?

42

Page 43: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Experiments addressing COs:The experiments mentioned above address CO2

Ex. No. 7 Producer – Consumer Problem

AIM: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 PROBLEMChoices are1. Producer Routine2. Consumer Routine3. Display the contents of the buffer4. ExitEnter your choice: 1

Enter the item to be added: p1Item inserted successfully

Choices are1. Producer Routine2. Consumer Routine3. Display the contents of the buffer4. ExitEnter your choice: 1

Enter the item to be added: p2

43

Page 44: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Item inserted successfully

Choices are1. Producer Routine2. Consumer Routine3. Display the contents of the buffer4. ExitEnter your choice: 2

Item p1 is successfully consumed

Choices are1. Producer Routine2. Consumer Routine3. Display the contents of the buffer4. ExitEnter your choice: 1

Enter the item to be added: p3Item inserted successfully

Choices are1. Producer Routine2. Consumer Routine3. Display the contents of the buffer4. ExitEnter your choice: 3p2p3

Choices are1. Producer Routine2. Consumer Routine3. Display the contents of the buffer4. ExitEnter your choice: 4Sample 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.

44

Page 45: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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

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 FITEnter No. of Blocks: 5Enter the 0st block size: 500Enter the 1st block size: 100Enter the 2st block size: 250Enter the 3st block size: 650Enter the 4st block size: 850Enter No. of Process: 5Enter the size of 0st process: 450Enter the size of 1st process: 605Enter the size of 2st process: 820Enter the size of 3st process: 110Enter the size of 4st process: 230

Process Block Size820 850605 650450 500

45

Page 46: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

230 250110 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

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 FITEnter No. of Blocks: 5Enter the 0st block size: 120Enter the 1st block size: 230Enter the 2st block size: 340Enter the 3st block size: 450Enter the 4st block size: 560Enter No. of Process: 5Enter the size of 0st process: 530Enter the size of 1st process: 430Enter the size of 2st process: 630Enter the size of 3st process: 203Enter the size of 4st process: 130

Process Block Size530 120

46

Page 47: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

430 230630 340203 450130 560

The process 3 [size 203]allocated to block 230The process 4 [size 130]allocated to block 340The process 1 [size 430]allocated to block 450The process 0 [size 530]allocated to block 560The 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 FITEnter No. of Blocks: 5Enter the 0st block size: 520Enter the 1st block size: 147Enter the 2st block size: 236Enter the 3st block size: 289Enter the 4st block size: 600Enter No. of Process: 5Enter the size of 0st process: 196Enter the size of 1st process: 245Enter the size of 2st process: 570Enter the size of 3st process: 145

47

Page 48: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Enter the size of 4st process: 600

Process Block Size145 600196 520245 289570 236600 147

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

.

48

Page 49: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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:

              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.

49

Page 50: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

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.

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 : 200enter number of queue elements5

 Enter the work queue238913242187

 Enter the disk head starting posision:100                                   MENU

                 1. FCFS

                 2. SSTF

                 3. SCAN

                 4. EXIT

Enter your choice: 1

 FCFS50

Page 51: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

*****

 Total seek time : 421

                 MENU

                 1. FCFS

                 2. SSTF

                 3. SCAN

                 4. EXIT

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

51

Page 52: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

                 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 SJF5.

Experiments addressing COs:

The experiments mentioned above address CO5Ex. 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

52

Page 53: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

Step1: Start the programStep2: Declare the required variables and initialize it.Step3; Get the frame size and reference string from the userStep4: 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 framesStep7: Display the page faultStep7: Terminate the program

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

 LRU page replacement algorithm        ----------------------------------------- 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 replacement2. 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 FIFO7. Which is the best replacement algorithm and why?

                                                                                                                                                                                                                                                                                                                                                                             Experiments addressing COs:

53

Page 54: OPERATING SYSTEM LAB RECORD - WordPress.com · Web viewCSE 284 – Operating Systems Lab List of Programs 1. Study of basic Commands in Linux Operating System 2. Shell programming

The experiments mentioned above address CO5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

54