1

I have some piece of code which goes like this:

pipe(fd);
child_map[0] = fd[0];
child_map[1] = fileno(stdout)
child_map[2] = fileno(stderr);

pid = fork();

if (child_process)
    dup(child_map[0], STDIN_FILENO)
    dup(child_map[0], STDOUT_FILENO)
    dup(child_map[0], STDERR_FILENO)
    execvp(argv[0], argv)  /* child process can be either "grep" or "more" etc */
else if parent_process
    return;

My problem is, after I redirect the output to grep/more (which can be the child process), I am not able to get the terminal prompt back. The command o/p is printed fine on the terminal though. But I do not get back the prompt and I can see that the "more" or "grep" process is running in the background. I need to enter ctrl+C to get the prompt back. I know it has got something to do with the file descriptors not being closed etc, but I do not know how to resolve this.

This is actually being done from another process context. And I can see that the parent process is still running. It doesn't terminate unless I terminate it. So there isnt any question of the child being orphaned. [hoisted from Vin's comment, clarification value is questionable -msw]

msw
  • 42,753
  • 9
  • 87
  • 112
Vin
  • 717
  • 1
  • 12
  • 26

2 Answers2

0

Parent process should wait for the child process to finish.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • The parent process is the one which is feeding the input to more/grep. So if parent keeps waiting for the child process to end, there is no use. – Vin Jul 19 '11 at 17:42
  • The parent should feed the input until there is no more, then close the input file descriptor and use wait(3p) until the child has exited. – Antti Jul 19 '11 at 18:26
0

The issue was with the improper closing of the already opened file descriptors. Some had to be closed, which I hadnt done. So the process used to keep waiting for input.

Vin
  • 717
  • 1
  • 12
  • 26
  • this also is a good resource : http://stackoverflow.com/questions/916900/having-trouble-with-fork-pipe-dup2-and-exec-in-c – Vin Aug 03 '11 at 16:09