operating system os

OS Thread Model

« Previous Tutorial Next Tutorial »

To keep track of which instruction to execute next, the thread has a program counter.

Thread has registers to hold the current working variables.

Thread has a stack to store the execution history with one frame for each procedure called but not yet returned from.

Since a thread must execute in some process, but the thread and its process are different concepts and can be treated separately.

Processes are used to group the resources together and threads are the entities that scheduled for the execution on Central Processing Unit.

What threads add to the process model is to allow more than one execution to take place in the same process environment, to a large degree independent of one another.

Note - Having more than one thread, running in parallel in one process is analogous to having more than one process running in parallel in one computer.

The situation of allowing multiple threads in the same process is called multithreading.

Threads take turns running whenever a multithreaded process is run on a single-CPU system.

The system gives the illusion of separate sequential processes running in parallel, when switching back and forth among multiple process. In the same way, multithreading works.

The Central Processing Unit (CPU) switches rapidly back and forth among the threads providing the illusion that the threads are running in parallel.

Unlike different processes, different threads in a process are not independent.

Threads have the same address space, means that they also share the same global variables.

Since every thread can access every memory address within the process address space, one thread can read, write, or sometime even completely wipe out stack of another thread.

There is no any protection between threads only because it is impossible and shouldn't be necessary.

Threads also share the same set of open files, child processes, alarms, and signals etc. as shown in the table given below.

Per Process Items Per Thread Items
Address space Program counter
Global variables Registers
Open files Stack
Child processes State
Pending alarms
Signals and signal handlers
Accounting information

A thread can be in one of the following four states.

  1. Running
  2. Blocked
  3. Read
  4. Terminated

Each thread has its own stack as shown in the figure given below.

thread model

The stack of each thread contains one frame for each procedure called but not yet returned from.

Processes generally start with a single thread present, whenever multithreading is present. And this thread has the ability to create a new thread just by calling a library procedure.

« Previous Tutorial Next Tutorial »


Quick Links
Signup - Login - Give Online Test