在c中实现stream水线什么是最好的方法来做到这一点?(自己的linuxshell)

在c中实现stream水线什么是最好的方法来做到这一点?(自己的linuxshell),第1张

概述在c中实现stream水线 什么是最好的方法来做到这一点? (自己的linuxshell)

我想不出有什么办法可以在c中实现stream水线 *** 作。 这就是为什么我决定写在这里。 我不得不说,我明白了pipe / fork / mkfifo是如何工作的。 我已经看到了很多实现2-3个pipe道的例子。 这很容易。 我的问题开始,当我要实现壳,pipe道数不明。

我现在得到了什么:例如。

ls -al | tr az AZ | tr AZ az | tr az AZ

我把这样的线路变成这样的东西:

array[0] = {"ls","-al",NulL"} array[1] = {"tr","az","AZ",NulL"} array[2] = {"tr",NulL"} array[3] = {"tr",NulL"}

所以我可以使用

如何检查一个文件是否被另一个应用程序在C ++中打开?

以编程方式将Microsoft Print中的文件名和path设置为pdf打印机

使用System.Net.WebSockets处理多个子协议

linux串行读取块minicom

WinAPI C ++:重新编程窗口resize

execvp(array[0],array)

稍后的。

现在,我相信一切都好。 问题开始,当我试图将这些函数的input/输出redirect到彼此。

以下是我如何做到这一点:

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",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",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",1); // changing stdout -> pdesc[1] int desc_read = open("queue",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); } }

谢谢。

如何在没有pcap_breakloop的情况下打破pcap_loop的循环

在linux上编译具有依赖关系的C#项目

在/ sys文件系统中创build条目

将.NETconfiguration为在windows 8上使用3.5,在windows 8上使用4.5

项目错误:QT中的未知模块:快速

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(...);

在每个分支shell(close,dup2,pipe等)中运行的序列看起来像一个函数(取所需进程的名称和参数)。 请注意,直到每个exec调用,一个分支的shell的副本正在运行。

编辑:

Patryk:

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。 你是否暗示在tr开始之前ls才会完成? 这并不是真正的上图。 启动tr之前,shell不会等待ls完成。 它依次启动管道中的所有进程,为每个进程设置stdin和stdout ,以便进程链接在一起, ls的stdin stdout到tr stdin ; tr的stdin stdout到下一个tr stdin 。 这就是dup2电话正在做的事情。

进程的运行顺序由 *** 作系统(调度程序)决定,但显然如果tr运行并从一个空的stdin读取它必须等待(阻塞),直到前面的过程写入管道。 甚至在tr读取它的stdin之前, ls可能会完成,但是它也可能不会。 例如,如果链中的第一个命令是连续运行并沿途产生输出的东西,那么管道中的第二个命令将不时被调度,以便管理第一个沿着管道发送的任何东西。

希望澄清一些事情:-)

这可能是值得使用libpipeline 。 它负责你所有的努力,你甚至可以在你的管道中包含功能。

问题是你正试图一次做所有事情。 把它分成更小的步骤。

1)解析你的输入来得到ls -al | 出来了。 1a)从这里你知道你需要创建一个管道,将其移动到标准输出,然后启动ls -al 。 然后将管道移到标准输入。 当然还有更多,但是你不用担心代码。

2)解析下一个段获得tr az AZ | 。 回到第1a步,只要你的下一个产卵命令的输出是在某处传送。

总结

以上是内存溢出为你收集整理的在c中实现stream水线 什么是最好的方法来做到这一点? (自己的linuxshell)全部内容,希望文章能够帮你解决在c中实现stream水线 什么是最好的方法来做到这一点? (自己的linuxshell)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264676.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存