A Process is an executing application, an executing copy of software. Threads have long been known as Light Weighted processes and are the basic unit of program execution.

Threads that are directly scheduled by the operating system are also called kernel-level threads. The main thread program uses algorithms to implement child threads, which we call user-level threads.

Each process gets a piece of time allocated by the operating system when it executes, and if it exceeds this time, the next process (thread) executes. Again, modern operating systems schedule threads directly, not processes.

When a process (thread) is running, it experiences three states:

  • Once a process (thread) is created, it is queued, and it is in the Ready state.

  • When it is the process’s turn to execute, the state becomes “Running”.

  • When a process (thread) runs out of time allocated by the operating system, it returns to the Ready state.

Sometimes a process (thread) will enter a “Block” state while waiting for data to be read from disk or for a printer to respond.

Process (thread) switching

  1. When the operating system detects that a process (thread) needs to be switched, it is very dangerous to directly control the PC pointer jump, so the operating system needs to send an “interrupt” signal to the CPU to stop the executing process (thread).

  2. When the CPU receives an interrupt signal, the executing process (thread) stops immediately. Note that since the process (thread) is stopped immediately, it has not had time to save its state, so subsequent operating systems must do this.

  3. After the operating system takes over the interrupt, a small piece of very low-level programming (usually written in assembly) must be performed immediately to help the register retain the state of the previous process (thread) before the register data is corrupted.

  4. After the operating system saves the process state, it executes the scheduler to determine the next process (thread) to be executed.

  5. Finally, the operating system executes the next process (thread).

After a process (thread) has been selected to execute, it will continue to complete the task it was interrupted from. This requires the operating system to perform a small piece of the underlying program to help the process (thread) recover.

Atomic operations are operations that are indivisible.

A race condition means that multiple threads compete to read and write a resource (memory address). In this condition, the value of the last resource is unpredictable, but depends on the execution order of the race.

Such program segments that access shared resources are called critical sections. In a critical section, a program fragment accesses a shared resource, creating a race condition where the value of the shared resource ultimately depends on the timing of the program’s execution and is therefore not deterministic.

Resolving competitive conditions

  1. Avoid critical section ThreadLocal
  2. The CAS instruction
  3. Tas instruction

The goal of the TAS directive is to set a memory address with a value of 1. First, compare the memory address data with the value of 1. If the memory address is 0, set the address to 1. If it is 1, it fails. 4. Lock