Ibrahim.I
Ibrahim.I

Reputation: 329

Pass long values from forked process to its parent

I want to pass a value of type float, double, long from the child process to the parent. I fork the parent first then wait for the child to end its job and here I want the child to return a big value (of type long int). I found a way to pass normal int values using wait(int*) and exit(int) but I want to pass long values not int.

Upvotes: 0

Views: 205

Answers (1)

Duck
Duck

Reputation: 27562

No, don't do that - even if it is somehow working. You need to use shared memory or some IPC like a pipe. Wait uses the returned value to indicate why the child terminated (e.g. a signal) and you shouldn't be messing with that. The value you are seeing in parent is altered from what you use as an exit code.You don't say what OS you are using but from the linux man pages:

The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).


The basic flow is simple.

declare pipe.

fork()

if (parent)
    close write end of pipe
    read loop until eof
    close read end of pipe
    wait on child

if (child)
    close read end of pipe
    write what you are going to write to pipe
    close write end of pipe
    exit

Upvotes: 1

Related Questions