[英]How to redirect the IO of child process created by fork() and uses exec() function?
我正在用C语言编写Shell。 用户应该能够执行各种命令,并使用pipe(|)将一个命令的输入重定向到另一个命令。 主shell进程是父进程,并为每个命令派生新进程,在子进程中,该命令由exec *()函数调用。
但是我不知道如何将一个子进程的标准输入/输出重定向到另一个。
也许这可以帮助您。
int f=open(somefile, O_WRONLY | O_CREAT, S_IRWXU);
dup2(f,1); //redirecting output to file
//execute your first command here using fork
close(f);
int f=open(somefile, O_RDONLY | O_CREAT, S_IRWXU);
dup2(f,0); //this will give the ouput of the first command to stdout
//i.e. the input is present for second one
//execute your second command here
close(f);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.