几天我就提出了一个问题.我的解决方案符合接受的答案中的建议.但是,我的一个朋友提出了以下解决方案:
请注意,代码已更新几次(查看编辑修订版)以反映下面答案中的建议.如果您打算给出新的答案,请考虑这个新代码,而不是那些有很多问题的旧代码.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int fd[2], i, aux, std0, std1;
do {
std0 = dup(0); // backup stdin
std1 = dup(1); // backup stdout
// let's pretend I'm reading commands here in a shell prompt
READ_COMMAND_FROM_PROMPT();
for(i=1; i<argc; i++) {
// do we have a previous command?
if(i > 1) {
dup2(aux, 0);
close(aux);
}
// do we have a next command?
if(i < argc-1) { …Run Code Online (Sandbox Code Playgroud) 我有这样的代码......
c = fork();
if(c==0) {
close(fd[READ]);
if (dup2(fd[WRITE],STDOUT_FILENO) != -1)
execlp("ssh", "ssh", host, "ls" , NULL);
_exit(1);
}
close(fd[WRITE]);
Run Code Online (Sandbox Code Playgroud)
fd [READ]和fd [WRITE]是管道文件描述符.
当我连续运行时,当我使用ps ax时,会有很多僵尸进程.如何纠正这个?这是因为我没有使用父进程来等待子进程的退出状态...
我有一个这样的电话。
int fd[2];
pipe(fd)
Run Code Online (Sandbox Code Playgroud)
进而
dup2(fd[WRITE],STDOUT_FILENO)
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 dup 调用将 1 和 2 复制到 fd[WRITE]?
fd = open("/dev/null", O_RDWR);
if (fd == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
"open(\"/dev/null\") failed");
return NGX_ERROR;
}
if (dup2(fd, STDIN_FILENO) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN) failed");
return NGX_ERROR;
}
if (dup2(fd, STDOUT_FILENO) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT) failed");
return NGX_ERROR;
}
if (fd > STDERR_FILENO) {
if (close(fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() failed");
return NGX_ERROR;
}
}
Run Code Online (Sandbox Code Playgroud)
man告诉我dup2() makes newfd be the copy of oldfd, closing newfd first if necessary.: …
我只是为了我的问题的上下文发布我的代码.我并没有明确地寻找你来帮助修复它,我更希望了解dup2系统调用,我只是从手册页和其他许多stackoverflow问题中找不到.
pid = fork();
if(pid == 0) {
if(strcmp("STDOUT", outfile)) {
if (command->getOutputFD() == REDIRECT) {
if ((outfd = open(outfile, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
return false;
command->setOutputFD(outfd);
if (dup2(command->getOutputFD(), STDOUT_FILENO) == -1)
return false;
pipeIndex++;
}
else if (command->getOutputFD() == REDIRECTAPPEND) {
if ((outfd = open(outfile, O_CREAT | O_WRONLY | O_APPEND)) == -1)
return false;
command->setOutputFD(outfd);
if (dup2(command->getOutputFD(), STDOUT_FILENO) == -1)
return false;
pipeIndex++;
}
else {
if (dup2(pipefd[++pipeIndex], STDOUT_FILENO) == -1)
return false;
command->setOutputFD(pipefd[pipeIndex]);
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用multiprocessingpackage 生成第二个进程,我希望将 stdout 和 stderr 重定向到第一个进程。我正在使用multiprocessing.Pipe对象:
dup2(output_pipe.fileno(), 1)
Run Code Online (Sandbox Code Playgroud)
哪里output_pipe有 的实例multiprocessing.Pipe。然而,当我尝试在另一端阅读时,它就挂起了。我尝试阅读 using Pipe.recv_byteswith a limit,但这会引发OSError. 这是否可能,或者我应该切换到一些较低级别的管道功能?
我在使用了一些麻烦dup2,试图既重定向stdout并stderr进入同一个输出文件.
我正在使用这个解释性代码示例:(gcc 4.8.2,Ubuntu 14.04)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define USE2FILES
int main()
{
int f1, f2, status;
f1 = open("test.out", O_CREAT | O_WRONLY, 0644);
if (f1 == -1) {
perror("open(): ");
}
status = dup2(f1, STDOUT_FILENO);
if (status == -1) {
perror("dup2(): ");
}
#ifdef USE2FILES
close(f1);
#endif
#ifdef USE2FILES
f2 = open("test.out", O_CREAT | O_WRONLY, 0644);
if (f2 == -1) {
perror("dup2(): ");
}
#else …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 dup2 将 stdout 重定向到另一个文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int newfd;
if ((newfd = open("output_file.txt", O_CREAT|O_TRUNC|O_WRONLY, 0644)) < 0) {
exit(1);
}
printf("Luke, I am your...\n");
dup2(newfd, 1);
printf("Foobar.\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当第一次printf打印换行符时\n,Luke, I am your...将被打印到屏幕上并被Foobar写入output_file.txt,如果第一个printf没有打印换行符printf("Luke, I am your...");,则两个字符串都将被写入output_file.txt。因此,当没有换行符(\n)时, printf 似乎会将第一个字符串写入缓冲区。
那么到底发生了什么?
这是一个代码片段。
int saved_stdout = dup(1);
int fd = open("file.txt", O_WRONLY | O_CREAT, 0640);
close(1);
dup(fd);
close(fd);
printf("This text should go into the file\n");
//restore stdout
dup2(saved_stdout, 1);
printf("stdout restore");
Run Code Online (Sandbox Code Playgroud)
我正在尝试了解 dup 和 dup2。所以我最初将我的 file.txt 连接到 stdout。所以每当我使用 printf 时,我应该写入 file.txt 而不是 stdout。但是,一旦我完成了这种用法,我也想将它恢复回来,所以我最后也使用了 dup2。
问题是文本“This text should go into the file\n”实际上从未进入文件,而是打印在标准输出上。为什么这样?我跟踪它,却发现 dup2 调用发生在 printf("This text..."); 之前。声明,为什么呢?
所以,如果我这样做:
dup2(0, backup); // backup stdin
dup2(somefile, 0); // somefile has four lines of content
fgets(...stdin); // consume one line
fgets(....stdin); // consume two lines
dup2(backup, 0); // switch stdin back to keyboard
Run Code Online (Sandbox Code Playgroud)
我在这一点上发现.. stdin仍然包含我没有消耗的两条线.这是为什么?因为无论重定向多少次,只有一个缓冲区?我如何摆脱剩下的两行,但是当我想回到它时,仍然记得我在somefile流中的位置?