septembre + octobre
This commit is contained in:
BIN
SCR/TP05/ex1
Executable file
BIN
SCR/TP05/ex1
Executable file
Binary file not shown.
17
SCR/TP05/ex1.c
Normal file
17
SCR/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/TP05/ex2.c
Normal file
28
SCR/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/TP05/ex3.c
Normal file
31
SCR/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/TP05/exe
Executable file
BIN
SCR/TP05/exe
Executable file
Binary file not shown.
36
SCR/TP05/pipe-ex.c
Normal file
36
SCR/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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user