Partial preview of the text
Download Lab manual operating systems and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!
21CSC202J-Operating Systems LAB MANUAL 21CSC202J - OPERATING SYSTEMS SEMESTER - III SCHOOL OF COMPUTING SRM INSTITUTE OF SCIENCE AND TECHNOLOGY (Deemed University u/s 3 of UGC Act 1956) Kattankulathur, Chengalpattu District, 603 202. © SRMIST LIST OF EXPERIMENTS 21CSC202J-Operating Systems Exp. . . Page No. Title of the experiment Number 1 a. Operating system Installation b. Booting Process of Linux 2 a. Operating system Installation b. Booting Process of Linux 3 Shell Programs 4 Process Creation 5 Multithreading 6 Mutual Exclusion-Semaphore and Reader Writer Solution 7 Dining Philosopher problem 8 CPU Scheduling Algorithms - FCFS and SJF 9 CPU Scheduling Algorithms — Priority and Round Robin 10 Bankers Algorithm - Deadlock Avoidance 11 Memory Allocation Techniques First-Best-Worst Fit 12 Page Replacement Algo - FIFO-LRU-LFU 13 Disk Scheduling Algorithms-FCFS-SCAN-C-SCAN 14 File Allocation-Sequential-Indexed 15 File organization schemes for single level and two-level Directory © SRMIST 21CSC202J-Operating Systems Exp. No.:01(b) BOOTING PROCESS OF LINUX Date: Aim: To understand the booting process of LINUX Procedure: Press the power button on your system, and after few moments you see the Linux login prompt. From the time you press the power button until the Linux login prompt appears, the following sequence occurs. The following are the 6 high level stages of a typical Linux boot process. Power On aeDe BIOS MBR GRUB c= Tnit ——£ ————— Runlevel Step 1.,BIOS e BIOS stands for Basic Input/Output System e Performs some system integrity checks e Searches, loads, and executes the boot loader program. e It looks for boot loader in floppy, CD-ROMs, or hard drive. You can press a key (typically F12 or F2, but it depends on your system) during the BIOS startup to change the boot sequence. © Once the boot loader program is detected and loaded into the memory, BIOS gives the control to it. e So, in simple terms BIOS loads and executes the MBR boot loader. Step 2. MBR. * MBR stands for Master Boot Record. © SRMIST 21CSC202J-Operating Systems It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda MBR is less than 512 bytes in size. This has three components 1) primary boot loader info in Ist 446 bytes 2) partition table info in next 64 bytes 3) mbr validation check in last 2 bytes. It contains information about GRUB (or LILO in old systems). So, in simple terms MBR loads and executes the GRUB boot loader. Step 3. GRUB GRUB stands for Grand Unified Bootloader. If you have multiple kernel images installed on your system, you can choose which one to be executed. GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file. GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem). Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS. #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/boot/grub/splash.xpm.gz hiddenmenu title CentOS(2.6.18-194.elISPAE) root(hd0,0) kernel/boot/vmlinuz-2.6.18-194.el5PAE ro root=-LABEL=/ initrd /boot/initrd-2.6.18-194.el5PAE.img As you notice from the above info, it contains kernel and initrd image. So, in simple terms GRUB just loads and executes Kernel and initrd images. Step 4. Kernel Mounts the root file system as specified in the “root=” in grub.conf Kernel executes the /sbin/init program Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid. initrd stands for Initial RAM Disk. initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware. Step 5. Init Looks at the /etc/inittab file to decide the Linux run level. Following are the available run levels o 0-halt o 1-Single user mode o 2-—Multiuser, without NFS o 3-Full multiuser mode o 4-unused o 5-Xil © 6-—reboot © SRMIST 21CSC202J-Operating Systems Exp. No.:2(a) BASIC LINUX COMMANDS Date: Aim: To execute basic linux commands Procedure: a) Basics 1. echo SRM > to display the string SRM 2. clear > to clear the screen 3. date > to display the current date and time 4. cal 2003 > to display the calendar for the year 2003 5. cal 6 2003 > to display the calendar for the June-2003 6. passwd > to change password b) Working with Files 1. Is > list files in the present working directory 1s | > list files with detailed information (long list) ls —a > list all files including the hidden files 2. cat> fl > to create a file (Press “d to finish typing) cat f1 > display the content of the file fl 3. we fl > list no. of characters, words & lines of a file fl we —c fl > list only no. of characters of file fl we —w f1 > list only no. of words of file fl we —I fl > list only no. of lines of file fl 4. cp fl £2 > copy file fl into f2 5. mv fl f2 > rename file fl as f2 6. rm fl > remove the file fl 7. head —5 fl > list first 5 lines of the file fl tail —5 fl > list last 5 lines of the file f1 c) Working with Directories 1. mkdir elias > to create the directory elias © SRMIST 21CSC202J-Operating Systems cd elias > to change the directory as elias rmdir elias > to remove the directory elias rF YON pwd > to display the path of the present working directory 5. cd > to go to the home directory cd .. > to go to the parent directory cd - > to go to the previous working directory cd / > to go to the root directory d) File name substitution 1. Is f? > list files start with ‘f and followed by any one character 2. Is *.c > list files with extension ‘c’ 3. 1s [gpy]et > list files whose first letter is any one of the character g, p or y and followed by the word et 4. 1s [a-d,l-m]ring ~ list files whose first letter is any one of the character from a to d and | to m and followed by the word ring. e) I/O Redirection Input redirection we —1 < ex1 > To find the number of lines of the file ‘ex1’ Output redirection who > f2 > the output of ‘who’ will be redirected to file f2 cat >> fl > to append more into the file fl f) Piping Syntax : Command! | command2 Output of the command] is transferred to the command? as input. Finally output of the command? will be displayed on the monitor. Example. cat fl | more >list the contents of file f1 screen by screen head —6 f1 |tail -2 > prints the 5th & 6th lines of the file fl. g) Environment variables 1. echo $HOME > display the path of the home directory 2. echo $PS1 > display the prompt string $ 3. echo $PS2 > display the second prompt string ( > symbol by default ) © SRMIST 21CSC202J-Operating Systems Exp. No.:2(b) FILTERS and ADMIN COMMANDS Date: Aim: To execute filters and admin commands. Procedure: A. FILTERS 1. cut 2. grep 3. sort Used to cut characters or fileds from a file/input Syntax : cut -cchars filename -ffieldnos filename By default, tab is the filed separator(delimiter). If the fileds of the files are separated by any other character, we need to specify explicitly by —d option cut -ddelimitchar -ffileds filename Used to search one or more files for a particular pattern. Syntax : grep pattern filename(s) Lines that contain the pattern in the file(s) get displayed pattern can be any regular expressions More than one files can be searched for a pattern -v option displays the lines that do not contain the pattern -I list only name of the files that contain the pattern -n displays also the line number along with the lines that matches the pattern Used to sort the file in order Syntax : sort filename Sorts the data as text by default Sorts by the first filed by default -t option sorts the file in descending order -u eliminates duplicate lines -o filename writes sorted data into the file fname © SRMIST 21CSC202J-Operating Systems -tdchar sorts the file in which fileds are separated by dchar -n sorts the data as number +1n skip first filed and sort the file by second filed numerically 4. uniq Displays unique lines of a sorted file Syntax : uniq filename -d option displays only the duplicate lines -c displays unique lines with no. of occurrences. 5. diff Used to differentiate two files Syntax : diff f1 £2 compare two files f1 & f2 and prints all the lines that are differed between fl & f2. Questions: QI. Write a command to cut 5 to 8 characters of the file fl. $ Q2. Write a command to display user-id of all the users in your system. $ Q3. Write a command to check whether the user judith is available in your system or not. (use grep) $ Q4. Write a command to display the lines of the file f1 starts with SRM. $ Q5. Write a command to sort the file /ete/passwd in descending order $ Q6. Write a command to display the unique lines of the sorted file £21. Also display the number of occurrences of each line. $ Q7. Write a command to display the lines that are common to the files f1 and f2. $ © SRMIST 21CSC202J-Operating Systems sudo delgroup groupname 12. Create a user with group sudo adduser username groupname 13. See the password expiry value for a user, sudo chage -l username 14. Make changes sudo chage username 15. GUI Tool for user management If you do not want to run the commands in terminal to manage users and groups, then you can install a GUI add-on . sudo apt install gnome-system-tools Once done, type users-admin Q11. Create a user ‘elias’. Login to the newly created user and exit. $ Q12. Disable the user ‘elias’, try to login and enable again. $ © SRMIST 21CSC202J-Operating Systems Exp. No.:3 SHELL PROGRAMS Date: Aim: To execute shell programs in linux environment Procedure: How to run a Shell Script e Edit and save your program using editor e Add execute permission by chmod command e Run your program using the name of your program ./program-name Important Hints e No space before and after the assignment operator Ex. sum=0 e Single quote ignores all special characters. Dollar sign, Back quote and Back slash are not ignored inside Double quote. Back quote is used as command substitution. Back slash is used to remove the special meaning of a character. e Arithmetic expression can be written as follows : i=$((i+1) or i=$(expr$i + 1) * Command line arguments are referred inside the programme as $1, $2, ..and so on « $* represents all arguments, $# specifies the number of arguments e read statement is used to get input from input device. Ex. read a b 1. Syntax for if statement Synatax-01: if [ condition ] then # commands to execute if condition is true fi Example: if [ Sage -ge 18 ] then echo "You are eligible to vote." fi © SRMIST 21CSC202J-Operating Systems * # default commands (if no pattern matches) esac Example: echo "Enter a number between | and 3:" read num case $num in 1) echo "You selected One" 2) echo "You selected Two" 3) echo "You selected Three" *) 3 echo "Invalid choice" esac 3. Syntax for for-loop for var in value] value2 value3 do # commands using $var done Example: for color in red green blue do echo "Color is $color" done Syntax for While loop while [ condition ] do # commands to execute while condition is true done Example: © SRMIST 21CSC202J-Operating Systems count=1 while [ $count -le 5 ] do echo "Count is $count" count=$((count + 1)) done 4. Syntax for printf statement printf "format-string" [arguments...] e Break and continue statements functions similar to C programming e Relational operators are —It, -le, -gt, -ge, -eq,-ne Example: (i>= 10) is written as [ $i-ge 10 ] e Logical operators (and, or, not) are -o, -a, ! Example: (a>b) && (a>c) is written as [ $a —gt $b —a $a —gt $c ] e Two strings can be compared using = operator Q1. Given the following values, num=10, x=*, y="date’ a="Hello, 'he said'”. Execute and write the output of the following commands: echo num echo $num echo $x echo ‘$x’ echo “$x” echo $y echo $(date) echo $a echo \$num echo \$$num Q2. Find the output of the following shell scripts $ vi ex31 echo Enter value for n read n sum=0 i=l while [ $ile $n ] do sum=$((sum+i)) i-$((i+2)) done echo Sum is $sum © SRMIST 21CSC202J-Operating Systems Exp. No.:4 PROCESS CREATION Date: Aim: To create a process using fork() and understand the usage of getpid(), getppid(), wait() functions Procedure: Compilation of C Program Step 1 : Open the terminal and edit your program and save with extension “.c’ Example: nano test.c Step 2 : Compile your program using gcc compiler Example: gcc test.c > Output file will be “a.out” (or) gcc —0 test text.c > Output file will be “test” Step 3 : Correct the errors if any and run the program Example: ./a.out (or) ./test Syntax for process creation Returns 0 in child process and child process ID in parent process. Other Related Functions ¢ int getpid() > returns the current process ID e int getppid() > returns the parent process ID © wait() > makes a process wait for other process to complete Virtual fork e vfork() is similar to fork but both processes shares the same address space. Q1. Find the output of the following program #include #include int main() { int a=5,b=10,pid; printf("Before fork a=%d b=%d \n",a,b); pid=fork(); if(pid—=0) { © SRMIST a=at1; b=b+1; else } return 0; } Output: Q2. Rewrite the program in Q1 using vfork() and write the output Q3. Calculate the number of times the text “SRMIST” is printed. printf("In child a=%d b=%d \n",a,b); sleep(1); a=a-1; b=b-1; printf("In Parent a=%d b=%d \n",a,b); #include #include int main() { forkQ; forkQ; forkQ); printf(“SRMIST\n”); return 0; } Output: © SRMIST 21CSC202J-Operating Systems