This commit is contained in:
2023-10-23 13:07:05 +02:00
parent 4f9ed8483e
commit 667dae6f1a
26 changed files with 469 additions and 45 deletions

20
SCR/TP05/ex1.1.c Normal file
View File

@@ -0,0 +1,20 @@
#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;
}

24
SCR/TP05/ex1.2.c Normal file
View File

@@ -0,0 +1,24 @@
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
// ls -i -l /tmp >> log
//
int main(int argc, char *argv[])
{
int redirection = open("./log",O_WRONLY | O_APPEND | O_CREAT , 0644);
assert(redirection >= 0);
// dup2 ferme 1, et duplique redirection
dup2(redirection,1);
close(redirection);
execlp("ls","ls","-i","-l","/tmp",NULL);
assert(0);
return 0;
}

76
SCR/TP05/prime.c Normal file
View File

@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void filter(int p)
{
while(1){
int n;
ssize_t nb_read = read(0,&n,sizeof(int));
if (nb_read <= 0)
break;
if ((n%p)!=0)
write(1,&n,sizeof(int));
}
}
void seq(int n)
{
for (int i = 2; i <=n; i++)
write(1,&i,sizeof(int));
}
void chef(void)
{
while(1){
int p;
pid_t f;
int t[2];
ssize_t nb_read = read(0,&p,sizeof(int));
if (nb_read <= 0)
break;
printf("new prime %d\n",p);
pipe(t);
f = fork();
if (f == 0){
close(t[0]);
dup2(t[1],1);
close(t[1]);
filter(p);
exit(0);
}
close(t[1]);
dup2(t[0],0);
close(t[0]);
}
}
int main(int argc, char *argv[])
{
pid_t p;
struct sigaction sa = {0};
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD,&sa,NULL);
int t[2];
int N = strtol(argv[1],NULL,0);
pipe(t);
p = fork();
if (p == 0){
close(t[0]);
dup2(t[1],1);
close(t[1]);
seq(N);
exit(0);
}
close(t[1]);
dup2(t[0],0);
close(t[0]);
chef();
return 0;
}