ajout code
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void clc(){
|
||||||
|
|
||||||
|
|
||||||
|
//pipes
|
||||||
|
int pipe1[2];// PIPE A
|
||||||
|
int pipe2[2];// PIPE B
|
||||||
|
if(pipe(pipe1) == -1) return;
|
||||||
|
if(pipe(pipe2) == -1) return;
|
||||||
|
|
||||||
|
|
||||||
|
char buffer[2048];
|
||||||
|
char buffer2[2048];
|
||||||
|
//fgets(buffer, 2047, stdin);
|
||||||
|
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
if(pid < 0) return;
|
||||||
|
if (pid == 0){
|
||||||
|
|
||||||
|
while(read(pipe1[0],buffer ,2047 )){
|
||||||
|
int n = atoi(buffer);
|
||||||
|
|
||||||
|
if(n == -1) exit(0);
|
||||||
|
char buf = n *n;
|
||||||
|
|
||||||
|
write(pipe2[1], &buf , 1);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else{
|
||||||
|
|
||||||
|
read(pipe2[0], buffer2, 2048);
|
||||||
|
if(strcmp(buffer2, "-1") == 0) {
|
||||||
|
|
||||||
|
close(pipe1[0]);
|
||||||
|
close(pipe1[1]);
|
||||||
|
close(pipe1[0]);
|
||||||
|
close(pipe1[1]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("The Oracle says the square is: %s\n", buffer2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
clc();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -14,14 +14,20 @@ ___( o)>
|
|||||||
Coic Coic
|
Coic Coic
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void clc(int n){
|
void clc(){
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
int pipe1[2];
|
int pipe1[2];
|
||||||
int pipe2[2];
|
int pipe2[2];
|
||||||
|
char buffer[2048];
|
||||||
|
|
||||||
if(pipe(pipe2) == -1) return;
|
if(pipe(pipe2) == -1) return;
|
||||||
if(pipe(pipe1) == -1) return;
|
if(pipe(pipe1) == -1) return;
|
||||||
|
|
||||||
|
fgets(buffer, 2047, stdin);
|
||||||
|
n = atoi(buffer);
|
||||||
|
pipe1[1] = n;
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
if(pid < 0) return;
|
if(pid < 0) return;
|
||||||
@@ -31,18 +37,15 @@ void clc(int n){
|
|||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
|
|
||||||
if(pipe1[0] != NULL) pipe2[1] = n* n;
|
if(pipe1[0]){
|
||||||
|
pipe2[1] = n* n;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Teacher ? WTF what are you doing with my uncle ? Why do you have an eggplant in your right hand and a riding whip in your left hand ? WTF is going on here
|
//Teacher ? WTF what are you doing with my uncle ? Why do you have an eggplant in your right hand and a riding whip in your left hand ? WTF is going on here
|
||||||
else {
|
else {
|
||||||
char buffer[2048];
|
|
||||||
|
|
||||||
fgets(buffer, 2047, stdin);
|
|
||||||
|
|
||||||
int n = 0;
|
|
||||||
n = atoi(buffer);
|
|
||||||
|
|
||||||
if(n == -1) {
|
if(n == -1) {
|
||||||
close(pipe1[0]);
|
close(pipe1[0]);
|
||||||
@@ -53,13 +56,15 @@ void clc(int n){
|
|||||||
|
|
||||||
wait(NULL); //on attends le Child
|
wait(NULL); //on attends le Child
|
||||||
|
|
||||||
printf("The Oracle says the square is: %i", pipe2[0]);
|
printf("The Oracle says the square is: %i\n", pipe2[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
|
clc();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
//[0] -> lecture | [1]-> ecriture
|
//[0] -> lecture | [1]-> ecriture
|
||||||
int pipe1[2];
|
int pipe1[2];
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user