unix system programming qa

Upload: msthenmozhi

Post on 02-Jun-2018

256 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Unix System Programming QA

    1/18

    DEPARTMENT OF MCA, PESIT, BANGALORE 85

    III SEMESTER- MCA Ist TEST- Sept-2014Su!e"t# $%&' S(ste) P*+*))&% Su.C+/e#

    1MCA51

    SOL$TION

    PART-A

    .1The major diferences between ANSI C and K&R C [Kernighan and Ritchie] areas ollows

    !"nction #rotot$#ingS"##ort o the const and %olatile data t$#e "ali'ers(S"##ort wide characters and internationali)ation(*ermit "nction #ointers to be "sed witho"t dereerencing(

    Fu%"t&+% p*+t+t(p&%

    ANSI C ado#ts C++ "nction #rotot$#e techni"e where "nctionde'nition and declaration incl"de "nction names,arg"ments- data t$#es,and ret"rn %al"e data t$#es( This enables ANSI C com#ilers to chec. or

    "nction calls in "ser#rograms that #ass in%alid n"mber o arg"ments orincom#atible arg"ments- data t$#e(These '/ a major wea.ness o K&R Ccom#ilers in%alid "nction calls in "ser #rograms oten #ass com#ilation b"tca"se #rograms to crash when the$ are e/ec"ted(

    ANSI C # /t-t(pe u%"t&+%-%)e 3/t t(pe

    p*)ete* %)e,..

    E'# &%t 13&%t , &%t 6

    7R C # /t-t(pe u%"t&+%-%)e 3p*)ete*

    %)e,..

    E9# &%t 13 , 6 &%t , 6

  • 8/11/2019 Unix System Programming QA

    2/18

    Supp+*t + t:e "+%st %/ ;+>>>?char ch < 0io;#ort? 20read 'rst b$te o data02ch < 0io;#ort? 20read second b$te o data027

    I io;#ort %ariable is not declared to be %olatile when the #rogram iscom#iled, the com#iler ma$ eliminate second ch < 0io;#ort statement, as itis considered red"ndant with res#ect to the #re%io"s statement(

    Supp+*t ?&/e ":*"te*s %/ &%te*%t&+%

  • 8/11/2019 Unix System Programming QA

    3/18

    Pe*)&t u%"t&+% p+&%te*s t+ e use/ ?&t:+ut /e*ee*e%"&% ANSI C Da "nction #ointer can be "sed li.e a "nction K&R C D re"ires the #ointer to be de reerenced to call the "nction

    &%"

  • 8/11/2019 Unix System Programming QA

    4/18

    In addition to the abo%e attrib"tes, NIE s$stems also store the major andminor de%ice n"mbers or each de%ice 'le( All the abo%e attrib"tes areassigned b$ the .ernel to a 'le when it is created(The attrib"tes that are constant or an$ 'le are

    !ile t$#e !ile inode n"mber !ile s$stem IF Gajor and minor de%ice n"mber

    The other attrib"tes are changed b$ the ollowing NIE commands or s$stemcalls

    NIE command S$stem call Attrib"tes changed

    chmod chmod Changes access #ermission, last change time

    chown chown Changes IF, last change time

    chgr# chown Changes HIF, last change time

    To"ch "time Changes last access time, modi'cation time

    ln lin. Increases hard lin. co"nt

    rm "nlin. Fecreases hard lin. co"nt( I the hard lin.co"nt is )ero, the 'le will beremo%ed rom the 'le s$stem

    %i, emac Changes the 'le si)e, last access time, lastmodi'cation time

    PART-B

    .

    A 'le in a NIE or *SIE s$stem ma$ be one o the ollowing t$#es

    J( Reg"lar 'le( Firector$ 'leL( !I! 'leM( Fe%ice 'le( Oin. !ile

  • 8/11/2019 Unix System Programming QA

    5/18

    Reu

  • 8/11/2019 Unix System Programming QA

    6/18

    a"tomaticall$ in%o.e an a##ro#riate de%ice dri%er "nction to #erorm theact"al data transer between the #h$sical de%ice and the a##licationAn a##lication #rogram in t"rn ma$ choose to transer data b$ either acharacter1based3%ia character de%ice 'le5 or bloc.1based3%ia bloc. de%ice'le5

    A de%ice 'le is created in NIE %ia the m.nod command

    ere , c 1 character de%ice 'le JJ 1 major de%ice n"mber 1 minor de%ice n"mber

    F+*

  • 8/11/2019 Unix System Programming QA

    7/18

    The b"fer is discarded when all #rocesses close their reerences

    3stream #ointers5 to the !I! 'le( Fata stored in a !I! b"fer is tem#orar$(

    A !I! 'le ma$ be created %ia the m.'o command(

    The ollowing command creates a !I! 'le 3i it does not e/ists5

    m.'o 2"sr2#rog2'o;#i#e

    The ollowing command creates a !I! 'le 3i it does not e/ists5m.nod2"sr2#rog2'o;#i#e #

    !I! 'les can be remo%ed "sing rm command(

    S()+

  • 8/11/2019 Unix System Programming QA

    8/18

    VCC 1F ;*SIE;SRCP 0(C

    *rogram to chec. and dis#la$ ;*SIE;8PRSIN constant o the s$stem onwhich it r"ns

    Ude'ne ;*SIE;SRCPUde'ne ;*SIE;C;SRCP JWWL=WOUincl"deBiostream(hUincl"deB"nistd(hint main356

    Uide ;*SIE;8PRSINco"tBB9S$stem conorms to *SIE:BB9;*SIE;8PRSINBBendl?Uelseco"tBB9;*SIE;8PRSIN "nde'nedXn:?

    Uendiret"rn =?

    7

    The read "nction etches a '/ed si)e o bloc. o data rom a 'le

    reerenced b$ a gi%en 'le descri#tor(

    The #rotot$#e o read "nction is

    I s"ccess"l, read ret"rns the n"mber o b$tes act"all$ read(

    I "ns"ccess"l, read ret"rns DJ(

    The 'rst arg"ment is an integer, desc that reers to an o#ened 'le(

    The second arg"ment, b" is the address o a b"fer holding an$ data

    read(

    The third arg"ment s#eci'es how man$ b$tes o data are to be read

    rom the 'le(

  • 8/11/2019 Unix System Programming QA

    9/18

    The si)e;t data t$#e is de'ned in the Bs$s2t$#es(h header and

    sho"ld be the same as "nsigned int( There are se%eral cases in which the n"mber o b$tes act"all$ read is

    less than the amo"nt re"ested

    @hen reading rom a reg"lar 'le, i the end o 'le is reached beore

    the re"ested n"mber o b$tes has been read( !or e/am#le, i L=b$tes remain "ntil the end o 'le and we tr$ to read J== b$tes,read ret"rns L=( The ne/t time we call read, it will ret"rn = 3end o'le5(

    @hen reading rom a terminal de%ice( Normall$, "# to one line is

    read at a time( @hen reading rom a networ.( Q"fering within the networ. ma$

    ca"se less than the re"ested amo"nt to be ret"rned( @hen reading rom a #i#e or !I!( I the #i#e contains ewer b$tes

    than re"ested, read will ret"rn onl$what is a%ailable(

    Y(

    *SIE(J de'nes a set o eat"re test macro-s which i de'ned on a s$stem,means that the s$stem has im#lemented the corres#onding eat"res( Allthese test macros are de'ned in u%&st/.: header(

    Fetu*e test )"*+ Ee"ts & /e>%e/

    POSI9QOBCONTROL The s$stem s"##orts the QSF st$le jobcontrol(

    POSI9SAEDIDS Pach #rocess r"nning on the s$stem.ee#s the sa%ed set IF and the set1HIF, so that the$ can change itsefecti%e "ser1IF and gro"#1IF to those%al"es %ia sete"id and setegid A*IZs(

    POSI9CONRESTRICTED I the de'ned %al"e is 1J, "sers ma$

    change ownershi# o 'les owned b$them, otherwise onl$ "sers with s#ecial#ri%ilege ma$ change ownershi# o an$'le on the s$stem(

    POSI9NOTR$NC I the de'ned %al"e is 1J, an$ long#athname #assed to an A*I is silentl$tr"ncated to NAGP;GAE b$tes,otherwise error is generated(

  • 8/11/2019 Unix System Programming QA

    10/18

    POSI9DISABLE I de'ned %al"e is 1J, there is nodisabling character or s#ecialcharacters or all terminal de%ice 'les(therwise the %al"e is the disablingcharacter %al"e(

    L&)&ts ":e"&% t C+)p&

  • 8/11/2019 Unix System Programming QA

    11/18

    bloc. o data that ma$be atomicall$ readrom or written to a#i#e

    POSI9MA9INP$T Ga/im"m ca#acit$ o a

    terminal-s in#"t "e"e3b$tes5

    POSI9MA9CANON Ga/im"m si)e o aterminal-s canonicalin#"t "e"e

    POSI9SSIUEMA9 L>> Ga/im"m %al"e thatcan be stored in assi)e;t1t$#ed object

    POSI9TUNAMEMA9

    L Ga/im"m n"mber ocharacters in a time

    )one name

    T:e +

  • 8/11/2019 Unix System Programming QA

    12/18

    POSI9SIG$E$EMA9

    L Ga/im"m n"mber o realtime signals that a#rocess ma$ "e"e atan$ time(

    POSI9SEMNSEMSM

    A9

    Ga/im"m n"mber o

    sema#hores that ma$ be"sed sim"ltaneo"sl$ #er#rocess(

    POSI9SEMAL$EMA9

    L>> Ga/im"m %al"e that ma$be assigned to asema#hore(

    PART-C

    .WIn NIE s$stem 8, the .ernel maintains a 'le table that has an entr$ o

    all o#ened 'les and also there is an inode table that contains a co#$ o 'leinodes that are most recentl$ accessed( A #rocess, which gets created whena command is e/ec"ted will be ha%ing its own data s#ace 3data str"ct"re5wherein it will be ha%ing 'le descri#tor table( The 'le descri#tor table will beha%ing an ma/im"m o *PN;GAE 'le entries( @hene%er the #rocess callsthe +pe% "nction to o#en a 'le to read or write, the .ernel will resol%e the#athname to the 'le inode n"mber( The ste#s in%ol%ed are

    J( The .ernel will search the #rocess descri#tor table and loo. or the'rst "n"sed entr$( I an entr$ is o"nd, that entr$ will be designated toreerence the 'le (The inde/ o the entr$ will be ret"rned to the #rocess

    as the 'le descri#tor o the o#ened 'le(( The .ernel will scan the 'le table in its .ernel s#ace to 'nd an"n"sed entr$ that can be assigned to reerence the 'le( I an "n"sedentr$ is o"nd the ollowing e%ents will occ"r

    The #rocess 'le descri#tor table entr$ will be set to #oint to this

    'le table entr$( The 'le table entr$ will be set to #oint to the inode table entr$,

    where the inode record o the 'le is stored( The 'le table entr$ will contain the c"rrent 'le #ointer o the

    o#en 'le( This is an ofset rom the beginning o the 'le where

    the ne/t read or write will occ"r( The 'le table entr$ will contain an o#en mode that s#eci'es that

    the 'le o#ened is or read onl$, write onl$ or read and write etc(This sho"ld be s#eci'ed in o#en "nction call(

    The reerence co"nt 3rc5 in the 'le table entr$ is set to J(

    Reerence co"nt is "sed to .ee# trac. o how man$ 'ledescri#tors rom an$ #rocess are reerring the entr$(

  • 8/11/2019 Unix System Programming QA

    13/18

    The reerence co"nt o the in1memor$ inode o the 'le is

    increased b$ J( This co"nt s#eci'es how man$ 'le table entriesare #ointing to that inode(

    I either 3J5 or 35 ails, the +pe% s$stem call ret"rns 1J 3ail"re2error5

    Normall$ the reerence co"nt in the 'le table entr$ is J,i we wish to increasethe rc in the 'le table entr$, this can be done "sing or.,d"#,d"# s$stemcall( @hen a o#en s$stem call is s"cceeded, its ret"rn %al"e will be an integer3'ledescri#tor5( @hene%er the #rocess wants to read or write data rom the'le, it sho"ld "se the 'le descri#tor as one o its arg"ment(

    The ollowing e%ents will occ"r whene%er a #rocess calls the "

  • 8/11/2019 Unix System Programming QA

    14/18

    .X

    This is "sed to establish a connection between a #rocess and a 'le i(e( it is"sed to o#en an e/isting 'le or data transer "nction or else it ma$ be alsobe "sed to create a new 'le(The ret"rned %al"e o the o#en s$stem call is the 'le descri#tor 3rown"mber o the 'le table5, which contains the inode inormation(The #rotot$#e o o#en "nction is

    I s"ccess"l, o#en ret"rns a nonnegati%e integer re#resenting the

    o#en 'le descri#tor( I "ns"ccess"l, o#en ret"rns DJ(

  • 8/11/2019 Unix System Programming QA

    15/18

    The 'rst arg"ment is the name o the 'le to be created or o#ened( This

    ma$ be an absol"te #athname or relati%e #athname( I the gi%en #athname is s$mbolic lin., the o#en "nction will resol%e

    the s$mbolic lin. reerence to a non s$mbolic lin. 'le to which it reers( The second arg"ment is access modes, which is an integer %al"e that

    s#eci'es how act"all$ the 'le sho"ld be accessed b$ the calling#rocess(

    Henerall$ the access modes are s#eci'ed in Bcntl(h( 8ario"s access

    modes areORDONLV 1 o#en or reading 'le onl$ORONLV 1 o#en or writing 'le onl$ORDR 1 o#ens or reading and writing 'le(

    There are other access modes, which are termed as access modi'er ags,and one or more o the ollowing can be s#eci'ed b$ bitwise1Ring themwith one o the abo%e access mode ags to alter the access mechanism o

    the 'le(OAPPEND 1 A##end data to the end o 'le(OCREAT 1 Create the 'le i it doesn-t e/istOE9CL 1 Henerate an error i ;CRPAT is also s#eci'ed and the 'lealread$ e/ists(OTR$NC 1 I 'le e/ists discard the 'le content and set the 'le si)e to)ero b$tes(ONONBLOC7 1 S#eci$ s"bse"ent read or write on the 'le sho"ldbe non1bloc.ing(ONOCTTV 1 S#eci$ not to "se terminal de%ice 'le as the calling

    #rocess control terminal(

    To ill"strate the "se o the abo%e ags, the ollowing e/am#le statemento#ens a 'le called 2"sr2di%$a2"s# or read and write in a##end mode

    &%t /K+pe%3us*/&;(uspH,ORDR Y OAPPEND,06 I the 'le is o#ened in read onl$, then no other modi'er ags can be "sed( I a 'le is o#ened in write onl$ or read write, then we are allowed to "sean$ modi'er ags along with them(The third arg"ment is "sed onl$ when a new 'le is being created( Thes$mbolic names or 'le #ermission are gi%en in the table

  • 8/11/2019 Unix System Programming QA

    16/18

    .8a5ard and S$mbolic Oin.s A hard lin. is a NIE #athname or a 'le( Henerall$ most o the NIE 'leswill be ha%ing onl$ one hard lin.( In order to create a hard lin., we "se the command

  • 8/11/2019 Unix System Programming QA

    17/18

  • 8/11/2019 Unix System Programming QA

    18/18

    The ollowing lists some C librar$ "nctions and the "nderl$ing NIE A*Is thatare "sed to #erorm %ario"s 'le "nctions