64 lines
1022 B
C
64 lines
1022 B
C
#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();
|
|
|
|
|
|
}
|