OS Day 1 2 3

download OS Day 1 2 3

of 30

Transcript of OS Day 1 2 3

  • 7/26/2019 OS Day 1 2 3

    1/30

    ASSIGNMENT 1.1: Introduction to Unix/Linux architecture, features,Unix File System.

    ASSIGNMENT 1.2: Familiarization with basic Unix/Linux commands,creating a scrit.

    UNIX

    U!I" is an oerating system which was #rst de$eloed in the %&'(s,and has been under constant de$eloment e$er since. )y oeratingsystem, we mean the suite of rograms which ma*e the comuterwor*. It is a stable, multi+user, multi+tas*ing system for ser$ers,des*tos and latos.

    U!I" systems also ha$e a grahical user interface -UI similar toicrosoft 0indows which ro$ides an easy to use en$ironment.1owe$er, *nowledge of U!I" is re2uired for oerations which aren3tco$ered by a grahical rogram, or for when there is no windowsinterface a$ailable, for examle, in a telnet session.

    UNIX ARCHITECTURE

  • 7/26/2019 OS Day 1 2 3

    2/30

    UNIX INTERFACES

    UNIX SHELL

    4he shell acts as an interface between the user and the *ernel. 0hena user logs in, the login rogram chec*s the username and assword,and then starts another rogram called the shell. 4he shell is acommand line interreter 5LI. It interrets the commands the usertyes in and arranges for them to be carried out. 4he commands arethemsel$es rograms: when they terminate, the shell gi$es the useranother romt 6 on our systems.

    4he adet user can customize his/her own shell, and users can usedi7erent shells on the same machine.

    4he tcsh shell has certain features to hel the user inuttingcommands.

    Filename 5omletion + )y tying art of the name of a command,#lename or directory and ressing the 84ab9 *ey, the tcsh shell willcomlete the rest of the name automatically. If the shell #nds more

  • 7/26/2019 OS Day 1 2 3

    3/30

    than one name beginning with those letters you ha$e tyed, it willbee, romting you to tye a few more letters before ressing thetab *ey again.

    1istory + 4he shell *ees a list of the commands you ha$e tyed in. Ifyou need to reeat a command, use the cursor *eys to scroll u anddown the list or tye history for a list of re$ious commands.

    UNIX UTILITY PROGRAMS

    4here are large number of utility rograms which are classi#ed into 'categories which are

    %. File and directory maniulation commands.

    . Filters.

    ;. . System ?dministration.

    '. iscellaneous.

    UNIX KERNEL

    Fig: ?roximate structure of -eneric Unix @ernel

  • 7/26/2019 OS Day 1 2 3

    4/30

    4he *ernel of U!I" is the hub of the oerating system: it allocatestime and memory to rograms and handles the #lestore andcommunications in resonse to system calls.

    ?s an illustration of the way that the shell and the *ernel wor*

    together, suose a user tyes rm my#lewhich has the e7ect ofremo$ing the #le my#le. 4he shell searches the #le store for the#le containing the rogram rm, and then re2uests the *ernel,through system calls, to execute the rogram rmon my#le. 0henthe rocess rm my#lehas #nished running, the shell then returnsthe U!I" romt 6 to the user, indicating that it is waiting forfurther commands.

    UNIX FILE SYSTEM

    4he U!I" #lesystem contains se$eral di7erent tyes of #les:

    Ardinary Files

    o Used to store your information, such as some text you ha$ewritten or an image you ha$e drawn. 4his is the tye of #lethat you usually wor* with.

    o ?lways located within/under a directory #le

    o Bo not contain other #les

    Birectories

    o )ranching oints in the hierarchical tree

    o Used to organize grous of #les

    o ay contain ordinary #les, secial #les or other directories

    o !e$er contain CrealC information which you would wor* withsuch as text. )asically, Dust used for organizing #les.

    o ?ll #les are descendants of the root directory, named / located at the to of the tree.

  • 7/26/2019 OS Day 1 2 3

    5/30

    Secial Files

    o Used to reresent a real hysical de$ice such as a rinter,tae dri$e or terminal, used for I/A oerations

    o Unix considers any de$ice attached to the system to be a#le + including your terminal:

    )y default, a command treats your terminal as thestandard inut #le stdin from which to read its inut

    Eour terminal is also treated as the standard outut #lestdout to which a command3s outut is sent

    o 4wo tyes of I/A: character and bloc*

    o Usually only found under directories named /de$

    ' characters

  • 7/26/2019 OS Day 1 2 3

    6/30

    xtensions: may be used to identify tyes of #leslibc.a + archi$e, library #lerogram.c + 5 language source #lealha.f + Fortran source #lexwds.o + AbDect/executable codemygames. + 5omressed #le

    1idden Files: ha$e names that begin with a dot . For examle:.cshrc .login .mailrc .mwmrc

    Uni2ueness: as children in a family, no two #les with the samearent directory can ha$e the same name. Files located insearate directories can ha$e identical names.

    Teser$ed Filenames:/ + the root directory slash. + current directory eriod.. + arent directory double eriodV + your home directory tilde

    UNIX SYSTEM PATH NAMES

    Secify where a #le is located in the hierarchically organized #lesystem

    ust *now how to use athnames to na$igate the U!I" #lesystem

    ?bsolute

  • 7/26/2019 OS Day 1 2 3

    7/30

    cd ../2uattro + relati$e athnamecd /usr/home/2uattro + absolute athname

    UNIX FILE PARENT-CHILD RELATIONSHIP

    In U!I" all #les are related to each other. 4he #le system on U!I" is thecollection of all these related #les organized in a hierarchical structure.It is reresented by the following:

    FILE & DIRECTORY COMMANDSDIRECTORY COMMANDS

    %. wd + rint wor*ing directory. 4ells you which directory youare currently in.wd

    . m*dir + ma*e directory. 0ill create the new directory in yourwor*ing directory by default.m*dir data

    ;. cd + change to seci#ed directory. ay secify either the

    absolute or relati$e athname. cd with no athnamechanges to your home directory.cd .. + change to arent directorycd + change to home directory

    =. rmdir + remo$e directory. Birectories must be emty beforeyou remo$e them. rmdir roDect%

    4o recursi$ely remo$e nested directories, use the rmcommand with the +r otion:rm +r dirctoryWname

    FILE COMMANDS

    %. ls + lists #lesls +l + show contents of wor*ing directoryls +d + show all directory

    . cat+ 4his command is useful for $iewing as well as forconcatenating hence the name CcatC #les together.

    http://www.cis.rit.edu/class/simg211/unixintro/man/mkdir.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/cd.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/rmdir.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/ls.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/cat.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/cat.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/cd.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/rmdir.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/ls.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/cat.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/mkdir.txt
  • 7/26/2019 OS Day 1 2 3

    8/30

    cat test test% + shows contents of both #les on terminal.cat #le% #le O #le; + adds #le% and #le to ma*e #le;

    ;. c + coies #les. 0ill o$erwrite unless otherwise seci#ed.c samle.f samle.f + coies samle.f to samle.f

    =. m$ + mo$es #les. 0ill o$erwrite unless otherwise seci#ed. ust

    also ha$e write ermission in the destination directory.m$ samle.f samle.f + mo$es samle.f to samle.fm$ dir% newdir/dir + mo$es contents of directory dir% tonewdir/dirm$ X.txt chat% + mo$es all #les with .txt suYx todirectory chat%

    >. date Z shows the system date.'. cat Z shows the calendar.

    catNmonthO NyearO + Show calendar for the month of theseci#c year.

    http://www.cis.rit.edu/class/simg211/unixintro/man/cp.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/mv.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/cp.txthttp://www.cis.rit.edu/class/simg211/unixintro/man/mv.txt
  • 7/26/2019 OS Day 1 2 3

    9/30

    ASSIGNMENT 2.1:

  • 7/26/2019 OS Day 1 2 3

    10/30

    PROCESS TO MODIFY ACCESS PERMISSIONS

    E.g.Temo$e write ermissions w for all classes a, re$enting

    anyone from writing to the #le:

    Pls +l our)estTeferenceFile+rw+rw+r++ teamleader usguys &' ?r _ %:>;

    our)estTeferenceFilePchmod a+w our)estTeferenceFilePls +l our)estTeferenceFile+r++r++r++ teamleader usguys &' ?r _ %:>;

    our)estTeferenceFileE.g. Set the ermissions for the owner and the grou ug to read and

    execute rx only no write ermission on referenceLib, re$entinganyone to add #les.

    Pls +ld referenceLibdrwxr+++++ teamleader usguys &' ?r _ %:>; referenceLibPchmod ug^rx referenceLibPls +ld referenceLibdr+xr+x+++ teamleader usguys &' ?r _ %:>; referenceLib

    VI EDITOR

    4he $i editor short for $isual editor is a screen editor which isa$ailable on almost all Unix systems.

    It has three oeration modes:%. 5ommand ode. Insert ode;. x ode

    4he syntax used to create or oen a #le in $i editor is:

    $i N#lename.extensionO

  • 7/26/2019 OS Day 1 2 3

    11/30

    SHELLSCRIPT PROGRAMMING

    Q.0rite a menu dri$en shell rogram for erforming the followingoerations.

    Cod!echo Cnter #rst number: C

    read aecho Cnter second number: C

    read becho Cnter an oeration ],+,X,/,6,`: C

    read o

    case Po in] r^Kexr Pa ] PbKHH+ r^Kexr Pa + PbKHHJX r^Kexr Pa JX PbKHH/ r^Kexr Pa / PbKHH6 r^Kexr Pa 6 PbKHH` r^Kecho CPa ` PbC bcKHHX echo CIn$alid InutC

    exitHHesac

    echo Pa Po Pb C^C Pr

    O"#$"#!nter #rst number:(nter second number:%(nter an oeration ],+,X,/,6, :](]%(^;(

    nter #rst number:(nter second number:%(nter an oeration ],+,X,/,6, :/

  • 7/26/2019 OS Day 1 2 3

    12/30

    (/%(^

    ASSIGNMENT %: aximum G inimum number in set,

  • 7/26/2019 OS Day 1 2 3

    13/30

    O"#$"#!nter the number of integers:;nter the integers:(

    ;(%(

    4he max $alue is ;(4he min $alue is %(

  • 7/26/2019 OS Day 1 2 3

    14/30

    %.2.0rite a shell rogram to #nd the rime numbers in a gi$en rangeof numbers.

    Cod!echo Cnter the number to test:C

    read numcount^end^Kexr Pnum / Krime^truewhile 8 Pcount +le Pend 9do

    if 8 Kexr Pnum 6 PcountK ^^ ( 9then

    rime^falsebrea*

    #count^Kexr Pcount ] %Kdoneif 8 Prime ^^ true 9then

    echo C4he number is rimeCelse

    echo C4he number is !A4 rimeC#

    O"#$"#!

    nter the number to test:%

    4he number is rime

    nter the number to test:%

    4he number is !A4 rime

  • 7/26/2019 OS Day 1 2 3

    15/30

    %.%.0rite a shell rogram to chec* if a gi$en number is alindrome ornot.

    Cod!echo Cnter the number:C

    read numtem^Pnums^(while 8 Ptem Q^ ( 9do

    r^Kexr Ptem 6 %(Ks^Kexr Ps JX %( ] PrKtem^Kexr Ptem / %(K

    doneif 8 Ps ^^ Pnum 9

    then echo C4he number is a alindromeCelse

    echo C4he number is !A4 a alindromeC#

    O"#$"#!nter the number:%%

    4he number is a alindromenter the number:%

    4he number is !A4 a alindrome

    ASSIGNMENT .1! Sum of the Series of !atural !umbers where theLimit is gi$en by the user

  • 7/26/2019 OS Day 1 2 3

    16/30

    Cod!

    Q/bin/bash

    echo +n Cnter n to calculate sum of #rst n natural numbers: C

    read nsum^(fori^%HiN^nHi]]do

    sum^Kexr Psum ] PiKecho +n CPi]C

    doneecho +e CJb^PsumC

    Output:

    nter n to calculate sum of #rst n natural numbers: =%]];]=^%(

  • 7/26/2019 OS Day 1 2 3

    17/30

    ASSIGNMENT .2!)ubble Sort an array of user inuts

    Code:

    Q/bin/bash

    echo +n Cnter the number of inuts: Cread nfori^(H iNnH i]]do

    echo +n Cnter inut: Cread a8i9

    done

    fori^(H iNn+%H i]]do

    forD^(H DNn+i+%H D]]do

    if 8 Pa8D9M +gt Pa8D]%9M 9then

    tem^Pa8D9Ma8D9^Pa8D]%9Ma8D]%9^Ptem

    #done

    done

    echo C4he sorted array is:C Pa8X9M

    Output:nter the number of inuts: >nter inut: =nter inut: nter inut: ;nter inut: 'nter inut: %

    4he sorted array is: % ; = '

  • 7/26/2019 OS Day 1 2 3

    18/30

    ASSIGNMENT .%:5alculator from the terminal

    Cod!

    Q/bin/bashecho +n Cnter #rst number: Cread aecho +n Cnter second number: Cread becho +n Cnter an oeration ],+,X,/,6,`: Cread ocase Po in

    ] r^Kexr Pa ] PbKHH+ r^Kexr Pa + PbKHH

    JX r^Kexr Pa JX PbKHH/ r^Kexr Pa / PbKHH6 r^Kexr Pa 6 PbKHH` r^Kecho CPa PbC bcKHHX echo CIn$alid InutC

    exitHHesacecho Pa Po Pb C^C Pr

    Output:

    nter #rst number: =nter second number: nter an oeration ],+,X,/,6,`: `= ` ^ %'

    nter #rst number: >>nter second number: >nter an oeration ],+,X,/,6,`: 6>> 6 > ^ (

  • 7/26/2019 OS Day 1 2 3

    19/30

    ASSIGNMENT .:)inary search

    Cod!

    Q/bin/bashecho +n Cnter the number of inuts: Cread nfori^(H iNnH i]]do

    echo +n Cnter inut: Cread arr8i9

    doneecho +n Cnter the number to search: C

    read search insertion sortfori^%H iNnH i]]do

    tem^Parr8i9MforD^i+%H DO^(H D++do

    if 8 Parr8D9M +gt Ptem 9then

    arr8D]%9^Parr8D9M

    else brea*#

    donearr8D]%9^Ptem

    doneecho C4he sorted array isC Parr8X9M binary search on sorted arraylower^(uer^Kexr Pn + %Kwhile 8 Plower +le Puer 9do

    sum^Kexr Plower ] PuerKmid^Kexr Psum / Kif 8 Parr8mid9M ^^ Psearch 9then

    echo C4he element exists at indexC Pmidexit

  • 7/26/2019 OS Day 1 2 3

    20/30

    elif 8 Parr8mid9M +gt Psearch 9then

    uer^Kexr Pmid + %Kelse

    lower^Kexr Pmid ] %K

    #doneecho C4he element was !A4 foundC

    Output:

    nter the number of inuts: >nter inut: ==nter inut: nter inut: ;;

    nter inut: %%nter inut: nter the number to search:

    4he sorted array is %% ;; ==4he element was !A4 found

    nter the number of inuts: _nter inut: =;nter inut: 'nter inut: ;%

    nter inut: 'nter inut: ;nter inut: _'nter inut: ;nter inut: '>nter the number to search: '

    4he sorted array is ; ; ;% =; ' '> ' _'4he element exists at index '

  • 7/26/2019 OS Day 1 2 3

    21/30

    ASSIGNMENT '.1!5hec* if a gi$en word is a

  • 7/26/2019 OS Day 1 2 3

    22/30

    ASSIGNMENT '.2! 5ount the number of $owels in a word

    Cod:

    Q/bin/bash

    echo +n Cnter the word: Cread strlen^Kexr length PstrKcount^(for i^%H iN^lenH i]]do

    c^Kexr Pstr cut +c PiKif 8 Pc ^ 3a3 +o Pc ^ 3e3 +o Pc ^ 3i3 +o Pc ^ 3o3 +o Pc ^ 3u3 9then

    count^Kexr Pcount ] %K

    #doneecho C4he number of $owels isC Pcount

    Output:

    nter the word: umbrella4he number of $owels is ;

    nter the word: ale

    4he number of $owels is

  • 7/26/2019 OS Day 1 2 3

    23/30

    ASSIGNMENT '.%!?ccet or TeDect a string based on its length

    Cod!Q/bin/bash

    echo +n Cnter the string: Cread strlen^Kecho CPstrC wc +mKlen^Kexr Plen + %Kecho C4he number of characters isC Plen

    if 8 Plen +ge %( 9then

    echo C?ccetedCelse

    echo CTeDectedC#

    Output:

    nter the string: 1ello is there anybody in there4he number of characters is ;%?cceted

    nter the string: ale deli$ery

    4he number of characters is %=?cceted

    nter the string: madam4he number of characters is >TeDected

  • 7/26/2019 OS Day 1 2 3

    24/30

    ASSIGNMENT (.1!

  • 7/26/2019 OS Day 1 2 3

    25/30

    ASSIGNMENT (.2! 5reate a child rocess of the current rocess

    Code:include Nstdio.hOinclude Nunistd.hO

    int main$oid

    int idHid ^ for*HrintfCIB^6d

  • 7/26/2019 OS Day 1 2 3

    26/30

    ASSIGNMENT (.%!Bemonstrate an orhan rocess

    Code:include Nstdio.hOinclude Nunistd.hO

    int main$oid

    int idHid ^ for*Hif id ^^ (

    slee>HrintfCIB^6d

  • 7/26/2019 OS Day 1 2 3

    27/30

    ASSIGNMENT (.!Bemonstrate a zombie rocess

    Code:include Nstdio.hOinclude Nunistd.hO

    int main$oid

    int idHid ^ for*Hif id O (

    slee>HrintfC

  • 7/26/2019 OS Day 1 2 3

    28/30

    Code:include Nstdio.hOinclude Nunistd.hOint main$oid

    int idHid ^ for*Hif id ^^ (

    rintfC51ILB

  • 7/26/2019 OS Day 1 2 3

    29/30

    include Nstdio.hOinclude Nunistd.hOint main

    int id, iH

    id ^ for*Hfor i^(H iN%(H i]]

    if id O (

    id ^ for*Hif id ^^ (

    rintfCI am rocess 6d with

  • 7/26/2019 OS Day 1 2 3

    30/30

    ASSIGNMENT ).2!5reate ultile 5hild