In Linux, a running instance of a program is called a process and it runs in its own memory space. 1
Each time you execute a command, a new process starts. 2
A process is an ‘active’ entity and it means that the binary code is running in the RAM at the moment, 3
as opposed to a program or command, which is considered to be a ‘passive’ entity - 4
it’s just a file on the disk. A single program or command can create many processes when run multiple 5
times. 6
Just imagine that you want to copy two large files and you run the 7
cp command once for each file. 8
Each instance of the running command is a process. But not all commands create a process. At the beginning 9
of the course 10
of the course, I said that there are 2 types of commands in Linux: commands that are executable files on the disk and 11
shell built-in commands. To check if a command is an executable file or a shell built in use 12
the type command: type ls 13
Ls is an executable file type rm; type cp; type type; type is a shell built in or type 14
cd is a shell built in; type umask and so on. 15
Only when running a command, which is an executable file a new process is created. 16
If a shell built in command is run, no process is created. In Linux 17
each process has a process id (PID) and it's also associated with a particular user and group. 18
The process has the access rights of that user and group. 19
If an executable file that represents the command does not have the SUID bit set, then the process 20
has the permission of the user who executes it. 21
This is normal behavior and is very important related to the whole system security. 22
If a non privileged user runs a normal command like cat, the process that starts has the same rights as 23
the non privileged user that executes the command. 24
In this example, the cat command was not allowed to view the shadow file because the user student that 25
ran the command does not have that permission. 26
The student user has no permission. 27
But if the binary file that represents the command does have suid bit set, then the process has the 28
permissions of the owner of the binary file no matter who executes the command. 29
I've gone deep into permissions and SUID in the previous section, take a look there if you need 30
a short recap. 31
For example if a user runs the password command, the process has root access because the 32
binary file has the suid bit set and it's owned by root. 33
This is very important regarding processes and systems security. 34
If you wonder why Linux is more secure than Windows and there are no viruses, then this is the answer. 35
Normally, any malware or virus a user installs and runs will have the user’s rights in the system 36
and they are extremely limited. 37
Just to make my point clear, let's suppose a virus gets into the system and wants to remove all the 38
files. The user who got tricked to install the virus is not root, it's a non privileged user. 39
This is the Linux principle that says that you should not use root for common activities like browsing 40
the web, reading emails or editing documents. 41
In fact, in many distributions like Ubuntu, the root login is disabled. 42
So I simulate such a virus and try to delete all configuration files. I'm logged in as a non privileged 43
user called student, so rm -rf /etc/. 44
And the star, I want to delete all the files. I'm hitting enter without any fear. 45
As you can see, nothing has happened: the command, so the possible virus couldn't do anything harmful 46
to the system; you can replace rm with anything else you wish. 47
Let's go ahead and talk about parent and child processes. 48
In Linux, all the processes in the operating system are created when another process executes fork() 49
system call. There’s an exception, which is the first process, that starts when the system is booting 50
and that’s init or systemd and has the process id 1 51
This is init, it's the first process that starts and has the process id1. 52
The process that used the fork system call is the parent process and the creative process is its child. 53
In other words, a parent process is a process that has created one or more child processes. 54
A parent may have multiple children, but a child process has only one parent process. 55
When you execute a command like ls or who or a new process is created and the shell, which is normally 56
bash is its parent. 57
So bash was the parent of the process who. 58
Other types of processes are zombie and orphan processes. 59
The operating system maintains a table that associates every process to the data necessary for its functioning. 60
When a process terminates its execution the operating system, releases most of the resources and information 61
related to that process, a terminated process whose data has not been collected is called a zombie 62
or defunct process. 63
Normally, zombie processes are removed quickly from the memory and don't use any of the system 64
resources. Orphan processes are the opposite situation to zombie processes. 65
Referring to the case in each a parent process terminates before its child processes. It's the situation when 66
you close the terminal while there are still processes running in it. 67
I'm executing a command that will take a long time. 68
And I'm closing the terminal. The ls command became orphan; by default, all of those processes will 69
receive the hangup signal and get terminated. 70
We'll talk about signals later in this section. 71
There's also the possibility to be adopted by another process, such as init need or systemd. 72
We'll talk about this in another lecture when we'll discuss the no hope command. In the context of processes 73
you'll also hear the term thread. 74
What is the difference between a thread and the process? 75
Multiple threads can exist within the same process and they share resources such as memory, while 76
different processes do not share these resources. 77
Let’s consider a word editor that’s running. It’s a process. 78
Examples of threads in the same process are automatic spell check, automatic saving of 79
the file while writing and maybe auto-completion. Threads are basically subprocesses that run in the 80
same memory context of a single process and make the application responsive. 81
Threads may share the same data while executing. 82
Now that you know what is the process and the thread, maybe you wonder what is a task? 83
Although the term task is more used on Windows, on Linux, there are commands like top, which sometimes 84
call processes tasks. 85
So tasks and processes are synonyms.