Threads and Process

What is Thread?
  • A thread is a flow of execution through the process code, with its own program counter, system registers and stack. A thread is also called a light weight process
  • Threads provide a way to improve application performance through parallelism.
  • Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process.
  • Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control.
Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors. 

Thread Levels

There are two broad categories of thread implementation:

  • User-Level Threads -- Thread Libraries.
  • Kernel-level Threads -- System Calls.
There are merits to both, in fact some Operating Systems allow access to both levels (e.g. Solaris).

User-Level Threads (ULT)

In this level, the kernel is not aware of the existence of threads -- All thread management is done by the application by using a thread library. Thread switching does not require kernel mode privileges (no mode switch) and scheduling is application specific.
Kernel activity for ULTs: 

  • The kernel is not aware of thread activity but it is still managing process activity. 
  • When a thread makes a system call, the whole process will be blocked but for the thread library that thread is still in the running state. 
  • So thread states are independent of process states
Advantages of  ULT:
  • Thread switching does not involve the kernel -- no mode switching.
  • Scheduling can be application specific -- choose the best algorithm.
  • ULTs can run on any OS -- Only needs a thread library
Disadvantages of  ULT:
  • Most system calls are blocking and the kernel blocks processes -- So all threads within the process will be blocked.
  • The kernel can only assign processes to processors -- Two threads within the same process cannot run simultaneously on two processors
Kernel-Level Threads (KLT)

In this level, All thread management is done by kernel No thread library but an API (system calls) to the kernel thread facility exists. The kernel maintains context information for the process and the threads, switching between threads requires the kernel Scheduling is performed on a thread basis.

Advantages of KLT

  • The kernel can simultaneously schedule many threads of the same process on many processors blocking is done on a thread level.
  • Kernel routines can be multithreaded.
Disadvantages of KLT:

Thread switching within the same process involves the kernel, e.g if we have 2 mode switches per thread switch this results in a significant slow down.


Real Time Example of Threads 

When writing games you need to do more than one thing at once.  Threads offer a way of automatically allowing more than one thing to happen at the same time. Java has threads as a fundamental feature in the language. This will make your life much easier when you write games in Java. Threads are related to processes and multitasking.

Example :
  • Suppose that you are a poor student and you need to earn money. To earn money you get a job answering technical support calls for a company specializing in software to derive new weaving patterns for Turkish carpets. As it turns out there is not a very large user base and you only get 4 or 5 calls a day (but the pay is good).
     
  • You have to revise for an exam. You hit upon the idea of revising while you are waiting for calls. When a call interrupts you go away from you books and deal with the customer.
  • The last example was easy. You had plenty of time to go back and forward between the books and the exam. 
  • Now consider a more important situation. You are going to play in a counter strike tournament and at the same time you need to watch an important Hockey match between India and Russia while you are doing your revision. 
  • You can again multitask. You spend a little bit of time on each activity swapping between them. If you are fast enough you might be able to do all three (almost at the same time).
Context Switching :
  • Going back to the first example. Suppose that you have spent too much of your time on fast living and your memory is not as good as it should be. 
  • When you answer the phone you put a bookmark in the book so you can remember where you are.
  • When you go back to the book you use the bookmark to work out where you are.
  • This is an example of a context switch. You want to go from one task to another you save information to remember where to go back to.
Definition of Context Switching
The computer does the same thing with running programs. If it wants to switch tasks. It saves all the information needed (program counter, values of variables), so the program can be returned to.
 

This can happen without the program knowing it. Think of it as cryogenics for programs. The operating system forcefully stops the current task saves that state and starts another task from a previously saved state.

Benefits of Threads vs Processes
If implemented correctly then threads have some advantages of (multi) processes, They take:

  • Less time to create a new thread than a process, because the newly created thread uses the current process address space.
  • Less time to terminate a thread than a process.
  • Less time to switch between two threads within the same process, partly because the newly created thread uses the current process address space.
  • Less communication overheads -- communicating between the threads of one process is simple because the threads share everything: address space, in particular. So, data produced by one thread is immediately available to all the other threads.
Threads and Processes
There is a technical distinction between threads and processes. That need not worry us here. You can think of a process as a whole program (such as Emacs, word, minesweeper, Mozilla). The operating systems allows more than one process to run at the same time.
 
A thread is a unit of execution with in a process. That has less overhead and quicker context switch time. Threads can share data while processes can not (not always true but true enough). Threads are lightweight processes.


Multitasking
A computer can give the illusion of doing more than one thing at once by time slicing. Each task is given a small amount of time to run before the next task is switched in. Context switches take care of all this. Of course every thing runs slower, but you have more flexibility.

No comments:

Post a Comment