简体   繁体   English

fork中的fork()调用

[英]fork() call in c

I have used fork() to create 2 different processes operating on 2 different address spaces. 我使用fork()创建了在2个不同地址空间上运行的2个不同进程。 Now, in parent process I need the value of a variable from child's address space or if the child process can modify the variable in parent's address space. 现在,在父进程中,我需要来自子地址空间的变量值,或者子进程是否可以修改父地址空间中的变量。 Is this possible? 这可能吗?

No, once you've forked, each process gets its own address space and you'll have to look into either: 不,一旦你分叉,每个进程都有自己的地址空间,你将不得不考虑:

  • some form of IPC between the processes to access each others data (such as shared memory or message queues). 某些形式的IPC在进程之间访问彼此的数据(例如共享内存或消息队列)。
  • some more lighweight variant of fork that allows sharing of data (including possibly threading). 一些更轻量级的fork变体,允许共享数据(包括可能的线程)。

一旦有两个进程,共享数据就需要进程间通信:文件,管道或共享内存。

If you mean exchanging data between these two processes you can not. 如果您的意思是在这两个流程之间交换数据,则不能。 You can do it by system APIs like SharedMemory, Message Passing, Pipeline, Socket, ... 您可以通过系统API(如SharedMemory,Message Passing,Pipeline,Socket,...)来实现。

由于您使用fork命令创建了两个进程,因此两个进程将位于不同的地址空间中,因此它们只能通过IPC,消息传递,管道,共享内存等进行通信。否则,一个进程无法访问其他进程数据,因为它们具有特定于进程的进程数据和类似的线程也有特定于线程的数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM