开发者

Not getting back terminal prompt after fork-exec-dup

开发者 https://www.devze.com 2023-03-21 08:43 出处:网络
I have some piece of code which goes like this: pipe(fd); child_map[0] = fd[0]; child_map[1] = fileno(stdout)

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 开发者_JAVA百科(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]


Parent process should wait for the child process to finish.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号