Lucy Kim during lecture. Okay.
Huh.
Could principal set up a second webcam, that would be funny.
Hey, let's get started here.
Like, recording on Christmas on.
recording on Christmas on.
Okay. Good morning. So we're, we're now recording, what's going to start talking so first off, case you didn't notice assignment to is out.
So, what assignment to is, is It's really just a remix of 3000 shell.
There's, there's not much in the way of new concepts, the one thing that Oh, slightly new is I made a little shell script, you're going to use a shell script, just to see things because
they'll try it out. I I'll demonstrate this, you know, demonstrate, well need demonstrating, you play with this run these commands, these questions but, you know, I'll be talking about the concepts today.
So,
I want to talk about some things that have kind of in the background but I don't think I put it all together for you.
A question 11 of tutorial three.
Oh question 11 of tutorial three. Let's check to see how that fits into what I was planning on discussing. Okay, cool. Let's move you down a little bit. No, no, no.
resizing Windows will not quite ready for that.
You go to tutorial three.
Question 11.
Ah.
Oh, the only thing that's finalized, is I haven't made solutions. Okay, let's say so.
Okay, so I'm not planning on making any changes, I think I've got the whole assignment I've asked all I've done is asked.
Okay, so let's talk about okay so we got a couple of questions here for asking me to talk about tutorial three.
So let's see.
Oh yes.
So we're going to we're going to address that and we'll be addressing this one only for this was actually a less number 11 that I want. I'm actually going to go through the material of this tutorial in more depth.
Today, but I'm from a slightly different perspective. So what's this make p list output the parent process ID for each process. So the question is, is how do you find out the parent process it.
Well, I'm going to talk this one things that we're talking about but when you go into slash proc, what's going to work with.
So if you look at this, you can always see, if you look at this file for any process, it tells you a process ideas here, and always have the PP ID. That's the parent process ID.
So all you have to do in order to answer a question 11 in the tutorial is you have to read this file for the process and extract this field, which, which should be pretty straightforward.
So, let's.
But I want to start to talk.
Let's talk at the top here.
And it comes down to what is this.
What is this program to it.
And really, in order to understand what this program is doing. You have to think about what is a processes, lifestyle.
So,
whatnot my life cycle.
So, what is the process life cycle, where does a process start.
Hmm. What's the first thing that happens. So first is you know process is born.
This is via a fork.
So process for fourth executive asked to start with an existing of process.
I'm missing foreclosure conception Think of it as fork.
Close this is closed with certain arguments.
So that's a question. Does it have to be No. Is this the only way you can create a fork What if you just run it as an executable.
That does not create a process so this is this is the key.
So, a processes board with forks, it has to start with existing process when I did the.
So.
Yes Rocio you complaining.
So, if we do a ps3.
I'll make this a little bigger.
Because then you will see more.
If you do ups tree.
So if you're a ps3, you can see the relationships. all these are parent versus child.
There's a couple of abbreviations in here like I think if there's like two or more or less what, you know, this one these two things like they've got a bunch of ones underneath it, but you can see that system D is serving as the internet, it's the first
process. And then it's started up all these things and then there's a GNOME Desktop manager was started oh it's the session. Then there's your here's like system D has Firefox, and my gnome session notes all the all the stuff that's running on my desktop.
We can see here and just how they're related to lots of things so.
Yeah, yeah.
So, so if you don't use for executive files which are we are actually making a child process from ultra. Exactly.
I'll come there to system deeds.
There's so there's the initial system D and the way system is set up is when you log in, and a graphical session what you're actually asking, yep, you have to ask part of system D to do stuff system D is complicated.
It's kind of a new way of doing things, the old way was this older net which was much simpler and system includes a lot of other things but I think overall system be as good but it's complicated.
We'll talk more it'll come up more when we talk about how you log in, and and stuff like that but first, but the but the main thing now is just focus on.
There's a whole lot of process here is currently like why are there so many copies of these things. Some of these are actually so in kernel level threads of the things because basically everything.
And, you know, that's another thing we'll, we'll talk about but but there's a lot of stuff going on here but I want you to remember here now is a process comes from another process.
There's no such thing as a fresh process.
So well so okay so you have to start with that so.
So, yes, so system D is kind of the opposite he's basically the god process. Yes, it's in charge of everything else system he's a bit more than that's a lot of things but yeah that system D is like is that at the top of the hierarchy.
Okay, so there is a first one.
So anytime you use for clone that process will appear in ps3. Exactly. That's exactly why I'm.
If you watch I pseudo kill one yes but here, your computer is going to reboot Well, you have to have proper privileges.
Now,
so.
so, so what this is saying is, when you have, so if you will get your process tree here there's a lot of stuff here.
What happens if one of these prophecies in the middle terminates what happens to the children I mean normally you think well gay there now just sort of disconnected from the tree.
You cannot have a disconnected sub tree in a Unix system, all processes have to be part of the process hierarchy. They have to fit in somewhere. So those, they have those, those leaf nodes have to be repaired.
They have to be put somewhere in the hierarchy and there's normally one. So, I think I haven't actually check this. I think it is but I'm pretty sure the, the system D session that's like this, like this system D, I think, ends up becoming the parent
of any process that we do for that child. Traditionally, if you orphan the process it will go straight up to the main system D ID. This will call the net.
But nowadays they go to this one so they're kind of stick with the user.
So, this is a.
So this this top given if you try messing it up, you're going to have this hierarchy.
And the reason up.
So when a process terminates.
No. Note that we have process terminates.
You know what is. So let's look at any program. Like any of these programs here, it's always it main your main always returns and it.
That's a Unix ism. That is because every process is like a function it has a return value.
And, you know, traditionally if you return zero from a process, then it means everything went fine if you return a non zero value it means something went wrong.
So, so that's.
Someone has to get that value.
So, if there is an apparent process to retrieve that value, then someone else has to take over those duties, so someone else becomes fair basically has to be adopted.
Um, how does the parent process. Get the return value of the child is one specific system call.
Do y'all remember what system called that is. Wait. Exactly. Wait.
So that's why, if you look here in the signal handler.
You know, so if it's a. If it's sick child they also here, if we receive sick child.
Remember signals, our messages.
So, signals a small message.
So, this is so this this part is very important for understanding the code here, when a process terminates, it returns a value that value, the colonel holds on to that value and sends a sick child to the parent saying hey your child died.
And so when the parent calls weight is then given that return value.
Yes, very far, yes I sir.
So that's what's happening in the signal handler is we're getting when the signal handler by this when we say, it didn't read what so we say if sick up, we got to figure out if it has any single but sick child we say, what are you doing, so here we can
The signal handler here is.
Now that the signal here is sick child. So, a child died. So, let's call wait. And when we call wait with Stata, you know, give it a basically a pointer to status and the PhD so PhD tells us what process.
What child I could be could have multiple children. We know that the processes and then we have it status and the status thing is actually not exactly what the return value was is the return value plus some other stuff.
And so you have to use these macros to get exactly what about you know like the actual status is encoded in like w exit status like gives us that they're basically certain bits from it.
I think you only get a bit so return value.
And the rest of the bits are used by the system.
Yeah no so so it doesn't go straight to the parent. And in fact, So this is.
So the killer process that has children know
has.
Yeah, I suspect the terminology of Unix is going to need to change because this whole Parent Child thing is just kind of brutal, but it comes, but it's part of a larger thing and it's not like unit came up with this, it's a tree.
When we talk about trees we taught nodes and trees talk parent and child and all that stuff so I can't blame unit for that but the problem is when you put it into the context of processes you have a notion of creating and destroying which is like life
and death and then you have a parent and children, it just gets messy, especially when we get to this part.
Um,
yeah he checks. Yeah, get get changed master yeah so.
So if a child terminates who gets knowledge of the PhD of the child as terminate the parent does when it calls wait.
Ah.
But, um, yeah these are masters the Masters things a lot more problematic heuristic more, I think, weird. But, um, but anyway so what is the parent doesn't call to call wait.
So the current the current.
So,
so this is when we say a zombie process. It's really just saying that you have your list of processes, and there's this list of business processes in there that has terminated.
But the colonel can't clean it up, because it's waiting for the parent process to call wait so can get the value back so it's just sort of sitting there in the process table and you can't get rid of it because it's already terminated so you have to send
the signal and it won't do anything, so it's just there. So, it's a dead process that yet is somehow still alive because it's an entry in it. Hence, a zombie in fact.
Yes, so yeah so the parent never calls Wait, this up.
So the question will keep running within the ground. Yes. What will keep running as a zombie until the person they killed so notice a zombie doesn't.
So,
So a process can be in many states and.
Let's see.
Okay here process state codes. So this shows you the states that a process can be.
So, a process can be a default can be in the zombie state and process can be dead should see that interrupted asleep running a reasonable. In the kernel thread so we will talk much more about all these things because they personally require understanding
what the Colonel's doing and scheduling of things we will talk about that but what we're writing to saying is it normally processes should either be running or some casinos Rs, or D.
Those are kinds of things you'd normally see a processing, it's either going to be running or waiting to run because we have many more processes, then cores and only one process can run on a course at a time so they have to take turns.
And that's one of the key jobs, the colonel is deciding which through process can run on which for.
Yeah.
For a good way mess of a computer for a bunch of children never call which they take a lot of rooms occur. So it turns out the some of the processes do take upstate definitely take up a huge amount of state.
The main problem is there just
in principle.
you had thousands and thousands of zombie process because you had some parent process of creating all these children process. They're not terminating them you could get into a situation where the process table was filmed.
And you couldn't create any new prophecies because they're all these zombies.
That's, but otherwise it doesn't really take up. There's not much in the way of resources don't like it takes that much memory.
So why would the colonel kill the child college, so.
So question is why.
So, one basic idea of operating system design is the colonel tries to implement mechanism, not policy.
And the reason of IT folks on employing mechanism rather than policy is because policy change people want to do different things with system and so make it flexible This is sometimes a bit of a cop out but it's.
But it's a thing so.
So, really, that's, that's up to the user space.
Okay. So we catch up here on this so.
So no matter what happens when a parent is killed his child dies as well, no no no no no.
So.
So one question is Unix really is a multitasking kind of system.
So what I'm going to do right now, let's do a little test, and I'm not sure actually what this is going to happen here so I'm kind of curious.
Well no, this is too big. Let's make a new window here.
First of all, first I'm gonna catch up first.
Cesare across happen.
So always I have to explain sleeping.
Yeah, assign the child to use specific subsystem exactly that's what happens with it but let's, let's, I'm going to try some which I, I'm not sure what it's going to do so let's.
Okay.
Okay. I'm now logged into. So this is the this is my VM here.
Now, I'm going to do a.
Let's make a, I'm gonna make a shell script.
Okay so,
so I'm making a shell script. So what's the one cool thing about Unix is executable don't just have to be
binary files, they can also be scripts. And the idea here is the first line says what program is going to interpret it. So this could be Node. js, this could be Python, this could be any, any sort of language that's willing to take, you know, a text file
input, and then run an interpreter on it so here we're making a shell script is just a program for bash.
Oh, you didn't say how can I watch, I should show you how to get rid of the password. Yeah.
I don't want to do that right now, I will remind me and I will post something to the forums because I have extra time at the end I'll show you that but I got into this here for so let's.
So let's see now let's do for.
So I. It's a programming language we have,
we have, we have shell scripts.
We have four loops. So one cool thing is there's this program 110 sequence is a program that just output sequence of numbers.
So now, one 1000.
Okay, so, So I've now made this little thing called.
What do I call it yeah so this is a little shell script which will just print out numbers.
I'll make it executable bigger for you all.
Now, it's just going to sleep for one second, and then keep printing out numbers and it will do this up to entice know zero indexing.
Nice no zero indexing. Well it's, um, I could have said,
I could have made it zero.
So, yeah. Well,
I'll go over that yes I will, I will go over, so I'm still talking about tutorial three and this is all part of it.
There's also stuff I want to address I'm not. I am coming back here but so this thing right here now this do stuff.
Now if I output it out, I said all of its output to.
And I put an ampersand on this. So now what I've done is, so if I just do this
is just going to walk here, and I'm going to sit here for 1000 seconds, because it's going to take a long time to run and I'm going like you know that's kind of annoying I don't want to sit here while it's going on.
I want to go do other stuff. So I can hit control See, I can do stuff.
And we can see that they got 16 seconds in and then terminated. But now, if I do an ampersand at the end. That makes it go into the background, so all that means is, instead of waiting.
And so this all has to do with semantics of weight.
But they first hear us now. I got this job running.
So I'm going to exit.
I want to see now.
Oh good.
It's still running.
So,
okay so let's see I explain it so.
So notice it's running still. So I tell my staff means follow it so it's still running.
But now I log out I no longer connected the computers there, but
that process is
to two documents.
It's still running.
So this is a background process.
So,
so what you saw my doing an ampersand you're busy telling. Don't call Wait, immediately because we call the way to immediately you're going to be waiting on the.
You're going to be waiting on the process, and it's going to be.
It just going to sit there and do nothing. But if we put it that it will now, just go on to prompt you can type another command, and thus we have the situation where, you know, as a background and background prophecies.
Okay, so.
Okay, so what's this okay oh yes I've got 200% okay so.
So a common thing you want to do if you want to send all output to a file.
So, this app. This app ampersand yes I it's confusing because you also use ampersand for background but if you kind of remember is a mnemonic because.
Okay, so.
Okay, there you go so
yes control C control these two files so right think
so yeah we're getting to get into signals in a second. So, so these are these are ways of, you know, this is what you want to do and.
So, because if I didn't run, like if I had just run this in the background.
Let me put this way, let's let's all do. Okay, so let's waste all right. Okay, so how do I.
So I can find it there let's get terminated by killing it 6152.
If not go gone is terminated.
Okay so,
so, so these are so now I've done that you notice it isn't running anymore. But now what if I just typed.
Let's say I just typed this here.
But I put it in the background.
So now what's happening is, I'm seeing the mixing.
So, it's running in the background but it's sending its output to standard out
after on top. It's still, but it's still going to be keep messing it up.
But they're, they're both competing for things. So it's very annoying now let's say I just log out.
So I'm out.
And now I log back in.
Let's see now
really kept it running. It didn't terminate. Haha.
I thought that was gonna die. No, no okay so things or smoke so
no no it's oh no it's sleeping. Oh, you're right it is sleeping.
I didn't see whether. Well,
it's still there. I'm wondering if it lost access to terminal and is just waiting to write or no that'd be kind of interesting to investigate if it's still I mean, I think it's blocked on the terminal, because you've logged out and so the terminal went
away and so connections there, I think it might die eventually. But yeah,
The moral of the story boys kill your children.
Can you say watch I don't use Walker.
I don't actually use watch very often.
Oh, watch executed periodically okay that's a.
You can do that.
That's fine, that that's a nose that's a different kind of thing.
Does it get tourney when you exit the show so I did exit the show. That was the issue I'm talking about here, so I have.
So now I'm going to know how am I logging out, so I can log out by typing exits.
But actually all I did was I typed control D default typically to log out because control D.
So now I can take control D. I've logged out when I logged out that bash died. So actually, I'm now curious who.
So it's still running.
Okay, so
where is.
Where is do stuff.
Oh, there it is. So look at that do stuff is right there, which is running sleep, and it is now a child of system D.
I did kill the previous run. This is a new run, but it's still there I log out log back in. So once there once it's a child of system D.
So, let's see.
So we see now, it's yet it's still there. And so,
so let's kill I killed it now.
Now it's gone.
So we talked about background processes as demons demons, I think Damon I think because da mo one is number one is and so what is a mythological usages a demon is someone who does things for you on your class like a magical creature so we do a ps3.
And you look here, you'll notice that there's all these prophecies running in the background. These and you notice most, many of them, not all of them Crohn's kind of to old things but yeah but a network the whole kit dr CES log the snap the SSH the nurse,
those are all demons at D they're all running in the background system D system demon.
So, so yeah so that's that's that's all this, that's all the background stuff so.
So the classic way you make a demon is you fork off a child that will then fork off another child, start the program, and then that child you created terminates, so now it has no parent.
And so it gets repaired into the system D, and now it's a background process
P it says team has profits okay so okay so.
Okay.
What you can kill it.
You might kill it. But I'm.
so, so this is a cool thing about background process and one thing to remember if you're trying to terminate things is this, if system D has a rule, saying that this process should always be running.
If you terminate it, it will die.
But then system D will respond it.
Why do you need. So.
that's one of the key functions of user IDs and profit Unix, is that you normally can only send signals to your own processes, because they're yours in route can send a signal to any process.
So if a demon may be running his route or maybe running as another user, so you may have to become route to do it but really what you should be doing is instead of trying to directly kill it.
You should be telling system D to start it or stop it.
If it were just repairing to the system the system be what we started because it didn't start. Exactly.
Yes, you've got, you know, you don't need, you don't need root to start a demon. I'm saying is you can have demons, I could have a demon running the background as the user student.
And that is a perfectly legit.
So you can definitely start up a network server.
But you can't start on any port.
So well that's a separate thing that we'll talk about later but you can't do that you know you can so multiple us can be on the same system in different accounts.
So, the entire point of Unix is to be.
So the root user is just another account. It's like the student account is like any other The only difference is the colonel listens to it, and does things that route.
Ask for it's like you want to send a signal there sure you want to kill it. You want to start something sure you want to not a lot of things we haven't talked about but route can do anything, because the colonel.
Otherwise, if the colonel wasn't listening to it.
Does root get priority when it comes to scheduling potentially because otherwise you'd be at the root is like admin on users.
It's the same concept.
So yeah, the root user is just a user so
yes a for Mac OS is limited so so the route you can also kick off other accounts are logged in, right, the root user can do almost anything.
So,
okay so so yeah so understanding when you look at a Unix system.
Use have this user column.
And this says who the who these users are and you'll notice that the lots of the background stuff is lots of root stuff.
But then there's some stuff that is.
Here's my students stuff.
But if I look at like say my system here.
You'll notice that lots of stuff is rude to background stuff.
But some stuff like system D has its own user for stuff, you know, a va he like there's like this other user here, this, I'm
the CES log user, You know,
God clue. Wow.
The website user.
OK, RT kit user, you know, there's, there's a whole lot of users on a system.
Anyway, so little bit off track when he's make sure what we're talking about here, I'm tall process hiking or fans will be talking about signals signals or messages we talked about foreground background prophecies.
I'm not too far, or less what the key system calls of the life cycle that's. Yeah.
can root access other users stuff.
Normally, yes, unless the power has been limited.
So a user account to sign will we will talk about but yes Etsy password shows your your users.
So let's go back to the life cycle here.
Okay so where's my life cycle so where's so so so it's born.
Okay.
Process life cycle.
runs a program by.
So you do a fork in road road program with exactly.
And then you.
This is the basic thing that a process you forget you do an executive you to load a program. and then it terminates with an exit.
Okay,
same signal.
This is all the process of what happens with the signal it's all
signals
are there we go.
That's what signal SIG killin six stops I was right.
So, posses get these messages.
And these messages mostly you can register handler.
event handlers Yes.
Sing killers default
signal.
It doesn't. So by default it does not do kill dash nine.
Kill that kill, kill.
Okay.
There we go, there kill.
So I do as I said, kill, kill 71 three, but I had to type it while it was entering with the thing so.
Ah.
yeah so control C.
is p kill any different.
What is PKL.
Oh, yeah, it's just it's just researching it IP kill is just searching it for a name for a pattern. So, as opposed to it. So yeah, those are still sending the same signals again you can verify whether they're sending the same signals or not based on
know based on what's, you know what's happening,
or check to see the tutorial questions on so think of.
So I covered I think 11.
Ah.
So let's talk about this.
What line is a six to seven.
So now we're.
What's going on here with this
lady asked that question.
So why should you use the term first, is it safer.
So that's basically if you do a normal termination with sick term.
You can do clean up you can save data and stuff but if you do say kill you're telling it die die die. and it doesn't get a chance to do anything.
So, you know, you might lose data.
So, yeah, so, so okay so um so we can set up your own signal handlers Yes, in fact, the entire point of much of this code is, oh, here's the signal handler.
And what do we do in main is we this is code to set the signal. Now, there are older ways of setting signal handlers that are much friendlier, but they aren't so standard.
And so instead what got standardized are these much more general ways of saying signal handlers, which are kind of broke and in order to set them I have to always look them up and go like what do I do what I put in, you know, like it's a, it's a, It's
a bit complicated and API for if you have only simple uses.
But that's what I'm doing here is I'm creating a struct I'm Ziering it out.
And then, I'm, I saw I'm initializing it, you know, I initialize the structure, and then I'm registering the signal handler here.
So I'm registering I'm rushing a single handle for sick child, and for SIG hop, I'm giving it the address of the same function. So the same function will be called when either signal, it you know it's called.
So you have to set SIG option for each signal and main and a handler for each of them and signal handler. Yes, or you can create a completely separate function, then signal handler like you could make a complete different one it doesn't have to be the
same function but if you're going to use a different function.
You would have to create a separate structure. So you could make multiple structures or changes.
You know multiple structures, and then
defined different functions to handle them or you can just go into single handler and add additional conditions on what the signal is and have a handle on them.
Both are fine.
So, so we got a good, so so what's happening in here then is, it's calling here, it just runs through says what single you got was the last one out of the singles you could add them here arbitrarily, but I'd probably add them in right here before sick
child because the rest of the code kind of seems this now what's this P equals minus one, it means that weight, so.
Okay.
So this is, it's important to understand the possibility of what wait does. And that's what all is happening in the code here.
So just return to the state of the child process that to me along with the pod are only state so the state of the.
Basically you, you get a you always get the PhD, and you get, you always get the PhD, and you get the state that's just part of the API. Wait, returns a PhD, and you get the status, you get basically you're given a status, you know, integer, and it fills
it in. That's, that's the API now.
So remember, signal handlers run on the main thread.
So,
so, So, so singles, are.
So signals are very primitive form of concurrency and this is one thing I wanted to point out, and actually you can see this, if we go to assignment to.
If you look at its design.
You'll notice that the signal handler is very simple.
And let me I can actually run this real quick. we've got a little bit of time here.
So,
I'll do my local machine. Okay, so.
Okay, so.
So if I run 3000 captures if. No,
No countdown.
So if I run this.
Then I will.
Why don't want to run this.
So, so if you just run this like this.
Oh yeah, this is what's going to happen is you have this waiting thing happen.
So it's this weights. The countdown runs for 10 seconds, and then it goes so now How did it do this the good countdown is the shell script it just loops from 10 to one, and then Prince blast off.
And if we look at that wasn't captured about log, which is where things go by default, you'll see that you have the output whereas 10 days of the blast often so that's what happened but notice what's happening here is you have this little, you know, it's
waiting for things to happen.
How is that waiting happening is we've got this.
So the child is sitting here. So the child of course starts the process so that's what starts the countdown main does all this but the parent here is waiting for the, for the countdown thing to run.
And while it's running it has this loop in this loop is wild child not exited sleep and write a period so it just keeps writing this period here and just does this until it's done.
And then it returns the status of what the child did.
Note that is not calling.
Wait here.
It doesn't call Wait here. But what we're white does get called as the signal handler.
So what it does is if the ceiling is like, if you get a sick child, it does it here. Why don't I print here.
The reason I don't print years because doing any sort of IO or anything in a signal handler is inherently dangerous.
You really shouldn't do it.
What you shouldn't do is single hand lotion sets that because it's running who knows that what context. So here I just set a global variable to the fact of the child exit and I put the status here, and I'm, you know, I check it to value and when its values
done, then I go through this and I sleep while, while not doing this, but the single handler just changes some state does as little as possible before running.
Okay.
Also the pro will only start printing when the chance to me, no no no no but this one prints.
So if I do this, if I do
it terminates almost immediately.
Oh, also the problem is different with a childhood oh if you print the print in the hell yes yes if you put it in the handler it would only start doing that but you could have it somehow.
Exit or it could print its messages like all these messages here, you could print those in the signal handler.
But that is not the way you normally want to do it, because net here notice we're doing right system calls. Watch we're here, we're building a buffer, and then we're doing the right system call here.
Yeah, you don't. Don't do that. Don't do that. So this is more how you should have a single hand should be doing as little as possible. Anyway, I'm over time.
I will stop this and we will.