In the parent process, you try to read from an empty pipe, the read()
will block until data is available, so it blocks until child process wakes up and call write()
. Write something to the pipe after you call pipe()
,
pipe(fd);
write(fd[1], &num, sizeof(num));
it would solve your problem.
And what Joachim Pileborg says about handling reaping of the child processes is important.