我想在Linux上編寫一個可以執行單進程命令,雙進程管道和I/O重定向的shell模擬器。 然而,正如我正確執行單個過程一樣,流水線也存在一些問題。在Linux上使用管道和I/O重定向的Shell模擬器
Copy_STDIO(); //Copy the stdio for the restoration.
Redirection(); //Determine whether should do Redirection.
Pipeline();
Restore_stdio(); //Restore the stdio.
以上是我的主要功能。首先,我在I/O重定向之後複製STDIO以進行恢復。然後我將我的文件描述符複製到STD_IN和STD_OUT。然後我執行Pipeline,最後我恢復我的STD_IN和STD_OUT。一切都聽起來很完美,但實際上並非如此。我的OUTPUT重定向失敗,這意味着它沒有向目標文件寫入任何內容(即,如果在單個情況下:ls> 123,123不顯示任何內容),當我的程序仍在運行時。但是當我用exit(EXIT_SUCCESS)終止程序時,它出現了!!(如果用ctrl + c,它也會失敗),我不知道爲什麼!
void Redirection()
{
fd_out = open(filename[0],O_WRONLY | O_TRUNC | O_CREAT,S_IRWXU | S_IRWXG | S_IRWXO); //O_WRONLY and O_CREAT must use at the same time.
dup2(fd_out,STD_OUTPUT);
close(fd_out);
}
void Copy_STDIO()
{
STDIN_COPY = dup(STD_INPUT); //Copy for the stdin and stdout
STDOUT_COPY = dup(STD_OUTPUT);
}
void Pipeline()
{
if(pipeline)
{
pipe(&fd[0]);
childID=fork();
if(childID==0)
{
if(fork()!=0)
{
close(fd[1]);
dup2(fd[0],STD_INPUT);
close(fd[0]);
execvp(args2[0],args2);
}
else
{
close(fd[0]);
dup2(fd[1],STD_OUTPUT);
close(fd[1]);
execvp(args[0],args);
}
}
usleep(5000);
}
}
void Restore_stdio()
{
dup2(STDIN_COPY,STD_INPUT); //Restore the output and input to the stdin and stdout.
dup2(STDOUT_COPY,STD_OUTPUT);
close(STDIN_COPY);
close(STDOUT_COPY);
}
你能提供一個最小的可覈查的例子,又名一段代碼可以編譯? –
文件需要被關閉才能看到和閱讀。退出(0);關閉所有打開的文件。 –
@ArifBurhan我應該在哪裏添加exit(0); ?我將它重定向到STD_OUTPUT後關閉了我的fd_out。 –