I'm writting in c a program that the father ask the user to insert the number to calculate the factorial of a number. That number is calculated in factorial.c
So i guess what i need to do is:
-Father ask a number to the user:
-Factorial.c read the number and calculate the number
-Factorial.c write the number calculated
-Do a dup2 to redirect the output
// 2 Pipe and Fork created
if(x == 0){ //Father
close(p1[0]);
write(p1[1],&number_by_keybord, BUFFER);
close(p1[1]);
close(p[1]);
dup2(p[0],0); //Read the number returned by factorial.c
read(p[0], &num_factorial,BUFFER);
printf("Pipe: factorial %d\n", num_factorial);
close(p[0]);
wait(NULL);
}else{
execlp("./factorial", "factorial", NULL);
perror("Exec error\n");
}
And now the factorial.c
void main(){
read(0,&num,sizeof(int));
//Number calculated
write(1,&result,sizeof(int));
return 0;
}
But when i execute the program freezes or just put a random number calculating
There is any problema with the dup2? i guess to retrive the correct result i have to do the dup2( , 0) and in factorial i need a read and a write
Thank you!