Ajout de l'exo 1 TP 5
This commit is contained in:
parent
7376474b67
commit
eb6adbb2b5
34
README.md
34
README.md
@ -425,3 +425,37 @@ tom@Error404:/mnt/c/Users/naser/Documents/scr/r305_dm/TP3/Exo6$ ps -o pid,ppid,p
|
||||
- avec `SIGQUIT` la réinitialisation du calcul avec ctrl+\ depuis le terminal. (faites en sorte que toutes les valeurs restent cohérentes)
|
||||
|
||||
> Dans chaque handler, les 2 autres signaux seront bloqués.
|
||||
|
||||
## Exercice 2
|
||||
|
||||
> Écrire un programme `mytimeout.c` dont l'usage est
|
||||
```c
|
||||
$mytimeout nbsec com [arg ...]
|
||||
```
|
||||
|
||||
- Il lance la commande `com [arg ...]` (fork-and-exec).
|
||||
- Il attend `nbsec` secondes, ou la fin de son fils.
|
||||
- Si la commande n'est pas encore terminée au bout de du temps, il lui envoie le signal `SIGTERM`.
|
||||
|
||||
> En cas de terminaison de la commande, il renvoie son code de retour, sinon renvoie le code 124.
|
||||
|
||||
## Exercice 3
|
||||
|
||||
## Exercice 4
|
||||
|
||||
# TP 5 : Redirections, tubes
|
||||
|
||||
## Exercice 1
|
||||
|
||||
1. Écrire un programme C pour calculer la taille du tampon système associé à un tube. (on pourra rendre l'écriture non bloquante avec `fcntl`)
|
||||
|
||||
2. Écrire un programme C qui a le même comportement que la commande shell
|
||||
|
||||
```shell
|
||||
ls -i -l /tmp >> log
|
||||
```
|
||||
3. Écrire un programme C qui a le même comportement que la commande shell
|
||||
|
||||
```shell
|
||||
ls . | wc -l
|
||||
```
|
15
TP5/Exo1/exo1_1.c
Normal file
15
TP5/Exo1/exo1_1.c
Normal file
@ -0,0 +1,15 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int t[2];
|
||||
int size;
|
||||
assert(pipe(t) == 0);
|
||||
size = fcntl(t[0], F_GETPIPE_SZ);
|
||||
printf("size = %d\n", size);
|
||||
return 0;
|
||||
}
|
46
TP5/Exo1/exo1_2.c
Normal file
46
TP5/Exo1/exo1_2.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Usage: %s <file>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int pip, fd[2], outfile, d;
|
||||
char buf;
|
||||
pip = pipe(fd);
|
||||
assert(pip != -1);
|
||||
|
||||
pid_t p = fork();
|
||||
assert(p != -1);
|
||||
|
||||
outfile = open(argv[1], O_WRONLY | O_CREAT, 0644);
|
||||
|
||||
switch (p)
|
||||
{
|
||||
case 0:
|
||||
close(fd[0]);
|
||||
d = dup2(fd[1], 1);
|
||||
execl("/usr/bin/ls", "ls", "-i", "-l", "/tmp", NULL);
|
||||
close(fd[1]);
|
||||
exit(0);
|
||||
default:
|
||||
close(fd[1]);
|
||||
while (read(fd[0], &buf, sizeof(char)) > 0)
|
||||
{
|
||||
write(outfile, &buf, sizeof(char));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
close(outfile);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
36
exo1_3.c
Normal file
36
exo1_3.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fd[2], p;
|
||||
char c;
|
||||
|
||||
p = pipe(fd);
|
||||
assert(p != -1);
|
||||
|
||||
pid_t pr = fork();
|
||||
assert(pr != -1);
|
||||
|
||||
switch (pr)
|
||||
{
|
||||
case 0:
|
||||
close(fd[0]);
|
||||
dup2(fd[1], 1);
|
||||
execl("/usr/bin/ls", "ls", ".", NULL);
|
||||
exit(0);
|
||||
|
||||
default:
|
||||
close(fd[1]);
|
||||
dup2(fd[0], 0);
|
||||
execl("/usr/bin/wc", "wc", "-l", NULL);
|
||||
}
|
||||
|
||||
close(fd[0]);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user