Os Lab Details

58
Sri SAI RAM INSTITUTE OF TECHNOLOGY Sai Leo Nager, West Tambaram, Chennai – 44. LAB MANUAL CS 2257 OPERATING SYSTEM LAB IV SEM

Transcript of Os Lab Details

Page 1: Os Lab Details

Sri SAI RAM INSTITUTE OF TECHNOLOGY

Sai Leo Nager, West Tambaram, Chennai – 44.

LAB MANUAL

CS 2257 OPERATING SYSTEM LAB IV SEM

Department of information technology

Page 2: Os Lab Details

LIST OF EXPERIMENTS

1. Basic commands in UNIX.

2. Shell programming

3. a.Write a C program using a fork ,vfork, getpid and getppid system call.

3. b.Write a C program using wait, exit and sleep system calls within a

process.

4. a.Write a C program to simulate ls command.

4. b.Write a C program to simulate grep command

5. Write a C program to implement First come First serve scheduling

Algorithm. Compute and print the average waiting time and average

Turnaround time.

6. Write a C program to implement Shortest Job First scheduling Algorithm.

Compute and print the average waiting time and average turnaround time.

7. Write a C program to implement Round Robin Scheduling Algorithm.

Compute and print the average waiting time and average turnaround time.

8. Write a C program to implement Priority scheduling Algorithm. Compute

and print the average waiting time and average turnaround time.

9. Write a C program to implement Inter Process Communication using

Pipes.

10. Write a C program to implement Producer – Consumer Problem using

semaphores.

11. Write a C program to implement First Fit, Best Fit and Worst Fit

12. Write a C program to implement LRU page replacement algorithm.

13. Write a C program to implement FIFO page replacement algorithm.

14. a. Write a C program to implement Contiguous File Allocation technique.

14. b. Write a C program to implement Linked File Allocation Technique.

Page 3: Os Lab Details

14.c.Write a C program to implement Indexed File Allocation Technique.

OPERATING SYSTEM INTRODUCTION

Operating System:

OS is System software and it is defined as an organized collection of software consisting of procedures for operating a computer. It provides an environment for execution of programs and acts as a interface between the user and the hardware of the computer system.

Operating System interacts with user in two ways:

Operating System commands. Enables user to interact directly with operating system. Operating System calls provide an interface to a running program and the operating

system. System calls in UNIX are written in C.

History of UNIX:

Ken Thompson of AT &T Bell Laboratories designed UNIX in late 1960’s. Two versions of UNIX that emerged are AT &T Unix and BSD UNIX. In 1989, AT&T and Sun Microsystems joined together and developed System V

release 4(SVR4). Two of the main standards mainly in use are POSIX(Portable Operating System

Interface) and X/Open standard, In 1988,MIT formed Consortium X/Open developed vendor-neutral X-Window system.

The UNIX Operating System:

UNIX is time-sharing operating system (OS), which consists of kernel, file system, shell and collection of tool utilities.

Features of UNIX:

Multi-user, Multitasking, time sharing. Portability Modularity File structure Security Strong network support & advanced graphics.

Features of LINUX:

An Open-source UNIX like operating system Initially created by Linux towards for PC architecture. Developer community world-wide contribute to its enhancement and growth.

Page 4: Os Lab Details

Operating System Services:

1. Kernel: It performs, Control of execution of Processes, Scheduling of Processes, Memory management, File System service, Controlled access to peripherals.

2. Hardware Transparency: Two Modes used here, User Mode and Kernel Mode.

3. Interrupts and Exceptions: Interrupt: It is a signal by which priorities and instruction-scheduling sequence Can be controlled

Exception: It is an unexpected event like references to an illegitimate address caused by events external to process.

4. Processor Execution levels: Based on priority level of an interrupt the kernel masks certain interrupts in order that a critical process may proceed without any error.

5. Memory Management: The Concept of a virtual machine is central to memory management under UNIX.

UNIX System Architecture:

UNIX is layered operating system. It has four layers of architecture, inner most layers is for the hardware and provides services for the OS.The second layer is kernel and makes a interface for hardware system. The third layer consists of utility programs and application programs. The fourth layer is for the user interaction.

The User Perspective:

File System Processing Environment Building block primitives

Result:

Thus the Introduction of Operating System has been studied.

Page 5: Os Lab Details

BASIC LINUX GENERAL COMMANDS

Aim:

To study the basics of Unix Operating System, Commands of UNIX and Linux.

Commands used in Linux:

General Commands

1. date: This tells the current date and time.2. who: Gives the details of the user who have logged in to the system.3. whoami: Gives the details regarding login time and system’s name for connection

being used.4. man:It displays the manual page of our terminal with the command5. head and tail:’head’ is used to display the intial part of the text file and ‘tail’ is

used to display the last part of the text file.6. pwd:It displays full path name for current directory we are working in.7. ls:It displays the list of files in the current working directory.8. mkdir:it is used to create a new directory9. cd:it is used to change from working directory to any other directory to any other

directory specified.10. rmdir:it is used to remove the directory specified in the command line.11. cat:This command is used to list the contents of the file we specified.12. cp:this command is used to create duplicate copies of ordinairy files.13. mv:This command is used to rename and move ordinairy and directory files.14. ln:This is used top link a file.15. rm:This command is used to remove one or more files from directory,this can be

used to delete all files as well as directories.16. chmod:change the access permissions of a file or a directory.17. chown:change owner ID of the files or directories.18. wc: counts and displays the lines,words,and characters in the files specified.19. grep: searches the files for pattern.20. cut: cut selected fields of each line of a file.21. paste: merges the corresponding lines of given files.22. sort: arrange lines in alphabetic or numeric order.

Result:

Thus the General commands of Linux have been studied.

Page 6: Os Lab Details

VI EDITOR COMMANDS

Aim:

To study the VI editor commands.

Theory:

The vi editor is a line-oriented editor and is not very easy to use. But it is simple; you can quickly learn enough commands to use it for editing your java programs.

Command mode and edit mode:

In vi, you will need to shift from command mode to edit mode and back again.You need to be in command mode to move the cursor around one place in text to another.

Opening and exiting commands

: vi :- invokes vi with blank editing screen in command mode: vi:- filename :- invokes vi on existing file: w :- writes(saves) existing file(command mode only;<ESC> from insert mode:wFN* writes(saves) to new file(command mode only; <ESC> from insert mode:x :- writes(saves) file and exits vi (command mode only; <ESC> from insert mode:q :- quits vi without saving (command mode only; <ESC> from insert modeq! :- quits vi without saving any changes to any file (command mode only; <ESC>

from insert mode

Movement commands

:h : move cursor left one character:j : move cursor down one line:k : move cursor up one line:l : move cursor right one character :w : move cursor forward one word:b : move cursor backward one word:e : move cursor to the end of word^F : move cursor forward one screenful^B : move cursor back one screenful^D : move cursor down half screenful ^U : move cursor up half screenful

Page 7: Os Lab Details

Editing commands

Result:

Thus the VI Editor commands of Unix and Linux have been studied.

(i) :- Insert mode; inserts text before current cursor position

A Insert mode; append text following cursor positionA Insert mode; appends text at the end of lineO Open a new line below the current line and insert textO Open a new line above the current line and insert textR Replace character under cursorR Overtype mode;<ESC> terminates overtypeS Substitute following text for character at cursor position;<ESC> terminates

text entry mode S Substitute text on entire line<ESC> Return to visual command mode from insert mode.X Delete Character at cursor positionX Delete character before cursor positionDw Delete word at cursor positionDd Delete current lineEx Change text object at cursor position.P Put yanked text after or below cursorP Put yanked text before or above the cursor. Repeat last editU Undo last editU Restore current line

Page 8: Os Lab Details

EX. NO. : 1. BASIC COMMANDS IN Linux

AIM: To study the basic commands in Linux.

COMMANDS:

1. TASK : To display the system date and time. COMMAND : date. SYNTAX : date. EXPLANATION: This command displays the current system date and time on the screen.

OUTPUT : Tue Jun 19 11:37:17 GMT 2007.

2. TASK : To display the current month. COMMAND : date. SYNTAX : date +%m. EXPLANATION: This command displays the current month on the screen. OUTPUT : 06.

3. TASK : To display the name of the current month. COMMAND : date. SYNTAX : date +%h. EXPLANATION: This command displays the name of the current month on the screen. OUTPUT : Jun.

4. TASK : To display the current system date. COMMAND : date. SYNTAX : date +%d. EXPLANATION: This command displays the current system date on the screen. OUTPUT : 19.

5. TASK : To display the current system date (year). COMMAND : date. SYNTAX : date +%y. EXPLANATION: This command displays the current year on the screen. OUTPUT : 09.

Page 9: Os Lab Details

6. TASK : To display the current system time. COMMAND : date. SYNTAX : date +%H. EXPLANATION: This command displays the current system time (in hours) on the screen. OUTPUT : 11.

7. TASK : To display the current system time. COMMAND : date. SYNTAX : date +%M. EXPLANATION: This command displays the current system time (in minutes) on the screen. OUTPUT : 43.

8. TASK : To display the current system time. COMMAND : date. SYNTAX : date +%S. EXPLANATION: This command displays the current system time (in seconds) on the screen. OUTPUT : 15.

9. TASK : To display the calendar of the current month. COMMAND : calendar. SYNTAX : cal. EXPLANATION: This command displays the calendar of the current month on the screen. OUTPUT : Jun 07

S M T W T F S1 2

3 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 30

10. TASK : To display user-defined message. COMMAND : echo. SYNTAX : echo “message”. EXPLANATION: This command displays on the screen the argument of the echo command. OUTPUT : echo “OS”.

OS

Page 10: Os Lab Details

11. TASK : To display the details of all users. COMMAND : who. SYNTAX : who. EXPLANATION : This command lists the information about all the users who have logged on to that system. OUTPUT :

12. TASK : To display the user detail. COMMAND : who. SYNTAX : whoami. EXPLANATION : This command displays information about the current user of the system on the screen. OUTPUT : root.

13. TASK : To create a directory. COMMAND : make directory. SYNTAX : mkdir. EXPLANATION : This command is used to create a new directory with the specified name. EXAMPLE : mkdir student. OUTPUT : The directory “student” is created.

14. TASK : To change directory. COMMAND : change directory. SYNTAX : cd directory name. EXPLANATION : This command is used to switch from one directory to another. EXAMPLE : cd staff. OUTPUT : The directory “staff” is switched onto.

15. TASK : To delete a directory. COMMAND : remove directory. SYNTAX : rmdir directory name EXPLANATION : This command is used to delete the specified directory. EXAMPLE : rmdir student. OUTPUT : The “student” directory is deleted.

16. TASK : To come out of a sub-directory. COMMAND : change directory. SYNTAX : cd .. EXPLANATION : This command helps in switching to the main directory. OUTPUT :

Page 11: Os Lab Details

17. TASK : To list all the files and directories. COMMAND : list. SYNTAX : ls. EXPLANATION : This command displays all the files and directories of the system. OUTPUT :

18. TASK : To create a file. COMMAND : cat. SYNTAX : cat> file name. EXPLANATION : This command leads to the creation of a new file with the specified file name and contents. EXAMPLE : cat> wind. OUTPUT : A null file called “wind” is created.

19. TASK : To view a file. COMMAND : cat. SYNTAX : cat file name. EXPLANATION : This command displays the contents of the specified file. EXAMPLE : cat wind. OUTPUT : Contents of the file called “wind” will be displayed on the screen.

20. TASK : To copy a file. COMMAND : copy. SYNTAX : cp sourcefile destinationfile. EXPLANATION : This command produces a copy of the source file and is stored in the specified destination file by overwriting its previous contents. EXAMPLE : cp sun moon. OUTPUT : The contents of “sun” file will be copied to the “moon” file.

21. TASK : To move a file. COMMAND : move. SYNTAX : mv sourcefile destinationfile. EXPLANATION : After moving the contents of the source file into destination file, the source file is deleted. EXAMPLE : mv sun moon. OUTPUT : After copying contents from the “sun” file to “moon” file, the “sun” file is deleted. 22. TASK : To display / cut a column from a file. COMMAND : cut.

Page 12: Os Lab Details

SYNTAX : cut –c no. file name. EXPLANATION : This command displays the characters of a particular column in the specified file. EXAMPLE : cut –c3 moon. OUTPUT : Those characters occurring in the 3rd column of the file called “moon” are displayed.

23. TASK : To delete a file. COMMAND : remove. SYNTAX : rm file name. EXPLANATION : This command deletes the specified file from the directory. EXAMPLE : rm sun. OUTPUT : The file called “sun” will be deleted.

24. TASK : To retrieve a part of a file. COMMAND : head. SYNTAX : head -no. of rows file name. EXPLANATION : This command displays the specified no. of rows form the top of the specified file. EXAMPLE : head -1 sun. OUTPUT : The first row of the file called “sun” is displayed.

25. TASK : To retrieve a file. COMMAND : tail. SYNTAX : tail -no. of rows file name. EXPLANATION : This command displays the specified no. of rows form the bottom of the specified file. EXAMPLE : tail -1 moon. OUTPUT : The last row of the file called “moon” is displayed.

26. TASK : To sort the contents of a file. COMMAND : sort. SYNTAX : sort file name. EXPLANATION : This command helps in sorting the contents of a file in ascending order. EXAMPLE : sort win. OUTPUT : The contents of the file “win” are displayed on the screen in a sorted order.

27. TASK : To display the no. of characters in a file. COMMAND : word count. SYNTAX : wc file name.

Page 13: Os Lab Details

EXPLANATION : This command displays on the screen the no. of rows, words, and the sum of no. of characters and words. EXAMPLE : wc ball. OUTPUT : The no. of rows, words, and no. of characters present in the file

“ball” are displayed.

28. TASK : To display the calendar of a year. COMMAND : cal. SYNTAX : cal year. EXPLANATION : This command displays on the screen the calendar of the specified

year. EXAMPLE : cal 2007. OUTPUT : The calendar of the year 2007 will be displayed.***********************************************************************

OUTPUT FOR BASIC UNIX COMMANDS

Fedora release 8 (Werewolf)Kernel 2.6.23.1-42.fc8 on an i686login: studentPassword:Last login: Thu Feb 12 00:59:23 from 83.0.11.54[student@localhost ~]$ mkdir unix[student@localhost ~]$ cd unix [student@localhost unix]$ cat>file1welcome to unix[student@localhost unix]$ cat>file2basic commands[student@localhost unix]$ cat file1 file2>file3[student@localhost unix]$ cat file3welcome to unixbasic commands[student@localhost unix]$ cp file3 file4[student@localhost unix]$ cat file4welcome to unixbasic commands[student@localhost unix]$ mv file4 file5[student@localhost unix]$ cat file5welcome to unix[student@localhost unix]$ wc file2 1 3 16 file2[student@localhost unix]$ file file2file2: ASCII text[student@localhost unix]$ rm file1 file2 file3 file5

Page 14: Os Lab Details

[student@localhost unix]$ cat file3cat: file3: No such file or directory[student@localhost unix]$ cd ..[student@localhost ~]$ rmdir unix[student@localhost ~]$ file unix /*unix: directory/bin: directory/boot: directory/dev: directory/etc: directory/home: directory/lib: directory/lost+found: directory/media: directory/misc: directory/mnt: directory/net: directory/opt: directory/proc: directory/root: directory/sbin: directory/selinux: directory/srv: directory/sys: directory/tmp: sticky directory/usr: directory/var: directory[student@localhost ~]$ cat>file1welcome to unix [student@localhost ~]$ mkdir unix[student@localhost ~]$ cd unix[student@localhost unix]$ cat>file3its my third file[student@localhost unix]$ cat > file4it is my 4th file [student@localhost unix]$ ls -ltotal 16-rw-rw-r-- 1 student student 18 2009-02-12 01:12 file3-rw-rw-r-- 1 student student 18 2009-02-12 01:13 file4[student@localhost unix]$ ls file4 -l-rw-rw-r-- 1 student student 18 2009-02-12 01:13 file4 [student@localhost unix]$ chmod u+x file3 [student@localhost unix]$ chmod g+x file3[student@localhost unix]$ ls file3 -l-rwxrwxr-- 1 student student 18 2009-02-12 01:12 file3

Page 15: Os Lab Details

[student@localhost unix]$ pwd/home/student/unix[student@localhost unix]$ echo UNIXUNIX[student@localhost unix]$ ls [a-m]*file3 file4

[student@localhost unix]$ dateThu Feb 12 01:22:34 IST 2009[student@localhost unix]$ date +%m02[student@localhost unix]$ date +%aThu[student@localhost unix]$ cal February 2009Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 28

[student@localhost unix]$ cal 2009 2009

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

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

July August SeptemberSu Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 1 1 2 3 4 5 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12

Page 16: Os Lab Details

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

[student@localhost unix]$ cal 8 2010 August 2010Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31

[student@localhost unix]$ bcbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'.5*525[student@localhost unix]$ whostudent pts/1 2009-02-11 23:56 (83.0.11.200)student pts/3 2009-02-11 23:59 (83.0.11.43)student pts/9 2009-02-12 00:02 (83.0.11.45)student pts/8 2009-02-12 00:04 (83.0.11.49)student pts/4 2009-02-12 00:06 (83.0.11.46)student pts/16 2009-02-12 00:17 (83.0.11.39)student pts/18 2009-02-12 00:19 (83.0.11.52)student pts/19 2009-02-12 00:20 (83.0.11.50)student pts/6 2009-02-12 00:22 (83.0.11.56)student pts/22 2009-02-12 00:24 (83.0.11.37)student pts/24 2009-02-12 00:25 (83.0.11.41)student pts/12 2009-02-12 00:26 (83.0.11.48)student pts/13 2009-02-12 00:28 (83.0.11.7)student pts/23 2009-02-12 00:38 (83.0.11.8)student pts/15 2009-02-12 00:41 (83.0.11.156)student pts/25 2009-02-12 00:42 (83.0.11.9)student pts/7 2009-02-12 00:48 (83.0.11.38)

Page 17: Os Lab Details

student pts/2 2009-02-12 00:49 (83.11.0.35)student pts/14 2009-02-12 00:50 (83.0.11.36)student pts/26 2009-02-12 00:51 (83.0.11.59)student pts/27 2009-02-12 00:51 (83.0.11.47)student pts/5 2009-02-12 00:58 (83.0.11.44)student pts/28 2009-02-12 01:01 (83.0.11.200)student pts/21 2009-02-12 01:08 (83.0.11.42)student pts/10 2009-02-12 01:09 (83.0.11.54)student pts/20 2009-02-12 01:11 (83.0.11.55)student pts/11 2009-02-12 01:24 (83.0.11.53)

[student@localhost unix]$ who am istudent pts/28 2009-02-12 01:01 (83.0.11.200)

[student@localhost unix]$ telnet hostnametelnet: hostname: Temporary failure in name resolutionhostname: Host name lookup failure[student@localhost unix]$ telnet 83.0.11.100Trying 83.0.11.100...Connected to 83.0.11.100.Escape character is '^]'.Fedora release 8 (Werewolf)Kernel 2.6.23.1-42.fc8 on an i686login: studentPassword:Last login: Thu Feb 12 01:26:10 from 83.0.11.59[student@localhost ~]$ which cat/bin/cat[student@localhost ~]$ df -kFilesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/VolGroup00-LogVol00 109507368 5309072 98545816 6% //dev/sda6 194442 12319 172084 7% /boottmpfs 511484 0 511484 0% /dev/shm

[student@localhost ~]$ who | wc -l30[student@localhost ~]$ who | wc 30 150 1559[student@localhost ~]$

[student@localhost ~]$ comm file1 file2unix Unix[student@localhost ~]$ diff file1 file21c1

Page 18: Os Lab Details

< unix\ No newline at end of file---> Unix\ No newline at end of file[student@localhost ~]$ cat >fil1abcdefghijklm

[student@localhost ~]$ head fil1abcdefghij[student@localhost ~]$ tail fil1efghijklm

[student@localhost ~]$ cat>sp1jkdfgjkfghl;j

Page 19: Os Lab Details

job[student@localhost ~]$ sort sp1fghl;jjkdfgjkjob[student@localhost ~]$ spell sp1fghljkdfgjk[student@localhost ~]$ uniq sp1jkdfgjkfghl;jjob[student@localhost ~]$ wc sp1 2 3 18 sp1[student@localhost ~]$ cat>st1working in unixbasic unix commandsbasic shell commandsc programs in unix[student@localhost ~]$ cat st1 | sort | uniqbasic shell commandsbasic unix commandsc programs in unixworking in unix[student@localhost ~]$

Page 20: Os Lab Details

EX. NO. :2 SHELL PROGRAMMINGDATE :

A Linux shell is a command language interpreter, the primary purpose of which is to translate the command lines typed at the terminal into system actions. The shell itself is a program, through which other programs are invoked

What is a shell script ? A shell script is a file containing a list of commands to be executed by the Linux shell. shell

script provides the ability to create your own customized Linux commands Linux shell have sophisticated programming capabilities which makes shell script powerful

Linux tools

How to work with shell ?

Step1: In the dollar prompt type $ vi < file name>Where vi is the editor ,it will open a new window in which you can type the program you want

Step2:After typing the program press ESC and : together then at the bottom of the vi screen you can see i.e. prompt .In that type as wq which means write and quit i.e. the content what is typed will be written and saved into that file that has been created

Step3:Once wq is typed at the : prompt ,the prompt would change to $ symbol in which you have to do the following

$ sh < file name >

Sh – command is used to run the shell program<file name> - is the name of the file for which the output is to be got

Basically to print a text in the your shell programs echo command is used

Page 21: Os Lab Details

SYNTAX FOR LOOPING STATEMENTS

IF –THEN-ELSE CONSTRUCT

if [ condition]

then <action> else statements fi (end of if)

WHILE while <condition> do <statements> Done

CASE

Case $<option> in1) <statements>;;2) <statements>;;3) ..4)

. . . *) <error statement>;; esc

FOR loop

For(( intitialization;condition;incremetation/decrementation))

Page 22: Os Lab Details

PROGRAM 1:echo “Enter the number:”read numberi= 1fact= 1while [$i –le $number]dofact= `expr $fact \* $i `i= `expr $i +1`doneecho “The factorial of $number is $fact.”

SAMPLE OUTPUT:Enter the number:3The factorial of 3 is 6.

PROGRAM 2: echo “Enter two numbers:”read aread bc=`expr $a + $b `echo “The sum is $c.”

SAMPLE OUTPUT:

Enter two numbers:36The sum is 9.

PROGRAM 3:echo “Enter the number:”read nx= $nsum= 0while [ $n –gt 0]doy= `expr $n % 10`z= `expr $y \* $y \* $y`sum= `expr $sum + $z`n= `expr $n / 10`doneif [ $x –eq $sum]thenecho “ $x is an Armstrong number.”

Page 23: Os Lab Details

elseecho “$x is not an Armstrong number.”fi

SAMPLE OUTPUT 1:Enter a number:153153 is an Armstrong number.

SAMPLE OUTPUT 2:Enter a number:3333 is not an Armstrong number.

PROGRAM 4:echo “Enter the first number:”read aecho “Enter the second number:”read becho “Enter the third number:”read cif [$a –gt $b –a $a –gt $c]then echo “$a is greater.”elif [$b –gt $c]thenecho “$b is greater.”elseecho “$c is greater.”fi

SAMPLE OUTPUT:

Enter the first number:3Enter the second number:6Enter the third number:99 is greater.

PROGRAM 5:echo “Enter any two numbers:”read aread becho “The numbers are $a and $b.”

Page 24: Os Lab Details

c= $aa= $bb= $cecho “The swapped numbers are $a and $b.”

SAMPLE OUTPUT:

Enter any two numbers:39The numbers are 3 and 9.The swapped numbers are 9 and 3.

PROGRAM 6:a= -1b= 1c= 0echo “Enter the limit:”read necho “FIBONACCI SERIES”for (i =1; i <=n; i++) doc= `expr $a + $b`echo $ca=$bb=$cdone

SAMPLE OUTPUT:Enter the limit:5FIBONACCI SERIES01123

PROGRAM 7:echo “enter the day of the week (1-7)read ncase $n in1)echo ”1 is Sunday”;;2)echo ”2 is Monday”;;3)echo “3.is Tuesday”;;4)echo ”2 is Wednesday”;;

Page 25: Os Lab Details

5)echo “3.is thursday”;;6)echo ”2 is Friday”;;7)echo “3.is Saturday”;;*)echo ”Invalid option”;;

SAMPLE OUTPUT 1:Enter the day of the week(1-7)1Sunday

SAMPLE OUTPUT 2:Enter the day of the week(1-7)9Invalid option

PROGRAM 8:echo “ enter string 1”read s1echo “enter string2”read s2if [ $s1 == $s2] then echo “ the given string are equal” else echo “The given strings are not equal”fi

SAMPLE OUTPUT 1:

enter string 1 ram enter string 2 sam The given strings are not equal

SAMPLE OUTPUT 2: enter string 1 sam enter string 2 sam The given strings are qual

Page 26: Os Lab Details

Ex.No:3.a CREATING NEW PROCESS USING FORK AND VFORK

Aim

To write a program to create a new process using fork and vfork.

Algorithm

Fork()

1. Create child process using fork ().2. Check the process id returned by fork, if the process id is 0 displays the running process

is child.3. If the process id is non-zero display the running process is parent process.4. Show that the variables are not shared between parent and child process

Vfork().

1. Create child process using fork ().2. Check the process id returned by fork, if the process id is 0 displays the running process

is child.3. Check the process id returned by fork, if the process id is non-zero display the running

process is parent.4. Show how the variables are not shared between parent and child process.

psudo code:

FORK() & VFORK()

**********pid=fork();if(pid==0){printf("\n\t\t\t\tchild process\n");data=data+100;printf("process id=%d\n",getpid());printf("process id of the parent is=%d\n",getppid());printf("Value of data is %d\n",data);}else{

***************}}

Page 27: Os Lab Details

SAMPLE OUTPUT: Child process Process id=4474 Process id of the parent is=4473 Value of data is 300 Parent process Process id of the parent=4473 Process id=4473 Value of data is 200

Result: Thus creating new process using fork and vfork has been verified.

Ex.No:3.b HANDLING WAIT, EXIT AND SLEEP WITHIN PROCESS

Aim

To write a program using wait, sleep and exit system calls.

Algorithm

1. Create child process using fork() system call.2. Check the process id,if the process id is 0 then display that the running process is child.3. Delay the child process completion by using sleep() system call.4. Make the parent process do wait for the child process completion by using wait system

call.5. Use exit() system call in child process after sleep time gets expired.

pseudo code:

*****************if((k=wait(&status))!=pid)printf("Error");else if(WIFEXITED(status)){printf("NORMAL TERMINATION,EXIT STATUS=%d\n",WEXITSTATUS(status));

}elseprintf("OTHER THAN NORMAL TERMINATION");exit(0);}}

SAMPLE OUTPUT:

CHILD PROCESS ID=6144

Page 28: Os Lab Details

PARENT PROCESSNORMAL TERMINATION,EXIT STATUS=7

Result : Thus the program for handling wait,exit and sleep within process has been executed.

Ex.No:4a IMPLEMENTATION OF LS SYSTEM CALL

Aim:

To implement the unix command ‘ls’ which displays the files in the directory.Algorithm:

1. Include the header file dirent.h.2. DIR is the internal structure to maintain information about the directory being read.3. DECLARE the structures dirent to get the files in the directory.4. Open the directory given at the command line.5. Set a while loop to read the files under the directory.6. Display the names of the files that reside in the given directory.7. Lose the directory that was opened.

Pseudocode:******************************************************getcwd(pathname);n=scandir(pathname,&namelist,0,alphasort);if(n<0)printf("error");else{for(i=0;i<n;i++)printf("%s \n",namelist[i]->d_name);}

OUTPUT:[44208104008@localhost oslab]$ cc ls.c[44208104008@localhost oslab]$ ./a.out....contiguous.c.swo.contiguous.c.swp.linked.c.swp.worstfit.c.swpINPUTRESULT: Thus implementation of the UNIX commands ‘ls ’which displays the files in the directory is executed and the output is verified.

Ex.No:4b IMPLEMENTATION OF GREP SYSTEM CALLAim:

Page 29: Os Lab Details

To implement the unix command ‘grep’ which displays the files in the directory.Algorithm:

1. Include the header file dirent.h.2. DIR is the internal structure to maintain information about the directory being read.3. DECLARE the structures dirent to get the files in the directory.4. Open the directory given at the command line.5. Set a while loop to read the files under the directory.6. Display the names of the files that reside in the given directory.7. Lose the directory that was opened.

Pseudocode:*****************if(a[j]==b[0])i++;j++;}if(i!=0)printf("The char is found");elseprintf("\n Not found");********************SAMPLE OUTPUT:[44208104008@localhost cc grep.c[44208104008@localhost oslab]$ ./a.outEnter the string : Bala Enter the char to be searched : l The char is foundRESULT: Thus implementation of the UNIX commands ‘grep’which displays the files in the directory is executed and the output is verified.

Ex.No:5 IMPLEMENTATION OF FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Aim:To write a program to implement the FCFS scheduling algorithm

Algorithm:1. Start the process2. Declare the array size3. Get the number of processes to be inserted4. Get the value5. Start with the first process from it’s initial position let other process to be in queue6. Calculate the total number of burst time 7. Display the values8. Stop the processProgram pseudocode:

*******printf("enter the number of process");

scanf("%d",&n);

printf("enter the burst times");

for(i=0;i<n;i++)

Page 30: Os Lab Details

scanf("%d",&b[i]);

printf("\nenter the arrival times");

for(i=0;i<n;i++) scanf("%d",&a[i]); g[0]=0;

for(i=0;i<10;i++) g[i+1]=g[i]+b[i];

for(i=0;i<n;i++)

{ w[i]=g[i]-a[i]; t[i]=g[i+1]-a[i]; awt=awt+w[i];

att=att+t[i]; }

awt =awt/n; att=att/n; printf("\n\tprocess\twaiting time\tturn arround time\

n");

for(i=0;i<n;i++)

{ printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);

}

printf("the average waiting time is %f\n",awt);

printf("the average turn around time is %f\n",att);

Sample Output:

enter the number of process 4

enter the burst times

4 9 8 3

enter the arrival times

0 2 4 3

process waiting time turn arround time

p0 0 4

p1 2 11

p2 9 17

p3 18 21

the average waiting time is 7.250000

the average turn around time is 13.250000

Result:

Thus the program for FCFS is implemented and verified.

Page 31: Os Lab Details

Ex.No.6.IMPLEMENTATION OF SHORTEST JOB FIST SCHEDULING ALGORITHM

Aim:To implement the shortest job first scheduling algorithm

Algorithm:1. Start the process2. Declare the array size3. Get the number of elements to be inserted4. Select the process which have shortest burst will execute first5. If two process have same burst length then FCFS scheduling algorithm used6. Make the average waiting the length of next process7. Start with the first process from it’s selection as above and let other process to be in queue6. Calculate the total number of burst time 7. Display the values8. Stop the processProgram pseudocode:

******for(i=0;i<10;i++)

{ b[i]=0;w[i]=0; }

printf("enter the number of process");

att=att+t[i];

}

awt=awt/n;

att=att/n;

printf("\n\t process \t waiting time \t turn around time \n");

Sample Output:

enter the number of process 5

enter the burst times

2 4 5 6 8

process waiting time turn around time

p[0] 0 2

p[1] 2 6

p[2] 6 11

p[3] 11 17

p[4] 17 25

Page 32: Os Lab Details

the average waitingtime is 7.200000

the average turn around time is 12.200000

Result The program for SJF was implemented and hence verified

Ex.No:7 IMPLEMENTATION OF ROUND ROBIN SCHEDULING ALGORITHMAim:

To write a program to implement the round robin scheduling algorithmAlgorithm:1. Start the process2. Declare the array size3. Get the number of elements to be inserted4. Get the value5. Set the time sharing system with preemption6. Define quantum is defined from 10 to 100ms7. Declare the queue as a circular8. Make the CPU scheduler goes around the ready queue allocating CPU to each process for the time interval specified9. Make the CPU scheduler picks the first process and sets time to interrupt after quantum expired dispatches the process If the process have burst less than the time quantum than the process release the CPU10. If the process have bust greater then time quantum then time will go off and cause interrupt to OS and the process put into the tail of ready queue and the schedule select next process11. Display the results12. Stop the processSample Output

Enter the no. of process: 4

Enter the process name and burst time for the process

Enteer the process name: 1

Enter burst time for the process 1: 8

Enteer the process name: 2

Enter burst time for the process 2: 3

Enteer the process name: 3

Enter burst time for the process 3: 6

Enteer the process name: 4

Enter burst time for the process 4: 1

PROCESS NAME BURST TIME

1 8

Page 33: Os Lab Details

2 3

3 6

4 1

PRESS 1.ROUND ROBIN 2.EXIT

1

ROUND ROBIN SCHEDULING

Enter the time slice:

2

PROCESS NAME REMAINING TIME TOTAL TIME

1 6 2

2 1 4

3 4 6

4 0 7

1 4 9

2 0 10

3 2 12

1 2 14

3 0 16

Result The program for round robin scheduling was implemented and hence verified

Ex.No.8.PRIORITY SCHEDULING ALGORITHMAim: To write a program for the priority scheduling algorithm.Description:

A priority is associated with each process and the CPU is allocated to the process with the highest priority.

Priority scheduling can be either preemptive and non-preemptive When a process arrives at the ready queue, if the priority of newly arrived process is

higher than it will preempt the currently running process. A non-preemptive priority scheduling algorithm will simply put the new process at the

head of the ready queues. Algorithm:

1. Define the process with the process id, process writing time pwt, priority-pr, burst time –bt using structure.

2. Initialize the total waiting time (tt) to zero.3. Read the number of process in the queue4. Read the process id and process burst time for n jobs

Page 34: Os Lab Details

5. Read the process priority 6. Arrange the process according to their priority assigned.

pseudo Code:********printf("\n Executing Process Priority Scheduling\n");fc[0].wait=0;for(i=0;i<n;i++){fc[i+1].wait=fc[i].st+fc[i].wait;tot_wait=tot_wait+fc[i].wait;}printf("\nProcess\tService\tPriority\tWaiting Time\n");for(k=0;k<n;k++){printf("\n%s\t%d\t%d\t%d\n",fc[k].p,fc[k].st,fc[k].pr,fc[k].wait);}avg=tot_wait/n;printf("\n Average Waiting Time=%f\n",avg);}********SAMPLE OUTPUT:enter the number of process: 3 Enter the Process Name: aenter service time: 3Enter the Priority: 2 Enter the Process Name: benter service time: 5Enter the Priority: 1Enter the Process Name: center service time: 5Enter the Priority: 6Executing Process Priority Schedulingprocess priority service waiting time b 5 1 0

a 3 2 5

c 5 6 8 average waiting time:4.000000Result:

Thus the program for priority scheduling has been executed.

Ex no: 9 INTERPROCESS COMMUNICATION

Aim To write a program for inter process communication using pipes.

Page 35: Os Lab Details

Algorithm1. Create parent and child process using fork().2. Create the pipe using pipe() command.3. Close the write end when read is performed.4. Close the read end when write is performed.

pseudo code: ********

printf("\n\t PARENT PROCESS ID: %d\n",pid,fd[1]);close(fd[1]);read(fd[0],buff,32);printf("\n\n\t THE MESSAGE IS RECEIVED FROM CHILD\n");printf("\n\n THE MESSAGE:%s",buff);*****

SAMPLE OUTPUT: CHILD PROCESS

CHILD PROCESS:0 PIPE ID IS :3PARENT PROCESS ID: 7201 THE MESSAGE IS RECEIVED FROM CHILD THE MESSAGE: UNIX WORLD

Result:

Thus the program for inter process communication has been executed.Ex.No.10.PRODUCER CONSUMER PROBLEM

Aim To write a program for producer consumer problemAlgorithm

1. Create parent and child process using fork().2. Create the mutex using command.3. Close the write end when read is performed.4. Close the wait when signal is performed.

Pseudocode:*******int signal(int);printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");while(1){

printf("\nENTER YOUR CHOICE\n");scanf("%d",&n);

switch(n)void producer(){

mutex=wait(mutex);full=signal(full);empty=wait(empty);

Page 36: Os Lab Details

x++;printf("\nproducer produces the item%d",x);mutex=signal(mutex);

}void consumer(){

mutex=wait(mutex);full=wait(full);empty=signal(empty);printf("\n consumer consumes item%d",x);x--;mutex=signal(mutex);

}SAMPLE OUTPUT:-1.PRODUCER2.CONSUMER3.EXITENTER YOUR CHOICE1producer produces the item1ENTER YOUR CHOICE1producer produces the item2ENTER YOUR CHOICE1

producer produces the item3Ex.No.11 MEMORY MANAGEMENT Aim:

To implement first fit memory management technique.Algorithm:

1. Declare the structure memory with the process id, status start and end of the node and their difference.

2. Get the number of nodes and ending address of the node which defines the memory.3. Select the option from the menu tot insert, delete, display or to exit.4. If insert option is selected, get the process id and size of the process.5. The size of the process files in the memory, then print found, else display as no memory

space.6. Give the status of the process inserted to be 1.7. If delete option is selected, then get the id of the process to be deleted and change the pid

and status to 0.8. If display option is selected then display the process with there id, start and end of node,

size and status.9. Exit is chosen then stop the execution.

pseudo code:*********for(i=0;i<np;i++){printf ("Enter mem required for process P%d = ",i+1);scanf("%d", &pr[i]);

Page 37: Os Lab Details

}printf("\nMemory Partition Information");printf("\n-------------------------\n");for(j=0;j<nb;j++){printf("Enter Block size of Block B%d = ",j+1);scanf ("%d",&mempart[j]);}while (1){printf("1.First Fit");printf("\n2.Best Fit");printf("\n3.Worst Fit");printf("\n4.Exit");printf("\nEnter Your Choice");scanf("%d",&ch);switch(ch){case 1:printf("\nFirst Fit");printf("\n=============\n");firstfit();break;case 2:printf("\nBest Fit");printf("\n=============\n");bestfit();break;case 3:printf("\nWorst Fit");printf("\n=============\n");worstfit();break;case 4:exit(0);}************************/SAMPLE OUTPUT:Memory Allocation PolicyEnter number of Process 4Enter number of Partition blocks4Process Information-------------------------Enter mem required for process P1 = 500Enter mem required for process P2 = 50Enter mem required for process P3 = 110Enter mem required for process P4 = 300

Page 38: Os Lab Details

Memory Partition Information-------------------------Enter Block size of Block B1 = 100Enter Block size of Block B2 = 200Enter Block size of Block B3 = 150Enter Block size of Block B4 = 6001.First Fit2.Best Fit3.Worst Fit4.ExitEnter Your Choice1

First Fit=============P/D Pr.mem B/D Block New Block--------------------------------------------------

1 500 4 6001002 50 1 100503 110 2 200904 300 Insufficient memoryPress Enter to continue....1. First fit2. Best Fit3. Worst Fit4. Exit

Result: Thus Memory management techniques are implemented and studied.

Ex.No.12. IMPLEMENTATION OF FIFO PAGE

REPLACEMENT ALGORITHM

AimTo write a program to implement FIFO page replacement algorithm

Algorithm1. Start the process2. Declare the size with respect to page length3. Check the need of replacement from the page to memory4. Check the need of replacement from old page to new page in memory5. Forma queue to hold all pages6. Insert the page require memory into the queue

Page 39: Os Lab Details

7. Check for bad replacement and page fault8. Get the number of processes to be inserted9. Display the values10. Stop the processProgram pseudocode: **********************

for(i=1;i<=n;i++) { printf("%d\t\t",a[i]); avail=0; for(k=0;k<no;k++)

if(frame[k]==a[i]) avail=1; if (avail==0) { frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%d\t",frame[k]);**************************/Sample Output

ENTER THE NUMBER OF PAGES: 20

ENTER THE PAGE NUMBER : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

ENTER THE NUMBER OF FRAMES :3

ref string page frames7 7 -1 -10 7 0 -11 7 0 12 2 0 103 2 3 1 Page Fault Is 15

Result

The program for FIFO page replacement was implemented and hence verified.

Ex.No.13 IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHMAim

To write a program a program to implement LRU page replacement algorithmAlgorithm 1. Start the process2. Declare the size3. Get the number of pages to be inserted4. Get the value

Page 40: Os Lab Details

5. Declare counter and stack6. Select the least recently used page by counter value7. Stack them according the selection.8. Display the values9. Stop the processProgram pseudocode:

******************for(i=1;i<n;i++)

{ c1=0; for(j=0;j<f;j++) { if(p[i]!=q[j]) c1++; } if(c1==f){ { c++; if(k<f) { q[k]=p[i]; k++; for(j=0;j<k;j++) printf("\t%d",q[j]); printf("\n"); } Sample outputEnter no of pages:10

Enter the reference string:7 5 9 4 3 7 9 6 2 1

Enter no of frames:3

7

7 5

7 5 9

4 5 9

4 3 9

4 3 7

9 3 7

9 6 7

9 6 2

1 6 2

The no of page faults is 10

Page 41: Os Lab Details

Result:The program for LRU page replacement was implanted and hence verified

Ex.No.14.a .IMPLEMENTATION OF CONTIGUOUS FILE ALLOCATIONAim

To write a program a program to implement contiguous file allocationAlgorithm 1. Start the process2. Declare the size3. Get the number of pages to be inserted4. Get the value5. Declare counter and stack6. Select the least recently used page by counter value7. Stack them according the selection.8. Display the values9. Stop the processPseudocode:************************printf("\n Enter the no of blocks");scanf("%d",&n);for(i=0;i<=n;i++){b[i].b_id=i;b[i].b_alloted=0;}printf("Enter the no of block already alloted");scanf("%d",&n0);for(i=0;i<n0;i++){printf("\n Enter the block");scanf("%d",&bname);b[bname].b_alloted=100;}printf("\n Enter process name \n");*******************

SAMPLE OUTPUT:[44208104008@localhost oslab]$ cc contiguous.c[44208104008@localhost oslab]$ ./a.out Enter the no of blocks3Enter the no of block already alloted2 Enter the block1 Enter the block2 Enter process name5Enter process size2 Process not allocated

Page 42: Os Lab Details

b[0]-- 0 b[1]-- 100 b[2]—100

Ex.No.14.b. IMPLEMENTATION OF LINKED FILE ALLOCATION TECHNIQUEAim

To write a program a program to implement linked file allocationAlgorithm 1. Start the process2. Declare the size3. Get the number of pages to be inserted4. Get the value5. Declare counter and stack6. Select the linked file used page by counter value7. Stack them according the selection.8. Display the values9. Stop the processPseudocode:********printf("Enter no of block allocated");scanf("%d",&n0);if(n0>n)flg=1;}while(flg=1);for(i=0;i<n0;i++){printf("Enter the block no");do{if(flg2==1){printf("Block no does not exits..Re-enter\n");flg2=0;}scanf("%d",&bname);************}}else{printf("NO space to allocated\n");}printf("\n");}SAMPLE OUTPUT:Enter No.of blocks 3Enter No. of blocks allocate 1Enter block no 2Process Name is A

Page 43: Os Lab Details

Enter process size 2Linkage structure of blocks0 ---> 1 --->b[0]--A b[1]--Ab[2]--Z

Ex.No.14.c IMPLEMENTATION OF INDEX FILE ALLOCATION TECHNIQUEAim

To write a program a program to implement index file allocationAlgorithm 1. Start the process2. Declare the size3. Get the number of pages to be inserted4. Get the value5. Declare counter and stack6. Select the index file used page by counter value7. Stack them according the selection.8. Display the values9. Stop the processPseudocode:*******************************printf("Enter the block no");do{if(flg2==1){printf("Block no does not exits..Re-enter\n");flg2=0;}scanf("%d",&bname);if(bname>=n)flg2=1;}while(flg2==1);******************Sample output:allocated 2Enter Block no 2Enter block no 4Process Name is AEnter process size 3Linkage Structure of Blocks0---> 1--->3 --->b[0]--A b[i]--Ab[2]--Z b[3]--A b[4]==zb[5]==# b[6]-- b[7]--Directory stuructureProcess Index

Page 44: Os Lab Details

5#