Inhzus

Official

OS Notes: Processes & Threads


Processes and threads

Thread usage

Processes are used to group resources together, threads are the entities scheduled for execution on the CPU.

per-process itemsper-thread items
address space
global variables
open files
child processes
pending alarms
signals and signal handlers
accounting information
program counter
registers
stack
state

User space threads

Pros

Cons

Threads in the kernel

The kernel’s thread table hold each thread’s registers, state, and other information.

Due to the greater cost of creating and destroying threads in the kernel, some systems take an environmentally correct approach and recycle their threads.

Pros

Cons

Making single-threaded code multithreaded

  1. Global variables, such as erroro maintained by UNIX, break consistency.

    Assign each thread its own private global variables. (thread_local)

  2. Many library procedures are not reentrant.

    To rewrite the entire library is nontrivial with a real possibility of introducing subtle errors.

    Provide each procudure with a jacket that sets a bit to mark the library as in use.

  3. A process with multiple threads must also have multiple stacks. If the kernel is not aware of all the stacks, it cannot grow them automatically upon stack fault.

When to schedule

  1. When a new process is created.
  2. When a process exits.
  3. When a process blocks on I/O or some other reason.
  4. When an I/O interrupt occurs.
  5. At each clock interrupt or at every k-th clock interrupt.

Scheduling in batch systems

Scheduling in interactive systems