0

Because of COW, linux will assign a page to a child process only once it has been written to. It will also assign a new page for the stack only once it is changed. So for example, if after a fork() syscall, we call printf in the child, since the stack has changed we will get a page fault.

However i'm not sure about the following code:

fork();
char *arr = mmap(... some args.. MAP_FILED|MAP_PRIVATE);

Since both father and son run the mmap, I would assume that a page fault will occure on the second line because we call a function (well a syscall, actually) and also create a local variable, hence changing the stack. Is this correct?

TL;DR:

Does mmap causes a page fault after we fork?

Eminem
  • 143
  • 6
  • 2
    Any local variable assignment or function call will cause the stack frame page to be copied. There's nothing different about `mmap()`. – Barmar Jul 23 '21 at 18:27
  • @Barmar So there will be a page fault (major?) at line 2? Notice that the mmap if file backed and private, i'm not sure if that changes anything. – Eminem Jul 23 '21 at 18:33
  • I though we're just talking about the stack frame memory, not the memory being used for the mapping. That doesn't cause a page fault until you dereference the pointer. – Barmar Jul 23 '21 at 18:41
  • @Barmar Wait, isn't this comment a contradiction to your answer? You said that even assigining arr to null will cause a page fault due to a new page for the stack. – Eminem Jul 23 '21 at 18:44
  • There's a page fault on the stack frame immediately. There's a page fault on the mapped memory when you dereference the pointer. – Barmar Jul 23 '21 at 18:45

2 Answers2

0

Calling mmap(), or any function, will cause a page fault on the stack because it needs to save the return address on the stack and then create a new stack frame for the function being called.

And even an assignment to a local variable would cause a page fault without a function call. So simply doing:

fork();
char *arr = NULL;

might cause a page fault (I say "might" because the compiler might optimize this to initialize the memory once, before the function call; also, the arr variable could be stored in a register rather than memory).

Most code that uses fork() saves the return value in a variable, so it knows whether it's in the parent or child. So usually there's a page fault as soon as fork() returns (again, unless a register is used for that variable).

In all these cases, there will just be a fault in either the parent or child process, not both. The first one to modify the stack frame makes a copy, then the COW flag is cleared, so the other process can continue to use the original page.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • causing a page fault will happen in the stack if a new page has to be allocated or swapped in, this is not the case in the majority of pushes, while after a fork a lot of page faults happen due to the COW thing. – Luis Colorado Jul 27 '21 at 09:24
0

The page fault happens after mmap has returned, and you start using your allocated segment. Before that, the kernel checks for errors, and assigns the resources to you (administratively) but it doesn't actually assign them to you until you make the actual request (this is when you access the memory itself with a read or write to that memory) This follows the technique of lazy initialization, that makes starting of a process a more smooth task, and doesn't unnecesaryly allocate resources that finally aren't used by the process.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31