I'm supposed to return the sum of first 12 terms of Fibonacci series from child process to parent one but instead having 377
, parent gets 30976
.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
pid_t childpid;
int i, fib_sum=0, fib1=1, fib2=1, temp, status;
childpid=fork();
if(childpid!=0)
{
wait(&status);
fprintf(stderr, "%d\n", status);
}
else
{
for(i=1; i<=12; i++)
{
temp=fib1;
fib_sum=fib1+fib2;
fib1=fib_sum;
fib2=temp;
}
fprintf(stderr, "%d\n", fib_sum);
return fib_sum;
}
}
What am I doing wrong?
fib_sum
in the parent, it isfib_sum = (status >> 8) & 0x7F
– Conchapopen()
, and read the result. – Sacrilegiousint
(or anuintptr_t
) usingsigqueue()
andsigwaitinfo()
in POSIXy systems, if a pipe or socket is not allowed. – Hexachlorophenewait()
, did you? – Hamulusman 2 wait
: "WEXITSTATUS(wstatus) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main()." – Hamulus0..255
. – Discernible