Change iostreams in child process

一世执手 提交于 2019-12-01 22:15:48

You need to redirect the file descriptors 0 (standard input) and 1 (standard output) after fork() your child:

switch (fork()) {
case 0: {
    close(0);
    if (open(name, O_RDONLY) < 0) {
        deal_with_error();
    }
    ...

You might want to open the files directed to in the parent process. Having the files readily open probably makes error handling easier. In this case you'd use dup2() to associate the correct file descriptor with the file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!