How Linux handles Processes [The Complete Guide]

In this lesson we’re going to review how Linux handles Processes.

What exactly is a Process?

So, what exactly is a Process.

For our purposes a process is a program that’s been loaded from a long-term storage of device like the hard disk drive into your system RAM and is currently being processed by the CPU on the motherboard.

Now there are actually many different types of programs that can be run in order to create a process.

Let’s take a look at what they are.

Creating a Process:

  1. Binary executables
  2. Internal shell commands
  3. Shell scripts

1. Binary executables

The first type of program that you can run to create a process are binary executables.

These are programs that were originally created as a text file using some type of programming language like C or C++ or something like that.

And then that text file was run through a compiler to create a binary executable file that can actually be processed by the CPU.

Binary executables are compiled, they’re compiled into a binary file containing the 0’s and 1’s that the CPU can run.

Now that’s not the only way you can create a process on a Linux system, you can also create a process by running an internal shell command.

2. Internal shell command

Understand that many of the commands that you enter at the shell prompt aren’t actual binary files instead they are commands that are actually rolled into the shell program itself.

For example, if you were to enter the exit command at the shell prompt, you’re actually running an internal shell command.

If you were to look in the file system you would not find an binary executable file anywhere on your Linux system named exit, instead the necessary computer code associated with the exit function is stored within the shell program code itself.

Likewise, these Internal shell commands are compiled into the shell program itself.

3. Shell scripts

The last way that you can create a process on the Linux system is to run a shell script.

Now these are text files that are executed through the shell itself.

Basically, you open up a text file and then you put in all the commands that you want to run within the text of the shell script.

And then you make that shell script executable and then the shell will dynamically interpret that shell script when you run it and do whatever it is you’ve programmed that script to do.

Now because these shell scripts are not compiled, we say that they are interpreted.

The shell scripts on the other hand are not compiled into 0’s and 1’s, they are interpreted on the fly by the shell itself and then a new process is created from the interpreted code.

Multitasking

Now be aware that the Linux operating system can actually run multiple processes concurrently at the same time on a single CPU.

Now depending upon how your Linux system is being used it may have a limited number of processes running at any given point in time or if it’s a heavily used system it may have hundreds of processes running concurrently.

Infact I can almost guarantee you that your Linux system will have many processes running at the same time.

Now just a second ago I said that it can have multiple processes running concurrently and I use the term concurrently loosely because most CPUs can’t truly run multiple processes at the same time.

Instead what happens is the Linux operating system itself quickly switches between the various processes running on the CPU making it appear as if the CPU is working on multiple processes concurrently.

Multitasking
Multitasking

The CPU for example, might run this process for a second and then quickly switch to second process and then quickly switch to this third process and then switch back to the first process again, go to the second one again and then to the third one and do the whole thing all over again.

This makes it appear as if the CPU is working on all these multiple processes concurrently.

However, what’s really happening is the CPU extra only executes a single process at a time, all the other processes are that are currently running are actually waiting in the background for their turn with the CPU.

Now the operating system maintains a schedule that determines when each process is allowed to access the CPU in this thing is called multitasking.

Now because the switching between processes happens so fast it appears to you and me at least to carbon-based life-forms that the CPU is actually executing these multiple processes all at the same time.

Now be aware however that there are two exceptions to this rule.

Multicore CPUs

Multicore CPUs
Multicore CPUs

The first exception are multi-core CPUs.

Now we have just one physical CPU installed in a slot on the motherboard but within that CPU we have two cores that function as two separate CPUs.

Because there are multiple cores inside the CPU package, each one can run its own set of processes like we saw previously.

One core can execute one process while the other core works on a different process.

Now each course still multitasks like we saw before, the first core works on Process-23 then it jumps to Process-24 then it jumps to Process-25 then it jumps back to Process-23 then it goes to Process-24 and then it goes to Process-25 and so on.

But while it’s working on Process-23, 24 and 25 the Second core is working at the same time on Process-26, 27 and 28 and it’s doing the same multitasking thing that the first core is.

So effectively this one CPU here could be running Process-23 and Process-26 at exactly the same time, One on the First core one on the Second core.

Hyperthreading CPUs

The second exception to this rule are Hyperthreading CPUs.

Now a hyper threading CPU is designed such that one single CPU can run two processes at a time.

Multithreading CPUs

Now before we go any further, I need to point out that on Intel CPUs this feature is called Hyper threading but on AMD CPUs this feature is called symmetric multi-threading.

However, in common usage the two terms tend to just be used interchangeably.

So, if you hear the word hyper threading or multi-threading, we’re actually referring to the same thing.

So, in this case we can have one processor running Process 23 and Process 24 and multi-threading CPUs also multitask, so it runs Process 23 and 24.

And then it switches as we talked about earlier to Process 25 and 26 then it jumps back it works on Process 23 and 24 again and then it switches to Process 25 & 26 again and jumps back to Process 23 and 24 again and keeps going through the multitasking loop.

So multi-threading CPUs like multi-core CPUs can run more than one process at once and you know this really speeds up the processing in the Linux system.

Multicore Multithreading CPUs

Now if you really want to speed things up you can look at a multi-core CPU that supports hyper threading.

Multicore Multithreading CPUs

So, in this situation we have two core two or more cores in the processor and each of those cores is capable of running on two processes at a time.

Depending upon how many cores you have in the processor this can make for a wicked fast system.

So, in this situation we have one processor it has two cores and each of these cores supports hyper threading.

So, this one CPU in the slot can run four processes at the same time.

It’ll first work on Process 23 and 24 on the first core and Process 27 and 28 on the Second core and then it also uses a multitasking just like a standard single core single thread CPU so it’ll switch and work on Process 25, 26 and 29 and 30.

The key point to remember though is that it’s working on four separate Processes concurrently at the same time on the system.

Types of Processes

Now before we go on you need to understand that the Linux Operating System implements several types of processes.

Not all of the processes running on your Linux system are the same.

1. User Processes

First of all, some processes are created by the end-user when they run a command from the shell prompt or maybe they click on an icon at the Graphical User Interface (GUI).

These are called user processes because they were created by the user.

Now user processes are usually associated with some kind of end user program running on the system.

So, in this example I ran the firefox command from the shell prompt which will load the Firefox Web Browser on the system.

$ ps -a
PID   TTY      TIME     CMD
35109 tty2     00:00:29 firefox
35344 tty2     00:00:01 WebExtensions
35350 tty2     00:00:05 Web Content
35418 tty2     00:00:03 Web Content
35521 tty2     00:00:01 Web Content
35565 tty2     00:00:00 Web Content
35599 pts/0    00:00:00 ps

And when I did this two user processes were created firefox and WebExtensions.

The important thing to remember about user processes is that they’re called from within a shell and they’re therefore associated with that shell session.

2. System Processes

But not all processes running on your system or user processes, in fact most of the processes running on a Linux system will be system processes these are created by Daemons.

Unlike a user process a system process usually does not provide any type of end user interface that they can use to interact with it.

Instead it’s used to provide a system service like maybe a Web Server or an FTP server or maybe File Sharing or Printer sharing.

These system processes run in the background and your end users don’t typically interact directly with them.

A list of system processes are shown here and you’ll notice right away that this system has many system processes running.

# ps -e
PID TTY          TIME CMD
  1 ?        00:00:10 systemd
  2 ?        00:00:00 kthreadd
  3 ?        00:00:00 rcu_gp
  4 ?        00:00:00 rcu_par_gp
  6 ?        00:00:00 kworker/0:0H-kblockd
  8 ?        00:00:00 mm_percpu_wq
  9 ?        00:00:03 ksoftirqd/0
 10 ?        00:00:05 rcu_sched
 11 ?        00:00:00 migration/0
 12 ?        00:00:00 watchdog/0
 13 ?        00:00:00 cpuhp/0
 15 ?        00:00:00 kdevtmpfs

You only see just a fraction of the total number of processes listed by the output of this command here.

Now system processes are usually but not always loaded by the Linux operating system itself when the system first boots up.

Therefore, because they’re loaded during system boot-up they are not associated with a particular shell instance.

This is a key difference between User processes and System processes.

User processes are tied to the particular shell that they were called from whereas System processes are not.

Conclusion

I hope that now you have a good understanding of How Linux handles Processes.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

Here are a few other hand-picked guides for you to read next:

Leave a Comment