开发者

confusing fork system call

开发者 https://www.devze.com 2022-12-24 19:15 出处:网络
i was just checking the behaviour of fork system call and i found it very confusing. i saw in awebsite that

i was just checking the behaviour of fork system call and i found it very confusing. i saw in a website that

Unix will make an exact copy of the parent's address space and give 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开发者_Python百科 %c\n",y);
}
}

the output of the above program is :

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

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

so from the above mentioned statement it seems that child and parent have separet address spaces.this is the reason why char value is printed separately and why am i seeing the address of the variable as same in both child and parent processes.?

Please help me understand this!


Basically the concept of virtual memory gives a view to the process as if it is the sole owner of the system. It feels it has access to the complete memory.

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

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

So, though both in parent and child the same address is present, this is just the virtual address. and each maps to a different physical address.

Hope it helps!!


You're right. As soon as you call fork(), two identical processes exist. Therefore, the address of y is the same for the copy of y in each process. The only difference between the two processes is that in one, fork() returned 0, and in the other it returned the PID of the child. Your program is using that information to do different behaviour in the parent and child, so you get the appropriate output.

In "real life", operating systems do a lot of optimizations to make fork() really fast. That means that the actual physical behaviour probably doesn't involve a complete copy of the memory space. Logically, however, you can treat it as such.


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


Address 221 P Street is a separate building from address 221 C Street. Their contents differ even though they have the same address number.


You have 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 runs in two parts. One is child and the other one is parent. If you call for address from a variable that used before fork() ,you will get the same result both for parent and child.

0

精彩评论

暂无评论...
验证码 换一张
取 消