I'm attempting to get my child process to generate a random number between 1 -9, then send it to my parent process for which it would display it to screen each time control + Z is pressed. I'm also using dup2() to change the stream from printf and scanf to the read and write ends of the pipe for each process.

The code works as below, you press control C to start the main program. Now it waits for control Z to be pressed for each time a random number is sent from child and displayed at parent. The issue I am facing is, it seems the child only runs <strong>once</strong> when control Z is pressed again, it only runs the parent code. The pipe also only ever shows the same number and never changes so its not being updated. I also noticed that if I remove the printf line before the dup function <strong>printf("a \n")</strong>, it prints out random numbers....however the problem with the child running once still exists.

is a disconcerting start. The signal() function sets the signal handler for SIGINT, but it no way waits for a signal to arrive. The simplest fix for that is to add a call to <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html" rel="nofollow">pause()</a> after that block of code.

is sub-optimal and/or confusing. Since pipe1 is only set when pipe() is called, there's no need to test it on each iteration. Error messages should be reported on standard error, and should not have trailing blanks. The code should be:

Your code carefully closes the pipe after the first cycle, but never reopens it. That is ultimately why the second and subsequent iterations fail. Your code would be better if you didn't try to do everything on each cycle. Also, using standard output for debugging information is subject to various problems; it is better to use standard error instead — especially when standard output isn't working properly.

<h3>Instrumented code</h3>

Here's an instrumented version of your code. The err_syserr() function is almost generic; the condition using usleep() is peculiar to this code and ensures that the output of the terminated error message is normally sequenced. I put the tests on the function calls that would be failing — the first close failed on the second cycle because the pipe descriptors are all closed at the end of the first cycle. (Note that reusing pipe() is no help after the fork() — the pipes in the parent and child would not be connected to each other.)

Here's a revised version of the code with what seems to me more appropriate logic. It doesn't include err_syserr() because the function calls aren't failing. The selection and secondSel variables aren't needed; the signal handler becomes a stub that just contains return; (which could be omitted). It doesn't loop like mad at the start because I put a pause in place to wait for the interrupt.

I used SIGQUIT to terminate the program since interrupts are disabled.

Answer2:

The issue you are having is due to closing the pipe after the first number is sent from the child to the parent.

However, the main problem is that your code is disorganized, which makes it harder to figure out what is going on and increases the chance of mistakes. Rather than having everything in one big loop, I would suggest restructuring your code as follows:

wait for SIGINT
create pipe
fork
if child:
set up stdout using dup2()
while true:
wait for SIGTSTP
write to pipe
else:
set up stdin using dup2()
while true:
wait for SIGTSTP
read from pipe
etc.

Contact Us

No Copyright Statement

This site is a non-profit exchange learning website. All resources are collected online. Copyright belongs to its copyright owner. This site does not enjoy copyright. If you think it is harmful to your copyright, please contact us and we will delete it at the first time.

Statement of Compliance with the Law

The information collected in this website does not mean that XSZZ. ORG agrees with its statement or description, nor does it constitute any suggestion. It is only for the study and reference of interested parties. If you need to use it, you must abide by the provisions of Chinese law.