简体   繁体   English

fork后,父子进程是否共享pipe创建的文件描述符?

[英]After fork, do the parent and child process share the file descriptor created by pipe?

int main()
{
    int data_processed;
    int file_pipes[2];
    const char some_data[] = "123";
    char buffer[BUFSIZ + 1];
    pid_t fork_result;

    memset(buffer, '\0', sizeof(buffer));

    if (pipe(file_pipes) == 0) {
        fork_result = fork();
        if (fork_result == -1) {
            fprintf(stderr, "Fork failure");
            exit(EXIT_FAILURE);
        }

// We've made sure the fork worked, so if fork_result equals zero, we're in the child process.

        if (fork_result == 0) {
            data_processed = read(file_pipes[0], buffer, BUFSIZ);
            printf("Read %d bytes: %s\n", data_processed, buffer);
            exit(EXIT_SUCCESS);
        }

// Otherwise, we must be the parent process.

        else {
            data_processed = write(file_pipes[1], some_data,
                                   strlen(some_data));
            printf("Wrote %d bytes\n", data_processed);
        }
    }
    exit(EXIT_SUCCESS);
}

Based on my understanding, the child process created by fork doesn't share variables with its parent process.根据我的理解,由 fork 创建的子进程不与其父进程共享变量。 Then, why here the parent can write to one file descriptor and child process can get the data by reading from another file descriptor.那么,为什么这里父进程可以写入一个文件描述符,而子进程可以通过从另一个文件描述符读取数据。 Is this because they are controled somehow by the pipe function internally?这是因为它们在内部被 pipe function 以某种方式控制吗?

File descriptors, including pipes, are duplicated on fork -- the child process ends up with the same file descriptor table, including stdin/out/err and the pipes, as the parent had immediately before the fork.文件描述符,包括管道,在fork上是重复的——子进程最终得到相同的文件描述符表,包括 stdin/out/err 和管道,就像父进程在 fork 之前一样。

Based on my understanding, the child process created by fork doesn't share variables with its parent process.根据我的理解,由 fork 创建的子进程不与其父进程共享变量。

This isn't entirely true -- changes to variables are not shared with the parent, but the values that the parent had immediately prior to the fork are all visible to the child afterwards.这并不完全正确——对变量的更改不会与父级共享,但父级在分叉之前拥有的值在之后对子级都是可见的。

In any case, pipes exist within the operating system, not within the process.在任何情况下,管道都存在于操作系统中,而不是进程中。 As such, data written to one end of the pipe becomes visible to any other process holding a FD for the other end.因此,写入 pipe 一端的数据对于持有另一端 FD 的任何其他进程可见。 (If more than one process tries to read the data, the first process to try to read() data gets it, and any other processes miss out.) (如果有多个进程尝试读取数据,则第一个尝试read()数据的进程会获取数据,而其他任何进程都会错过。)

The variables are not shared eg if you write file_pipes[0] = 999 in the child, it will not be reflected in the parent.变量不共享,例如,如果您在子级中写入file_pipes[0] = 999 ,它将不会反映在父级中。 The file descriptors are shared (FD number x in the child refers to the same thing as FD number x in the parent).文件描述符是共享的(子项中的 FD 编号x指的是与父项中的 FD 编号x相同的东西)。 This is why (for example) you can redirect the output of a shell script which executes other commands (because they share the same standard output file descriptor).这就是为什么(例如)您可以重定向执行其他命令的 shell 脚本的 output 的原因(因为它们共享相同的标准 output 文件描述符)。

You're right - ordinary variables aren't shared between the parent and the child.你是对的 - 普通变量不会在父母和孩子之间共享。

However, pipes are not variables.但是,管道不是变量。 They're a pseudo-file specifically designed to connect two independent processes together.它们是专门设计用于将两个独立进程连接在一起的伪文件。 When you write to a pipe, you're not changing a variable in the current process - you're sending data off to the operating system and asking it to make that data available to the next process to read from the pipe.当您写入 pipe 时,您不会更改当前进程中的变量 - 您正在将数据发送到操作系统并要求它使该数据可用于下一个进程以从 pipe 读取。

It's just like when you write to a real, on-disk file - except that the data isn't written to disk, it's just made available at the other end of the pipe.这就像你写入一个真实的磁盘文件一样——除了数据没有写入磁盘,它只是在 pipe 的另一端可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不希望父级及其子进程共享同一文件描述符表 - Do not want a parent and its child process to share the same file descriptor table fork()之后创建的子进程的父pid_t - Parent pid_t of a child process created after fork() 如何关闭跨 fork 继承的子进程中的文件描述符。 那么父进程是否保持打开状态 - how to close a file descriptor in child process inherited across a fork. So does the parent process remains open or not [APUE]父和子在fork之后共享相同的文件偏移量吗? - [APUE]Does parent and child share the same file offset after fork? 分支后由子级创建的文件是否也与父级共享? - Is the file created by child after fork, also shared with parent? 子进程退出时,父级应如何关闭管道文件描述符 - How should parent close pipe file descriptor when child process exits 管,叉和执行 - 父母与子女过程的双向沟通 - Pipe, Fork, and Exec - Two Way Communication Between Parent and Child Process 叉,父和子过程 - Fork, Parent and child process 有没有办法在使用 fork 和 execl 创建的子进程之后在父进程中执行代码? - Is there a way to execute code in the parent process after the child, which is created using fork and execl? 使用fork()函数使子进程和父进程共享内存 - using fork() function to make the child and parent process share memory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM