简体   繁体   English

在fork()命令之后创建管道时,究竟发生了什么?

[英]What exactly happens when you create a pipe after a fork() command?

Do you have two pipes then? 那你有两个管子吗? And when the parent process writes something into the pipe can the child process read it? 当父进程将某些内容写入管道时,子进程可以读取吗?

What will be the difference if you create a pipe before the fork? 如果在叉子之前创建管道,会有什么区别?

When I tryed it out it just messed up with my data I wantet to transfer from child to parent and I got some crzy symbols instead of an integer. 当我尝试它时,它只是弄乱了我的数据,我想将其从子级转移到父级,并且得到了一些疯狂的符号,而不是整数。

If you create a pipe after a fork() , in both the child and parent process, you have two pipes - one in the child and one in the parent. 如果在子进程和父进程中的fork()之后创建管道,则将有两个管道-一个在子进程中,一个在父进程中。 Each process owns both ends of its respective pipe. 每个进程都拥有其各自管道的两端。 Neither pipe is attached to both the child and parent process, and neither process will be able to communicate with the other via the pipe that it owns. 子进程和父进程均未连接任何管道,并且两个进程都无法通过其拥有的管道与另一个进程进行通信。

If you create a pipe before a fork() , there is one pipe, and each process (parent and child) will have a file descriptor referring to each end of the pipe (because the child naturally inherits the file descriptors of the parent). 如果在fork()之前创建一个管道,则有一个管道,并且每个进程(父进程和子进程)都将具有一个引用该管道两端的文件描述符(因为子进程自然会继承父进程的文件描述符)。 In this situation the processes can communicate by writing to / reading from alternate ends of the pipe. 在这种情况下,进程可以通过在管道的另一端进行写入/读取来进行通信。

It is usual practice, if you want to create a pipe to communicate between a child and parent process, to create the pipe before the fork, and close one (different) end of the pipe in each process. 如果要创建一个在子进程和父进程之间进行通信的管道,在派生之前创建管道并在每个进程中关闭管道的一个(不同的)末端,这是通常的做法。 Since pipes are generally unidirectional, this allows one-way communication between the processes. 由于管道通常是单向的,因此可以在过程之间进行单向通信。 You can use a socket instead (via socketpair ), or create two pipes (before forking), if you want bi-directional communication. 如果要进行双向通信,则可以改用套接字(通过socketpair ),也可以创建两个管道(在分叉之前)。

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

相关问题 当你派生一个父进程时,活动子进程会发生什么,所有活动进程都会创建另一个进程吗? 叉子(); - What happens to active child processes when you fork a parent, do all active processes create another process ? Fork(); 当 fork() 处于某个条件时会发生什么? - What happens when fork() in a condition? 在C语言中取消引用静态变量时会发生什么情况? - What exactly happens when you dereference a static variable in C? 在C中,将NULL指针传递给strcmp()时会发生什么? - In C, what exactly happens when you pass a NULL pointer to strcmp()? 当您传递已经分配的值指针时,会发生什么? - What happens exactly when you pass by value pointer that is already allocated? 在Unix中调用fork()时会发生什么? - What Happens When I Call fork() in Unix? 基本的c问题-在c中声明静态变量时会发生什么? - Basic c question - What exactly happens when you declare a static variable in c? 当您对与 dup2() 复制的管道文件描述符调用 close() 时会发生什么? - What happens when you call close() on a pipe file descriptor that was duplicated with dup2()? 我完成这项作业后会发生什么 - what exactly happens when I do this assignment 包含头文件时到底发生了什么? - What exactly happens when a header file is included?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM