问题标签 [dup2]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
11935 浏览

c - dup2、stdout 和 stderr 出现问题

当这个程序运行时,“stderr”行显示在“stdout”行之前。为什么?我认为 dup2 会使 stderr 和 stdout 使用相同的文件描述符,因此缓冲应该没有问题。我在 Solaris 10 上使用 gcc 3.4.6。

0 投票
3 回答
2884 浏览

c++ - 僵尸进程和分叉

我有这样的代码...

fd[READ] 和 fd[WRITE] 是管道文件描述符。

当我连续运行它时,当我使用 ps ax 时有很多僵尸进程。如何纠正这种情况?这是因为我没有使用父进程等待子进程的退出状态...

0 投票
4 回答
5582 浏览

c - dup2() and exec()

}

My sample_a.c code is below:

In the above code, what I really want to do is: 1) Redirect output from the child process to pipe and have parent read from the pipe and print it out to stdout.

I am able to redirect child process output( "printf") from stdout to the pipe but I am not able to redirect execlp's child process, xterm, output to the pipe.

Can anybody help me this?

0 投票
2 回答
3357 浏览

c++ - 将 FROM stderr 重定向到另一个文件描述符

我的程序调用打印到标准错误的库函数。我想进行干预,以便所有对文件描述符 #2 的写调用都将被发送到其他地方。

这是我的第一次尝试:

在这里,fd 成功地从open("/foo/bar",O_APPEND|O_CREAT)

此函数返回 true 后,std::cerr<<"blah"转到终端而不是文件。

我究竟做错了什么?

谢谢。

更新

谢谢,larsmans,但我还没到那里......

这输出

到标准输出并且给定的文件是空的。(如果不存在,则正确创建。)

0 投票
1 回答
4027 浏览

c++ - dup2 将 stdout 和 stderr 重定向到另一个文件描述符

我有这样的电话。

接着

有没有办法使用 dup 调用将 1 和 2 复制到 fd[WRITE]?

0 投票
1 回答
8986 浏览

file - Redirecting stdout to file after a fork()

I'm working on a simple shell, but right now I am just trying to understand redirection. I'm just hard coding an ls command and trying to write it to a file for now. Currently, the ls runs, and the output file is created, but the output still goes to stdout and the file is blank. I'm confused as to why. Thanks in advance.

Here is my code:

0 投票
3 回答
5433 浏览

c - 写入文件描述符

在下面的代码段中,我将ls命令的输出重定向到完美工作的输入wc -l。现在我还想ls使用以下代码将命令的输出重定向到名为“beejoutput.txt”的文件,但它不起作用。需要帮忙。

0 投票
3 回答
10647 浏览

c - STDIN_FILENO 和 STDOUT_FILENO 在 c 中是只读的吗?

man告诉我dup2() makes newfd be the copy of oldfd, closing newfd first if necessary.

但不是只读STDIN_FILENO的吗?STDOUT_FILENO

还是dup2根本没有改变newfd

0 投票
0 回答
378 浏览

linux - 数据在(TCP)套接字中消失

我有这个主要是原型的 TCP 套接字服务器,它接受一个连接,然后运行一个用户指定的程序来与另一端对话。神秘的是 write() 被调用并返回,但没有输出到达客户端。

strace 输出(作为执行程序运行“cat”)如下所示:

而在客户端则什么都没有发生:

我已经准备好相信 execve'd 程序应该像使用 send/recv/shutdown 而不是 read/write/close 一样对待套接字,但我到目前为止看到的文档似乎建议 close() 应该按设计工作,并且仅在半关闭连接时才需要关闭。Unix Sockets FAQ提到未发送的数据应该在关闭时刷新而不设置任何 SO_LINGER 选项,Linux 手册页 socket(7) 声称“当套接字作为 exit(2) 的一部分关闭时,它总是在后台徘徊. " 给它足够的输出会导致第一部分到达客户端。

为了完整起见,这里是程序...

0 投票
2 回答
702 浏览

c - C中的多个管道

我想在 c 中实现多管道,所以我可以做这样的事情,这|||意味着将标准输入复制到 N 管道命令):

cat /tmp/test.log ||| wc -l ||| grep test1 ||| grep test2 | grep test3

这将返回给我文件中的行数文件中包含“test1”字符串行以及文件中包含“test2”&&“test3”字符串的行

换句话说,这将具有以下 3 个常规管道的效果:

有人已经实现了这样的东西吗?我没有找到任何东西... 注意:我知道它可以使用脚本语言或 bash 多个文件描述符来完成,但我正在搜索 C 代码来完成它。

谢谢!