Let's get an intuitive idea of what a process is and what some of its properties are. I've opened up three windows here so that we can have multiple shell sessions. And if you're following along with the Macintosh and you want to do this yourself, I'm using the iTerminal program to get this nice windowed layout here.
And the program just prints out to the console the Hello World string. And right now this is just a text file. This is our program in text format. But it's not running, obviously. We're just looking at the text file. So what I want to do is over on the right, let's log in to our machine. And we're going to do that in both of these windows.
And up top, I'm going to change into the Vagrant folder so that we have access to these shared files. And what I want to do is to execute this text file. And at that point, the text file, the program will be loaded into a process and run on the operating system. So to do that, I can use the Ruby interpreter and I can type the short-lived.rb file. And it will run and then exit.
And so some of these programs, they will just run and exit right away. So you could call them short-lived programs. But what happened here was this was loaded up, this text file was loaded up into a process, and that process was run on the machine. And then it exited when it was complete.
Now when a process exists, it normally leaves a status code. And the status code will tell us whether the process exited successfully, or because of some kind of error. And in shell you can get access to that status code by echoing out dollar sign question mark.
And notice in this case the exit code is zero. And on Unixy systems, zero means that the process completed successfully. If it was one or something larger, it would mean that there was some kind of error and we could check the error code using other commands. We could check the error message using other commands.
So we've just run a short-lived program here using the Ruby interpreter. And it just executed this one line, and then it just quit. Let's try a longer-lived process, one that just keeps going. Now let's open up the long-lived file. So on the left I'll open up long-lived.rb.
And notice this is a little bit different, this code file. It's going to loop forever, so it just says while true. And then it'll print hey there to the console and then sleep for 5 seconds. And it just does that again and again until we kill the program. So over in the right, I can run that program again by typing Ruby and long-lived. And notice it just stays alive. It doesn't exit right away.
Next up, let me show you a program that we can use to see which processes are running on the operating system. It's a program called ps. And if you want to learn about all the different options just like with any other command on a Unix system, you can type man ps, and that will show you how to use the program and all of its various options.
So if we just type ps and no options, we're going to be able to see the processes that are running in the current Bash session for the logged in user. Notice that the processes that are running are Bash, because that's the shell that we're using currently, and ps, which is the command that we just typed up here.
And notice that each process gets a Process ID or PID for short. And the process ID is how the operating system uniquely identifies the process. Now if we want to see the process that's running up here in a different shell session, I can use an option called a. So I can just say ps a.
Now notice we're seeing a few more processes here, including the long-lived process or the long-lived code file that we executed up here. So this is the command that spawned the process, and this is the representation of the process. And we have a process ID here of 2662, which is the unique identifier for the process that's running up here that was given to this by the operating system.
Processes can form hierarchies or trees where there's a parent and a child. And those trees can be made visible to us by using another option to ps called the f option. And it stands for format. It's going to format the output a little bit differently so that we can see the hierarchy of processes.
Take a look at the one that we're running up here, and notice that it has a parent. And its parent is Bash And the Bash process has its own pid of 2611. So we've got a parent of Bash and a child of our Ruby program, and each of them has their own process and their own process ID.
Now, the reason that this process down here became a child of Bash is because we used Bash to run the program in the first place. So Bash, if you recall from the shell class, is the shell session that we're in up here. It's the program that's allowing us to type commands. And what it's done is it's gone to the operating system and on our behalf, asked it to run this program, this Ruby program on the left. And so that's why it's a child.
Now, if you were to kill the parent process by exiting out of it, for example, it will kill all the children processes as well. And to see that what we could do is just close out of this window up above, and that will exit out of the shell session. And let's try typing ps af again. And notice that it's completely gone. So the Bash session was killed, and also the child process was killed that was run from Bash. Now, there's another Bash process running here. But that's the one that we're running here in this window. So instead of having two Bash processes, now we only have one.
All right, I've brought us back to where we started. And let's recap what we've seen so far. We start off with a code file, which is just a text file like what I've got over on the left. And what we do is we ask the operating system to run this program. And in order to do that, it creates a new process, which you can think of as a data structure creative for us by the operating system. It loads our program and runs it in that process.
You see now, processes have a process ID which uniquely identifies it, and that they can have parents and children. And we've seen how a process can be short-lived, where it exits almost right away when it's done, or long-lived, where it just continues to run until we kill it. Next up, we're going to look at a process that can communicate through a network port.
So this just an example of an HTTP network program. And the process itself is going to be long-lived and listening on this port. So we're going to start it over on the right. And in order to do that, we need to install Node.js. So you can type sudo apt get install Node.js. And it's Node.js, not just node on Ubuntu systems. So just remember that.
And let's make sure it's installed by typing Node.js and using the version with flag. And sure enough, we have version zero 01025. So that's good enough for our purposes. Now let's start up our server by typing node http server, and now we have-- I always make this mistake because I'm so used to it being node, and not Node.js.
OK, now we've got a server running on local host 3,000. And if you're not convinced, you can come down to the lower window here and use curl to send an HTTP request to that service. So local host 3,000. And it should just send back the text string hello world.
Now let's use the ps command again to take a look at this process running as a tree. Notice that just like before, we have a process. This here showing us the program that was used to run this program. So we are using the Node.js interpreter, we're running this code file here. And the operating system created a process where the process ID of 8320. So that uniquely identifies this process.
And it is a child of Bash because we're running the program right in Bash. Notice that it's actually running. It hasn't existed yet, it just kind of stays alive. So that's similar to if you're running an application server in development, like if you just type Meteor, or if you type Rails.s if you're using Rails. So it's a child of Bash, and if we kill this window up here, we'll kill the child process as well. So processes can bind to network ports, and it's one of the most common ways that we'll communicate between processes is by using networking.