[MUSIC].
Now that we know what a process is, let's
see how we create processes.
We're going to use a model called the
fork-exec model.
So fork creates a copy of the current
process.
And execve replaces the current process
code and address space with a code for a
different program, okay?
So, fork creates a copy an entire copy of
the current process and execve replaces
the codes and the data, and the address
space of the process with the code for a
different program, okay?
And these two servers are, we call system
calls, okay?
System calls is part of the operating
system programming interface, okay?
So note that we going to be doing what
Linux provides here, and Windows is
slightly different, but the concepts are
essentially the same, okay?
So, and there are other system calls to
do process management.
For example, there's getpid, which
returns an ID of the current process, the
process that's calling the system call.
This exit, which just kills and, and ends
a process.
And there's wait and waitpid which is
used to synchronize processes running,
okay?
So, let's see what fork does in more
detail.
So, it creates new processes and when you
call fork, fork takes no parameters and
it returns this PID type.
Essentially, it's just the idea that the
process was newly create that was newly
created, okay?
So, when you call, when a person, say
that we have this example here, you can
call fork.
And what fork does is it creates a new
process that's identical to the calling
process, called the parent process.
And we turned 0 to the child process.
And we turn, returns the child, the child
process idea to the parent process, okay?
I know that this can get a little
confusing because it kind of is.
and the reason is that fork is unique
because he called once, but it returns
twice.
Because when you call, when you're
calling, you have a processor in it, you
called fork, okay?
Now, we're going to replicate the process
and going to have two different processes
here.
This is the parent and this is the child.
It is going to return the process ID to
the parent and it's going to return 0 to
the child.
So in this example here, we call fork
here then, and then we check.
If the pid return is 0, that means that
this is going to be executing the child
and if not 0, that's going to be executed
in the parents, okay?
So, let me go to just one more level of
detail, okay?
Suppose that we have a process n, and
we're executing the piece of code we just
showed you, okay, when we call fork, it's
going to create a process called m, okay?
And they run the same program, alright?
Because it's an entire replica byte for
byte, and, and the entire state[UNKNOWN]
but now they are both running at the same
time, okay?
So now process n continues executing.
So, it's going to relieve pid, right?
The pid is going to recieve M which is
the, the ID of, of the child process,
okay?
And now the child process is running and
it's going to receive 0, okay?
Great, so now since process n received m,
it's going to execute this side
of[UNKNOWN] okay?
And this one's going to execute as well,
so you're going to see hello from child,
okay?
Pretty cool, isn't it?
So which one is first?
Well, we definitely know that this fork
here executed before anything else, but
which one is first?
Well, even, as soon as the child process
is creating, it could be anything, it's
non-deterministic really, okay,?
So, one could be executing.
But the only thing we know is that
processing up to the point where the fork
is executed process and executed first.
So so, the example things to remember
that the parent and child both run the
same code, okay?
So, when we distinguish the child by the
value returned from fork, okay?
So, which one runs after the fork, as I
said before, is undefined, it's
non-deterministic, okay?
It has to be one of them.
But you might run multiple times, you
might see different things, okay?
So, the one thing to note here is that
they both start the same state but after
the fork, they both have a private copy
of the state, okay?
Same variables, same call stack, same
files descriptors and so on.
But then, after the point, they can
diverge the execution, right?
Because that is what's going to make this
interesting.
And we are going to see how to use that
in the next video.
Wyszukiwarka
Podobne podstrony:
03 Skutki malpiego procesu (2007)03 ulotka new age03 procesywatki (2)Old Process, New Technology Modern MokumeCreative Writing New York Times Essay Collection Writers On Writing2009 03 Parallel Thinking Optimizing Bash Scripts for Multi Core Processors03 modelowanie procesu03 Przebieg procesu technologicznego i kwas mlekowy2005 03 A New Look[FFF] Highschool DxD NEW 03 [807B900B]03 Procesor i jego architekturaThe Visual Process Method A New Method to Study Physical AttractivenessWyroki w procesie o przemyt silników do Iraku utrzymane (03 02 2009)03 Wykonywanie czynności związanych z procesem sprzedażyidE38863 03new 4więcej podobnych podstron