TD /TP – Initiation à la Programmation système : Fichiers
Exercice 0
Expliquez les programmes donnés en exemples du cours.
Exercice 1:
On considère le programme ci-dessous.
1) Qu’affiche-t-il quand [Link] et [Link] existent tous les deux ?
2) Quelle différence si [Link] n’existe pas ? Et si [Link] n’existe pas ?
#include <stdio.h>
#include <fcntl.h> /* O_RDONLY */
#include <errno.h>
int main(){
int fd1, fd2;
fd1 = open("[Link]", O_RDONLY, 0);
close(fd1);
fd2= open("[Link]", O_RDONLY, 0);
printf("fd2 = %d (errno=%d)\n", fd2, errno);
return 0;
}
Exercice 2
On suppose que le fichier [Link] contient les six caractères ”ABCDEF”.
On considère les trois programmes ci-après.
Qu’affichent chacun de ces programmes ? Justifiez.
#include <stdio.h>
#include <fcntl.h>
int main() {
int fd1, fd2; char c;
fd1=open("[Link]", O_RDONLY, 0);
fd2=open("[Link]", O_RDONLY, 0);
read(fd1, &c, 1);
read(fd2, &c, 1);
printf("c = %c\n", c);
return 0;
}
#include <stdio.h>
#include <fcntl.h> /* O_RDONLY */
int main(){
int fd; char c;
fd=open("[Link]", O_RDONLY, 0);
if (fork() == 0) {
read(fd, &c, 1);
return 0;
}
wait(NULL);
read(fd, &c, 1);
printf("c = %c\n", c);
return 0;
}
#include <stdio.h>
#include <fcntl.h> /* O_RDONLY */
int main(){
int fd1, fd2; char c;
fd1=open("[Link]", O_RDONLY, 0);
read(fd1, &c, 1);
fd2 = dup(fd1);
read(fd2, &c, 1);
printf("c = %c\n", c);
return 0;
}