Just like I covered, the system calls for foreign manipulation, I will now cover some of the main
process related system calls.
There are four main system calls that I'll be covering in this lesson.
The first one is the fourth system called next is the exact the exit.
And lastly, the wait system call to get started with the fork system called this system called creates
a child process basically a new process that's created that is an exact copy of the process that is
making the system call.
But now it runs as a new process.
This process that is created does not start at the beginning of what is copied over, but it starts
at the exact point of where the fork system call left off.
I will talk more about this in the next slide after the fork.
The parent any child process have identical memory, and as you can see, it returns an integer that
is the process ID of the due process.
So this is a little bit better of a diagram to demonstrate the fork system called.
On this side, we have the initial program that you would write and you would normally just write it
sequentially.
So here is it executing sequentially.
It goes this way, and once it gets into the fourth system call, two processes are then created or
this is more of the original one.
It's not recreated, but the creates a child process.
But for now, let's cover the parent process.
As you can see, the parent process is assigned an ID.
For now, we're just going to say the parent process ID is two five four six.
It copied over to the exact same copy to the parent process is executing at the exact same point that
the system call with that.
Likewise, the child process has a process idea of zero.
The children always have a process idea of zero.
And as you can see, it is also executing right at the point of where the first system call took place.
It is the exact same process, with the exact same memory running from the exact same spot, except
now it's a child process.
The next system call is the exact executive.
This basically loads a program into a process address space.
The program is loaded in the calling process, address space and the old program is over and no longer
exist.
It takes in the program name.
This is the program that you want to run in the process, and it also takes in a list of arguments which
could be an array of strings for the arguments of the program.
You are loading into the new process.
And as we've seen before, it returns an integer.
If it's a negative one, that means it's filled.
If not, it's worked fine so very quickly.
The diagram again on exactly we have the process executing sequentially, right when it gets to the
point in the code where it has an exact V system, call it splits again.
It creates a new process called in this case, is just the new child, and it has the idea of zero and
it has its own process, which is the parent.
It is assigned a parent ID of two five four six.
As you can see, the parent process is still executing a right where it left off after the exact mean
command.
However, the new process in green.
Copies in the new program that was a parameter, a copies in the program name right there into the new
process memory straight space.
So as you can see, it starts running from the top again.
It overrides all the old process that was originally there.
This is how you basically launch a specific program.
The next system call is the exit system call.
This causes a normal process termination.
It's basically just causing a process to exit, and it returns the code, which is the return code to
the parent process.
And there are certain things that you can do with that.
And I'll be sure to cover that.
And lastly, we have the wait system call the wait system called Waits for a process completion.
It is basically a calling process to wait until any process created by the call and process exits.
I know that was kind of confusing, but it's basically waiting for any of the children to end and windows,
and then it can continue with the parent code and working its way down the sequential diagram again.
And this will be returning the process ID of the process that executed.
And now we're going to head over to my VM to look at some process examples using the fork system call.
And we'll also be looking at the other system call center, such as wait and exit.
OK, the first example that I want to show you is just a simple fork program, and this will show you
the parent child relationship.
So to get started, we will just begin with the fort system call, and usually what you do is just create
a variable called pad, which is the process ID.
And you will then do your command.
Now that we have the fort system, call and vote to show you the how it's interacts and I can go through
more right when it's Fort A returns process ID.
If that since this is creating two separate processes, we can do a quick check to see what the process
ID is.
So if the process ID is zero, then can't take if it is zero.
So if it is zero, then we could print hello from the child.
And let's just do a quick print to process it.
And while some print the parent ID just so we can see a little bit better.
And let's just do a new line.
And there are some helper functions that we can use to get the actual child I.D. or process ID.
And we can do that by using the function call Jeopardy.
And also get paid, which is get the parent process ID.
And I think that's one two and that's good.
And for now, we're going to hear about that else.
Process ID is not a zero, which means it is a parent.
Hello.
Hello from the parent party.
Child, Paddy.
And just like we've done before, you can now do get paid.
And also did to actually verify put the pad is.
This is just a really quick test.
However, something that we do need to include is the wait system call, which we can go over really
quick and show you how that will work and what you can do, not because we don't care at this moment.
So this process idea will go into this section of the code.
This is the child process that will run and everything after that if the process is not a zero, the
parent process.
So for now, I'm going to save this do a quick GCSE simple fork.
Looks like there is an air for the Earth, so it looks like a bat, and let's compile it again clear
and let's run it.
Let's see what the output we have from our program is so hell from a child.
Thirty six, ninety five with a parent ID of thirty six ninety four.
And as you can see, the parent process is printing the process of itself, which is nine three six
nine four which matches this parents I.D. And the child process has a pad of three six nine five.
And from the parents perspective, it says that the child process is three six nine five.
So what it is doing, it is executing this program from the top going to this fork system call.
And it is then checking the iPad.
So in the parent process, it has a process ID of three six nine four.
So which means that it skips this part and goes over here.
And this is now waiting for the child process to finish executing.
Once the child process prints out this information, which is this information, then it moves on to
executing its code from the right where the fork took place and the parent skips this part and enters
into here.
It's a little bit confusing, but if you think back to that diagram, I had the slide.
It's basically copying over the exact same program, but looking at the return value of the fork.
Children return zero so you can do whatever program you wanted you here and any code you want relating
to the parent you can throw here.
Something to visualize this better.
We can throw in a sleep for 10 seconds.
System call, which will cause this child process to wait 10 seconds after it executes its print statement.
So let's go ahead and compile our program.
But let's put in a new name, so we will be able to see it better when we're looking to the process.
So GTC simple before that show will just do a simple.
Oh, simple work.
And if we go ahead and run that program, simple fork and we've run a command post essay yesterday,
and as you can see, it is running the processes after the forest.
So these processes are three, seven, eight two and three seven eight three.
And as you can see it, matches the process items that are printed here and that if we go ahead and
do it again, you can see that they are no longer running three, seven, eight two and three seven
eight three.
So that's just a quick way.
We can do it just to verify that there are two processes have been created because we ran the command
when the process was still in execution before the exit statement took place.
And we have these two processes right here called Simple Fork, and these are the process ideas right
here.
Next, I want to show you how to use the Exec. V system, call for processes and just to get started,
I want to show you that I have to file so far my temp folder called Exact, for example, and Helloworld
Dot C.
So if we take a look into the Hello world underscored that C program, it is simply just doing a print
statement of Hello World.
What we're going to do is go ahead and compile this, and we're going to give it a name of hello underscore
world.
So now this is a binary version of the program Hello, world at sea.
And as you can see, when we've run it, it prints Hello World.
However, what I want to do is use a process that calls that program.
Instead of doing DashPass Helloworld, we can call a different program that will load Hello World into
its process, memory, address space and run it.
OK.
To start this, it is very simple to do, and we are basically just going to be using the fork system,
call the exact V system, call, wait and exit.
So we're actually going to be using all for the system calls that I've covered.
The first thing that we always do is do our fork system call for now.
So this is just a process ID.
And we're going to be stupid equals fork.
And from this point on, it creates the child process.
If the process ID is zero.
Then we are going to front our V system.
And as always, you should be checking these return values.
But just for simplicity, I'm going to just cover it and hope there's no errors.
So we're going to do our exactly and this is where you pass in the program name.
And as we stated before, the program name is called Hello Underscore World, so we can simply type
in Hello underscore world.
And it takes an argument, so we're going to put an argument value there, and we can basically just
create an empty character array.
But just, you know, one out of you and was put a zero in it.
And so it's just going to pass in the argument for now.
And after that is done, we want it to wait because we want to wait executing.
And normally you should check this value to make sure that depending on what the weight value returns.
So we're going to do weights, why?
And then as we're going to use these because these values don't really mean anything to us right now.
But these are going to be returning the return code.
Where's the ANZ?
And lastly, it will exit with a status of zero, as always.
So it's a very simple program.
So just running through it really quick again, we are establishing some general variables up here.
We are then walking our program.
And if the process had zero, which means it's the trial process.
So if we want, we can put a print out for a year child process, new line.
Once we do fork it, we get the processes that have returned, and if that process I devalue is a zero,
if that means the child process to running.
So we want to exact the the Hello World program, and that's the program that we always we already pre
compiled.
So our program is calling another program.
It is loading this program into its process, memory address space.
And then we are waiting for this program to finish executing because the wait system call will wait
for any children process system calls to finish executing.
So let's go ahead and see the exact example that's due, for example.
And as you can see, we have the ADA out.
We have the source files for the two programs and we have the Helloworld source code.
For now, let's just go ahead and run it out.
And as you can see it, prints child process and then Hello world.
And remember, our process for our program source right now does not contain anything.
Say hello, world exclamation mark.
Our program is simply calling this program in this process.
So we're using the exact VE system call to load a different program, and we're able to run programs
this way.
OK, now that we have a bass file and skeleton for launching a program in a child process, I want to
now show you how to implement JCC using system calls.
So what this will do is we will be able to run a program and it'll basically create another process
to execute the code from a file.
So just like how we've done this hello world program, there are actually programs in our user bin and
it's called in usr bin.
And in here there are a bunch of programs.
And as you can see, there is a program called GCSE that is a binary program that we can call to in
order to accomplish what we want.
And there's a lot of cool things that you can do with this.
But so we have this whole world that see which is just simply the Hello world, and we want our process
to compile that program.
So how can we create a program that compiles another see source file?
That's kind of weird, but I will go through it really quickly to get it back to let's get back to this.
So we first want to execute the program that we want.
The program is just located in usr bin sgfxi.
And as you can see, we are passing in arguments.
So what we need to do here is edit our arguments.
These arguments are in string form.
So for now this we are passing in the command that we want to send to the DCC.
So normally when we want to compile, let's say, Helloworld, I see we would have GCSE Hello World
and then Dash, Oh, hello world, something like that.
So what we can do is we can actually put that into an array form.
And I'm going to do it just formatted like this for now, but it doesn't matter how you do it.
So we're going to output it as Hello, world.
And lastly, we're going to do pass in the file name.
And last year you got zero, which I can go over that later, but for now, we'll just leave it at that.
We will be passing in five parameters one, two, three, four or five.
If the process ideas here, which means it's a child process, it will execute this program, which
is located in user Ben G6E and.
This is a separate process that will be running and the parent process will be sitting here waiting
for the child to finish executing.
It will be passing in the arguments into this program.
So this program is basically done now and we can go ahead and just run.
So this was just an example of how to implement GCW System Paul, the JCC Command.
And you can do this with any of the commands that you used to like you can.
If you're interested, you can implement like the LS Command, Move Command and stuff like that.
So before we get going, let me just show you that there are no binaries in our folder.
There are just two source files.
So what we can do is actually compile our GCC example, because that's the problem that we are on our
own.
And now you can see there's a binary for 8Ot out there's and there's the two source files.
But what's going to happen when we run out?
It is going to run through this for command and is going to execute this program and passing arguments,
which are these?
So this arguments are compiling the Helloworld at sea and it's naming it.
Hello, underscore world.
This is the name of the binary that we can run.
So we run it out, says child process, which is correct.
And then exactly.
And it ran with no errors.
Now if we do Alice, you can see that we now have a binary for Helloworld.
So this program was able to execute the GCC command in a child process.
And if we go ahead and run Hello world.
You can see that a prince hello world.
So that's a really cool example, and there's a lot you can do with this.
Like I said, this is a very basic course to begin with, but there are some complex things that we
can do and accomplish with this in a more immediate or advanced level course.