update
This commit is contained in:
BIN
SCR/SCR2/TP05/ex1
Executable file
BIN
SCR/SCR2/TP05/ex1
Executable file
Binary file not shown.
20
SCR/SCR2/TP05/ex1.1.c
Normal file
20
SCR/SCR2/TP05/ex1.1.c
Normal 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/SCR2/TP05/ex1.2.c
Normal file
24
SCR/SCR2/TP05/ex1.2.c
Normal 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;
|
||||
}
|
17
SCR/SCR2/TP05/ex1.c
Normal file
17
SCR/SCR2/TP05/ex1.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
int t[2];
|
||||
char p;
|
||||
int nbchar = 0;
|
||||
pipe(t);
|
||||
while (write(t[1],&p,sizeof(char)) > 0){
|
||||
nbchar ++;
|
||||
printf("%d\n",nbchar);
|
||||
}
|
||||
close(t[0]);
|
||||
close(t[1]);
|
||||
return 0;
|
||||
}
|
28
SCR/SCR2/TP05/ex2.c
Normal file
28
SCR/SCR2/TP05/ex2.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main() {
|
||||
char *command1[] = {"ls","ls", "-l", NULL};
|
||||
char *command2[] = {"wc","wc", "-l", NULL};
|
||||
int p[2];
|
||||
|
||||
pipe(p);
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
close(p[1]);
|
||||
dup2(p[0], STDIN_FILENO);
|
||||
execvp(command2[0], command2+1);
|
||||
exit(-1);
|
||||
}
|
||||
else{
|
||||
close(p[0]);
|
||||
dup2(p[1], STDOUT_FILENO);
|
||||
execvp(command1[0], command1+1);
|
||||
exit(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
31
SCR/SCR2/TP05/ex3.c
Normal file
31
SCR/SCR2/TP05/ex3.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main() {
|
||||
char *command1[] = {"ls","ls", ".", NULL};
|
||||
char *command2[] = {"wc","wc", "-l", NULL};
|
||||
int p[2];
|
||||
|
||||
pipe(p);
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
close(p[0]);
|
||||
dup2(p[1], STDOUT_FILENO);
|
||||
execvp(command1[0], &command1[1]);
|
||||
close(p[1]);
|
||||
exit(0);
|
||||
}
|
||||
else{
|
||||
close(p[1]);
|
||||
dup2(p[0], STDIN_FILENO);
|
||||
//dup2(STDIN_FILENO,p[0]);
|
||||
execvp(command2[0], &command2[1]);
|
||||
close(p[0]);
|
||||
exit(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
SCR/SCR2/TP05/exe
Executable file
BIN
SCR/SCR2/TP05/exe
Executable file
Binary file not shown.
36
SCR/SCR2/TP05/pipe-ex.c
Normal file
36
SCR/SCR2/TP05/pipe-ex.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main() {
|
||||
int ecrit;
|
||||
int lu;
|
||||
int p[2];
|
||||
|
||||
pipe(p);
|
||||
pid_t pf = fork();
|
||||
|
||||
if (pf == 0) {
|
||||
pid_t pf1 = fork();
|
||||
if (pf1 == 0) {
|
||||
while (read(p[0],&lu,sizeof(int)) > 0){
|
||||
printf("%d received %d\n", getpid(), lu);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
pid_t pf2 = fork();
|
||||
if (pf2 == 0) {
|
||||
}
|
||||
else{
|
||||
while(1){
|
||||
sleep(3);
|
||||
ecrit = getpid();
|
||||
printf("%d sent %d\n", ecrit, ecrit);
|
||||
write(p[1],&ecrit,sizeof(int));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
76
SCR/SCR2/TP05/prime.c
Normal file
76
SCR/SCR2/TP05/prime.c
Normal 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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user