xuxin wang
xuxin wang

Reputation: 7

My child process cannot receive the last value from parent process using Pipe

in my program, child process should receive some value from keyboard, and send to parent process. Parent process should calculate the sum and return to child process. My program is shown below:

3 4 5 -1 child(4753): Sending 3 to parent Parent(4751): Received 3 Parent(4751): Sending 3 back Parent(4751): Received 4 Parent(4751): Sending 7 back Parent(4751): Received 5 child(4753): Received 3 Parent(4751): Sending 12 back Parent(4753): Received 4 Parent(4753): Sending 7 back Parent(4753): Received 5 Parent(4753): Sending 12 back

From the result, the last step is Parent send 12 back but child cannot received 12 and show the total sum. Therefore, anyone has some ideas why the result is not my expected and how to modify my code?

Thanks in advance.

Upvotes: 1

Views: 61

Answers (1)

tofro
tofro

Reputation: 6073

Your parent process just runs to an end without waiting for the child to finish. Thus the child is killed before it can actually print a result.

You need to add a wait() or waitpid() in the proper place of the parent process.

Upvotes: 1

Related Questions