Os Lab Manual-1

April 15, 2017 | Author: diranj | Category: N/A
Share Embed Donate


Short Description

Download Os Lab Manual-1...

Description

CONTENTS Exercise No

Title of Exercise

Page No

CYCLE I 1

BASIC LINUX COMMANDS

2

SHELL PROGRAMMING

3

UNIX SYSTEM CALLS

4

UNIX I/O SYSTEM CALLS

5

SIMULATION OF UNIX COMMANDS(ls, grep)

6

CPU SCHEDULING ALGORITHMS -FCFS

7

CPU SCHEDULING ALGORITHMS –SJF

8

CPU SCHEDULING ALGORITHMS –Priority scheduling. CYCLE - II

9 10 11 12 13 14

CPU SCHEDULING ALGORITHMS - Round Robin scheduling INTER PROCESS COMMUNICATION USING SHARED MEMORY INTER PROCESS COMMUNICATION USING PIPES PRODUCER- CONSUMER PROBLEM USING SEMAPHORES IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMS-FIFO IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMS-LRU

15

MEMORY MANAGEMENT SCHEMES - II

16

FILE ALLOCATION TECHNIQUES AUGMENTED EXPERIMENTS

17

DEAD LOCK – BANKER’S ALGORITHM

18

DINING PHILOSOPHERS PROBLEM

Ex.No:1 CS 2257 OPERATING SYSTEMS LAB

1

Date: 1. BASIC LINUX COMMANDS Objective To study different types of UNIX/LINUX command syntax and check the output of each command. Introduction to UNIX Operating System An Operating System (OS) can be defined as a software program designed to control the hardware, manage the system resources and supervises interaction between the system and its users. Types of Operating System • Single User Operating Systems are those which are used in PCs. Eg.DOS • Multi User Operating Systems are those which can handle multiple users as well as peripherals simultaneously. Eg.UNIX Functions of a Multi user Operating system • Isolating the user from the hardware. • Providing a transparent layer to communicate with hardware. • Scheduling tasks of various users. • Managing resources and allocating them to various users. • Resolving conflicting requests of various users. • Monitoring and auditing system operations Structure of UNIX Operating System Application Shell Kernel Hardwar e

Kernel Kernel forms the core of the UNIX operating system. This interacts with the hardware. It is loaded into the memory when a system is booted. Its functions are, • •

Memory Management File Management and security

CS 2257 OPERATING SYSTEMS LAB

2

• • •

Process Scheduling and Management Managing Hardware devices. Allocating time for different users and processes

Shell The Shell in one layer above the kernel. It acts as a command interpreter for the command input by the user. The Shell takes the user command as input, interacts with the kernel using system calls and the kernel in turn interacts with the hardware. Additionally shell contains a programming language which enables users to write shell scripts on their own. The Unix Shell performs the following functions: • It acts as command interpreter • It expands the various meta characters used in file operations (like ?,[] etc) • It is responsible for redirecting the output of one command as the input of another command in a redirection or filtering operation initiated from the command prompt. • It is responsible for executing the shell scripts which use the programming language of the shell • It is responsible for setting up the environment for the user. The most important shells are the Bourne Shell, C shell Korn shell etc. Features of UNIX OS * Multitasking Multitasking is the capability of the operating system to perform various tasks.ie., A single user can perform various tasks. *Multi-user capability This allows several users to use the same computer to perform their tasks. *Security Every user has a login name and a password. So, accessing another user’s data is impossible without permission *Portability UNIX is portable because it is written in a high level language ©. So UXIX can be run on different computers. *Communication: UNIX supports the following communications. i) Between the different terminals connected to the UNIX server. ii) Between the users of one computer to the users of another Programming facility: UNIX is highly programmable, the UNIX shell programming language has all the necessary ingredients like conditional and control structures (Loops) and variables.

Structures of a UNIX file system

dev /lib

lib

etc

home

bin

tmp

library files

CS 2257 OPERATING SYSTEMS LAB

3

/dev Contains file that link hardware devices /tmp temporary storage of files /home user home directories Getting started with UNIX Switching the system ON will provide the user with login prompt. Here we enter the login name. Then it prompts for the password. The password is not echoed on the screen to protect the privacy of the war. If both are correct, then we will get the S prompt.

UNIX Commands Basic Commands I file and Directory Related commands 1) pwd This command prints the current working directory. Syntax: $ pwd 2) ls This command displays the list of files in the current working directory. $ ls $ ls -a: It displays all files in current directory including hidden files and directory. $ ls r*: It displays files whose begins with ‘*’ specifies number of characters. $ls r?:This displays files which are having r from second character irrespective of 1st character. $ ls[a-m]* :It lists files whose names begin with alphabets from a to m $ ls [!a –g]*:List all files not begin with character a to g. $ls –l Lists the files in the long format. It is the way of controlling accessibility of file each of groups and other. $ls –t lists in the order of last modification time $ls –d Lists directory instead of contents $ls -u Lists in order of last access time 3) cd This command is used to change from the working directory to any other directory specified. $cd directory-name 4) cd .. This command is used to come out of the current working directory. $cd .. 5) mkdir This command helps us to make a directory.

CS 2257 OPERATING SYSTEMS LAB

4

$mkdir directory-name 6) rmdir This command is used to remove a directory specified in the command line. It requires the specified directory to be empty before removing it. $rmdir directory-name 7) cat This command helps us to list the contents of a file we specify. $cat [option][file] $cat filename-Used to display the content of the file $cat > filename – This is used to create a new file. $cat >>filename – This is used to append the contents of the file 8) cp This command helps us to create duplicate copies of ordinary files. $cp source-filename destination-filename 9) mv This command is used to move files from one place to another place (Renaming). $mv source-filename destination-filename 10) ln This command is to establish an additional filename for the same ordinary file. $ln firstname secondname 11) rm This command is used to delete one or more files from the directory. $rm [option] filename $rm -i -asks the user if he wants to delete the file mentioned. $rm -r -Recursively delete the entire contents of the directory as well as the directory itself. 12) man This command is used to syntax help of various commands. $man command-name $man who

CS 2257 OPERATING SYSTEMS LAB

5

II) Process and status information commands 1) who This command gives the details of who all have logged in to the UNIX system currently. $ who 2) who am i This command tells us as to when we had logged in and the system’s name for the connection being used. $who am i 3) date This command displays the current date in different formats. +%D +%H +%M +%S +%T

mm/dd/yy Hr-00 to 23 Min-00 to 59 Sec-00 to 59 HH:MM:SS

+%w +%a +%h +%r +%y

Day of the week Abbr.Weekday Abbr.Month Time in AM/PM Last two digits of the year

4) echo This command will display the text typed from the keyboard. $echo Eg: $echo “Have a nice day” Output: Have a nice day 5) Executing more command at a time: The semicolon operator overcomes the limitation of executing only one command at a time. SYNTAX: $ command1; $ command2 $ pwd;who 6) tput and clear: It clears the screen and places a$ prompt at left top corner of screen SYNTAX: $ tput clear $clear

CS 2257 OPERATING SYSTEMS LAB

6

7) calendar : cal: It is used to keep track of our days, it displays specified month of year. SYNTAX: $ cal month or year 8) calculator: bc: Offers on online calculator and can be invoked by command be calculator is programmable and has complex functions. SYNTAX: $ bc 9) Re-directing standard output to a file: SYNTAX: $ command > file. The symbol ‘>’ is the redirection operation. It sends the output of the command to a file as a device such as printer, disk, type etc SYNTAX: $ ls >file-name II Text related commands 1. head This command displays the initial part of the file. By default it displays first ten lines of the file. $head [-count] [filename] $head -3 filename ->displays first 3 lines of the file 2. tail This command displays the later part of the file. By default it displays last ten lines of the file. $tail [-count] [filename] $tail -3 filename ->displays last 3 lines of the file 3. wc This command is used to count the number of lines, words or characters in a file. $wc [-lwc] filename 4. find The find command is used to locate files in a directory and in a subdirectory. $find –name option This lists out the specific files in all directories beginning from the named directory. Wild cards can be used.

CS 2257 OPERATING SYSTEMS LAB

7

$find –type option This option is used to identify whether the name of files specified are ordinary files or directory files. If the name is a directory then use “-type d “and if it is a file then use “-type f”. $find –mtime option This option will allow us to find that file which has been modified before or after a specified time. The various options available are –mtime n(on a particular day),-mtime +n(before a particular day),-mtime –n(after a particular day) $find –exec option This option is used to execute some commands on the files that are found by the find command. $find $HOME -print will lists all files in your home directory. $find /work -name chapter1 -print will list all files named chapter1 in /work directory. 5) Diff diff command will compare the two files and print out the differences between them. Here I have two ascii text files. fileone and file two. $diff fileone filetwo 6) Cmp command. cmp command compares the two files. For exmaple I have two different files fileone and filetwo. $cmp fileone filetwo 7) Dircmp Command. dircmp command compares two directories. If i have two directories in my home directory named dirone and dirtwo and each has 5-10 files in it. Then $dircmp dirone dirtwo IV File Permission commands 1) chmod Changes the file/directory permission mode:

$ chmod 777 file1

Gives full permission to owner, group and others $ chmod o-w file1 Removes write permission for others.

CS 2257 OPERATING SYSTEMS LAB

8

$ chmod o +x file1 Add execute permission to others. V Useful Commands: 1) $exit Ends your work on the UNIX system. 2) $Ctrl-l or clear Clears the screen. 3) $Ctrl-c Stops the program currently running. 4) $Ctrl-z Pauses the currently running program. 5) more FILE

Display the contents of FILE, pausing after each screenful. There are several keys which control the output once a screenful has been printed. Will advance the output one line at a time. Will advance the output by another full screenful. "q" Will quit and return you to the UNIX prompt. 6) less

FILE "less" is a program similar to "more", but which allows backward movement in the file as well as forward movement. 7) lpr FILE

To print a UNIX text or PostScript file, type the following command at the system prompt: Meta characters Some special characters, called metacharacters may be used to specify multiple filenames. These characters substitute filenames or parts of filenames. The “*” This character is used to indicate any character(s) $ cat ap* This displays the contents of all files having a name starting with ap followed by any number of characters. The “?”

This character replaces any one character in the filename.

$ ls ?st list all files starting with any character followed by st.

CS 2257 OPERATING SYSTEMS LAB

9

The []

These are used to specify range of characters. $ ls [a-z]pple Lists all files having names starting with any character from a to z.

PIPES AND FILTERS In UNIX commands were created to perform single tasks only. If we want to perform multiple tasks we can go for pipes and filters. PIPES A pipe is a mechanism, which takes the output of a command as its input for the next command. $who | wc –l $cat text.c | head –3 FILTERS Filters are used to extract the lines, which contain a specific pattern, to arrange the contents of a file in a sorted order, to replace existing characters with some other characters, etc. 1. Sort filter The sort filter arranges the input taken from the standard input in alphabetical order. The sort command when used with “-r” option will display the input taken from the keyboard in the reverse alphabetical order. When used with “-n” option arranges the numbers, alphabets and special characters according to their ASCII value. If we want to sort on any one field, then sort provides us with an option called “+pos1 –pos2” option. sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents zzz aaa 1234 yuer wer qww wwe

Then running $sort testfile will give us output of 1234 aaa qww wer

CS 2257 OPERATING SYSTEMS LAB

10

wwe yuer zzz

Options: -b ignores leading spaces and tabs. -c checks whether files are already sorted. -d ignores punctuation. -i ignore non-printing characters -n sorts in arithmetic order. -ofile put output in a file. +m [-m] skips n fields before sorting, and sort upto field position m. -r reverses the order of sort. -u identical lines in input file appear only one time in output.

$ sort input1.txt input2.txt > output.txt 2. Grep filter This command is used to search for a particular pattern from a file or from standard input and display those lines on the standard output. Grep stands for “Global search for regular expression”. There are various options available with grep command. -v displays only those lines which do not match the pattern specified. -c displays only the count of those lines which match the pattern specified -n displays matched lines with line numbers -i displays matched pattern ignoring case distinction

$ grep hello *.txt $ grep -vi hello *.txt •

If you want to search all files in an entire directory tree for a particular pattern, you can combine grep with find using backward single quotes to pass the output from find into grep. So $ grep hello `find . -name "*.txt" -print` will search all text files in the directory tree rooted at the current directory for lines containing the word "hello".

Syntax: grep "word-to-find" {file-name} 3. Uniq filter

CS 2257 OPERATING SYSTEMS LAB

11

The uniq filter compares adjacent lines in the sorted input file and when used with different options displays single and multiple occurrences. -d displays only the lines which are duplicated in the input file. -u

displays only the lines with single occurrences. •

removes duplicate adjacent lines from a file. This facility is most useful when combined with sort: uniq

$ sort input.txt | uniq > output.txt

4. Pg and more filter These commands display the output of the command on the screen page by page. The difference between pg and more filter is that the viewing screen of the latter can be done by pressing space bar while that of the former is done by pressing enter. 5. Cut command One particular field from any file or from output of any command can be extracted and displayed using this cut command. One particular character can also be extracted using the –c option of this command. $cut options [files] for example if a file named testfile contains this is firstline this is secondline this is thirdline

Examples: $cut -c1,4 testfile will print this to standard output (screen) ts ts ts

It is printing columns 1 and 4 of this file which contains t and s (part of this).

Options: • -c list cut the column positions identified in list. • -f list will cut the fields identified in list. • -s could be used with -f to suppress lines without delimiters. 6. Tr command This command is used to translate characters taken from the standard input. This command when used with “-s” option is used to squeeze multiple spaces into a single space. $ tr "[a-z]" "[A-Z]" hi i am Vivek CS 2257 OPERATING SYSTEMS LAB

12

HI I AM VIVEK what a magic WHAT A MAGIC Press 7) Paste Command. Paste command merge the lines of one or more files into vertical columns separated by a tab. for example if a file named testfile contains this is firstline

and a file named testfile2 contains this is testfile2

then running this command $paste testfile testfile2 > outputfile will put this into outputfile this is firstline this is testfile2 it contains contents of both files in columns. $who | paste - - will list users in two columns. Options: -d'char' separate columns with char instead of a tab. -s merge subsequent lines from one file.

CS 2257 OPERATING SYSTEMS LAB

13

Exercises Part- I 1. Create a directory called stud. Change to stud directory. Verify whether you have Changed to the stud directory. Return to your original directory. 2. Create a file called top. Display the first lines from the beginning of the file and the last three lines of the file top. 3. List the contents of a file from the fourth line to the end of the file top. List the contents of a file from the seventh line, of the file top. 4. Create a file called list which contains sample data as follows: 1. Display the contents of the file sorted according to the marks in the descending order. 2. Display the names of the students in the alphabetical order ignoring the cases. 3. Display the list of students who have scored marks between 50 and 80. 4. Display the list of students and their registration numbers. 5. Sort the file according to the third field and dump in to a file called code. 5. Create a file called places whose sample data is as follows and answer the questions below. Bombay India 45677 Asia Karachi Pakistan 54876 Asia Nairobhi Kenya 32196 Africa 1. List the details for the countries USA, Kenya and Canada. 2. List the details for the continent asia ignoring the case. 3. Display the list of those countries whose population is between 40000 and 60000. 4. Extract the lines which end with “ia”. 6. Create a file emp as given and answer the questions below. E0001: malan: mktg: 5000 E0010: balan: acct: 7000 1. Sort the file on the employee department and display the name of the employee and the department. 2. List the employees who earn between 4000 and 6000. 3. Sort the file on the employee name in the reverse order and extract their codes and their names. 7. What is the command to be given if we want to display the entire text of a file into upper case? 8. Display the date in mm/dd/yy format, along with the present time in AM/PM. 9. List the various formats of date command.

CS 2257 OPERATING SYSTEMS LAB

14

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 - 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. To make the shell scripts interactive we can use read command. #!/bin/sh echo “ Give your name” read name echo “ Hello, Good Morning $name”

CS 2257 OPERATING SYSTEMS LAB

15

The variable with $ is called shell variable. We can assigh values to shell variables like name=raji The usage will be $ a=20 $ echo $a 20 SYNTAX FOR LOOPING STATEMENTS IF –THEN-ELSE CONSTRUCT if [ condition] then else



statements fi (end of if) The relational operators are -gt greater than -ge greater than and equal to -lt less than -le less than and equal to -ne not equal to -eq equal to To compare two strings the following operators are used str1 = str2 True if both the strings are same str1 != str2 True if strings are different -n string True if the string’s length is greater than zero -z string True if length of the string is zero string True if the string is not null WHILE while do Done CASE Case $ in 1) ;; 2) ;; 3) .. 4) *) ;; Esac [

CS 2257 OPERATING SYSTEMS LAB

16

For loop For(( intitialization;condition;incremetation/decrementation)) Function Function is series of instruction/commands. Function performs particular activity in shell i.e. it had specific work to do or simply say task. To define function use following syntax: Syntax: function-name ( ) { command1 command2 ..... ... commandN return }

Where function-name is name of you function, that executes series of commands. A return statement will terminate the function. Example: Type SayHello() at $ prompt as follows $ SayHello() { echo "Hello $LOGNAME, Have nice computing" return } To execute this SayHello() function just type it name as follows: $ SayHello Hello vivek, Have nice computing.

Example Scripts:

CS 2257 OPERATING SYSTEMS LAB

17

FINDING BIGGEST AMONG THREE NUMBERS Aim: To write a shell script to find biggest among three numbers. Algorithm: 1) START: Take three nos as n1,n2,n3. 2) Is n1 is greater than n2 and n3, if yes print n1 is bigest no goto step 5, otherwise goto next step 3) Is n2 is greater than n1 and n3, if yes, print n2 is bigest no goto step 5, otherwise goto next step 4) Is n3 is greater than n1 and n2, if yes , print n3 is bigest no goto step 5, otherwise goto next step END

CS 2257 OPERATING SYSTEMS LAB

18

Script:

CS 2257 OPERATING SYSTEMS LAB

19

Output:

Enter the value of a:20 Enter the value of b:45 Enter the value of c:60 C is greater

Result:

CS 2257 OPERATING SYSTEMS LAB

20

FACTORIAL Aim: To write a shell script to calculate the factorial value of a number. Algorithm: 1) START: read the value of ‘n’. 2) If n=1, assign i=fact =1 3) Construct a while loop as: * Calculate fact=fact * i * Increment I by 1 as i=i+1 4) Repeat step2 until i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF