如果管道流空闲了一段时间,如何关闭程序?
比如说:
一些程序 | closeidlepipe -t 500 |其他程序
是否有一些程序 closeidlepipe
可以在空闲一段时间后关闭 (-t 500
)?
timeout
可以在一段时间后关闭,但不与“idle”区别。
更新
重要的是要注意 someprogram
输出无穷无尽的二进制数据流。数据可能包含空字符 \0
并且应该逐字传送。
最佳答案
这是执行此操作的程序核心的一般形式。
while(1) {
struct timeval tv;
tv.m_sec = 0;
tv.m_usec = 500000;
int marker = 1;
select(1, &marker, NULL, NULL, &tv);
if (marker == 0) exit(1);
char buf[8192];
int n = read(0, buf, 8192);
if (n < 0) exit(2);
char *b = buf;
while (n)
{
int l = write(1, b, n);
if (l <= 0) exit(3);
b += l;
n -= l;
}
}
关于linux - Bash:如果管道 IO 空闲则关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40617948/