Categories
Linux Tutorial Series

Linux Tutorial Series – 106 – Viewing threads

Here is the video version, if you prefer it:

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:

mislav@mislavovo-racunalo:~$ ps m

PID TTY STAT TIME COMMAND

5008 pts/1 - 0:00 bash

- - Ss 0:00 -

6695 tty2 - 0:00 /usr/lib/gdm3/gdm-wayland-session /usr/bin/gnome-sess

- - Ssl+ 0:00 -

- - Ssl+ 0:00 -

- - Ssl+ 0:00 -

Where there is a dash instead of a PID, that’s a thread.

top - 20:44:14 up 96 days, 3:05, 1 user, load average: 0.58, 0.62, 0.62

Threads: 864 total, 1 running, 862 sleeping, 0 stopped, 1 zombie

%Cpu(s): 3.5 us, 4.1 sy, 0.0 ni, 92.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

MiB Mem : 7853.8 total, 1109.6 free, 4105.2 used, 2639.0 buff/cache

MiB Swap: 8066.0 total, 6836.9 free, 1229.1 used. 3052.6 avail Mem

I got to this by pressing H after I ran top.

Side note: I don’t think you will ever have to mangle with threads (I haven’t), but pays to know what they are and how to view them.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 177-178

Categories
Linux Tutorial Series

Linux Tutorial Series – 105 – A trip back down the OS lane – threads

Here is the video version, if you prefer it:

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