Tag: 管道

如何处理execvp中的错误?

我写了一个小程序(带有来自SO的代码),方便了printenv | sort | less printenv | sort | less printenv | sort | less 。 现在我想实现error handling,我从execvp开始。 它只是检查返回值还有什么? AFAIK我只检查返回值,如果它在此函数中为0则return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv); 。 那是对的吗? #include #include #include #include #include #include struct command { const char **argv; }; /* Helper function that spawns processes */ int spawn_proc (int in, […]

在C中使用管道用于父子IPC会使程序阻塞

我正在编写一个服务器,当fork接受套接字连接时,fork()关闭子进程。 当孩子与客户沟通时,它必须将一些信息发送回父母。 我正在使用管道来实现这一目标。 问题是当我尝试做父子IPC时,父母在读取孩子的输入时会阻塞。 这意味着,即使孩子们同时运行,他们也只能一次一个地处理,因为他们都在等待父母。 我的代码看起来像这样(为简洁起见,删除了错误检查): /* loop: wait for clients to connect */ for(;;) { pipe(write_to_parent); /* create pipe between parent and child */ pipe(write_to_child); /* create pipe between parent and child */ newsockfd = accept(…); pid = fork(); if (pid == 0) { /* child process */ close(write_to_parent[0]); close(write_to_child[1]); printf(“Connected!\n”); while ((rc = […]

通过管道重定向时捕获SetConsoleTextAttribute的效果?

我已经将使用CreateProcess生成的子进程的stdout重定向到管道。 它的工作正常,但据我所知,没有关于颜色变化的信息正在传递。 子进程正在使用SetConsoleTextAttribute来更改文本颜色 – 是否可以通过管道检测到这一点,如果是,如何检测? 我最终在RichEdit控件中显示输出,我想尽可能捕获颜色信息。 这是在C和XP和Vista上的Win32 API。

管道function无法正常执行

我已经构建了以下程序来尝试管道我自己的shell。 StringArray只是我构建的char** 。 代码运行正常,但是当我输入cat txt.txt | grep a cat txt.txt | grep a ,没有任何东西打印回屏幕。 在调试时,我看到代码似乎停止了152(打印输出命令所在的位置),其中pid==0和i==0 。 对于上下文,我在检测到管道后在另一个函数中调用此函数。 void doPipe(StringArray sa) { printf(“In 69\n”); int filedes[2]; // pos. 0 output, pos. 1 input of the pipe int filedes2[2]; int num_cmds = 0; char *command[256]; pid_t pid; int err = -1; int end = 0; // Variables […]

C语言库中的数据流

如何在C中执行数据流(管道和filter,流处理,基于流)? 而不是使用UNIX管道。 我最近遇到了stream.py 。 流是具有流水线机制的迭代,以实现数据流编程和简单的并行化。 我们的想法是获取一个函数的输出,该函数将一个iterable转换为另一个iterable并将其作为另一个这样的函数的输入插入。 虽然您已经可以使用函数组合执行此操作,但此包通过重载>>运算符为其提供了优雅的表示法。 我想在C中复制这种function的简单版本。我特别喜欢>> operator的重载以避免函数组合乱。 维基百科在1990年的一篇Usenetpost中指出了这一暗示 。 为何选择C? 因为我希望能够在微控制器和其他高级语言(Max,Pd *,Python)的C扩展中实现这一点。 *(具有讽刺意味的是Max和Pd是用C编写的,特别是为了这个目的 – 我正在寻找一些准系统)

如何阅读C中的管道内容?

我希望能够做到这一点: $ echo “hello world” | ./my-c-program piped input: >>hello world<< 我知道应该使用isatty来检测stdin是否是tty。 如果它不是tty,我想读出管道内容 – 在上面的例子中,那是字符串hello world 。 在C中这样做的推荐方法是什么? 这是我到目前为止所得到的: #include #include int main(int argc, char* argv[]) { if (!isatty(fileno(stdin))) { int i = 0; char pipe[65536]; while(-1 != (pipe[i++] = getchar())); fprintf(stdout, “piped content: >>%s<<\n", pipe); } } 我使用以下方法编译: gcc -o my-c-program my-c-program.c 它几乎可以工作,除了它似乎总是在管道内容字符串的末尾添加一个U + […]

在execlp()之后关闭管道是否必要?

在陈述我的问题之前,我已经阅读了几个关于堆栈溢出的相关问题,例如UNIX中的管道和复制函数 ,以及其他几个,但没有澄清我的困惑。 首先,代码,这是“Beginning Linux Programming”,第4版,第13章的示例代码: #include #include #include #include int main() { int data_processed; int file_pipes[2]; const char some_data[] = “123”; pid_t fork_result; if (pipe(file_pipes) == 0) { fork_result = fork(); if (fork_result == (pid_t)-1) { fprintf(stderr, “Fork failure”); exit(EXIT_FAILURE); } if (fork_result == (pid_t)0) // Child process { close(0); dup(file_pipes[0]); close(file_pipes[0]); // LINE A […]

将stdout与stderr区分开来

popen()替代方案 我的问题与上面发布的问题有关。 在第一个/接受的回复中,我们正在做: // Child. Let’s redirect its standard output to our pipe and replace process with tail close(pipefd[0]); dup2(pipefd[1], STDOUT_FILENO); dup2(pipefd[1], STDERR_FILENO); 但我想要的是区分ERROR和常规OUTPUT 。 我怎样才能做到这一点? 当我在STDERR得到任何东西时,我需要对它做出反应。 它没有多大意义,但是,我可以遵循吗?: int pipefd[3] /* instead of 2 */ dup2(pipefd[1], STDOUT_FILENO); dup2(pipefd[2], STDERR_FILENO); 我正在使用select来查看fd并查看输出是否可用。 但到现在为止,我只需要看看1 fd,现在我要看2。 注意 :管道只能有两个端部,对吗? 一个写入和另一个读取。 我怎样才能适应这个第三端:D ??

sockets之间的管道

我有一个充当镜像的C ++服务器。 什么进入了一个不同的sockets。 现在,它将套接字读入缓冲区并将其写入另一个套接字。 我想提高吞吐量。 我已经读过有关sendfile()和splice() ,但它似乎仅限于“文件到套接字”传输。 也许套接字之间的简单pipe()可以工作。 您有什么推荐的吗? 便携式解决方案将是完美的,但如果它只是Linux,那就没问题了。

读取过程中的Unix / Linux管道行为在写入过程之前终止

我有这个: $ ls -lh file -rw-r–r– 1 ankur root 181M Sep 23 20:09 file $ head -6 file z abc abc abc abc abc $ cat file | grep -m 1 z z 题: 为什么最后一行中的cat命令行没有过早地使用SIGPIPE死亡? 我认为这应该发生,因为grep终止与cat file相比,没有时间终止。 随着阅读过程的消失, cat将尝试写入破损的管道并且应该用SIGPIPE死掉。 更新: 我最后写了这个:readstdin.c # include # include int main() { ssize_t n ; char a[5]; n = […]