To view threads you are currently running, you can either execute ps m or execute top, then press H to see a list of all the threads. (Ward, 2014) Examples for both:
Today, let’s go back down the good ol’ OS lane and talk about something called threads. A side note: I tried to fit in “memory lane”, but I couldn’t; it wouldn’t fit the content.
A thread is a sequence of commands that can be executed. A process always has at least one thread, but it can have multiple threads.
“Why have multiple threads?”, you may ask. A fair question. If you divide up the task into multiple threads (and assuming you have multiple processor cores), each thread can execute in its own core and the job can be done faster. An example: Say you were multiplying two matrices – each was of dimensions 20 000 x 20 000. You could do it in one thread, or maybe you could create 4 threads, each dealing with a particular part of the matrix. Or you could have 8 threads, or 80 threads – doesn’t matter. The point is, the more threads you have, the faster the program will execute, if you have the appropriate processor (one that has multiple cores) or multiple processors in your system (as in servers).
A thing I want to mention: Even if you have one processor core, you may have many apps open and they seem to be working flawlessly. In that case, what is actually happening is that you have an illusion of multitasking – your processor is just switching between programs very fast. You don’t even notice that switch, but again, it is an illusion of parallel execution. (Ward, 2014)
A thing I also want to mention: Dealing with threads is hard. Like, real hard. An example: Say you have two threads running concurrently (at the same time). Let’s call them thread A and thread B. Say both of them want to write in the same memory location. What can happen? Well, thread A can write its result first, then thread B can write its result and you have the result of the thread B. The reverse is also possible – thread B can write its result and that result can get overridden by thread A. Or, thread A can be writing its result, thread B starts writing its result in the middle of thread A writing its result… It is a mess. And again, to repeat, it is hard to manage threads. There are mechanisms to deal with thread management, but just know that dealing with threads is hard.
Hope you learned something useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 5-6
Today, I will just throw a bug in your ear (this is a colloquialism in Croatia for “useful for you to know”) and tell you about two commands I have used – pgrep and pkill.
You most likely won’t be needing those commands in most cases, but sometimes you know only the process name, but don’t know its ID and you want to end the process. Then you can use pgrep in combination with xargs (see here: (“Can I chain pgrep with kill?,” n.d.)) or use pkill with the process name as the argument (“pkill(1) – Linux man page,” n.d.).
Let’s talk about how to kill a process. That means to end a process; to terminate it. You can do this with the kill command.
Usage of kill is this:
kill <PID>
where PID is the process ID of the process you want to kill (you can attain this ID using ps with its various options). By default, kill sends a TERM signal to the process. (Ward, 2014) You don’t really need to know any other signals. If the process doesn’t terminate after that, you can try using
kill -KILL <PID>
which sends the KILL signal, but use it as the last resort – it ends the process in an ugly way.
Hope you learned something new!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 31-32
Today we will take a look at signals. They are very important in regard to operating systems and so they are relevant for Linux. Signals are a way for processes to communicate with one another. (“Signal (IPC),” n.d.)
Processes can’t access each other’s memory locations. So if Process A has memory locations 0 to 99 and Process B has memory locations 100 to 199, then if Process A tries to access memory location 129, operating system will not allow it.
But, it is useful for processes to be able to communicate with each other. Let’s say Process A wants to somehow say to Process B “Stop! Stop!”. It can use a signal to do so.
There are a lot of signals you can send to a process and they are listed here: (“POSIX signals,” n.d.). You don’t need to know their names. Just know what they are used for – communication between processes (also called inter-process communication (IPC)).
The jobs command displays status of jobs in your current Terminal session. (Barrett, 2016) Jobs are programs you are running in your current shell. The difference between jobs and processes is that processes are a operation systems term, while jobs are shell specific term – job is any program you start in the shell (whether in the foreground or in the background). (“What is the difference between a job and a process?,” n.d.)
I never used this command so far, so just mentioning it here.
Thank you for reading!
References
Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Page 40
To view processes dynamically, use top. (Shotts, 2019) It displays which processes are using up your resources the most – the ones using the most resources is always at the top of the list.
An example:
mislav@mislavovo-racunalo:~$ top
top - 20:34:02 up 96 days, 2:55, 1 user, load average: 1.11, 0.79, 0.62
15544 mislav 20 0 11276 3644 3060 R 6.7 0.0 0:00.01 top
...
I won’t go into the details of the column names or the meaning of each element of the summary statistics here. I will leave that for review, when we cover all of the relevant concepts. For now, just remember that top is a command that displays the processes with the most resource-intensive processes closer to the top of the process list.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 137-139
The ps command is used to display the processes which are currently running. By running ps without any options, you get the processes associated with the current Terminal, as so:
mislav@mislavovo-racunalo:~/Linux_folder$ ps
PID TTY TIME CMD
10653 pts/1 00:00:00 bash
31210 pts/1 00:00:00 ps
PID is process ID. TTY is the controlling terminal associated with the process – this field is irrelevant. TIME is processor time the process is taking up – the more processor time a process takes up, the more demanding it is. CMD is the command with which we started the process; it’s the name of the process, with arguments, if arguments exist. (Shotts, 2019)
ps x gives us all the processes that our current user is running. An example:
The new column STAT is short for state. State is the process state – it indicates what the process is doing. The first capital S means that process is sleeping, and that means the process is waiting for input. There are other process states, which you can learn more about by typing man ps and navigating to a section titled PROCESS STATE CODES.
ps aux gives us the processes of all users. Its sample output is:
mislav@mislavovo-racunalo:~/Linux_folder$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
As you can see here, we can see which user started each process (the column USER). We have a couple of additional columns – %CPU signifies how much CPU (in percentages) the process are using, %MEM is the percentage of RAM the process are using, VSZ is the virtual memory size (think of virtual memory as being as large as your hard drive and RAM size combined), RSS (Resident Set Size) is the amount of RAM the process is using in kilobytes, and START is the time when the process had started.
This is how you can take a look at the processes running in your computer.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 135-137
There exist two kinds of processes. Ones that execute in the background and ones that execute in the foreground. (Ward, 2014)
Background processes are useful for situations where the program is going to be running for a long time or you want to be able to use your Terminal while the program is running. We have run all of our commands so far in the foreground – meaning we didn’t get back our Terminal until the command stopped executing. All of the commands we have used so far have relatively short running time so there was no need for running them in the background.
To give you an example of a program I could run in the background, let’s look at Mendeley (a program I use for citations). I am currently running it in the foreground and it looks like this:
See how at the end I don’t have another mislav@mislavovo-racunaloprompt? That’s because Mendeley is running in the foreground. If I wanted to run it in the background, I would write an & at the end of the command to start Mendeley and I would get something like this:
And I have my prompt back. Mendeley is now started in the background. The number 27961 is Mendeley’s process ID which I can use to manipulate it.
This is for you to know that you can run processes both in the background and in the foreground and to know what & is used for. Also, remember that every process has its process ID (PID), no matter if it is started in the foreground or the background.
Hope you found this useful!
References
Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 32-33
Today we are going to talk about identification, authentication and authorization. We will explain it on an example of me getting into a club.
Let’s say I want to get into a club. I approach the bouncer, and he asks me who I am. I answer with my full name. That is identification – I provided my identity. Then the bouncer asks me to prove my identity (either via my identity card or whatever else). That is called authentication. Then, if my identity card is valid and the bouncer verifies who I am, then the bouncer checks “the list” and sees if I am on the list – that is authorization. Authorization is checking if a particular user has particular rights – in this case, if I am allowed to enter the club. I enter the club and thus this LinkedIn article is nearing its end.
These mechanisms exist within the operating system and are important for security. Think of operating system as the club bouncer and of me as someone trying to mangle around with the processes running on my computer. We talked about user IDs associated with the process IDs. So, for example, if I am a user with ID 4 trying to modify a process started by user with ID 5, I would not be authorized to do so and the operating system would not allow me to do so. I don’t think it’s important for you to know the details of how this is done exactly, but just to know that this is done so that there is some measure of security within the system. This may not matter much if you are the only user on your computer, but imagine if there are multiple users – then it starts to matter.