Fork dup2阻止打印F,但不阻止fprintf?

Fork dup2阻止打印F,但不阻止fprintf?,fork,pipe,printf,dup2,Fork,Pipe,Printf,Dup2,因此,我为我的操作系统类分配了一个任务,在这个任务中,我将创建一个与管道连接的进程环,以便在它们之间传递消息。我发现了一些示例代码,我正在寻找适合(或至少理解)我的需要。示例代码(稍加修改)为: 当我将2作为argv[1]给出时 所以,我想知道,为什么dup2部分会阻止printf()执行?如果我甚至不能打印一些东西,我甚至不确定我是否能正确传递消息。还有,为什么fprintf()已经在那里工作了,但我不会放在那里 编辑:我想把这个交给我的教授/助教,但他们都不在城里,从现在到截止日期都无法访问

因此,我为我的操作系统类分配了一个任务,在这个任务中,我将创建一个与管道连接的进程环,以便在它们之间传递消息。我发现了一些示例代码,我正在寻找适合(或至少理解)我的需要。示例代码(稍加修改)为:

当我将2作为argv[1]给出时

所以,我想知道,为什么dup2部分会阻止printf()执行?如果我甚至不能打印一些东西,我甚至不确定我是否能正确传递消息。还有,为什么fprintf()已经在那里工作了,但我不会放在那里

编辑:我想把这个交给我的教授/助教,但他们都不在城里,从现在到截止日期都无法访问…

printf()不将数据发送到路径0,它使用
stdout
发送缓冲数据。当您通过向路径0复制某些内容来中断路径0时,您似乎正在中断过程中的
stdout

从dup2的手册页:
dup2()使newfd成为oldfd的副本,必要时先关闭newfd
。因此,当您调用
dup2(fd[0],STDIN\u FILENO)
时,您正在中断
stdout


您声明fprintf()正在工作,但printf()不工作。。。fprintf()的路径是什么?如果您使用的是
stderr
,那么它将继续工作是非常有意义的,因为您没有对该路径执行任何操作。

printf
打印到stdout,即文件描述符1(或等效的
stdout\u FILENO
)。正在当前标准输出上复制管道的文件描述符,这会产生关闭当前标准输出的副作用。因此,当您在调用特定的
dup2
之后尝试
printf
时,您实际上是在将数据打印到您刚刚创建的管道中,而不是打印到终端输出


fprintf(stderr,…)
仍然有效,因为它会打印到stderr,而不是stdout,并且stderr文件描述符(2,或者相当于
stderr\u FILENO
)在程序期间不会更改,所以它会继续打印到终端。

哇,是的,完全盯着这段代码看了太久了
/* Program 4.1 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

/* Sample C program for generating a unidirectional ring of processes.Invoke this program
 with a command-line arg ument indicating the number of processes on the ring.  Communication
 is done via pipes that connect the standard output of a process to the standard input of
 its successor on the ring.  After the ring is created, each process identifies itself with
 its process ID and the  process ID of its parent.  Each process then exits. */

void main(int argc,  char *argv[ ])
{
int master_pid = getpid();
printf("master pid: %i\n", master_pid);

int   i;             /* number of this process (starting with 1)   */
int   childpid;      /* indicates process should spawn another     */
int   nprocs;        /* total number of processes in ring          */
int   fd[2];         /* file descriptors returned by pipe          */
int   error;         /* return value from dup2 call                */
/* check command line for a valid number of processes to generate */
if ( (argc != 2) || ((nprocs = atoi (argv[1])) <= 0) ) {
    fprintf (stderr, "Usage: %s nprocs\n", argv[0]);
    exit(1);
}
/* connect std input to std output via a pipe */
if (pipe (fd) == -1) {
    perror("Could not create pipe");
    exit(1);
}
printf("%s\n", "test");
//this section is blocking printf()?
if ((dup2(fd[0], STDIN_FILENO) == -1) ||
    (dup2(fd[1], STDOUT_FILENO) == -1)) {
    perror("Could not dup pipes");
    exit(1);
}
printf("%s\n", "test");

if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
    perror("Could not close extra descriptors");
    exit(1);
}
/* create the remaining processes with their connecting pipes */
for (i = 1; i < nprocs;  i++) {
    if (pipe (fd) == -1) {
        fprintf(stderr,"Could not create pipe %d: %s\n",
                i, strerror(errno));
        exit(1);
    }
    if ((childpid = fork()) == -1) {
        fprintf(stderr, "Could not create child %d: %s\n",
                i, strerror(errno));
        exit(1);
    }
    if (childpid > 0)        /* for parent process, reassign stdout */
        error = dup2(fd[1], STDOUT_FILENO);
    else
        error = dup2(fd[0], STDIN_FILENO);
    if (error == -1) {
        fprintf(stderr, "Could not dup pipes for iteration %d: %s\n",
                i, strerror(errno));
        exit(1);
    }
    if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
        fprintf(stderr, "Could not close extra descriptors %d: %s\n",
                i, strerror(errno));
        exit(1);
    }
    if (childpid)
        break;
}

/* say hello to the world */
fprintf(stderr,"This is process %d with ID %d and parent id %d\n",
        i, (int)getpid(), (int)getppid());
wait(1);
exit (0);
}     /* end of main program here */
master pid: 30593
test
This is process 1 with ID 30593 and parent id 30286
This is process 2 with ID 30594 and parent id 30593