You are on page 1of 44

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

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. 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 1960s. 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.

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.

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 systems name for connection being used. 4. man:It displays the manual page of our terminal with the command 5. 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 directory 9. 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.

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 :: vi:: w ::wFN* :x ::q :q! :invokes vi with blank editing screen in command mode filename :- invokes vi on existing file writes(saves) existing file(command mode only;<ESC> from insert mode writes(saves) to new file(command mode only; <ESC> from insert mode writes(saves) file and exits vi (command mode only; <ESC> from insert mode quits vi without saving (command mode only; <ESC> from insert mode quits vi without saving any changes to any file (command mode only; <ESC> from insert mode Movement commands :h : :j : :k : :l : :w : :b : :e : ^F : ^B : ^D : ^U : move cursor left one character move cursor down one line move cursor up one line move cursor right one character move cursor forward one word move cursor backward one word move cursor to the end of word move cursor forward one screenful move cursor back one screenful move cursor down half screenful move cursor up half screenful

Editing commands

(i) :A A O O R R S

Insert mode; inserts text before current cursor position

Insert mode; append text following cursor position Insert mode; appends text at the end of line Open a new line below the current line and insert text Open a new line above the current line and insert text Replace character under cursor Overtype mode;<ESC> terminates overtype 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 position X Delete character before cursor position Dw Delete word at cursor position Dd Delete current line Ex Change text object at cursor position. P Put yanked text after or below cursor P Put yanked text before or above the cursor . Repeat last edit U Undo last edit U Restore current line

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

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 COMMAND : To display the current system date (year). : date.

SYNTAX : date +%y. EXPLANATION: This command displays the current year on the screen. OUTPUT : 09. 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 S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 10. TASK COMMAND SYNTAX : To display user-defined message. : echo. : echo message.

EXPLANATION: This command displays on the screen the argument of the echo command. OUTPUT : echo OS. OS 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 COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT : To delete a directory. : remove directory. : rmdir directory name : This command is used to delete the specified directory. : rmdir student. : 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 : 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 COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 20. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 21. TASK COMMAND SYNTAX EXPLANATION EXAMPLE : To view a file. : cat. : cat file name. : This command displays the contents of the specified file. : cat wind. : Contents of the file called wind will be displayed on the screen. : To copy a file. : copy. : cp sourcefile destinationfile. : This command produces a copy of the source file and is stored in the specified destination file by overwriting its previous contents. : cp sun moon. : The contents of sun file will be copied to the moon file. : To move a file. : move. : mv sourcefile destinationfile. : After moving the contents of the source file into destination file, the source file is deleted. : 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. 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 COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 24. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 25. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT 26. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT : To delete a file. : remove. : rm file name. : This command deletes the specified file from the directory. : rm sun. : The file called sun will be deleted. : To retrieve a part of a file. : head. : head -no. of rows file name. : This command displays the specified no. of rows form the top of the specified file. : head -1 sun. : The first row of the file called sun is displayed. : To retrieve a file. : tail. : tail -no. of rows file name. : This command displays the specified no. of rows form the bottom of the specified file. : tail -1 moon. : The last row of the file called moon is displayed. : To sort the contents of a file. : sort. : sort file name. : This command helps in sorting the contents of a file in ascending order. : sort win. : The contents of the file win are displayed on the screen in a

sorted order. 27. TASK COMMAND SYNTAX EXPLANATION EXAMPLE OUTPUT : To display the no. of characters in a file. : word count. : wc file name. : This command displays on the screen the no. of rows, words, and the sum of no. of characters and words. : wc ball. : The no. of rows, words, and no. of characters present in the file ball are displayed.

28. TASK COMMAND SYNTAX EXPLANATION

: To display the calendar of a year. : cal. : cal year. : 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 i686 login: student Password: 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>file1 welcome to unix [student@localhost unix]$ cat>file2 basic commands [student@localhost unix]$ cat file1 file2>file3 [student@localhost unix]$ cat file3 welcome to unix basic commands [student@localhost unix]$ cp file3 file4 [student@localhost unix]$ cat file4 welcome to unix basic commands [student@localhost unix]$ mv file4 file5 [student@localhost unix]$ cat file5 welcome to unix

[student@localhost unix]$ wc file2 1 3 16 file2 [student@localhost unix]$ file file2 file2: ASCII text [student@localhost unix]$ rm file1 file2 file3 file5 [student@localhost unix]$ cat file3 cat: 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>file1 welcome to unix [student@localhost ~]$ mkdir unix [student@localhost ~]$ cd unix [student@localhost unix]$ cat>file3 its my third file [student@localhost unix]$ cat > file4 it is my 4th file [student@localhost unix]$ ls -l total 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 [student@localhost unix]$ pwd /home/student/unix [student@localhost unix]$ echo UNIX UNIX [student@localhost unix]$ ls [a-m]* file3 file4 [student@localhost unix]$ date Thu Feb 12 01:22:34 IST 2009 [student@localhost unix]$ date +%m 02 [student@localhost unix]$ date +%a Thu [student@localhost unix]$ cal February 2009 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [student@localhost unix]$ cal 2009 2009 January Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 April Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 February Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 9 20 21 22 23 24 25 26 27 28 May Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 March Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 June Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

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

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

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

[student@localhost unix]$ cal 8 2010 August 2010 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [student@localhost unix]$ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 5*5 25 [student@localhost unix]$ who student 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 student student student student student student student student student student student student student student

pts/13 pts/23 pts/15 pts/25 pts/7 pts/2 pts/14 pts/26 pts/27 pts/5 pts/28 pts/21 pts/10 pts/20 pts/11

2009-02-12 00:28 (83.0.11.7) 2009-02-12 00:38 (83.0.11.8) 2009-02-12 00:41 (83.0.11.156) 2009-02-12 00:42 (83.0.11.9) 2009-02-12 00:48 (83.0.11.38) 2009-02-12 00:49 (83.11.0.35) 2009-02-12 00:50 (83.0.11.36) 2009-02-12 00:51 (83.0.11.59) 2009-02-12 00:51 (83.0.11.47) 2009-02-12 00:58 (83.0.11.44) 2009-02-12 01:01 (83.0.11.200) 2009-02-12 01:08 (83.0.11.42) 2009-02-12 01:09 (83.0.11.54) 2009-02-12 01:11 (83.0.11.55) 2009-02-12 01:24 (83.0.11.53)

[student@localhost unix]$ who am i student pts/28 2009-02-12 01:01 (83.0.11.200) [student@localhost unix]$ telnet hostname telnet: hostname: Temporary failure in name resolution hostname: Host name lookup failure [student@localhost unix]$ telnet 83.0.11.100 Trying 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 i686 login: student Password: Last login: Thu Feb 12 01:26:10 from 83.0.11.59 [student@localhost ~]$ which cat /bin/cat [student@localhost ~]$ df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00 109507368 5309072 98545816 6% / /dev/sda6 194442 12319 172084 7% /boot tmpfs 511484 0 511484 0% /dev/shm [student@localhost ~]$ who | wc -l 30 [student@localhost ~]$ who | wc 30 150 1559 [student@localhost ~]$

[student@localhost ~]$ comm file1 file2 unix Unix [student@localhost ~]$ diff file1 file2 1c1 < unix \ No newline at end of file --> Unix \ No newline at end of file [student@localhost ~]$ cat >fil1 a b c d e f g h i j k l m [student@localhost ~]$ head fil1 a b c d e f g h i j [student@localhost ~]$ tail fil1 e f g h i j k l m

[student@localhost ~]$ cat>sp1 jkdfgjk fghl;j job [student@localhost ~]$ sort sp1 fghl;j jkdfgjk job [student@localhost ~]$ spell sp1 fghl jkdfgjk [student@localhost ~]$ uniq sp1 jkdfgjk fghl;j job [student@localhost ~]$ wc sp1 2 3 18 sp1 [student@localhost ~]$ cat>st1 working in unix basic unix commands basic shell commands c programs in unix [student@localhost ~]$ cat st1 | sort | uniq basic shell commands basic unix commands c programs in unix working in unix [student@localhost ~]$

EX. NO. :2 DATE :

SHELL PROGRAMMING

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

SYNTAX FOR LOOPING STATEMENTS IF THEN-ELSE CONSTRUCT if [ condition] then else <action>

statements fi (end of if) WHILE while <condition> do <statements> Done CASE Case $<option> in 1) <statements>;; 2) <statements>;; 3) .. 4) . . . *) <error statement>;; esc FOR loop For(( intitialization;condition;incremetation/decrementation))

PROGRAM 1: echo Enter the number: read number i= 1 fact= 1 while [$i le $number] do fact= `expr $fact \* $i ` i= `expr $i +1` done echo The factorial of $number is $fact. SAMPLE OUTPUT: Enter the number: 3 The factorial of 3 is 6. PROGRAM 2: echo Enter two numbers: read a read b c=`expr $a + $b ` echo The sum is $c. SAMPLE OUTPUT: Enter two numbers: 3 6 The sum is 9. PROGRAM 3: echo Enter the number: read n x= $n sum= 0 while [ $n gt 0] do y= `expr $n % 10` z= `expr $y \* $y \* $y` sum= `expr $sum + $z` n= `expr $n / 10` done if [ $x eq $sum] then echo $x is an Armstrong number.

else echo $x is not an Armstrong number. fi SAMPLE OUTPUT 1: Enter a number: 153 153 is an Armstrong number. SAMPLE OUTPUT 2: Enter a number: 33 33 is not an Armstrong number. PROGRAM 4: echo Enter the first number: read a echo Enter the second number: read b echo Enter the third number: read c if [$a gt $b a $a gt $c] then echo $a is greater. elif [$b gt $c] then echo $b is greater. else echo $c is greater. fi SAMPLE OUTPUT: Enter the first number: 3 Enter the second number: 6 Enter the third number: 9 9 is greater. PROGRAM 5: echo Enter any two numbers: read a read b echo The numbers are $a and $b.

c= $a a= $b b= $c echo The swapped numbers are $a and $b. SAMPLE OUTPUT: Enter any two numbers: 3 9 The numbers are 3 and 9. The swapped numbers are 9 and 3. PROGRAM 6: a= -1 b= 1 c= 0 echo Enter the limit: read n echo FIBONACCI SERIES for (i =1; i <=n; i++) do c= `expr $a + $b` echo $c a=$b b=$c done SAMPLE OUTPUT: Enter the limit: 5 FIBONACCI SERIES 0 1 1 2 3 PROGRAM 7: echo enter the day of the week (1-7) read n case $n in 1)echo 1 is Sunday;; 2)echo 2 is Monday;; 3)echo 3.is Tuesday;; 4)echo 2 is Wednesday;;

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) 1 Sunday SAMPLE OUTPUT 2: Enter the day of the week(1-7) 9 Invalid option PROGRAM 8: echo enter string 1 read s1 echo enter string2 read s2 if [ $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

Ex.No:3.a Aim

CREATING NEW PROCESS USING FORK AND VFORK

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 { *************** } }

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 Aim To write a program using wait, sleep and exit system calls. Algorithm 1. 2. 3. 4. Create child process using fork() system call. Check the process id,if the process id is 0 then display that the running process is child. Delay the child process completion by using sleep() system call. 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)); } else printf("OTHER THAN NORMAL TERMINATION"); exit(0); } } SAMPLE OUTPUT: CHILD PROCESS ID=6144 HANDLING WAIT, EXIT AND SLEEP WITHIN PROCESS

PARENT PROCESS NORMAL TERMINATION,EXIT STATUS=7 Result: Thus the program for handling wait,exit and sleep within process has been executed.

Ex.No:4a
Aim:

IMPLEMENTATION OF LS SYSTEM CALL

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.swp INPUT RESULT: Thus implementation of the UNIX commands ls which displays the files in the directory is executed and the output is verified.

Ex.No:4b
Aim:

IMPLEMENTATION OF GREP SYSTEM CALL

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"); else printf("\n Not found");******************** SAMPLE OUTPUT:[44208104008@localhost cc grep.c [44208104008@localhost oslab]$ ./a.out Enter the string : Bala Enter the char to be searched : l The char is found RESULT: Thus implementation of the UNIX commands grepwhich displays the files in the directory is executed and the output is verified.

Ex.No:5
Aim:

IMPLEMENTATION OF FIRST COME FIRST SERVE SCHEDULING ALGORITHM

To write a program to implement the FCFS scheduling algorithm Algorithm: 1. Start the process 2. Declare the array size 3. Get the number of processes to be inserted 4. Get the value 5. Start with the first process from its initial position let other process to be in queue 6. Calculate the total number of burst time 7. Display the values 8. Stop the process Program pseudocode: *******printf("enter the number of process"); scanf("%d",&n); printf("enter the burst times"); for(i=0;i<n;i++) scanf("%d",&b[i]);

printf("\nenter the arrival times"); for(i=0;i<n;i++) for(i=0;i<10;i++) for(i=0;i<n;i++) { w[i]=g[i]-a[i]; att=att+t[i]; awt =awt/n; time\n"); for(i=0;i<n;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 p0 p1 p2 p3 waiting time 0 2 9 18 turn arround time 4 11 17 21 printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]); t[i]=g[i+1]-a[i]; } att=att/n; printf("\n\tprocess\twaiting time\tturn arround awt=awt+w[i]; scanf("%d",&a[i]); g[i+1]=g[i]+b[i]; g[0]=0;

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.

Ex.No.6.IMPLEMENTATION OF SHORTEST JOB FIST SCHEDULING ALGORITHM Aim: To implement the shortest job first scheduling algorithm Algorithm: 1. Start the process 2. Declare the array size 3. Get the number of elements to be inserted 4. Select the process which have shortest burst will execute first 5. If two process have same burst length then FCFS scheduling algorithm used 6. Make the average waiting the length of next process 7. Start with the first process from its selection as above and let other process to be in queue 6. Calculate the total number of burst time 7. Display the values 8. Stop the process Program 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 p[0] p[1] p[2] p[3] p[4] waiting time 0 2 6 11 17 turn around time 2 6 11 17 25

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 ALGORITHM Aim: To write a program to implement the round robin scheduling algorithm Algorithm: 1. Start the process 2. Declare the array size 3. Get the number of elements to be inserted 4. Get the value 5. Set the time sharing system with preemption 6. Define quantum is defined from 10 to 100ms 7. Declare the queue as a circular 8. Make the CPU scheduler goes around the ready queue allocating CPU to each process for the time interval specified 9. 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 CPU 10. 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 process 11. Display the results 12. Stop the process Sample 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 1 2 BURST TIME 8 3

3 4 1

6 1

PRESS 1.ROUND ROBIN 2.EXIT ROUND ROBIN SCHEDULING Enter the time slice: 2 PROCESS NAME 1 2 3 4 1 2 3 1 3 Result The program for round robin scheduling was implemented and hence verified Ex.No.8.PRIORITY SCHEDULING ALGORITHM Aim: 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 queue 4. Read the process id and process burst time for n jobs 5. Read the process priority 6. Arrange the process according to their priority assigned. REMAINING TIME TOTAL TIME 6 1 4 0 4 0 2 2 0 2 4 6 7 9 10 12 14 16

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: a enter service time: 3 Enter the Priority: 2 Enter the Process Name: b enter service time: 5 Enter the Priority: 1 Enter the Process Name: c enter service time: 5 Enter the Priority: 6Executing Process Priority Scheduling process priority service waiting time b a 5 3 1 2 0 5 8

c 5 6 average waiting time:4.000000 Result:

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. Algorithm 1. 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 :3 PARENT 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 problem Algorithm 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); 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.PRODUCER 2.CONSUMER 3.EXIT ENTER YOUR CHOICE 1 producer produces the item1 ENTER YOUR CHOICE 1 producer produces the item2 ENTER YOUR CHOICE 1

producer produces the item3 Ex.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]); } 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 Policy Enter number of Process 4 Enter number of Partition blocks4 Process Information ------------------------Enter mem required for process P1 = 500 Enter mem required for process P2 = 50 Enter mem required for process P3 = 110 Enter mem required for process P4 = 300 Memory Partition Information -------------------------

Enter Block size of Block B1 = 100 Enter Block size of Block B2 = 200 Enter Block size of Block B3 = 150 Enter Block size of Block B4 = 600 1.First Fit 2.Best Fit 3.Worst Fit 4.Exit Enter Your Choice1 First Fit ============= P/D Pr.mem B/D Block New Block -------------------------------------------------1 500 4 600 100 2 50 1 100 50 3 110 2 200 90 4 300 Insufficient memory Press Enter to continue.... 1. First fit 2. Best Fit 3. Worst Fit 4. Exit Result: Thus Memory management techniques are implemented and studied. Ex.No.12. IMPLEMENTATION OF FIFO PAGE REPLACEMENT ALGORITHM Aim To write a program to implement FIFO page replacement algorithm Algorithm 1. Start the process 2. Declare the size with respect to page length 3. Check the need of replacement from the page to memory 4. Check the need of replacement from old page to new page in memory 5. Forma queue to hold all pages 6. Insert the page require memory into the queue 7. Check for bad replacement and page fault 8. Get the number of processes to be inserted

9. Display the values 10. Stop the process Program 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 : ref string 7 0 1 2 0 3 Result page frames 7 -1 -1 7 0 -1 7 0 1 2 0 1 2 3 1 Page Fault Is 15 70120304230321201701 ENTER THE NUMBER OF FRAMES :3

The program for FIFO page replacement was implemented and hence verified. Ex.No.13 IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM Aim To write a program a program to implement LRU page replacement algorithm Algorithm 1. Start the process 2. Declare the size 3. Get the number of pages to be inserted 4. Get the value 5. Declare counter and stack 6. Select the least recently used page by counter value

7. Stack them according the selection. 8. Display the values 9. Stop the process Program 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 output Enter 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 7 4 4 4 9 9 9 1 5 5 5 3 3 3 6 6 6 9 9 9 7 7 7 2 2

The no of page faults is 10 Result: The program for LRU page replacement was implanted and hence verified

Ex.No.14.a .IMPLEMENTATION OF CONTIGUOUS FILE ALLOCATION Aim To write a program a program to implement contiguous file allocation Algorithm 1. Start the process 2. Declare the size 3. Get the number of pages to be inserted 4. Get the value 5. Declare counter and stack 6. Select the least recently used page by counter value 7. Stack them according the selection. 8. Display the values 9. Stop the process Pseudocode: ************************ 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 blocks3 Enter the no of block already alloted2 Enter the block1 Enter the block2 Enter process name 5 Enter process size 2 Process not allocated b[0]-- 0 b[1]-- 100 b[2]100

Ex.No.14.b. IMPLEMENTATION OF LINKED FILE ALLOCATION TECHNIQUE Aim To write a program a program to implement linked file allocation Algorithm 1. Start the process 2. Declare the size 3. Get the number of pages to be inserted 4. Get the value 5. Declare counter and stack 6. Select the linked file used page by counter value 7. Stack them according the selection. 8. Display the values 9. Stop the process Pseudocode:******** 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 3 Enter No. of blocks allocate 1 Enter block no 2 Process Name is A Enter process size 2 Linkage structure of blocks

0 ---> 1 ---> b[0]--A b[1]--A b[2]--Z Ex.No.14.c IMPLEMENTATION OF INDEX FILE ALLOCATION TECHNIQUE Aim To write a program a program to implement index file allocation Algorithm 1. Start the process 2. Declare the size 3. Get the number of pages to be inserted 4. Get the value 5. Declare counter and stack 6. Select the index file used page by counter value 7. Stack them according the selection. 8. Display the values 9. Stop the process Pseudocode: ******************************* 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 2 Enter Block no 2 Enter block no 4 Process Name is A Enter process size 3 Linkage Structure of Blocks 0---> 1--->3 ---> b[0]--A b[i]--A b[2]--Z b[3]--A b[4]==z b[5]==# b[6]-- b[7]-Directory sturucture Process Index 5#

You might also like