tp2 Correction PDF
tp2 Correction PDF
tp2 Correction PDF
void main ( )
{
pid_t p ;
p=f o r k ( ) ;
switch ( p )
{
case ( 0 ) :
// s l e e p ( 1 5 ) ;
p r i n t f ( " Le f i l s p i d e s t =%d e t mon p p i d e s t=%d\n" ,
getpid () ,
getppid ( ) ) ;
break ;
case ( − 1 ) :
// s l e e p ( 1 5 ) ;
p r i n t f ( " E r r e u r f o r k \n" ) ;
break ;
default :
p r i n t f ( " Le p e r e p i d e s t =%d e t mon p p i d e s t=%d\n" ,
getpid () ,
getppid ( ) ) ;
}
p r i n t f ( " Fin du p r o c e s s u s %d\n" , g e t p i d ( ) ) ;
}
x);
break ;
case ( − 1 ) :
p r i n t f ( " E r r e u r f o r k \n" ) ;
break ;
default :
x = x ∗ 5;
p r i n t f ( " Le f i l s p i d=%d p p i d=%d u i d=%d g i d=%d x=%d\n" , g e t p i d ( ) ,
getppid () ,
getuid () ,
getgid () ,
x);
}
p r i n t f ( " Fin programme \n" ) ;
}
#include<s t d i o . h>
#include<f c n t l . h>
#include<u n i s t d . h>
#include<s y s / s t a t . h>
#include<s y s / t y p e s . h>
void main ( )
{
pid_t p ;
char c h a i n e [ 3 ] ;
int desc ;
d e s c=open ( " t o t o " ,O_RDWR, 0 ) ;
p=f o r k ( ) ;
switch ( p )
{
case ( 0 ) :
p r i n t f ( " Le f i l s PID=%d PPID=%d\n" ,
getpid () ,
getppid ( ) ) ;
read ( desc , chaine , 2 ) ;
c h a i n e [ 2 ] = ’ \0 ’ ;
p r i n t f ( " c h a i n e l u e pa r l e f i l s %s \n" , c h a i n e ) ;
sleep (10);
c l o s e ( desc ) ;
break ;
case ( − 1 ) :
p r i n t f ( " E r r e u r f o r k \n" ) ;
break ;
default :
p r i n t f ( " Le p è r e V a l e u r de r e t o u r du f o r k=%d\n" , p ) ;
p r i n t f ( " Le p è r e PID=%d PPID=%d\n" ,
getpid () ,
getppid ( ) ) ;
sleep (3);
read ( desc , chaine , 2 ) ;
c h a i n e [ 2 ] = ’ \0 ’ ;
p r i n t f ( " c h a i n e l u e pa r l e p è r e %s \n" , c h a i n e ) ;
sleep (20);
w r i t e ( d e s c , "AB" , 2 ) ;
c l o s e ( desc ) ;
}
p r i n t f ( " Fin du Programme\n" ) ;
}
#include<u n i s t d . h>
#include<s t d i o . h>
#include<e r r n o . h>
#include<s t r i n g . h>
pid = f o r k ( ) ;
switch ( p i d )
{
case −1:
perror (" erreur fork :\ t" ) ;
break ;
case 0 :
sleep (1);
p r i n t f ( " f i l s PID : %d \ t PPID : %d\n" ,
getpid () ,
getppid ( ) ) ;
break ;
default :
i f ( ( a r g c ==2) && ( strcmp ( a r g v [ 1 ] , " s y n c h r o " )==0))
{
w a i t (& s t a t u t ) ;
p r i n t f ( " p e r e PID : %d \ t PPID : %d\n" ,
getpid () ,
getppid ( ) ) ;
}
else
{
p r i n t f ( " p e r e PID : %d \ t PPID : %d\n" ,
getpid () ,
getppid ( ) ) ;
w a i t (& s t a t u t ) ; } /∗ E v i t e r un p r o c e s s u s zombie ∗/
}
p r i n t f ( " \n\ t F i n du programme . PID : %d\n" , g e t p i d ( ) ) ;
}
}
i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
pid_t pid , p i d F i l s ;
int statut , i , iReturn ;
i n t iNbProc ;
i f ( argc !=2)
{
p r i n t f ( " L a n c e r l e programme a v e c un argument : \ n" ) ;
p r i n t f ( " \ t l e nombre de p r o c e s s u s f i l s a c r e e r \n" ) ;
}
else
{
s ys tem ( " c l e a r " ) ;
p r i n t f ( " \n\ t \ t \tATTENTE AUTOMATIQUE DE LA TERMINAISON DES FILS \n\n" ) ;
iNbProc = a t o i ( a r g v [ 1 ] ) ;
}
return ( 0 ) ;
}
{
p r i n t f ( "$" ) ;
fflush ( stdin );
s c a n f ( "%s " , commande ) ;
pid = f o r k ( ) ;
switch ( p i d )
{
case −1:
p e r r o r ( " e r r e u r de f o r k \n" ) ;
e x i t ( −1);
case 0 :
p r i n t f ( "%s PPID = %d , PID = %d \n" ,
commande ,
getppid () ,
getpid ( ) ) ;
i f ( strcmp ( commande , " e x i t " ) ! = 0 )
e x e c l p ( commande , commande , NULL ) ;
perror (" execlp " ) ;
// i l ne f a u t pas que l e f i l s c o n t i n u e
e x i t ( −1);
default :
p i d = w a i t (& s t a t u t ) ;
i f ( p i d==−1)
per ro r ( " l e wait " ) ;
else
p r i n t f ( " p r o c e s s u s zombi %d\n" , p i d ) ;
}
}
p r i n t f ( " \n \ t t o u t e s t t e r m i n e \n" ) ;