Hello. This is Dr Miller and this is Episode 14.14 of Assembly.
Today. We're going to write a system call when we're going to use into 80 in order to do it, and we're going to use both fork and wait
so four can wait. System calls.
What's our problem? To be solved. So we'd like to use fork system called to create two processes.
And then what the child is going to do is it's going to sleep for a while, and then the parent is gonna wait for the child, Take exit
on, then then each of these processes is going to print right before it finishes.
So a couple of interesting places you can look so you can look and understand fork and understand what it does. So fork allows you to create two different processes.
The i d for the parent is the child and the i d for the child is zero.
And then we also have weight,
and so weight is gonna wait for a process or a P i d. In order to finish,
we'll be able to see that it takes three parameters, and we're gonna have to set up those parameters when we make our call.
so we'll get ready for our print statements.
So inside of here, we're gonna have the child's
and then we'll have the parent
putting our new lines and our Knowles inside of there, and then we're gonna print off our child's I d.
We're gonna print a number. So it's got a percent D again, put a new line
and is your on there.
And then inside here, we have
print F that's external, and then we're actually going to use the sleep function.
so the very first thing that we're going to do is we're going to make our call to fork also create a couple labels. So I got my child.
And so both the child
we'll jump to done so that they don't do that code.
All right, so if we look up the system, call number four fork. That is the number two. So we're gonna go ahead and move into E X,
We're then going to just call into 80 so fork doesn't take any arguments. And so we can just go ahead and call
in 80. And then what we're gonna do is we're gonna look at the value for T a X, so the A X will either have the p i D. Or it'll have zero. So go ahead and compare that
So jump if it's not zero to our parent
and then jump zero or you could just to jump to the child or additionally because it's right after here
we will get a fall through.
So on our child, what we'd like to do is we'd like to do I sleep first and then we will go ahead and exit in this league just shows us
that the parent is, in fact, waiting for the child. So
I'm gonna push the number three for three seconds. Call sleep.
So it allows us to sleep for three seconds Ad yes, p
four to correct our stack,
Then we're going to move into the x r C, which is that string above
string to print off that string. And then we've got our call to jump and done.
All right, so the parent has a little more work to do um
so here, we're going to try and print off that child I d So that we know that the parent is we're ready for that child to go, So push X.
So that is the P i. D. Right that we got from up here. You did that comparison on. So would you go ahead and push that on the stack
push yaks. So push our format,
then call print off.
So you can print that.
I'll go ahead and correct our stack.
So we got two parameters on, there's We'll go ahead and add eat
and actually will go ahead and do four. And then
so if I take off four, I'll get off that print f argument, and then I want to get back that p i d. That I need to wait for
and so I'm going to go ahead and pop
e v X. So I'm gonna save that I d into yea X cause I'm gonna actually need it.
So when we call Wait.
This is the p i d to wait for
Okay. And then I'm gonna allocate on the stack for our,
the pointer that it needs. So it needs a pointer to, um store the information about the p i. D. And so we're just going to subtract a local very Blair So sub
Well, then go ahead and calculate that stacker. So sack pointer to movie, See Ex?
um and then we're gonna have to set our options again. All of our elements are going to be inside of registers, so our options for that coal are going to be zero, so we don't have any options,
and then we again have to look up the function for weight. P i d. So that I d Which is going to go in E x is the number seven slat is weight P i d.
And now we're ready to go ahead and, um, do our system call and use int 80
And then we pushed on a couple things to the stack. So add yes, p eight.
Um and then we're gonna go ahead and print that were done, so we'll go ahead and move into yea x p, which is that string at the top
so as we can see it printed off the child's p I. D. Um, the parent waited for the child, and then when the child finished it print that it printed that it was done, and then the parents was waiting for that. And so when we go into the assembly,
So the the act of waiting on a P i d will not return until that child has terminated. And so the parents will be waiting here on this line after it's called this
in order to execute, because it doesn't have,
the P I d for the child. So it waits until that child finishes, and then it can continue executing. So that's why we saw a delay.
And again, if we ran it again, we would see a new child. P i d.
It would wait for the child to be done approximately three seconds.
Um, and then the child would finish, and then the parent would finish.
So today we talked about using system calls, and we did both a fork and a weight.
So looking forward, we're gonna talk about strings and see and see how they are in assembly.
If you have questions, you can email me, Miller, MJ at you and Katie to you, and you can find me on Twitter at my house 30.