WittCode💻

Process vs Thread vs Core

By

Learn about a process vs thread vs core including concurrent vs parallel program execution, logical vs physical cores, how threads run in a process, the structure of a process vs thread vs core, and how to show the processes, threads, and cores of a computer using the linux terminal.

Table of Contents 📖

What is a Process?

A process is a program that is being executed. For example, we could have a program written in some high level programming language on a computer. When the program isn't being executed it is just a program, but when the program is being executed it becomes a process. We can list the running processes on linux by running the command ps -e.

ps -e

This should give output similar to the following.

PID TTY          TIME CMD
  1 ?        00:00:13 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-events_highpri
  9 ?        00:00:00 mm_percpu_wq
.
.
.
.
1794252 ?        00:00:04 chrome
1794450 ?        00:00:16 chrome
1794493 ?        00:00:00 kworker/10:1-events
1794551 ?        00:00:00 kworker/9:2-mm_percpu_wq
1794586 ?        00:00:00 kworker/6:0-mm_percpu_wq
1794699 ?        00:00:00 chrome
1794727 ?        00:00:00 chrome
1794784 ?        00:00:00 chrome
1795023 ?        00:00:00 kworker/7:1-mm_percpu_wq

Above, we can see four columns: PID, TTY, TIME, and CMD. PID is the ID of the process, TTY is the type of terminal the user is logged into, TIME is the time the process has been running, and CMD is the command that launched the process. This output shows that computers can run multiple processes at the same time. We can also see that one program can have many processes associated with it. For example, chrome has several processes listed.

A process can also be seen as a logical container that holds all the information about the program running such as its process ID, program instructions, memory space, data read from files, etc. We can get a more detailed view of the processes on a linux machine by using the command ps aux.

ps aux

Output should be similar to the following.

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 171832 13192 ?        Ss   Jul07   0:13 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    Jul07   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   Jul07   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   Jul07   0:00 [rcu_par_gp]
root           6  0.0  0.0      0     0 ?        I<   Jul07   0:00 [kworker/0:0H-events_hig
root           9  0.0  0.0      0     0 ?        I<   Jul07   0:00 [mm_percpu_wq]
root          10  0.0  0.0      0     0 ?        S    Jul07   0:00 [rcu_tasks_rude_]
root          11  0.0  0.0      0     0 ?        S    Jul07   0:00 [rcu_tasks_trace]
root          12  0.0  0.0      0     0 ?        S    Jul07   0:11 [ksoftirqd/0]
root          13  0.0  0.0      0     0 ?        I    Jul07   4:09 [rcu_sched]
root          14  0.0  0.0      0     0 ?        S    Jul07   0:01 [migration/0]
root          15  0.0  0.0      0     0 ?        S    Jul07   0:00 [idle_inject/0]
root          16  0.0  0.0      0     0 ?        S    Jul07   0:00 [cpuhp/0] 
.
.
.
.
.
.   
wittcode 1799532  6.6  1.2 1179198808 202868 ?   Sl   17:09   0:17 /opt/google/chrome/chrom
root     1799575  0.0  0.0      0     0 ?        I    17:09   0:00 [kworker/5:0-events]
wittcode 1799642  0.1  0.6 1179135640 97952 ?    Sl   17:11   0:00 /opt/google/chrome/chrom
wittcode 1799953  0.7  0.5 1179173704 88176 ?    Sl   17:13   0:00 /opt/google/chrome/chrom
wittcode 1799967  0.3  0.4 1179173700 66460 ?    Sl   17:13   0:00 /opt/google/chrome/chrom

Here, we can see more information such as %MEM, RSS, STAT, etc. %MEM shows the memory usage percentage, RSS stands for resident set size and shows the portion of RAM occupied by the process, and STAT shows the current process state.

What is a Thread?

Processes are made up of one to many threads. A thread is a unit of execution that is scheduled by the operating system and executed by the CPU. Specifically, a thread consits of a stack, thread ID, a program counter, and a set of registers. However, the threads in multithreaded processes, or processes that consist of more than one thread, share the code, data, files, etc. with other threads of the process. For example, a chat application built using Java could be multithreaded. In this application, one thread could be in charge of listening for incoming messages while another could be responsible for sending messages to other users in the chat. Both these threads would be unique, but they would work with the same code files of the process.

Image

To view the threads of a process, we can use the command ps -T -p , where pid is the ID of the process we want to check.

ps -T -p 1927867

This command lists all the threads of the provided process. The option -T lists all threads and -p specifies the process ID. Here, we are showing the threads that are part of the chrome process with the PID 1927867.

PID    SPID TTY          TIME CMD
1927867 1927867 ?        00:00:01 chrome
1927867 1927868 ?        00:00:00 ThreadPoolServi
1927867 1927870 ?        00:00:00 Chrome_ChildIOT
1927867 1927872 ?        00:00:00 GpuMemoryThread
1927867 1927875 ?        00:00:00 Compositor
1927867 1927876 ?        00:00:00 ThreadPoolSingl
1927867 1927877 ?        00:00:00 CompositorTileW
1927867 1927878 ?        00:00:00 CompositorTileW
1927867 1927879 ?        00:00:00 CompositorTileW
1927867 1927880 ?        00:00:00 CompositorTileW
1927867 1927881 ?        00:00:00 CompositorTileW
1927867 1928006 ?        00:00:00 MemoryInfra
1927867 1929982 ?        00:00:00 ThreadPoolForeg

SPID is the thread ID while PID is the process ID. Notice how the process ID is the same throughout while the thread ID is different.

What is a Core?

A core is essentially a CPU itself. Nowadays, computers can have single core or multi-core CPUs. A single core CPU can only process one program at a time while a multi-core CPU can execute programs simultaneously. However, even though single core CPUs cannot run processes in parallel, they can execute programs concurrently with time slicing. Essentially working on bits and pieces of a program before switching to another program, working on that one, switching back, and so on. Multi-core CPUs can execute programs simultaneously because different cores can work on different programs.

Image

We can list the amount of cores a computer has on linux by running the command lscpu.

lscpu

The lscpu command gets information about the CPU of the system. Output should be similar to the following.

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          12
On-line CPU(s) list:             0-11
Thread(s) per core:              2
Core(s) per socket:              6
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           158
Model name:                      Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Stepping:                        10
CPU MHz:                         2200.000
CPU max MHz:                     4100.0000
CPU min MHz:                     800.0000
BogoMIPS:                        4399.99
Virtualization:                  VT-x
L1d cache:                       192 KiB
L1i cache:                       192 KiB
L2 cache:                        1.5 MiB
L3 cache:                        9 MiB

From the information above, we can see that we have 6 cores per socket, 2 threads per core, and 12 CPUs. This is because of the existence of physical and logical cores. A physical core is the hardware on the CPU itself. Logical core is the multiplication of the amount of physical cores by the number of threads per core. For example, here we have a 6 core CPU with 2 threads per core. This equates to 12 logical cores or CPUs. Threads are mentioned here because from a core perspective, a thread is a sequence of commands given to the cores. Cores execute a program which makes it a process. One final thing to mention is that threads running on the same core are not really running in parallel, they only appear to be running in parallel as the CPU switches between them very quickly. However, threads executing on different cores can be executed in parallel.

Summary

But there we have it! If this article was helpful please consider donating by using the link at the top of the page, subscribing to my YouTube channel WittCode, and sharing this article.