gpt4 book ai didi

c - 在 C 中实现流水线。最好的方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:10 25 4
gpt4 key购买 nike

我想不出有什么方法可以在实际工作中用 c 实现流水线。这就是我决定在这里写的原因。我不得不说,我了解 pipe/fork/mkfifo 是如何工作的。我见过很多实现 2-3 条管道的示例。这很容易。我的问题开始了,当我必须实现 shell 时,管道数是未知的。

我现在得到的:例如。

ls -al | tr a-z A-Z | tr A-Z a-z | tr a-z A-Z

我将这样的行转换成类似的东西:

array[0] = {"ls", "-al", NULL"}
array[1] = {"tr", "a-z", "A-Z", NULL"}
array[2] = {"tr", "A-Z", "a-z", NULL"}
array[3] = {"tr", "a-z", "A-Z", NULL"}

所以我可以使用

execvp(array[0],array)

稍后。

直到现在,我相信一切都很好。当我尝试将这些函数输入/输出重定向到彼此时,问题就出现了。

这是我的做法:

    mkfifo("queue", 0777);

for (i = 0; i<= pipelines_count; i++) // eg. if there's 3 pipelines, there's 4 functions to execvp
{
int b = fork();
if (b == 0) // child
{
int c = fork();

if (c == 0)
// baby (younger than child)
// I use c process, to unblock desc_read and desc_writ for b process only
// nothing executes in here
{
if (i == 0) // 1st pipeline
{
int desc_read = open("queue", O_RDONLY);
// dup2 here, so after closing there's still something that can read from
// from desc_read
dup2(desc_read, 0);
close(desc_read);
}

if (i == pipelines_count) // last pipeline
{
int desc_write = open("queue", O_WRONLY);
dup2(desc_write, 0);
close(desc_write);
}

if (i > 0 && i < pipelines_count) // pipeline somewhere inside
{
int desc_read = open("queue", O_RDONLY);
int desc_write = open("queue", O_WRONLY);
dup2(desc_write, 1);
dup2(desc_read, 0);
close(desc_write);
close(desc_read);
}
exit(0); // closing every connection between process c and pipeline
}
else
// b process here
// in b process, i execvp commands
{
if (i == 0) // 1st pipeline (changing stdout only)
{
int desc_write = open("queue", O_WRONLY);
dup2(desc_write, 1); // changing stdout -> pdesc[1]
close(desc_write);
}

if (i == pipelines_count) // last pipeline (changing stdin only)
{
int desc_read = open("queue", O_RDONLY);
dup2(desc_read, 0); // changing stdin -> pdesc[0]
close(desc_read);
}

if (i > 0 && i < pipelines_count) // pipeline somewhere inside
{
int desc_write = open("queue", O_WRONLY);
dup2(desc_write, 1); // changing stdout -> pdesc[1]
int desc_read = open("queue", O_RDONLY);
dup2(desc_read, 0); // changing stdin -> pdesc[0]
close(desc_write);
close(desc_read);
}

wait(NULL); // it wait's until, process c is death
execvp(array[0],array);
}
}
else // parent (waits for 1 sub command to be finished)
{
wait(NULL);
}
}

谢谢。

最佳答案

Patryk,为什么要使用 fifo,而且流水线的每个阶段都使用相同的 fifo?

在我看来,每个阶段之间都需要一个管道。所以流程会是这样的:

Shell             ls               tr                tr
----- ---- ---- ----
pipe(fds);
fork();
close(fds[0]); close(fds[1]);
dup2(fds[0],0);
pipe(fds);
fork();
close(fds[0]); close(fds[1]);
dup2(fds[1],1); dup2(fds[0],0);
exex(...); pipe(fds);
fork();
close(fds[0]); etc
dup2(fds[1],1);
exex(...);

在每个 fork 的 shell 中运行的序列(关闭、dup2、管道等)看起来像一个函数(采用所需进程的名称和参数)。请注意,在每次调用 exec 之前,shell 的一个分支副本正在运行。

编辑:

帕特里克:

Also, is my thinking correct? Shall it work like that? (pseudocode): 
start_fork(ls) -> end_fork(ls) -> start_fork(tr) -> end_fork(tr) ->
start_fork(tr) -> end_fork(tr)

我不确定您所说的 start_fork 和 end_fork 是什么意思。您是在暗示 lstr 开始之前运行完成吗?这实际上不是上图的意思。在启动 tr 之前,您的 shell 不会等待 ls 完成。它按顺序启动管道中的所有进程,为每个进程设置 stdinstdout,以便进程链接在一起,stdout lstrstdintrstdout 到下一个trstdin。这就是 dup2 调用正在做的事情。

进程运行的顺序由操作系统(调度程序)决定,但显然如果 tr 运行并从空的 stdin 读取它必须等待(阻塞)直到前面的进程向管道写入内容。 ls 很可能在 tr 甚至从它的 stdin 读取之前运行完成,但同样有可能它不会。例如,如果链中的第一个命令持续运行并沿途产生输出,则管道中的第二个命令将不时安排以处理第一个沿管道发送的任何内容。

希望能澄清一点:-)

关于c - 在 C 中实现流水线。最好的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com