+ All Categories
Home > Documents > OS Lab Manual

OS Lab Manual

Date post: 16-Sep-2015
Category:
Upload: ram-bhagwan
View: 227 times
Download: 4 times
Share this document with a friend
Description:
OS Lab Manual and practicals. Helpful for BE CE/IT/CSE 4th sem students
Popular Tags:
33
GECG Gandhinagar CE Government Engineering College Sector-28, Gandhinagar Computer Department BE II SEM IV Operating System
Transcript
  • GECG Gandhinagar CE

    Government Engineering College Sector-28, Gandhinagar Computer Department

    BE II SEM IV Operating System

  • GECG Gandhinagar CE

    UNIX Commands 1) pwd COMMAND:

    Pwd - Print Working Directory. pwd command prints the full filename of the current

    working directory.

    SYNTAX:

    The Syntax is

    pwd [options]

    2) cd COMMAND:

    cd command is used to change the directory.

    SYNTAX:

    The Syntax is

    cd [directory | ~ | ./ | ../ | - ]

    3) ls COMMAND:

    ls command lists the files and directories under current working directory.

    SYNTAX:

    The Syntax is

    ls [OPTIONS]... [FILE]

    OPTIONS:

    -l Lists all the files, directories and their mode, Number of links,

    owner of the file, file size, Modified date and time and filename.

    -t Lists in order of last modification time.

    -a Lists all entries including hidden files.

    -d Lists directory files instead of contents.

    -p Puts slash at the end of each directories.

    -u List in order of last access time.

    -i Display inode information.

    4) rm COMMAND:

    rm linux command is used to remove/delete the file from the directory.

    SYNTAX:

    The Syntax is

    rm [options..] [file | directory]

  • GECG Gandhinagar CE

    OPTIONS:

    -f Remove all files in a directory without prompting the user.

    -I Interactive. With this option, rm prompts for confirmation before

    removing any files.

    5) mv COMMAND:

    mv command which is short for move. It is used to move/rename file

    from one directory to another. mv command is different from cp command

    as it completely removes the file from the source and moves to the directory specified, where cp

    command just copies the content from one file to another.

    SYNTAX:

    The Syntax is

    mv [-f] [-i] oldname newname

    OPTIONS:

    -f This will not prompt before overwriting (equivalent to --reply=yes). mv -f will move the file(s)

    without prompting even if it is writing over an existing target.

    -i Prompts before overwriting another file.

    6) cat COMMAND:

    cat linux command concatenates files and print it on the standard output.

    SYNTAX:

    The Syntax is

    cat [OPTIONS] [FILE]...

    OPTIONS:

    -A Show all.

    -b Omits line numbers for blank space in the output.

    -E Displays a $ (dollar sign) at the end of each line.

    -n Line numbers for all the output lines.

    7) cmp COMMAND:

    cmp linux command compares two files and tells you which line numbers are different.

    SYNTAX:

    The Syntax is

    cmp [options..] file1 file2

  • GECG Gandhinagar CE

    OPTIONS:

    - c Output differing bytes as characters.

    - l Print the byte number (decimal) and the differing byte values

    (octal) for each difference.

    - s Prints nothing for differing files, return exit status only.

    8) cp COMMAND:

    cp command copy files from one location to another. If the

    destination is an existing file, then the file is overwritten; if the destination is

    an existing directory, the file is copied into the directory (the directory is not

    overwritten).

    SYNTAX:

    The Syntax is

    cp [OPTIONS]... SOURCE DEST

    10) echo COMMAND:

    echo command prints the given input string to standard output.

    SYNTAX:

    The Syntax is

    echo [options..] [string]

    11) mkdir COMMAND:

    mkdir command is used to create one or more directories.

    SYNTAX:

    The Syntax is

    mkdir [options] directories

    OPTIONS:

    -m Set the access mode for the new directories.

    -p Create intervening parent directories if they don't exist.

    -v Print help message for each directory created.

    12) paste COMMAND:

    paste command is used to paste the content from one file to another file. It is also used to

    set column format for each line.

  • GECG Gandhinagar CE

    SYNTAX:

    The Syntax is

    paste [options]

    OPTIONS:

    -s Paste one file at a time instead of in parallel.

    -d Reuse characters from LIST instead of TABs .

    13) rmdir COMMAND:

    rmdir command is used to delete/remove a directory and its subdirectories.

    SYNTAX:

    The Syntax is

    rmdir [options..] Directory

    OPTIONS:

    -p

    Allow users to remove the directory dir name and its parent directories which become empty.

    Modify a user account. Changes may be made to the password, group

    membership, expiration date, and other attributes of a given user's account.

    With this command, a user's password may be locked, which has the effect

    of disabling the account.

    25

    groupmod

    Modify a given group. The group name and/or ID number may be changed

    using this command.

    14) cal

    This command will print a calendar for a specified month and/or year.

    To show this month's calendar, enter:

    cal

    To show a twelve-month calendar for 2008, enter:

    cal 2008

    To show a calendar for just the month of June 1970, enter:

    cal 6 1970

  • GECG Gandhinagar CE

    15) cat

    This command outputs the contents of a text file. You can use it to read brief files or to

    concatenate files together.

    To append file1 onto the end of file2, enter:

    cat file1 >> file2

    To view the contents of a file named myfile, enter:

    cat myfile

    16) cd

    This command changes your current directory location. By default, your Unix login session

    begins in your home directory.

    To switch to a subdirectory (of the current directory) named myfiles, enter:

    cd myfiles

    To switch to a directory named /home/dvader/empire_docs, enter:

    cd /home/dvader/empire_docs

    To move to the parent directory of the current directory, enter:

    cd ..

    To move to the root directory, enter:

    cd /

    To return to your home directory, enter:

    cd

  • GECG Gandhinagar CE

    17) chmod

    This command changes the permission information associated with a file. Every file (including

    directories, which Unix treats as files) on a Unix system is stored with records indicating who

    has permission to read, write, or execute the file, abbreviated as r, w, and x. These permissions

    are broken down for three categories of user: first, the owner of the file; second, a group with

    which both the user and the file may be associated; and third, all other users. These categories

    are abbreviated as u for owner (or user), g for group, and o for other.

    To allow yourself to execute a file that you own named myfile, enter:

    chmod u+x myfile

    To allow anyone who has access to the directory in which myfile is stored to read or execute

    myfile, enter:

    chmod o+rx myfile

    18) cp

    This command copies a file, preserving the original and creating an identical copy. If you already

    have a file with the new name, cp will overwrite and destroy the duplicate. For this reason, it's

    safest to always add -i after the cp command, to force the system to ask for your approval

    before it destroys any files. The general syntax for cp is:

    cp -i oldfile newfile

    To copy a file named meeting1 in the directory /home/dvader/notes to your current directory,

    enter:

    cp -i /home/dvader/notes/meeting1 .

    The . (period) indicates the current directory as destination, and the -i ensures that if there is

    another file named meeting1 in the current directory, you will not overwrite it by accident.

    To copy a file named oldfile in the current directory to the new name newfile in the mystuff

    subdirectory of your home directory, enter:

    cp -i oldfile ~/mystuff/newfile

    The ~ character (tilde) is interpreted as the path of your home directory.

  • GECG Gandhinagar CE

    Note: You must have permission to read a file in order to copy it.

    19) date

    The date command displays the current day, date, time, and year.

    To see this information, enter:

    date

    20) df

    This command reports file system disk usage (i.e., the amount of space taken up on mounted file

    systems). For each mounted file system, df reports the file system device, the number of blocks

    used, the number of blocks available, and the directory where the file system is mounted.

    To find out how much disk space is used on each file system, enter the following command:

    df

    If the df command is not configured to show blocks in kilobytes by default, you can issue the

    following command:

    df -k

    21) du

    This command reports disk usage (i.e., the amount of space taken up by a group of files). The du

    command descends all subdirectories from the directory in which you enter the command,

    reporting the size of their contents, and finally reporting a total size for all the files it finds.

    To find out how much disk space your files take up, switch to your home directory with the cd

    command, and enter:

    du

    The numbers reported are the sizes of the files; on different systems, these sizes will be in units

    of either 512 byte blocks or kilobytes. To learn which is the case, use the man command,

    described below. On most systems, du -k will give sizes in kilobytes.

  • GECG Gandhinagar CE

    22) find

    The find command lists all of the files within a directory and its subdirectories that match a set

    of conditions. This command is most commonly used to find all of the files that have a certain

    name.

    To find all of the files named myfile.txt in your current directory and all of its subdirectories,

    enter:

    find . -name myfile.txt -print

    To look in your current directory and its subdirectories for all of the files that end in the

    extension .txt, enter:

    find . -name "*.txt" -print

    In these examples, the . (period) represents your current directory. It can be replaced by the full

    pathname of another directory to search. For instance, to search for files named myfile.txt in

    the directory /home/user/myusername and its subdirectories, enter:

    find /home/user/myusername/ -name myfile.txt -print

    On some systems, omitting the final / (slash) after the directory name can cause find to fail to

    return any results.

    As a shortcut for searching in your home directory, enter:

    find "$HOME/" -name myfile.txt -print

    23) kill

    Use this command as a last resort to destroy any jobs or programs that you suspended and are

    unable to restart. Use the jobs command to see a list of suspended jobs. To kill suspended job

    number three, for example, enter:

    kill %3

    Now check the jobs command again. If the job has not been cancelled, harsher measures may be

    necessary. Enter:

    kill -9 %3

  • GECG Gandhinagar CE

    24) ls

    This command will list the files stored in a directory. To see a brief, multi-column list of the files

    in the current directory, enter:

    ls

    To also see "dot" files (configuration files that begin with a period, such as .login), enter:

    ls -a

    To see the file permissions, owners, and sizes of all files, enter:

    ls -la

    If the listing is long and scrolls off your screen before you can read it, combine ls with the less

    utility, for example:

    ls -la | less

    25) man

    This command displays the manual page for a particular command. If you are unsure how to use

    a command or want to find out all its options, you might want to try using man to view the

    manual page.

    For example, to learn more about the ls command, enter:

    man ls

    26) mv

    This command will move a file. You can use mv not only to change the directory location of a

    file, but also to rename files. Unlike the cp command, mv will not preserve the original file.

    Note: As with the cp command, you should always use -i to make sure you do not overwrite an

    existing file.

    To rename a file named oldname in the current directory to the new name newname, enter:

    mv -i oldname newname

    To move a file named hw1 from a subdirectory named newhw to another subdirectory named

    oldhw (both subdirectories of the current directory), enter:

    mv -i newhw/hw1 oldhw

  • GECG Gandhinagar CE

    If, in this last operation, you also wanted to give the file a new name, such as firsthw, you

    would enter:

    mv -i newhw/hw1 oldhw/firsthw

    27) pwd

    This command reports the current directory path. Enter the command by itself:

    pwd

  • GECG Gandhinagar CE

    Write a shell script to find factorial of given number n.

    echo enter a number;

    read n;

    i=1;

    while [ $n -ne 0 ]

    do

    i=expr $n\* $i

    n=expr $n-1

    done

    echo factorial of given no. is $i

    /*-------------------------INPUT/OUTPUT----------

    Enter the n value:

    6

    The factorial of 6 is:720

    -bash-3.2$

    ---------------------------------------------*/

  • GECG Gandhinagar CE

    Write a shell script which will accept a number and display first n prime numbers as

    output.

    echo "Enter a number upto which you want the prime numbers: "

    read n

    echo "Prime numbers upto $n are:"

    echo 2

    for (( i=2; i

  • GECG Gandhinagar CE

    Write a shell script which will generate first n fibonnacci numbers like: 1, 1, 2, 3, 5, 13,

    $ vi fibonacci.sh echo Enter n for Fibonacci series: - read n echo Fibonacci series is: - echo 0 echo 1 i=0 j=1 cnt=2 while [ $cnt le $n ] do k=`expr $i + $j` i= $j j= $k echo $k cnt=`expr $cnt + 1` done Output Enter n for Fibonacci Series:- 10 Fibonacci Series is:- 0 1 1 2 3 5 8

  • GECG Gandhinagar CE

    Display calendar of current month, year

    cal command

    Just enter cal command as follows:

    $ cal

    Output:

    August 2007

    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

    ncal command

    ncal command changes the output format:

    $ ncal

    Output:

    August 2007

    Mo 6 13 20 27

    Tu 7 14 21 28

    We 1 8 15 22 29

    Th 2 9 16 23 30

    Fr 3 10 17 24 31

    Sa 4 11 18 25

    Su 5 12 19 26

    Print calendar for year 2008

    $ cal 2008

  • GECG Gandhinagar CE

    Display todays date and time

    date +%FORMAT

    date +"%FORMAT"

    var=$(date +"%FORMAT")

    Example: Show current time

    Open a terminal and type the following command:

    date +"%T"

    Sample outputs:

    16:33:22

    To store time to a shell variable called now, enter:

    now=$(date +"%T")

    echo "Current time : $now"

    Sample outputs:

    Current time : 13:31:55

    Example: 12 hour clock time

    Pass the %r format to the date command:

    date +"%r"

    Sample outputs:

    01:37:06 PM

    To remove AM or PM from the output use, type:

    date +"%I:%M:%S"

    Sample outputs:

    01:39:30

    Sample shell script

  • GECG Gandhinagar CE

    #!/bin/bash

    # Purpose: Demo date command and menus

    # Author: nixCraft under GPL v2.x+

    # ------------------------------------------------------

    # Display text at given row and column

    show(){

    local x=$1

    local y=$2

    local txt="$3"

    # Set cursor position on screen

    tput cup $x $y

    echo "$txt"

    }

    while [ : ]

    do

    clear

    # Get the system time

    now="$(date +"%r")"

    # Show main - menu, server name and time

    show 10 10 "MAIN MENU for $HOSTNAME - $now"

    show 11 10 "1. System info"

    show 12 10 "2. Apache server stats"

    show 13 10 "3. MySQL server stats"

    show 14 10 "4. Firewall stats"

    show 15 10 "5. Exit"

    tput cup 16 10; read -t 2 -p "Choice [1-5] ? " usrch

    # do something

    case $usrch in

    1) read -t 2 -p "Showing system info, wait..." fakeinput;;

    2) read -t 2 -p "Showing apache info, wait..." fakeinput;;

    3) read -t 2 -p "Showing mysqld info, wait..." fakeinput;;

    4) read -t 2 -p "Showing firewall info, wait..." fakeinput;;

    5) echo "Bye."; exit 0;;

    esac

    done

    Run it as follows:

    chmod +x demo.sh

    ./demo.sh

  • GECG Gandhinagar CE

    Addition of two numbers

    echo enter 2 numbers

    read a

    read b

    c=`expr $a + $b`

    echo sum of two numbers is $c

    OUTPUT:

    enter 2 numbers

    12

    5

    multiplication of two numbers is 17

  • GECG Gandhinagar CE

    REVERSE THE NUMBER

    echo enter a number

    read n

    sum=0

    while(( n!=0 ))

    do

    a=`expr $a % 10`

    n=`expr $n / 10`

    sum=`expr 10 \* sum + $a`

    done

    echo the reverse of the number is $sum

    OUTPUT:

    enter the number

    1234

    the reverse of the number is 4321

  • GECG Gandhinagar CE

    PALINDROME

    echo enter a string

    read str

    count = `echo $str|wc - c`

    count = `expr $count - 1`

    while(( $count > 0 ))

    do

    temp = `echo $str|cut c $count`

    temp2=`echo $temp2 & temp`

    count `expr $count - 1`

    done

    if[ $str == $temp2 ]

    then

    echo the string is palindrome

    else

    echo the string is not a palindrome

    fi

    OUTPUT:

    enter the string

    malayalam

    the string is palindrome

    enter the string

    kongu

    the string is not a palindrome

  • GECG Gandhinagar CE

    STRING COMPARISON

    echo enter the two strings

    read str 1

    read str 2

    if [ $str1 == $str2]

    then

    echo two strings are equal

    else

    echo two strings are not equal

    fi

    OUTPUT:

    enter the strings:

    rose

    rose

  • GECG Gandhinagar CE

    ARITHMETIC OPERATIONS

    echo enter the 2 numbers

    read a

    read b

    c=`expr $a + $b`

    echo sum of two numbers = $c

    d=`expr $a - $b`

    echo subtraction of two numbers= $d

    e=`expr $a \* $b`

    echo multiplication of two numbers= $e

    f=`expr $a / $b`

    echo division of two numbers= $f

    OUTPUT:

    enter the two numbers

    10

    5

    sum of two numbers =15

    difference of two numbers =5

    multiplication of two numbers=50

    division of two numbers=2

  • GECG Gandhinagar CE

    MENU DRIVEN CALCULATOR

    echo enter the 2 values

    read a

    read b

    echo 1. Addition 2.Subtraction 3.Multiplication 4.Division

    echo Enter your choice

    read ch

    in

    1)c=`expr $a + $b`

    echo sum of two numbers = $c

    ;;

    2)c=`expr $a - $b`

    echo subtraction of two numbers= $c

    ;;

    3)c=`expr $a \* $b`

    echo multiplication of two numbers= $c

    ;;

    4)c=`expr $a / $b`

    echo division of two numbers= $c

    ;;

    *)echo Invalid choice

    ;;

    esac

  • GECG Gandhinagar CE

    OUTPUT:

    enter the two numbers

    10

    5

    1. Addition 2.Subtraction 3.Multiplication 4.Division

    Enter your choice : 3

    multiplication of two numbers=50

  • GECG Gandhinagar CE

    GREATEST OF THREE NUMBERS

    echo "enter 3 values"

    read a

    read b

    read c

    if [ $a -gt $b -a $a -gt $c ]

    then

    echo "the greatest value is $a elif [ $b -gt $c ]

    then

    echo "the greatest value is $b else

    echo "the greatest value is $c fi

    -

    OUTPUT:

    enter 3 values

    4

    8

    6

    the greatest value is 8

  • GECG Gandhinagar CE

    Employee Details

    echo Enter the employee name,designation and basic pay

    read name

    read design

    read basic

    case $desig

    in

    manager) da=`expr $basic \* 50 /100`

    hra=`expr $basic \* 20 /100`

    cca =`expr $basic \* 10 /100`

    pf=3000

    loan=1000

    ;;

    Clerk) da=`expr $basic \* 40 /100`

    hra=`expr $basic \* 30 /100`

    cca =`expr $basic \* 20 /100`

    pf=1500

    loan=750

    ;;

    peon) da=`expr $basic \* 30 /100`

    hra=`expr $basic \* 20 /100`

    cca =`expr $basic \* 10 /100`

    pf=1000

  • GECG Gandhinagar CE

    loan=500

    ;;

    *)echo Invalid designation

    ;;

    esac

    gross=`expr $gross - $pf - $loan`

    echo Employee Details

    echo Employee name : $name

    echo Employee designation : $desig

    echo Employee basic : Rs $basic

    echo Employee da : Rs $da

    echo Employee hra : Rs $hra

    echo Employee cca : Rs $cca

    echo Employee gross : Rs $gross

    echo Employee pf : Rs $pf

    echo Employee loan : Rs $loan

    echo Employee netpay : Rs $netpay

    Output:

    Enter Employee name,designation and basic pay:

    Priya

    Manager

    20000

    Employee Details:

  • GECG Gandhinagar CE

    Name : Priya

    Designation : manager

    Basic : Rs 20000

    Da : Rs 10000

    Hra :Rs 2800

    cca :Rs 2000

    gross pay : Rs 34800

    pf : Rs 3000

    loan : Rs 1000

    netpay : Rs 30800

  • GECG Gandhinagar CE

    Write an interactive file-handling shell program. Let it offer the user the

    choice of copying, removing, renaming, or linking files. Once the user has

    made a choice, have the program ask the user for the necessary information,

    such as the file name and so on.

    echo "1.COPY"

    echo "2.RENAME"

    echo "3.REMOVE"

    echo "4.LINK"

    echo "5.EXIT"

    echo "Enter your choice"

    read ch

    case $ch in

    1) echo "Enter the sources file"

    read s

    echo "Enter the destination file "

    read d

    cp $s $d

    ;;

    2) echo "Enter the old file name"

    read of

    echo "enter the new file name"

    read nf

    mv $of $nf

    ;;

    3) echo "Enter file name to delete"

    read $df

    rm $df

    ;;

    4) echo "Enter the file1"

    read f1

    echo "enter the file2"

    read f2

    ln $f1 $f2

    ;;

    5) exit 0

    ;;

    Esac

  • GECG Gandhinagar CE

    Write a program to create a process in UNIX.

    STEP 1: Start the program.

    STEP 2: Declare pid as integer.

    STEP 3: Create the process using Fork command.

    STEP 4: Check pid is less than 0 then print error else if pid is equal to 0 then execute

    command else parent process wait for child process.

    STEP 5: Stop the program

    PROGRAM:

    void main()

    {

    int id;

    id=fork();

    if(id

  • GECG Gandhinagar CE

    Display usernames those are currently logged in the system

    Using WHO Command

    WHO u

    Write a shell script that displays a list of all the files in the current directory to which the user has read,

    write and execute permissions.

    echo "The list of File Names in the curent directory."

    echo "Which have Read,Write and Execute permisions. "

    for file in *

    do

    if [ -f $file ]

    then

    if [ -r $file -a -w $file -a -x $file ]

    then

    ls -l $file

    fi

    fi

    done

    OUTPUT:

    $ls -l

    total 76

    -rw-rw-r-- 1 srianjaneya srianjaneya 159 Sep 5 21:22 10.sh

    -rw-rw-r-- 1 srianjaneya srianjaneya 216 Sep 5 21:30 11.sh

    -rwx--x--x 1 srianjaneya srianjaneya 231 Sep 5 20:04 1.sh

    -rw------- 1 srianjaneya srianjaneya 231 Sep 5 20:03 1.sh~

  • GECG Gandhinagar CE

    -rwx--x--x 1 srianjaneya srianjaneya 256 Sep 5 20:17 2.sh

    -rw------- 1 srianjaneya srianjaneya 287 Sep 5 20:16 2.sh~

    -rw-rw-r-- 1 srianjaneya srianjaneya 390 Sep 5 20:52 3.sh

    -rw-rw-r-- 1 srianjaneya srianjaneya 171 Sep 5 20:26 3.sh~

    -rw-rw-r-- 1 srianjaneya srianjaneya 249 Sep 5 20:33 4.sh

    -rw-rw-r-- 1 srianjaneya srianjaneya 249 Sep 5 20:32 4.sh~

    The list of File Names in the curent directory.

    Which have Read,Write and Execute permisions.

    -rwx--x--x 1 srianjaneya srianjaneya 231 Sep 5 20:04 1.sh

    -rwx--x--x 1 srianjaneya srianjaneya 256 Sep 5 20:17 2.sh

  • GECG Gandhinagar CE

    Write a shell program to concatenate two strings.

    Step1: Enter into the vi editor and go to the insert mode for entering the code

    Step2: Read the first string.

    Step3: Read the second string

    Step4: Concatenate the two strings

    Step5: Enter into the escape mode for the execution of the result and verify the output

    Program:

    echo enter the first string read str1

    echo enter the second string read str2

    echo the concatenated string is $str1$str2

    Sample I/P: Enter first string: Hello

    Enter first string: World

    Sample O/P:

    The concatenated string is HelloWorld


Recommended