3

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?

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
  • 6
    Each process has its own virtual memory map - the addresses are 'pretend'. – teppic Mar 09 '13 at 20:22
  • Read some good book about Unix or Linux programming like http://advancedlinuxprogramming.com/ and learn more about http://en.wikipedia.org/wiki/Virtual_memory and e.g. `mmap(2)`, `fork(2)`, and `execve(2)` syscalls. – Basile Starynkevitch Mar 09 '13 at 20:27
  • 3
    "I know when fork() is called, processes share everything, registers program counters etc." --- beware, you know something that is not true. – n. m. could be an AI Mar 09 '13 at 20:37
  • "It (fork) creates an exact duplicate of the original process, including all the file descriptors, registers--everything." Andrew Tannenbaum - Operating Systems Design and Implementation. By "share", i actually tried to mean clone, if that is what you say that I know something that is not true. They do not "share", they are "cloned" to the child processor. That was what i meant. – Varaquilex Mar 09 '13 at 20:44
  • 1
    @Volkanİlbeyli Right. It's a copy. It's not shared. – nos Mar 09 '13 at 20:51

2 Answers2

4

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.

thkala
  • 84,049
  • 23
  • 157
  • 201
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.

teppic
  • 8,039
  • 2
  • 24
  • 37
  • To be honest, I haven't learnt about threads yet, so I'm not confusing them:) +1 for your nice, informing answer, but the other answer is what I was looking for, therefore I accepted it. – Varaquilex Mar 10 '13 at 16:13