Saturday 2 November 2013

Multithreading


Introduction
            Multi-threading is the ability of a computer’s central processing unit to execute multiple threads in a single process. A thread is a building block for multithreaded systems or in simple terms a path taken by a process or application when executed. Operating systems or “OS”, as they are commonly called, foster the ability of multithreading in the modern world. This blog post is geared towards discussing concepts, advantages and disadvantages of multithreading.
            Before the concept of multi-threading can be understood fully, the main concept of a thread must be looked at briefly.


What is a Thread?  
            A thread is a basic unit of CPU utilization, that is, the smallest sequence of a program that can be executed independently while sharing the process’ resources (such as memory). Processes on the other hand, do not share resources. Therefore, a thread is generally referred to as a light-weight process contained by heavy-weight processes. Threads consist of a program counter, a processor state (including the current instruction pointer), a stack for running in user mode and a stack for running in kernel mode and a set of registers.
           


        Threads are divided into two main types: User threads and Kernel threads. User threads are threads used by application programmers, whereas, kernel threads are managed by the operating system. The relationship between user threads and kernel threads can be identified by their models: one-by-one, many-to-one and many-to-many.
These models are shown in the diagrams below.




Whats is Multithreading?
            Multithreading is the ability of an operating system to simultaneously execute the processing of multiple threads controlled by a single process within the same address space.
            Mutithreading is a way of providing a user with a high level of response since a single thread can run while other threads await instructions. For example; Multithreaded programing in Java have the ability for one subprogram or process to display an animation on the screen while other subprograms build the next sequence of animations to be displayed. Also multithreaded operating systems have the ability to log file changes, index data and manage windows at the same time as background tasks.
            In multithreading, a parent process contains all threads. Each thread within the parent process handles a separate transaction, algorithm or message. They share the resources and memory allocated to the parent process including the computing units, the CPU caches and the translation lookaside buffer (TLB). This makes it less costly to generate multiple threads.
            When executed on a multiprocessor, multiple threads can run in parallel across multiple processors as only one process may be executed on one processor. Hence, it is safe to say that multithreading takes advantage of a single core by using parallelism of both thread-level and instruction-level.
                                                                                                

Multithreaded Process vs Single threaded Process








Why Multithreading?
Let us now weigh the advantages and disadvatages of multithreading to see how beneficial multithreading can be.

Advantages of Multithreading

Ø  Inproved responsiveness: is faster communication between threads; and a process can be running even if some threads within the process are stalled, working on a lengthy task, or awaiting user interaction while a single-threaded application cannot respond to user input until it is done a particular task.
Ø  Faster application and more efficient processing:  multithreading takes advantage of a single core by using parallelism of both thread-level and instruction-level.
Ø  Simultaneous acess to to multiple applications
Ø  Reduced numbers of required servers
Ø  More efficient programming:
1. Threads are easier to create than processes: this is so because they do not require a separate address space.
2. There is greater control in module programming and the timing of application-related events.

Disadvantages of Multithreading
Ø  Difficulty of managing conconcurrency: threads may interfere with each other when sharing hardware resources (e.g.) caches or translation lookaside buffers (TLBs).
Ø  Extensive Programming
Ø  Debugging

Multithreading using multiple cores






FAQ’s of Multithreading

Question
        Why is multithreading important if I already have a computer with several cores?
Answer
        Processes are even faster: The multithreading process on a computer with a single processr allows a user to continue using a program even while another thread is doing some lengthy procedure but only one thread executes at a time. On the hand with a multiprocessor system, more than one processor may be running different threads in the same process. Hence, the execution of your program becomes significantly faster.
Question
        What is the difference between a thread and a process?
Answer
         A thread is the smallest sequence of a program that can be executed independently while sharing the process’ resources. It includes a processor context: which consists of a program counter, a processor state (including the current instruction/stack pointer), a stack for running in user mode and a stack for running in kernel mode and a set of registers.  Threads are executed sequentially with the ability to be interrupted so that another thread can be executed.
A process is a collection of threads and the necessary resources including memory, open files, windows and devices. It has a private memory address space in which the process's code and data are stored.




2 comments:

  1. I like your colour scheme, topic was easy to follow and the use of pictures make it more interesting overall good job.

    ReplyDelete
  2. The topic was made very clear. I really like the use of the FAQ's

    ReplyDelete