fork() 之后将标准输出重定向到文件
我正在开发一个简单的 shell,但现在我只是想了解重定向。我现在只是硬编码 ls 命令并尝试将其写入文件。当前,ls 运行并创建了输出文件,但输出仍然转到 stdout 并且文件为空。我很困惑为什么。提前致谢。
这是我的代码:
int main()
{
int ls_pid; /* The new process id for ls*/
char *const ls_params[] = {"/bin/ls", NULL}; /* for ls */
int file; /* file for writing */
/* Open file check user permissions */
if (file = open("outfile", O_WRONLY|O_CREAT) == -1) /* , S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP */
{
perror("Failed to open file");
_exit(EXIT_FAILURE);
}
ls_pid = fork(); /* Create new process for ls */
if (ls_pid == -1) /* error check */
{
perror("Error forking ls (pid == -1)");
_exit(EXIT_FAILURE);
}
else if (ls_pid == 0) /* Child of ls */
{
/* Redirect output to file */
if (dup2(file, STDOUT_FILENO) == -1) /* STDOUT_FILENO = 1 */
{
perror("Error duping to file");
_exit(EXIT_FAILURE);
}
close(file);
execvp("ls", ls_params); /* create the sort process */
/* execlp("ls", "ls", NULL); */
/* if this does not end the program, something is wrong */
perror("Exec failed at sort");
_exit(EXIT_FAILURE);
}
else /* ls parent (1) */
{
/* wait for child */
if (wait(NULL) == -1)
{
perror("fork failed on parent");
_exit(EXIT_FAILURE);
}
}
}
I'm working on a simple shell, but right now I am just trying to understand redirection. I'm just hard coding an ls command and trying to write it to a file for now. Currently, the ls runs, and the output file is created, but the output still goes to stdout and the file is blank. I'm confused as to why. Thanks in advance.
Here is my code:
int main()
{
int ls_pid; /* The new process id for ls*/
char *const ls_params[] = {"/bin/ls", NULL}; /* for ls */
int file; /* file for writing */
/* Open file check user permissions */
if (file = open("outfile", O_WRONLY|O_CREAT) == -1) /* , S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP */
{
perror("Failed to open file");
_exit(EXIT_FAILURE);
}
ls_pid = fork(); /* Create new process for ls */
if (ls_pid == -1) /* error check */
{
perror("Error forking ls (pid == -1)");
_exit(EXIT_FAILURE);
}
else if (ls_pid == 0) /* Child of ls */
{
/* Redirect output to file */
if (dup2(file, STDOUT_FILENO) == -1) /* STDOUT_FILENO = 1 */
{
perror("Error duping to file");
_exit(EXIT_FAILURE);
}
close(file);
execvp("ls", ls_params); /* create the sort process */
/* execlp("ls", "ls", NULL); */
/* if this does not end the program, something is wrong */
perror("Exec failed at sort");
_exit(EXIT_FAILURE);
}
else /* ls parent (1) */
{
/* wait for child */
if (wait(NULL) == -1)
{
perror("fork failed on parent");
_exit(EXIT_FAILURE);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到任何问题。我自己尝试了你的代码。它有效!我的简化代码如下所示:
我编译并执行它,并在输出文件中找到预期结果。
唯一的区别是我使用 S_IRUSER | 来打开。 S_IWUSER 权限选项,否则打开失败。我在你的代码中看到类似的东西,但不知何故你评论了它们......
I don't see any problem. I tried your code by myself. And it works! My simplified code looks like:
I compile an execute it, and find the expected results in outfile.
The only difference is that I feed open with the S_IRUSER | S_IWUSER permission options since otherwise the open fails. I see similar thing in your code but somehow you commented them...