The address space of a process is independent of each other. That is to say, each process has its own page Directory and page table. Once fork () is called, the address space of the Parent and Child processes is two spaces. If the child process modifies some of the memory, the parent process cannot be seen. For example, the following test:
# Include <stdio. h>
# Include <sys/types. h>
# Include <unistd. h>
Struct xx
{
Int;
};
Int main ()
{
Pid_t pid;
Struct xx s;
S. a = 5;
Struct xx * sp = & s;
Pid = fork ();
If (pid> 0)
{
Printf ("parent show % p, % p, a = % d \ n", sp, & sp-> a, sp-> );
Sp-> a = 4;
// While (sp-> a> 0 );
Sleep (10 );
Printf ("parent show % p, % p, a = % d \ n", sp, & sp-> a, sp-> );
Printf ("parent exit \ n"); // exit (0 );
}
Else
{
// While (sp-> a = 5 );
Printf ("child show % p, % p, a = % d \ n", sp, & sp-> a, sp-> );
Sp-> a =-1;
Printf ("child change a to % d \ n", sp-> );
}
Return 0;
}
The compilation and running result is:
[Root @ 141 test] # gcc test. c
[Root @ 141 test] #./a. out
Parent show 0x7fffe86b64a0, 0x7fffe86b64a0, a = 5
Child show 0x7fffe86b64a0, 0x7fffe86b64a0, a = 5
Child change a to-1
Parent show 0x7fffe86b64a0, 0x7fffe86b64a0, a = 4
Parent exit
Everything is clear ~
After a one-night project was called, the Parent and Child processes encountered a problem. Basic knowledge is still very important!
This article is from the blog "Xiaoqiang technology blog ".