Update markdown and tabs and stuff
This commit is contained in:
@@ -16,43 +16,43 @@ should not affect the other.
|
||||
|
||||
For example:
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int variable = 1;
|
||||
int pid = fork();
|
||||
int status;
|
||||
|
||||
if( pid )
|
||||
{
|
||||
// This is the parent
|
||||
printf("Parent says: %d\n", variable);
|
||||
variable = 2;
|
||||
printf("Parent says: %d\n", variable);
|
||||
waitpid(pid, &status, 0); // Let the child run
|
||||
printf("Parent says: %d\n", variable);
|
||||
} else {
|
||||
// This is the child
|
||||
printf("Child says: %d\n", variable);
|
||||
variable = 3;
|
||||
printf("Child says: %d\n", variable);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
{: .lang-c}
|
||||
:::c
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int variable = 1;
|
||||
int pid = fork();
|
||||
int status;
|
||||
|
||||
if( pid )
|
||||
{
|
||||
// This is the parent
|
||||
printf("Parent says: %d\n", variable);
|
||||
variable = 2;
|
||||
printf("Parent says: %d\n", variable);
|
||||
waitpid(pid, &status, 0); // Let the child run
|
||||
printf("Parent says: %d\n", variable);
|
||||
} else {
|
||||
// This is the child
|
||||
printf("Child says: %d\n", variable);
|
||||
variable = 3;
|
||||
printf("Child says: %d\n", variable);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
This simple program should output (assuming the parent is run first and
|
||||
is not interrupted):
|
||||
|
||||
Parent says: 1
|
||||
Parent says: 2
|
||||
Child says: 1
|
||||
Child says: 3
|
||||
Parent says: 2
|
||||
Parent says: 1
|
||||
Parent says: 2
|
||||
Child says: 1
|
||||
Child says: 3
|
||||
Parent says: 2
|
||||
|
||||
The virtual memory of the X86 architecture allows us to switch out the
|
||||
entire memory space in one strike, and that allows for this behavior.
|
||||
@@ -93,6 +93,7 @@ Finally, each area has a pointer to its owning process.
|
||||
Let's follow a memory area during part of a process' life.
|
||||
|
||||
###Setup
|
||||
|
||||
{: .center .noborder}
|
||||
|
||||
In the figure above we see two processes, _A_ and _B_.
|
||||
@@ -112,13 +113,15 @@ In other words, it is two memory pages long (assuming 4kb pages).
|
||||
|
||||
The user types
|
||||
|
||||
> gcc hello_world.c
|
||||
:::bash
|
||||
$ gcc hello_world.c
|
||||
|
||||
into the terminal and the shell program executes the `fork` system call.
|
||||
|
||||
This makes the kernel do a lot of things, one of which is create a new
|
||||
memory map for the new process. It then clones all memory areas into the
|
||||
new map.
|
||||
|
||||
{: .center .noborder}
|
||||
|
||||
The write flag of our area is unset and the CoW flag is set. The area is
|
||||
@@ -149,6 +152,7 @@ A while later, the parent process is scheduled in and it may also wish
|
||||
to write to the stack. This time the area is already split in two, and
|
||||
the required area has no copies, so it is just set as read/write and
|
||||
we're done.
|
||||
|
||||
{: .center .noborder}
|
||||
|
||||
Actually, the parent process will probably perform a `waitpid` syscall
|
||||
|
||||
Reference in New Issue
Block a user