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);
}

Recommended Answers

All 5 Replies

Try passing the address of newData...

computeFibonacci(&newData);
printFibonacci(&newData);

Also this:

int computeFibonacci();
int printFibonacci();

should be:

int computeFibonacci(shared_data*);
int printFibonacci(shared_data*);

I tested this on my system (Ubuntu Linux 10.4, compiled with GCC 4.4.3) and it does print the child process' PID, but the Fibonacci print does fail even with the correct prototype and argument types. The problem appears to be in either computeFibonacci() or printFibonacci().

BTW, with the Unix C compiler (or the GCC compiler, either one), it should be possible to set the name of the compiled executable to something other than just 'a.out' using the '-o' option. For example:

cc forkfib.c -o forkfib

Alternately, you can just use 'mv' to rename the file.

Thanks for the help.

I'm was using codeblocks on xubuntu 4 to help with debugging, but it looks like that won't work. With this and other working programs it doesn't step through the child process. The program compiled and ran in the debugger, or ran from command line but would only print the parent pid. I compiled on command line with gcc 4.4.1 and it prints the child pid after the parent.

Changed to &newData and it prints the child pid first like it should.

Not sure about the reference to a.out, the error fprintf a.out is a requirement, but my executable is named otherwise either in code blocks or command line gcc.

Thanks for all your help. Now I'll work on the fibonacci functions.

>> Now I'll work on the fibonacci function.

You need to initialize j before using it - since it's an automatic variable, it is NOT initialized to zero by the compiler.

I copied the fibonnaci functions into another part of the code and got them working (we have to print outside the child also). I also understand now about a.out. Our program is supposed to run with that command I find, when I look over the directions one last time. I'm glad you mentioned it. I don't think I've ever compiled a program in Linux without specifying the filename and didn't realize there was a default a.out. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.