November 30, 2020

ShortNote: Process, thread, multithreading


Process

A process is a program that is currently executing. It is defined as an entity that represents a basic unit of work to be implemented in a system.

The OS provides its own address space for each process. For portability purposes, the address space always starts at zero. No process other than the OS knows exactly which part of physical memory and how its address space is located.

Processes are the instances of your app. It contains everything needed to execute your app, this includes your stack, heap, and all other resources.

The system allocates resources to each OS process: disk space, input / output device, information transfer channel, and so on. Each process has the ability to create other processes and control their execution. Each process in the OS is assigned certain rights. These rights are maximum for the OS itself, have intermediate values ​​for OS subsystems (drivers), and the minimum rights correspond to running user programs. The OS for each process stores all information about these processes in special tables. These tables necessarily describe the rights of the process, the full state of the processor registers for a given process, the amount of memory allocated to the process and the mapping of this memory to real physical memory, as well as a list of all resources allocated to this process. When switching from one process to another, the OS uses these accounts. - Wikipedia

Despite iOS being a multitasking OS, it does not support multiple processes for one app. Thus you only have one process.


Thread

The thread (flow of execution) is the smallest sequence of programmed commands. A thread is an integral part of a process.

Multiple threads can exist in a single process, execute concurrently, and share resources such as memory, while different processes do not share these resources. In particular, the threads in a process share their executable code and variables at any given time.

Threads are a limited resource on iOS (or any POSIX compliant system). It’s limited to 64 threads at the same time for one process.


Parallelism vs Concurrency

Parallelism:

  • is about running multiple tasks simultaneously.
  • means that an application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time.
  • does not require two tasks to exist. It literally physically run parts of tasks OR multiple tasks, at the same time using the multi-core infrastructure of CPU, by assigning one core to each task or sub-task.
  • requires hardware with multiple processing units, essentially. In single-core CPU, you may get concurrency but NOT parallelism. Parallelism is a specific kind of concurrency where tasks are really executed simultaneously.

Concurrency:

  • means executing multiple tasks at the same time but not necessarily simultaneously. There are two tasks executing concurrently, but those are run in a 1-core CPU, so the CPU will decide to run a task first and then the other task or run half a task and half another task, etc. Two tasks can start, run, and complete in overlapping time periods i.e Task-2 can start even before Task-1 gets completed. It all depends on the system architecture.

To make it clearer:

A system is said to be concurrent if it can support two or more actions in progress at the same time. A system is said to be parallel if it can support two or more actions executing simultaneously. The key concept and difference between these definitions is the phrase “in progress.” This definition says that, in concurrent systems, multiple actions can be in progress (may not be executed) at the same time. Meanwhile, multiple actions are simultaneously executed in parallel systems. In fact, concurrency and parallelism are conceptually overlapped to some degree, but “in progress” clearly makes them different.

Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.


Multithreading

Multithreading is a generalized technique for introducing a combination of concurrency and parallelism into your program.