0% found this document useful (0 votes)
83 views3 pages

Give A Brief Overview of File I/O Commands With Example

The document provides an overview of file I/O commands in Unix/Linux systems, including open, close, read, write, lseek, and their usage. Open opens or creates a file and returns a file descriptor. Close closes a file descriptor. Read and write perform input and output operations on files using file descriptors, reading/writing a specified number of bytes at the current file offset. Lseek repositions the current file offset.

Uploaded by

knk761987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views3 pages

Give A Brief Overview of File I/O Commands With Example

The document provides an overview of file I/O commands in Unix/Linux systems, including open, close, read, write, lseek, and their usage. Open opens or creates a file and returns a file descriptor. Close closes a file descriptor. Read and write perform input and output operations on files using file descriptors, reading/writing a specified number of bytes at the current file offset. Lseek repositions the current file offset.

Uploaded by

knk761987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1a. Give a brief overview of file I/O commands with example. FILE I/O: ( o!rces:"#ola$% &r!

ce #ola$ 'nderstandin( 'nix/Lin!x )ro(rammin(* +hapter ," tevens% -. .ichard tevens /dvanced )ro(rammin( in the '0I1 Environment* +hapter 2man , f!nction3 Introd!ction #ost 'nix file I/O can be performed with six f!nctions: Open close read write lsee4 d!p* d!p, 5hese f!nctions are part of the )O I1 standard for '0I1 pro(rammin(* not part of /0 I + (and so* $o! will not find reference to them in 6arbison and teele7s + .eference #an!al.3 'nli4e the standard I/O ro!tines provided b$ /0 I + (s!ch as fscanf and fprintf which store the data the$ read in b!ffers3 these f!nctions are !nb!ffered I/O. 5he$ invo4e a s$stem call in the 4ernel* and will be called b$ the standard /0 I + f!nction calls in the '0I1 environment. File 8escriptors -hat happens when a process opens a file9 / process ma$ have several open files which it ma$ be readin( from and writin( to. It also has a c!rrent position within the file* which is the next b$te to be read or written. Each process has its own arra$ to 4eep trac4 of 5he file opened. 5he files stat!s (whether open for readin( or writin(: the file stat!s fla(s 3 5he c!rrent offset within the file -hen a file is opened or created b$ a process the 4ernel assi(ns a position in the arra$ called the file descriptor. Each entr$ of this arra$ act!all$ contains a pointer to a file table which stores each of the three pieces of information: file* file stat!s fla(s* and offset. 5he file table does not itself contain the file* b!t instead has a pointer to another table (called the vnode table3* which has vital information abo!t the file* incl!din( its location in memor$. 5he vnode table is !ni:!e for each file. -h$ this complicated se:!ence9: file descriptor ;;;< file table ;;;< vnode table It t!rns o!t to be ver$ flexible: 8ifferent processes can have file descriptors for the same file. / sin(le process can have several file descriptors for the same file thro!(h different file tables. / sin(le process can have different file descriptors for the same file table. 8ifferent process can have different file descriptors for the same file table. (5he processes m!st be related as parent to child* or (randchild. 5his involves for4in(.3 5his flexibilit$ is at the heart of how 'nix can implement file redirection (= and <3 and pipes (>3. 5he file descriptors are !ni:!e to a process (except !nder certain circ!mstances3* in that the inte(ers ma$ b$ re!sed b$ another process witho!t referrin( to the same file or location within a file. &$ convention 'nix shells (altho!(h not the 4ernel3 emplo$ the followin( val!es:File File 8escriptor )O I1 $mbolic +onstant tandard Inp!t ? 58I0@FILE0O tandard O!tp!t 1 58O'5@FILE0O

tandard Error , 58E.. 6ow man$ file descriptors ma$ be opened b$ a process9 One complication here is that this ma$ depend !pon the available memor$ for the process* the maxim!m inte(er siAe* or limits set b$ the s$stem administrator. Its val!e is potententiall$ !nlimited* b!t (!aranteed b$ )O I1 to be at least 1B. 8eterminin( the act!al val!e can be diffic!lt* since the val!e ma$ not be determined at compile time. If a hard limit has been set (so determined at compile time3* the s$mbolic constant O)E0@#/1 fo!nd in limits.h will be setC if the limit is determined at r!n time a call to s$sconf(@ +@O)E0@#/13 will prod!ce the val!e. &!t* it ma$ be indeterminate even at r!n time (see " tevens* pp. D,;D% for an alternative to determine this val!e.3 )O I1 does insist that the val!e sho!ld not chan(e d!rin( the processes lifetime. " tevens* pp. 22;D* D?* D,;D* DE;F% $stem +alls .emember that ever$thin( is a file in 'nix. &eca!se there are so man$ t$pes of files* several of the s$stem calls below will behave sli(htl$ differentl$ for files that are not re(!lar. I have not attempted to incl!de the exact behavior* or all the errors that can arise for files which are not re(!lar. Open: )!rpose open or create a file for readin( or writin( Incl!de Gincl!de=fcntl.h< If the optional third ar(!ment is !sed also incl!de: Gincl!de=s$s/t$pes.h< Gincl!de=s$s/stat.h< 'sea(e int open(const char Hpath* int fla(s"* mode@t mode%3C (5he third ar(!ment is optional.3 /r(!ments path: the (relative3 path to the file fla(s: the file stat!s fla(s mode: file permissions* !sed when creatin( a new file .et!rns ;1 on error file descriptor on s!ccess Errors 5oo n!mero!s to list all: see man , open E0O58I.: / component of the path prefix is not a director$. E/++E : )ermissions do not permit readin( or writin( EI 8I.: 5he named file is a director$ and it is to opened for writin(. E#FILE: 5he process has alread$ reached its limit for open file descriptors. 5he file descriptor ret!rned b$ open is (!aranteed to be the lowest n!mbered !n!sed descriptor. 5his is val!able to 4now when $o! want to redirect to a re(!lar file the inp!t to a command that expects to read standard inp!t (or write to standard o!tp!t3: 5o redirect standard inp!t simpl$ close 58I0@FILE0O (descriptor ?3 then open a new file;;which will be (iven descriptor ?. 5he val!es for the second ar(!ment* the file stat!s fla(s consists of bitwise O.7in( (7>73 the followin(:

One of these three m!st be incl!ded: [email protected] open for readin( onl$ [email protected] open for writin( onl$ [email protected]. open for readin( and writin( 5he followin( are optional ar(!ments O@/))E08 append on each write O@+.E/5 create file if it does not exist: .EJ'I.E mode O@5.'0+ tr!ncate siAe to ? 5here are fo!r other options* b!t these three are the most !sef!l* for now. ee " tevens* p.DK% for a description of all options. 5he val!e for mode m!st be incl!ded when O@+.E/5 is set. It is simpl$ the permissions* and can be written !sin( +7s octal representation (this is base ei(ht and starts with a leadin( Aero.3 For example* to re:!est that $o! (the creator3 have read and write privile(es and ever$one else have read privile(es onl$* $o! wo!ld specif$ open(LpathtofileL*[email protected] > O@+.E/5* ?BDD3C 5his is onl$ a re:!est on $o!r part* and this re:!est will be compared with the !mas4 to determine the final permissions of the file. ee especiall$ "#ola$* pp. FD;F%* or " tevens* p.EF;F1%for f!rther details. 0otice how open allows a process to have different file descriptors to the same file. 5hese ma$ have different file stat!s fla(s* and ma$ even have different offsets within the file. +lose: close is !sed to detach the !se of the file descriptor for a process. -hen a process terminates an$ open file descriptors are a!tomaticall$ closed b$ the 4ernel. close )!rpose delete a file descriptor Incl!de Gincl!de=!nistd.h< 'sea(e int close(int d3C /r(!ments d: a file descriptor .et!rns ;1 on error ? on s!ccess (the file descriptor deleted3 Errors E&/8F: d is not an active descriptor. EI05.: /n interr!pt was received. .ead: .ead starts at the file7s c!rrent offset* which is then offset b$ the n!mber of b$tes read (for re(!lar files .ead )!rpose read inp!t from file Incl!de Gincl!de=!nistd.h< 'sea(e ssiAe@t read(int d* void Hb!f* siAe@t nb$tes3C /r(!ments d: a file descriptor b!f: b!ffer for storin( b$tes read nb$tes: maxim!m n!mber of b$tes to read .et!rns ;1 on error n!mber of b$tes read and placed in b!f or ? if end of file Errors E&/8F: d is not an active descriptor..

EF/'L5: b!f points o!tside the allocated address space. E/G/I0: 5he file was mar4ed for non; bloc4in( I/O* and no data were read$ to be read. EI0M/L: 5he pointer associated with d was ne(ative. EIO: /n I/O error occ!rred while readin( from the file s$stem. 5he main reason the n!mber of b$tes read ma$ be less than the n!mber of b$tes re:!ested in nb$tes is that the end of the file was reached before the re:!ested n!mber of b$tes has been read. ee " tevens* p. ND% for several other reasons involvin( other t$pes of files. -rite: write starts at the file7s c!rrent offset* which is then offset b$ the n!mber of b$tes written to the file (for re(!lar files.3 write )!rpose write o!tp!t to file Incl!de Gincl!de=!nistd.h< 'sea(e ssiAe@t write(int d* void Hb!f* siAe@t nb$tes3C /r(!ments d: a file descriptor b!f: b!ffer for storin( b$tes to be written nb$tes: maxim!m n!mber of b$tes to read .et!rns ;1 on error n!mber of b$tes written Errors 5oo n!mero!s to list all: see man , write E&/8F: d is not an active descriptor. EF/'L5: 8ata to be written to the file points o!tside the allocated address space. EI0M/L: 5he pointer associated with d was ne(ative. EF&IG: /n attempt was made to write a file that exceeds the process7s file siAe limit or the maxim!m file siAe. E0O )+: 5here is no free space remainin( on the file s$stem containin( the file.. E/G/I0: 5he file was mar4ed for non; bloc4in( I/O* and no data were read$ to be read. EI05.: / si(nal interr!pted the write before it co!ld be completed. EIO: /n I/O error occ!rred while readin( from the file s$stem. Lsee4: Ever$ file descriptor has an associated c!rrent file offset* a n!mber of b$tes from the be(innin( of the file. .ead and write operations normall$ start at the c!rrent offset and ca!se the offset to be incremented the n!mber

of b$tes read or written. lsee4 explicitl$ repositions this offset val!e. lsee4 )!rpose reposition read/write file offset Incl!de Gincl!de=!nistd.h< 'sea(e off@t lsee4(int d* off@t offset* int base3C /r(!ments d: a file descriptor offset: the n!mber of b$tes to be offset base: the position from which the b$tes will be offset: EEO@ E5: offset b$tes from be(innin( of the file. EEO@+'.: offset b$tes from c!rrent val!e of offset. EEO@E08: offset b$tes from end of the file. .et!rns ;1 on error 5he res!ltin( offset location as meas!red in b$tes from the be(innin( of the file. Errors E&/8F: d is not an active descriptor.. EI0M/L: basenot a proper val!e. E )I)E: base associated with a non; re(!lar file (pipe* soc4et or FIFO.3 / file7s offset can be (reater than its c!rrent siAe. In this case* if the file is then written to it creates holes in the file* whose val!e is 7P?7. / re(!lar file ma$ not be offset before the be(innin( of the file lsee4 can be !sed to determine the c!rrent offset off@t c!rrposC cc!rrpos Q lsee4(fd* ?* EEO@+'.3C lsee4 can also be !sed to test a file descripter if it is a pipe* FIFO* or soc4et* since these are not capable of see4in(: the$ force a ret!rn of ;1 and set errno to E )I)E. +reat: +reat opens a file for writin(* creatin( a new file if one did not exist* or tr!ncatin( the c!rrent file* discardin( its contents* if a file does exist. /ct!all$* creat is now implemented b$ open. Its protot$pe* to(ether with the necessar$ header file is: Gincl!de=fcntl.h< int creat(const char Hpath* mode@t mode3C b!t is implemented as open(path* O@+.E/5 > O@5.'0+ > [email protected]* mode3C 1b. 8isc!ss the role of semaphores in I)+. Explain how to destro$ semaphores sets9 I)+: emaphores emaphores are a pro(rammin( constr!ct desi(ned b$ E. -. 8iR4stra in the late 1KB?s. 8iR4stra7s model was the operation of railroads: consider a stretch of railroad in which there is a sin(le trac4 over which onl$ one train at a time is allowed. G!ardin( this trac4 is a semaphore. / train m!st wait before enterin( the sin(le trac4 !ntil the semaphore is in a state that permits travel. -hen the train enters the trac4* the semaphore chan(es state to prevent other trains from enterin( the trac4. / train that is leavin( this section of trac4 m!st a(ain chan(e the state of the

semaphore to allow another train to enter. In the comp!ter version* a semaphore appears to be a simple inte(er. / process (or a thread3 waits for permission to proceed b$ waitin( for the inte(er to become ?. 5he si(nal if it proceeds si(nals that this b$ performin( incrementin( the inte(er b$ 1. -hen it is finished* the process chan(es the semaphore7s val!e b$ s!btractin( one from it. emaphores let processes :!er$ or alter stat!s information. 5he$ are often !sed to monitor and control the availabilit$ of s$stem reso!rces s!ch as shared memor$ se(ments. emaphores can be operated on as individ!al !nits or as elements in a set. &eca!se $stem M I)+ semaphores can be in a lar(e arra$* the$ are extremel$ heav$ wei(ht. #!ch li(hter wei(ht semaphores are available in the threads librar$ (see man semaphore and also +hapter 2?.23 and )O I1 semaphores (see below briefl$3. 5hreads librar$ semaphores m!st be !sed with mapped memor$ . / semaphore set consists of a control str!ct!re and an arra$ of individ!al semaphores. / set of semaphores can contain !p to ,N elements. In a similar fashion to messa(e :!e!es* the semaphore set m!st be initialiAed !sin( sem(et(3C the semaphore creator can chan(e its ownership or permissions !sin( semctl(3C and semaphore operations are performed via the semop(3 f!nction. 5hese are now disc!ssed below: InitialiAin( a emaphore et 5he f!nction sem(et(3 initialiAes or (ains access to a semaphore. It is protot$ped b$: int sem(et(4e$@t 4e$* int nsems* int semfl(3C -hen the call s!cceeds* it ret!rns the semaphore I8 (semid3. 5he 4e$ ar(!ment is a access val!e associated with the semaphore I8. 5he nsems ar(!ment specifies the n!mber of elements in a semaphore arra$. 5he call fails when nsems is (reater than the n!mber of elements in an existin( arra$C when the correct co!nt is not 4nown* s!ppl$in( ? for this ar(!ment ens!res that it will s!cceed. 5he semfl( ar(!ment specifies the initial access permissions and creation control fla(s. 5he followin( code ill!strates the sem(et(3 f!nction. Gincl!de =s$s/t$pes.h< Gincl!de =s$s/ipc.h< Gincl!de =s$s/sem.h< ... 4e$@t 4e$C /H 4e$ to pass to sem(et(3 H/ int semfl(C /H semfl( to pass tosem(et(3 H/ int nsemsC /H nsems to pass to sem(et(3 H/ int semidC /H ret!rn val!e from sem(et(3 H/ ... 4e$ Q ... nsems Q ... semfl( Q ... ... if ((semid Q sem(et(4e$* nsems* semfl(33 QQ ;13 S

perror(Lsem(et: sem(et failedL3C exit(13C T else ... +ontrollin( emaphores semctl(3 chan(es permissions and other characteristics of a semaphore set. It is protot$ped as follows: int semctl(int semid* int semn!m* int cmd* !nion sem!n ar(3C It m!st be called with a valid semaphore I8* semid. 5he semn!m val!e selects a semaphore within an arra$ b$ its index. 5he cmd ar(!ment is one of the followin( control fla(s: GE5M/L ;; .et!rn the val!e of a sin(le semaphore. E5M/L ;; et the val!e of a sin(le semaphore. In this case* ar( is ta4en as ar(.val* an int. GE5)I8 ;; .et!rn the )I8 of the process that performed the last operation on the semaphore or arra$. GE50+05 ;; .et!rn the n!mber of processes waitin( for the val!e of a semaphore to increase. GE5U+05 ;; .et!rn the n!mber of processes waitin( for the val!e of a partic!lar semaphore to reach Aero. GE5/LL ;; .et!rn the val!es for all semaphores in a set. In this case* ar( is ta4en as ar(.arra$* a pointer to an arra$ of !nsi(ned shorts (see below3. E5/LL ;; et val!es for all semaphores in a set. In this case* ar( is ta4en as ar(.arra$* a pointer to an arra$ of !nsi(ned shorts. I)+@ 5/5 ;; .et!rn the stat!s information from the control str!ct!re for the semaphore set and place it in the data str!ct!re pointed to b$ ar(.b!f* a pointer to a b!ffer of t$pe semid@ds. I)+@ E5 ;; et the effective !ser and (ro!p identification and permissions. In this case* ar( is ta4en as ar(.b!f. I)+@.#I8 ;; .emove the specified semaphore set. / process m!st have an effective !ser identification of owner* creator* or s!per!ser to perform an I)+@ E5 or I)+@.#I8 command. .ead and write permission is re:!ired as for the other control commands. 5he followin( code ill!strates semctl (3. 5he fo!rth ar(!ment !nion sem!n ar( is optional* dependin( !pon the operation re:!ested. If re:!ired it is of t$pe !nion sem!n* which m!st be explicitl$ declared b$ the application pro(ram as: !nion sem!n S int valC str!ct semid@ds Hb!fC !short Harra$C T ar(C Gincl!de =s$s/t$pes.h< Gincl!de =s$s/ipc.h< Gincl!de =s$s/sem.h< !nion sem!n S int valC str!ct semid@ds Hb!fC !short Harra$C

T ar(C int iC int semn!m Q ....C int cmd Q GE5/LLC /H (et val!e H/ ... i Q semctl(semid* semn!m* cmd* ar(3C if (i QQ ;13 S perror(Lsemctl: semctl failedL3C exit(13C T else ... emaphore Operations semop(3 performs operations on a semaphore set. It is protot$ped b$: int semop(int semid* str!ct semb!f Hsops* siAe@t nsops3C 5he semid ar(!ment is the semaphore I8 ret!rned b$ a previo!s sem(et(3 call. 5he sops ar(!ment is a pointer to an arra$ of str!ct!res* each containin( the followin( information abo!t a semaphore operation: 5he semaphore n!mber 5he operation to be performed +ontrol fla(s* if an$. 5he semb!f str!ct!re specifies a semaphore operation* as defined in =s$s/sem.h<. str!ct semb!f S !short@t sem@n!mC /H semaphore n!mber H/ short sem@opC /H semaphore operation H/ short sem@fl(C /H operation fla(s H/ TC 5he nsops ar(!ment specifies the len(th of the arra$* the maxim!m siAe of which is determined b$ the E#O)# confi(!ration optionC this is the maxim!m n!mber of operations allowed b$ a sin(le semop(3 call* and is set to 1? b$ defa!lt. 5he operation to be performed is determined as follows: / positive inte(er increments the semaphore val!e b$ that amo!nt. / ne(ative inte(er decrements the semaphore val!e b$ that amo!nt. /n attempt to set a semaphore to a val!e less than Aero fails or bloc4s* dependin( on whether I)+@0O-/I5 is in effect. / val!e of Aero means to wait for the semaphore val!e to reach Aero. 5here are two control fla(s that can be !sed with semop(3: I)+@0O-/I5 ;; +an be set for an$ operations in the arra$. #a4es the f!nction ret!rn witho!t chan(in( an$ semaphore val!e if an$ operation for which I)+@0O-/I5 is set cannot be performed. 5he f!nction fails if it tries to decrement a semaphore more than its c!rrent val!e* or tests a nonAero semaphore to be e:!al to Aero. E#@'08O ;; /llows individ!al operations in the arra$ to be !ndone when the process exits.

5his f!nction ta4es a pointer* sops* to an arra$ of semaphore operation str!ct!res. Each str!ct!re in the arra$ contains data abo!t an operation to perform on a semaphore. /n$ process with read permission can test whether a semaphore has a Aero val!e. 5o increment or decrement a semaphore re:!ires write permission. -hen an operation fails* none of the semaphores is altered. 5he process bloc4s (!nless the I)+@0O-/I5 fla( is set3* and remains bloc4ed !ntil: the semaphore operations can all finish* so the call s!cceeds* the process receives a si(nal* or the semaphore set is removed. Onl$ one process at a time can !pdate a semaphore. im!ltaneo!s re:!ests b$ different processes are performed in an arbitrar$ order. -hen an arra$ of operations is (iven b$ a semop(3 call* no !pdates are done !ntil all operations on the arra$ can finish s!ccessf!ll$. If a process with excl!sive !se of a semaphore terminates abnormall$ and fails to !ndo the operation or free the semaphore* the semaphore sta$s loc4ed in memor$ in the state the process left it. 5o prevent this* the E#@'08O control fla( ma4es semop(3 allocate an !ndo str!ct!re for each semaphore operation* which contains the operation that ret!rns the semaphore to its previo!s state. If the process dies* the s$stem applies the operations in the !ndo str!ct!res. 5his prevents an aborted process from leavin( a semaphore set in an inconsistent state. If processes share access to a reso!rce controlled b$ a semaphore* operations on the semaphore sho!ld not be made with E#@'08O in effect. If the process that c!rrentl$ has control of the reso!rce terminates abnormall$* the reso!rce is pres!med to be inconsistent. /nother process m!st be able to reco(niAe this to restore the reso!rce to a consistent state. -hen performin( a semaphore operation with E#@'08O in effect* $o! m!st also have it in effect for the call that will perform the reversin( operation. -hen the process r!ns normall$* the reversin( operation !pdates the !ndo str!ct!re with a complementar$ val!e. 5his ens!res that* !nless the process is aborted* the val!es applied to the !ndo str!ct!re are cancel to Aero. -hen the !ndo str!ct!re reaches Aero* it is removed. 0O5E:'sin( E#@'08O inconsistentl$ can lead to excessive reso!rce cons!mption beca!se allocated !ndo str!ct!res mi(ht not be freed !ntil the s$stem is rebooted. 5he followin( code ill!strates the semop(3 f!nction: Gincl!de =s$s/t$pes.h< Gincl!de =s$s/ipc.h< Gincl!de =s$s/sem.h< ... int iC int nsopsC /H n!mber of operations to do H/ int semidC /H semid of semaphore set H/ str!ct semb!f HsopsC /H ptr to operations to perform H/

... if ((semid Q semop(semid* sops* nsops33 QQ ;13 S perror(Lsemop: semop failedL3C exit(13C T else (void3 fprintf(stderr* Lsemop: ret!rned VdPnL* i3C ... )O I1 emaphores: =semaphore.h< )O I1 semaphores are m!ch li(hter wei(ht than are $stem M semaphores. / )O I1 semaphore str!ct!re defines a sin(le semaphore* not an arra$ of !p to twent$ five semaphores. 5he )O I1 semaphore f!nctions are: sem@open(3 ;; +onnects to* and optionall$ creates* a named semaphore sem@init(3 ;; InitialiAes a semaphore str!ct!re (internal to the callin( pro(ram* so not a named semaphore3. sem@close(3 ;; Ends the connection to an open semaphore. sem@!nlin4(3 ;; Ends the connection to an open semaphore and ca!ses the semaphore to be removed when the last process closes it. sem@destro$(3 ;; InitialiAes a semaphore str!ct!re (internal to the callin( pro(ram* so not a named semaphore3. sem@(etval!e(3 ;; +opies the val!e of the semaphore into the specified inte(er. sem@wait(3* sem@tr$wait(3 ;; &loc4s while the semaphore is held b$ other processes or ret!rns an error if the semaphore is held b$ another process. sem@post(3 ;; Increments the co!nt of the semaphore. 5he basic operation of these f!nctions is essence the same as described above* except note there are more specialised f!nctions* here. 5hese are not disc!ssed f!rther here and the reader is referred to the online man pa(es for f!rther details. sem@destro$(3 8estro$ a semaphore $nopsis: Gincl!de =semaphore.h< int sem@destro$( sem@t H sem 3C /r(!ments: sem / pointer to the sem@t obRect for the semaphore that $o! want to destro$. Librar$: libc 'se the ;l c option to :cc to lin4 a(ainst this librar$. 5his librar$ is !s!all$ incl!ded a!tomaticall$. 8escription: 5he sem@destro$(3 f!nction destro$s the !nnamed semaphore referred to b$ the sem ar(!ment. 5he semaphore m!st have been previo!sl$ initialiAed b$ the sem@init(3 f!nction. 5he effect of !sin( a semaphore after it has been destro$ed is !ndefined. If $o! destro$ a semaphore that other processes are c!rrentl$ bloc4ed on* the$7re !nbloc4ed* with an error (EI0M/L3. 8on7t mix named semaphore operations (sem@open(3 and sem@close(33 with

!nnamed semaphore operations (sem@init(3 and sem@destro$(33 on the same semaphore. .et!rns: ? !ccess. ;1 /n error occ!rred (errno is set3. Errors: EI0M/L Invalid semaphore descriptor sem. +lassification: )O I1 1??2.1 E# afet$: +ancellation point 0o Interr!pt handler 0o i(nal handler Ies 5hread Ies 2)a. Discuss briefly periodic task model. 5$pes of 5as4 W 5here are vario!s t$pes of tas4 X )eriodic X /periodic X poradic W 8ifferent exec!tion time patterns for the Robs in the tas4 W #!st be modelled differentl$ X 8ifferin( sched!lin( al(orithms X 8ifferin( impact on s$stem performance X 8ifferin( constraints on sched!lin( #odellin( )eriodic 5as4s W / set of Robs that are exec!ted repeatedl$ at re(!lar time intervals can be modelled as a periodic task W Each periodic tas4 Ti is a se:!ence of Robs Ji,1* Ji,,* Y* Ji,n X 5he phase of a tas4 Ti is the release time ri,1 of the first Rob Ji,1 in the tas4. It is denoted b$ Z i ([phi\3 X 5he period pi of a tas4 Ti is the minim!m len(th of all time intervals between release times of consec!tive Robs X 5he exec!tion time ei of a tas4 Ti is the maxim!m exec!tion time of all Robs in the periodic tas4 X 5he period and exec!tion time of ever$ periodic tas4 in the s$stem are 4nown with reasonable acc!rac$ at all times Modelling Periodic Tasks W 5he hyper-period of a set of periodic tas4s is the least common m!ltiple of their periods: H Q lcm(pi3 for i Q 1* ,* Y* n X 5ime after which the pattern of Rob release/exec!tion times starts to repeat* limitin( anal$sis needed W Example:

all tas4s in a s$stem: U Q ] ui W -e will !s!all$ ass!me the relative deadline for the Robs in a tas4 is e:!al to the period of the tas4 X It can sometimes be shorter than the period* to allow slac4 time #an$ !sef!l* real;world* s$stems fit this modelC and it is eas$ to reason abo!t s!ch periodic tas4s 2)b. . Why you need precedence constrains and data dependency? Explain precedence graph and task with example?

Modelling Periodic Tasks W 5he ratio ui Q ei/pi is the utilization of tas4 Ti X 5he fraction of time a periodic tas4 with period pi and exec!tion time ei 4eeps a processor b!s$ W 5he total utilization of a s$stem is the s!m of the !tiliAations of

You might also like