Sujet B

Transcription

Sujet B
Nom :
Prénom :
Contrôle n°2 - Sujet B
Afin de ne pas perdre de points bêtement, lisez bien le sujet en entier avant de commencer
et argumentez vos réponses, sans pour autant qu’elles ne soient trop longues.
Rappel : Les réponses doivent tenir sur la feuille. Toute tentative de triche sera sanctionnée.
Exercice 1 (3 points).
1. On considère le programme mywrite dont le code source se trouve ci-dessous. Décrire
les affichages qui apparaissent sur la sortie standard, sachant que le processus reçoit
un signal SIGHUP lorsque la fonction write est en train d’écrire le 12-ème caractère du
buffer.
1
# include <u n i s t d . h>
2
3
# define BUFFSIZE 25
4
5
6
7
8
void main ( ) {
char b u f f e r [ BUFFSIZE ] = " Luke , j e s u i s ton pere . " ;
w r i t e ( STDOUT_FILENO , b u f f e r , 1 5 ) ;
}
Réponse :
2. Même question avec le programme myfullwrite dont le code source est le suivant :
1
2
# include <errno . h>
# include <u n i s t d . h>
3
4
# define BUFFSIZE 25
5
6
7
8
9
i n t m y f u l l w r i t e ( i n t fd , char * b u f f e r , i n t s i z e ) {
int nwrite = 0;
int p ;
errno = 0 ;
10
11
12
13
14
while ( n w r i t e < s i z e ) {
p = w r i t e ( fd , b u f f e r + n w r i t e , s i z e − n w r i t e ) ;
i f ( p < 0 && errno ! = EAGAIN && errno ! = EINTR )
return p ;
L2 Systèmes d’exploitation
1/6
Nom :
Prénom :
n w r i t e += p ;
15
}
return 0 ;
16
17
18
}
19
20
21
22
23
void main ( ) {
char b u f f e r [ BUFFSIZE ] = " Luke , j e s u i s ton pere . " ;
m y f u l l w r i t e ( STDOUT_FILENO , b u f f e r , 1 5 ) ;
}
Réponse :
3. Décrire les affichages qui apparaissent sur la sortie standard si l’on remplace la fonction
main de myfullwrite par
1
2
3
4
void main ( ) {
char b u f f e r [ BUFFSIZE ] = " Luke , j e s u i s ton pere . " ;
m y f u l l w r i t e ( STDOUT_FILENO , b u f f e r , 3 0 ) ;
}
Réponse :
L2 Systèmes d’exploitation
2/6
Nom :
Prénom :
Exercice 2 (4 points). Écrivez un programme killson qui se duplique. Et où
— le fils affiche "Je suis le fils" (cet affichage doit apparaître en premier sur le terminal)
— le père affiche "Je suis le père, je vais tuer mon fils" (cet affichage doit apparaître en
premier sur le terminal) et tue son fils.
Réponse :
Exercice 3 (6 points). Ecrivez un programme stop3 qui comptabilise le nombre de "Ctrl-Z"
tapés et qui se termine au 3-ème "Ctrl-Z". On précise que les deux premiers "Ctrl-Z" doivent
être ignorés (et non faire passer le processus à l’état de zombie).
Réponse :
L2 Systèmes d’exploitation
3/6
Nom :
Prénom :
Exercice 4 (7 points). Ecrivez un programme countc qui exécute la commande
ls -l | grep .c | wc -l
L2 Systèmes d’exploitation
4/6
Nom :
A
Prénom :
Prototypes de certaines fonctions
void * a l l o c a ( s i z e _ t s i z e ) ;
int close ( int f i l d e s ) ;
i n t c l o s e d i r ( DIR * d i r p ) ;
i n t c h d i r ( const char * path ) ;
i n t dup ( i n t f i l d e s ) ;
i n t dup2 ( i n t f i l d e s , i n t f i l d e s 2 ) ;
void e x i t ( i n t s t a t u s ) ;
i n t e x e c l p ( const char * f i l e , const char * arg0 , . . . , const char * argN , NULL ) ;
i n t execvp ( const char * f i l e , char * const argv [ ] ) ;
int fchdir ( int fildes ) ;
p i d _ t f o r k ( void ) ;
i n t ftruncate ( i n t f i l d e s , o f f _ t length ) ;
L2 Systèmes d’exploitation
5/6
Nom :
Prénom :
char * getenv ( const char *name ) ;
p i d _ t g e t p i d ( void ) ;
p i d _ t g e t p p i d ( void ) ;
i n t k i l l ( p i d _ t pid , i n t s i g ) ;
o f f _ t l s e e k ( i n t f i l d e s , o f f _ t o f f s e t , i n t whence ) ;
i n t l s t a t ( const char * path , s t r u c t s t a t * buf ) ;
void * malloc ( s i z e _ t s i z e ) ;
void *memcpy ( void * s1 , const void * s2 , s i z e _ t n ) ;
i n t mkdir ( const char * path , mode_t mode ) ;
void *mmap( void * addr , s i z e _ t len , i n t prot , i n t f l a g s , i n t f i l d e s , o f f _ t o f f ) ;
i n t munmap( void * addr , s i z e _ t l e n ) ;
i n t open ( const char * path , i n t o f l a g , . . . ) ;
DIR * o p e n d i r ( const char * dirname ) ;
i n t pause ( void ) ;
int pipe ( int f i l d e s [ 2 ] ) ;
i n t p o l l ( s t r u c t p o l l f d f d s [ ] , n f d s _ t nfds , i n t timeout ) ;
s s i z e _ t read ( i n t f i l d e s , void * buf , s i z e _ t nbyte ) ;
s t r u c t d i r e n t * r e a d d i r ( DIR * d i r p ) ;
i n t rmdir ( const char * path ) ;
i n t setenv ( const char * envname , const char * envval , i n t o v e r w r i t e ) ;
i n t s i g a c t i o n ( i n t s i g , const s t r u c t s i g a c t i o n * r e s t r i c t act ,
s t r u c t s i g a c t i o n * r e s t r i c t oact ) ;
unsigned i n t s l e e p ( unsigned i n t seconds ) ;
i n t s t a t ( const char * path , s t r u c t s t a t * buf ) ;
p i d _ t w a i t p i d ( p i d _ t pid , i n t * s t a t _ l o c , i n t o p t i o n s ) ;
s s i z e _ t w r i t e ( i n t f i l d e s , const void * buf , s i z e _ t nbyte ) ;
struct dirent {
i n o _ t d_ino ;
char d_name [ ] ;
};
struct p o l l f d {
int
fd ;
short events ;
short r e v e n t s ;
};
Drapeaux pour open : O_EXEC, O_RDONLY, O_RDWR, O_WRONLY, O_APPEND, O_CREAT, O_TRUNC.
Drapeaux pour lseek : SEEK_SET, SEEK_CUR, SEEK_END.
Signal correspondant à la touche "Ctrl-C" : SIGINT.
Signal correspondant à la touche "Ctrl-\" : SIGQUIT.
Signal correspondant à la touche "Ctrl-Z" : SIGTSTP.
Signal SIGHUP : envoyé à tous les processus attachés à un terminal lorsque celui-ci est déconnecté du système. L’action par défaut est la mise à mort du processus.
Signal SIGINT : L’action par défaut est la mise à mort du processus.
Signal SIGQUIT : L’action par défaut est la mise à mort du processus.
Signal SIGKILL : utilisé pour arrêter l’exécution d’un processus. L’action par défaut est non
modifiable.
L2 Systèmes d’exploitation
6/6