Ajout de l'exo 1 TP 5

This commit is contained in:
2023-10-05 22:42:05 +02:00
parent 7376474b67
commit eb6adbb2b5
4 changed files with 132 additions and 1 deletions

15
TP5/Exo1/exo1_1.c Normal file
View 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
View 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;
}