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

List of Programs

1. Study of basic Commands in Linux Operating System

2. Shellprogrammingusingcontrolstatements

3. Shellprogrammingusingloops, patterns,expansions and substitutions

4. Write programsusingthe following systemcalls (fork, exec,getpid, exit,wait,close,stat,

opendir,readdir).

5. WriteprogramsusingtheI/Osystemcalls(open,read,write, etc).

6. SimulationofLinuxcommands.

7. ImplementationofCPUSchedulingAlgorithms(FCFS,SJF,RR,Priorty).

8. ImplementationofPage ReplacementAlgorithms(LRU,OPT,FIFO).

9. Implementationofmemoryallocationalgorithms(FirstFit,BestFit,WorstFit)

10.Implement the Producer–Consumerproblemusingsemaphores.

11.Simulation of Shared Memory Concept.

12.ImplementationofbankersAlgorithm.

13.Implementation Disk Scheduling Algorithms

BASIC LINUX COMMANDS

EX. NO. 1aWORKING 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.

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??

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

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.

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:24AM

viii. To display date of month

Syntax: $ date+%d

Output: 20

3. WHO

i. To display the login details

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:

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.

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

6 3 8

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

5 4 3

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

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. 2ePositive 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 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

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

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

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

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:

[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

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. 2oPattern 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.

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. 2pMultiple 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

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