TD2 4 PDF
TD2 4 PDF
TD2 4 PDF
Synchronisation pre/fils
crire un source C qui se ddouble et pour lequel le pre attend la terminaison du fils pour reprendre son excution. Faire afficher les informations de l'exercice 1 pour chacun des deux processus.
Recouvrement
crire un source C qui se ddouble et pour lequel le fils se recouvre par un nouveau processus. Faire afficher les informations de l'exercice 1 pour chacun des deux processus.
Analyse
Qu'affiche chacun des segments de programme suivants (ce n'est pas forcment la peine de les compiler pour rpondre la question) :
(a) for (i=1; i<=4; i++) { pid = fork(); if (pid != 0) printf("%d\n", pid);
} (b) for (i=1; i<=4; i++) { pid = fork(); if (pid == 0) break; else printf("%d\n", pid); } (c) for (i=0; i<=nb; i++) { p = fork(); if (p < 0) exit(1); execlp("prog", "prog",NULL); } wait(& !a!" ); (d) for (i=1; i<=nb; i++) { p1 = fork(); p# = fork(); if (p1 < 0) exit(1); if (p# < 0) exit(1); execlp("prog1", "prog1",NULL); execlp("progc", "prog",NULL); } wait(& !a!" );
AIDE
NAME !i$% & g%! proc% !i$%
SYNOPSIS #include <s s!ti"es#$% clock_t ti"es&struct t"s 'buf() DES*+IP,ION ti"es !or% !'% c"rr%n! proc% a !i$% in buf(
d%)in%d in /usr/include/sys/times.h* !$ !$ !$ !$ -"!i$%; - !i$%; -c"!i$%; -c !i$%; ./ " %r !i$% /. ./ 0 !%$ !i$% /. ./ " %r !i$% o) c'i+dr%n /. ./ 0 !%$ !i$% o) c'i+dr%n /. !'a! 'a1% %+ap %d
ti"es r%!"rn !'% n"$b%r o) c+oc, !ic, inc% !'% 0 !%$ 'a b%%n "p(
*ON-O+MIN. ,O 23r4, 2345, 67248, 8.769N, :25 4(; SEE A/SO ti"e(1), 0etrusa0e(#), wait(#)
NAME
g%!r+i$i!, and " ag% g%!r" ag%, %!r+i$i! & g%!. %! r% o"rc% +i$i! SYNOPSIS #include <s s!ti"e#$% #include <s s!resource#$% #include <unistd#$% int 0etrli"it &int resource1 struct rli"it 'rlim() int 0etrusa0e &int who1 struct rusa0e 'usage() int setrli"it &int resource1 const struct rli"it 'rlim() DES*+IP,ION 0etrli"it and setrli"it g%! and %! r% o"rc% r% p%c!i1%+0( resource 'o"+d b% on% o)* +/IMI,_*P2 +/IMI,_-SI3E +/IMI,_DA,A +/IMI,_S,A*4 +/IMI,_*O+E +/IMI,_+SS +/IMI,_NP+O* +/IMI,_NO-I/E +/IMI,_MEM/O*4 ./ ./ ./ ./ ./ ./ ./ ./ ./ <6U !i$% in %cond /. =a>i$"$ )i+% i?% /. $a> da!a i?% /. $a> !ac, i?% /. $a> cor% )i+% i?% /. $a> r% id%n! %! i?% /. $a> n"$b%r o) proc% % /. $a> n"$b%r o) op%n )i+% /. $a> +oc,%d&in&$%$or0 addr% i) i +i$i!
pac%/.
d%)in%d a
!r"c! r+i$i! { in! r+i$-c"r; in! r+i$-$a>; }; 0etrusa0e r%!"rn !'% c"rr%n! r% o"rc% " ag% , )or a who o) %i!'%r +2SA.E_SE/- or +2SA.E_*5I/D+EN# !r"c! r" ag% { !r"c! !i$%1a+ r"-"!i$%; ./ " %r !i$% " %d /. !r"c! !i$%1a+ r"- !i$%; ./ 0 !%$ !i$% " %d /. +ong r"-$a>r ; ./ $a>i$"$ r% id%n! %! i?% /. +ong r"-i>r ; ./ in!%gra+ 'ar%d $%$or0 i?% /. +ong r"-idr ; ./ in!%gra+ "n 'ar%d da!a i?% /. +ong r"-i r ; ./ in!%gra+ "n 'ar%d !ac, i?% /. +ong r"-$in)+!; ./ pag% r%c+ai$ /. +ong r"-$aC)+!; ./ pag% )a"+! /. +ong r"-n Bap; ./ Bap /. +ong r"-inb+oc,; ./ b+oc, inp"! op%ra!ion /.
./ b+oc, o"!p"! op%ra!ion /. ./ $% ag% %n! /. ./ $% ag% r%c%i1%d /. ./ igna+ r%c%i1%d /. ./ 1o+"n!ar0 con!%>! Bi!c'% /. ./ in1o+"n!ar0 con!%>! Bi!c'%
/.
+E,2+N 6A/2E 7n "cc% , ?%ro i r%!"rn%d( 7n %rror, &1 and errno i %! appropria!%+0( E++O+S E-A2/, rlim or usage poin! pac%( o"! id% !'% acc%
r%!"rn%d,
ib+% addr%
@ non& "p%r" %r !ri% !o " % setrli"it&( !o incr%a % !'% o)! or 'ard +i$i! abo1% !'% c"rr%n! 'ard +i$i!, or a "p%r" %r !ri% !o incr%a % DL4=4A-N7E4L9 abo1% !'% c"rr%n! ,%rn%+ $a>i$"$(
NAME
g%!%n1 & g%! an %n1iron$%n! 1ariab+% SYNOPSIS #include <stdlib#$% c$ar '0eten8&const c$ar 'name() DES*+IP,ION A'% 0eten8&( )"nc!ion %arc'% !'% %n1iron$%n! +i ! )or a !ring !'a! $a!c'% !'% !ring poin!%d !o b0 name( A'% !ring ar% o) !'% )or$ name = value( +E,2+N 6A/2E A'% 0eten8&( )"nc!ion r%!"rn a poin!%r !o !'% 1a+"% in !'% %n1iron$%n!, or NULL i) !'%r% i no $a!c'( *ON-O+MIN. ,O 2345 ;, 67248, :25 4(;, 427 FGFF SEE A/SO puten8(;), seten8(;), unseten8(;), en8iron(H)
NAME
g%!cBd, g%!-c"rr%n!-dir-na$%, g%!Bd & I%! c"rr%n! Bor,ing dir%c!or0 SYNOPSIS
#include <unistd#$% c$ar '0etcwd&c$ar 'buf1 si9e_t size() c$ar '0et_current_workin0_dir_na"e&8oid() c$ar '0etwd&c$ar 'buf() DES*+IP,ION A'% 0etcwd&( )"nc!ion copi% !'% ab o+"!% pa!'na$% o) !'% c"rr%n! Bor,ing dir%c!or0 !o !'% arra0 poin!%d !o b0 buf, B'ic' i o) +%ng!' size( 4) !'% c"rr%n! ab o+"!% pa!' na$% Bo"+d r%J"ir% a b"))%r +ong%r !'an size %+%$%n! , N2// i r%!"rn%d, and errno i %! !o E+AN.E; an app+ica!ion 'o"+d c'%c, )or !'i %rror, and a++oca!% a +arg%r b"))%r i) n%c% ar0( @ an %>!%n ion !o !'% 67248(1 !andard, ca!% !'% b"))%r d0na$ica++0 " ing "alloc&( on ca++( 4n !'i ca %, !'% a++oca!%d +%ng!' size "n+% size i +% !'an ?%ro, a++oca!%d a big a n%c% ar0( 4! i ind%%d, ad1i ab+%) !o free&( !'% b"))%r i) ob!ain%d !'i Ba0( 0etcwd&( a++o& i) buf i N2// b"))%r 'a !'% B'%n buf i po ib+% (and, !'%0 'a1% b%%n
0et_current_dir_na"e, B'ic' i on+0 pro!o!0p%d i) __2SE_.N2 i d%)in%d, Bi++ "alloc(;) an arra0 big %no"g' !o 'o+d !'% c"rr%n! dir%c!or0 na$%( 4) !'% %n1iron$%n! 1ariab+% P:D i %!, and i! 1a+"% i corr%c!, !'%n !'a! 1a+"% Bi++ b% r%!"rn%d( 0etwd, B'ic' i on+0 pro!o!0p%d i) __2SE_;SD i d%)in%d, Bi++ no! "alloc(;) an0 $%$or0( A'% buf arg"$%n! 'o"+d b% a poin!%r !o an arra0 a! +%a ! PA,5_MA< b0!% +ong( 0etwd do% on+0 r%!"rn !'% )ir ! PA,5_MA< b0!% o) !'% ac!"a+ pa!'na$%( +E,2+N 6A/2E N2// on )ai+"r% ()or %>a$p+%, i) !'% c"rr%n! dir%c!or0 i no! r%adab+%), Bi!' errno %! according+0, and buf on "c& c% ( *ON-O+MIN. ,O 67248(1 SEE A/SO c$dir(#), free(;), "alloc(;)(