How come forked processes do not affect each other when there is a global pointer?

和自甴很熟 提交于 2019-12-10 20:18:05

问题


I know the fork() function creates a process which is identical to its parents, only differs by the PID it has. They have the same variables initially, and changes made to these variables do not affect each other. But what happens when a global pointer variable is shared?

I have written some code and printed out the results. It appears that the parent and the child process have the pointer pointing to the same memory location, however changes made to these memory locations, i.e. *p = 1 in parent and *p = 2 in child, do not affect each other. Also note that I make the parent process wait(NULL) until the child process exits. So the child process changes the value pointed by the pointer having the same memory address of the parent process' pointer.

I know when fork() is called, parent process clone everything: registers, program counters etc. But how is that possible? Shouldn't the parent process have its variable's value changed after the child process exits? Is it due to the system puts everything (including the parent process' pointer variable's) onto stack and pops them when the child is terminated?


回答1:


When a process is forked, the new process is for all(?) intents and purposes a copy of the original, with its own virtual address space, file descriptors e.t.c. In a rather simplistic view, the same memory address will actually point to a different physical memory address for each process - you can have two equal pointers that point to completely different data.

Naturally, in modern operating systems things are not quite as simple. fork(), for example, does not actually copy everything as that would be a waste of both processor time and memory. The kernel makes use of a bit of page table manipulation to implement copy-on-write memory duplication. Additionally, it is possible to control to some degree which resources will be actually cloned to the child process, for both performance and correctness reasons.




回答2:


This is a very large topic, but here's a brief explanation. You're probably confusing execution threads with forked processes. In a Unix system each process is independent, and has its own virtual address space. That means that two processes that have a pointer set to the same address will in reality normally be using different addresses in physical memory. This concept is an essential part of any decent operating system - on more primitive ones a running program will have direct access to the physical memory. In fact, if you ran a thousand instances of an executable at once, you'd probably find that each one was using the exact same addresses for everything.

Aside from the security benefits of this, it also allows the 'text' part of a program (the binary instructions) to be shared by all instances of a running program. The kernel can map every process to the same addresses in physical memory.

It's not true that a child is an exact copy of the parent, or that is shares everything. There's a long list of things that are inherited, and things that are not. It's mostly the same - it has the same executable code, the same open files, etc. but it's a fully independent, separate process.



来源:https://stackoverflow.com/questions/15315482/how-come-forked-processes-do-not-affect-each-other-when-there-is-a-global-pointe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!