Contrôle n 2 - Sujet A 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 8-ème caractère du buffer. 1 # include <unistd. h> 2 3 # define BUFFSIZE 25 4 5 void main ( ) { 6 char b u f f e r [ BUFFSIZE ] = " Luke, j e suis ton pere. " ; 7 w r i t e ( STDOUT_FILENO, buffer, 1 5 ) ; 8 } 2. Même question avec le programme myfullwrite dont le code source est le suivant : 1 # include <errno. h> 2 # include <unistd. h> 3 4 # define BUFFSIZE 25 5 6 i n t m y f u l l w r i t e ( i n t fd, char buffer, i n t s i z e ) { 7 i n t nwrite = 0 ; 8 i n t p ; 9 errno = 0 ; 10 11 while ( nwrite < s i z e ) { 12 p = w r i t e ( fd, b u f f e r + nwrite, s i z e nwrite ) ; 13 i f ( p < 0 && errno! = EAGAIN && errno! = EINTR ) 14 return p ; 15 nwrite += p ; L2 Systèmes d exploitation 1/6
16 } 17 return 0; 18 } 19 20 void main ( ) { 21 char b u f f e r [ BUFFSIZE ] = " Luke, j e suis ton pere. " ; 22 m y f u l l w r i t e ( STDOUT_FILENO, buffer, 1 5 ) ; 23 } 3. Décrire les affichages qui apparaissent sur la sortie standard si l on remplace la fonction main de myfullwrite par 1 void main ( ) { 2 char b u f f e r [ BUFFSIZE ] = " Luke, j e suis ton pere. " ; 3 m y f u l l w r i t e ( STDOUT_FILENO, buffer, 3 2 ) ; 4 } L2 Systèmes d exploitation 2/6
Exercice 2 (4 points). Écrivez un programme killfather qui se duplique. Et où le père affiche "Je suis le père" (cet affichage doit apparaître en premier sur le terminal) le fils affiche "Je suis le fils, je vais tuer mon père" (cet affichage doit apparaitre en second sur le terminal) et tue son père. Exercice 3 (6 points). Écrivez un programme stop5 qui comptabilise le nombre de "Ctrl-C" tapés et qui se termine au 5-ème "Ctrl-C". On précise que les deux premiers "Ctrl-C" doivent être ignorés. L2 Systèmes d exploitation 3/6
Exercice 4 (7 points). Écrivez un programme sortadresses qui exécute la commande cat mailinglist.txt tr, \n sort L2 Systèmes d exploitation 4/6
A Prototypes de certaines fonctions void alloca ( s i z e _ t s i z e ) ; i n t close ( i n t 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 chdir ( 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 status ) ; i n t execlp ( 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 [ ] ) ; i n t f c h d i r ( i n t f i l d e s ) ; p i d _ t fork ( 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
char getenv ( const char name ) ; p i d _ t getpid ( void ) ; p i d _ t getppid ( void ) ; i n t k i l l ( p i d _ t pid, i n t s i g ) ; o f f _ t lseek ( i n t f i l d e s, o f f _ t offset, i n t whence ) ; i n t l s t a t ( const char path, struct 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 flags, 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 len ) ; i n t open ( const char path, i n t oflag,... ) ; DIR opendir ( const char dirname ) ; i n t pause ( void ) ; i n t pipe ( i n t f i l d e s [ 2 ] ) ; i n t p o l l ( struct p o l l f d fds [ ], nfds_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 ) ; struct d i r e n t readdir ( 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 overwrite ) ; i n t s i g a c t i o n ( i n t sig, const struct s i g a c t i o n r e s t r i c t act, struct s i g a c t i o n r e s t r i c t oact ) ; unsigned i n t sleep ( unsigned i n t seconds ) ; i n t s t a t ( const char path, struct s t a t buf ) ; p i d _ t w aitpid ( p i d _ t pid, i n t stat_loc, i n t options ) ; 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 d i r e n t { i n o _ t d_ino ; char d_name [ ] ; } ; struct p o l l f d { i n t fd ; short events ; short revents ; } ; 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