Friday, 11 May 2012

Multithreading and Operating Systems


A process is distinguished by 2 characteristics.
  1. Resource Ownership - A process is allocated resources from time to time.
  2. Scheduling/ Execution - The execution of a process follows an execution path (trace) through one or more programs. Thus, a process has an execution state (Running, Ready, etc.) and is an entity that is scheduled and dispatched by the OS.

The unit of dispatching is called a Thread or a Lightweight process. The unit of resource ownership is called the process or task.

A thread (or lightweight process) is basic unit of CPU utilization; it consists of:
  • program counter
  • register set 
  • stack space
A thread shares the following with peer threads:
  • code section
  • data section 
  • OS resources (open files, signals)
The unit of resource ownership is usually still referred to as a process or task. Heavyweight process is a task with one thread.

Multithreading

Multithreading refers to the ability of an OS to support multiple, concurrent paths of execution within a single process. The variations are –
  • Single user process, single thread (MS-DOS)
  • Multiple user processes, each with a single thread (Some variants of UNIX)
  • One process, multiple threads (Java runtime environment)
  • Multiple processes, multiple threads (Windows, Solaris, Modern versions of UNIX)
In a multithreaded environment, a process is defined as the unit of resource allocation and a unit of protection. The following are associated with processes:
  • A virtual address space that holds the process image
  • Protected access to processors, other processes (for inter-process communication), files, and I/O resources (devices and channels)
Within a process, there may be one or more threads, each with the following:
  • A thread execution state (Running, Ready, etc.)
  • A saved thread context when not running; one way to view a thread is as an independent program counter operating within a process.
  • An execution stack.
  • Some per-thread static storage for local variables.
  • Access to the memory and resources of its process, shared with all other threads in that process.
Benefits of Multithreading
  • Responsiveness - Allows a program to continue running even if a part of it is blocked.
  • Resource sharing
  • Economy - It is less time consuming to create and context switch threads than processes.
  • Parallel execution - In case of a multiprocessor architecture, it is possible for threads to run in parallel on different processors.
  • Thread context switch still requires a register set switch, but no memory management related work.
  • Threads share CPU and only one thread can run at a time.
User Level and Kernel Level Threads

User Level Threads

All the work of thread management is done by the application and the kernel is not aware of the existence of threads. The Advantages are
  • Thread switching does not require kernel mode privileges. Process does not switch to the kernel mode to do thread management.  
  • Scheduling can be application specific.
But the disadvantage is as the kernel assigns one process to only one processor at a time.  Hence a multithreaded application cannot take advantage of multiprocessing.

Kernel Level Threads

All the work of thread management is done by the kernel. The advantage is that the kernel can simultaneously schedule multiple threads from the same process on multiple processors. But the disadvantage is - transfer of control from one thread to another within the same process requires a mode switch to the kernel.

Source: Operating System Internals and Design Principles By William Stallings (5th Edition)


No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?