Thursday, 23 August 2012

Process vrs Thread

                                                                       The basic unit of CPU utilization is a thread .A thread is a single sequence stream within in a process .They are sometimes called lightweight processes Because threads have some of the properties of processes . In a process, threads allow multiple executions of streams. In many respect, threads are popular way to improve application through parallelism. The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel. Like a traditional process i.e., process with one thread, a thread can be in any of several states (Running, Blocked, Ready or Terminated).
Each thread has its own stack. Since thread will generally call different procedures and thus a different execution history. This is why thread needs its own stack

Processes Vs Threads :

Similarities
  • Like processes threads share CPU and only one thread active (running) at a time.
  • Like processes, threads within a processes, threads within a processes execute sequentially.
  • Like processes, thread can create children.
  • And like process, if one thread is blocked, another thread can run.
Differences
  • Unlike processes, threads are not independent of one another.
  • Unlike processes, all threads can access every address in the task .
  • Unlike processes, thread are design to assist one other. Note that processes might or might not assist one another because processes may originate from different users.

Why Threads?

Following are some reasons why we use threads in designing operating systems.
  1. A process with multiple threads make a great server for example printer server.
  2. Because threads can share common data, they do not need to use interprocess communication.
  3. Because of the very nature, threads can take advantage of multiprocessors.
Threads are cheap in the sense that
  1. They only need a stack and storage for registers therefore, threads are cheap to create.
  2. Threads use very little resources of an operating system in which they are working. That is, threads do not need new address space, global data, program code or operating system resources.
  3. Context switching are fast when working with threads. The reason is that we only have to save and/or restore PC, SP and registers.
But this cheapness does not come free - the biggest drawback is that there is no protection between threads.

There are two types of threads to be managed in a modern system: User threads and kernel threads:

User Threads :There are two types of threads to be managed in a modern system: User threads and  kernel threads.
Kernal Threads :Kernel threads are supported within the kernel of the OS itself. All modern OSes  support kernel level threads, allowing the kernel to perform multiple simultaneous  
 tasks and/or to service multiple kernel system calls simultaneously.


  • In a specific implementation, the user threads must be mapped to kernel threads, using one of the following strategies.

      Many-To-One Model

  • In the many-to-one model, many user-level threads are all mapped onto a single kernel thread.
  • Thread management is handled by the thread library in user space, which is very efficient.
  • However, if a blocking system call is made, then the entire process blocks, even if the other user threads would otherwise be able to continue.
  • Because a single kernel thread can operate only on a single CPU, the many-to-one model does not allow individual processes to be split across multiple CPUs.
  • Green threads for Solaris and GNU Portable Threads implement the many-to-one model.
                                                                 

      One-To-One Model

  • The one-to-one model creates a separate kernel thread to handle each user thread.
  • One-to-one model overcomes the problems listed above involving blocking system calls and the splitting of processes across multiple CPUs.
  • However the overhead of managing the one-to-one model is more significant, involving more overhead and slowing down the system.
  • Most implementations of this model place a limit on how many threads can be created.
  • Linux and Windows from 95 to XP implement the one-to-one model for threads.
                                               

       Many-To-Many Model  

  • The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models.
  • Users have no restrictions on the number of threads created.
  • Blocking kernel system calls do not block the entire process.
  • Processes can be split across multiple processors.
  • Individual processes may be allocated variable numbers of kernel threads, depending on the number of CPUs present and other factors.

                                     


Advantages of Threads over Multiple Processes

  • Context Switching    Threads are very inexpensive to create and destroy, and they are inexpensive to represent. For example, they require space to store, the PC, the SP, and the general-purpose registers, but they do not require space to share memory information, Information about open files of I/O devices in use, etc. With so little context, it is much faster to switch between threads. In other words, it is relatively easier for a context switch using threads.
  • Sharing    Treads allow the sharing of a lot resources that cannot be shared in process, for example, sharing code section, data section, Operating System resources like open file etc.

Disadvantages of Threads over Multiprocesses

  • Blocking     The major disadvantage if that if the kernel is single threaded, a system call of one thread will block the whole process and CPU may be idle during the blocking period.
  • Security    Since there is, an extensive sharing among threads there is a potential problem of security. It is quite possible that one thread over writes the stack of another thread (or damaged shared data) although it is very unlikely since threads are meant to cooperate on a single task.


                                                                                      .............. to be continued                           R.P

No comments:

Post a Comment