This is for an assignment. The child process never prints it's output, or calls the fibonacci functions. I've got other sample programs where the printf command works. Any help would be appreciated. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#define MAX_SEQUENCE 10
typedef struct
{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
}shared_data;
int value = 0;
int computeFibonacci();
int printFibonacci();
int main(int argc, char *argv[])
{
pid_t pid;
if (argc != 2)
{
fprintf(stderr, "useage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0 ) //convert ascii to int and check for out of range 0-9
{
fprintf(stderr, "%d must be from 0-9\n", atoi(argv[0])); //if out of range print error
return -1;
}
shared_data newData;
newData.sequence_size = atoi(argv[1]);
pid = fork();
if (pid < 0)
{
fprintf(stderr, "fork failed\n");
exit(-1);
}
else if (pid == 0)
{
value = getpid();
printf("child: value = %d\n", value);
computeFibonacci(newData);
printFibonacci(newData);
}
else if (pid > 0)
{
value = getpid();
printf("parent: value = %d\n", value);
wait(NULL);
}
return 0;
}
int computeFibonacci(shared_data *newData)
{
int Fib1 = 1;
int i, j, sum, Fib0 = 0;
for (i = 0; i < newData->sequence_size; i++)
{
if (i < 2 )
{
newData->fib_sequence[j] = i;
j++;
}
sum = Fib0 + Fib1;
Fib0 = Fib1;
Fib1 = sum;
newData->fib_sequence[j] = sum;
j++;
}
return(0);
}
int printFibonacci(shared_data *newData)
{
int i;
for (i = 0; i < newData->sequence_size; i++)
{
printf("%ld\n", newData->fib_sequence[i]);
}
return(0);
}