Confused fork system call

I was just checking the behavior of the fork system call and I found it very confusing. I saw on the website that

Unix will make an exact copy of the parent address space and pass it to the child. Therefore, the parent and child processes have separate address spaces

#include <stdio.h> #include <sys/types.h> int main(void) { pid_t pid; char y='Y'; char *ptr; ptr=&y; pid = fork(); if (pid == 0) { y='Z'; printf(" *** Child process ***\n"); printf(" Address is %p\n",ptr); printf(" char value is %c\n",y); sleep(5); } else { sleep(5); printf("\n ***parent process ***\n",&y); printf(" Address is %p\n",ptr); printf(" char value is %c\n",y); } } 

output of the above program:

  *** Child process *** Address is 69002894 char value is Z ***parent process *** Address is 69002894 char value is Y 

therefore, from the above statement, it seems that the child and the parent have separet address spaces. It is for this reason that the char value is printed separately and why I see the address of the variable as the same in both the child and the parent processes.

Please help me figure this out!

+4
source share
5 answers

In principle, the concept of virtual memory gives an idea of โ€‹โ€‹the process as if it is the sole owner of the system. He feels that he has access to full memory.

But in fact, the OS gives it only virtual memory, which is mapped to the actual OS memory using the MMU.

So, what happens in your case, each process (parent and child) has its own address space. And this is separate for both. Now here the address space refers to the virtual address space.

So, although both parents and the child have the same address, this is only a virtual address. and each is mapped to a different physical address.

Hope this helps!

+7
source

You're right. Once you call fork() , there are two identical processes. Therefore, the address y matches the copy of y in each process. The only difference between the two processes is that in one, fork() returns 0 , and in the other it returns the PID of the child. Your program uses this information to perform different actions in the parent and child, so you get the appropriate output.

In "real life", operating systems make many optimizations to make fork() very fast. This means that the actual physical behavior probably does not include a full copy of the memory space. Logically, however, you can consider this as such.

+6
source

They have a separate address space - that is why the same memory address is allowed to have different values. A memory address only makes sense in the context of a process.

+6
source

Address 221 P Street - a separate building with the address 221 C Street. Their contents are different in that they have the same address number.

+3
source

You need to replace pid with ptr . To get the parent and child ID, you must use pid in printf(" Address is %p\n",pid); instead of ptr . After fork() program starts in two parts. One of them is a daughter, and the other is a parent. If you call the address from the variable used before fork() , you will get the same result for both the parent and the child.

+1
source

Source: https://habr.com/ru/post/1305064/


All Articles