Sunday, 3 November 2013

Multithreading
http://www.catonmat.net/blog/wp-content/uploads/2009/03/multithreading-dag.jpg

Before we get into the main topic of this post allow me to define a few things.

What is a thread? - A thread is a flow of execution through a process code. Sometimes called a light weight process it has its own stack, program counter and system registers. In other words it is a seperate flow of control which belongs to a sole process and is not able to exist outside of one.

Types of threads
1. User Level Threads - The application is in charge of thread management. The kernel does not know of the thread's existence. A single thread is started by the application and that application initially runs on that thread.

2. Kernel Level Threads - These threads are managed by the kernel and are supported directly by the operating system.


And now the main event...............

Multithreading is the ability of a program to manage multiple request from a user or multple requests from multiple users. Each thread can operate independently even while all of them may be using the same resources. Operating systems work faster using threads than creating multiple processes.

Two applications of multithreading are:
1. In Server applications
2. In situation where large amounts of mathematical operations are needed.

The figure below shows a comparison between a single threaded and multithreaded process
Single vs Multithreaded Process


Types of Multithreading

Temporal
a)Block multithreading - In this process one thread may be running until it stalled due to an event that might take some time to occur. In this case the threading processor allows the next thread in the read-to-run list to be executed until the previous thread had received its data and can resume its function. This multithreading type is also know as Coarse-grained multithreading.

b)Interleaved multithreading - Also know as Fine-grained mulitithreading, this type of process removes all stalls from data dependencies from the execution pipeline. A data dependency is a program statement which refers to a previous statement. A pipeline is a set of data processing elements that are connected in series. The output of one element is the input of another. Notice by defining these two statements the first sentence is easier to grasp. Since one statement is dependent on another statement and its result, time will be needed to get the required data. The question is: How much time will it take?

Simultaneous multithreading - Instructions from more than one thread can be executed in any pipeline stage at a time. A super-scalar processor is used. This processor, during every CPU cycle, usually issues a number of instructions from a single thread but in simultaneous multiprocessing issues instructions from multiple threads.

Types of Multithreading Models
1. Many to One relationship - Many user threads are mapped to one kernel thread. Only one thread can access the kernel at a time therefore multiple threads cannot run in parallel.
2. One to One relationship - As the name suggests only one user thread is mapped to one kernel thread. This relationship allows threads to be more in sync than the many to one. 0S/2, Windows NT and Windows 2000 use this type of relationship.
3. Many to Many relationship - A number of user threads are mapped to more than one kernel thread. The number of kernel threads to user threads could be the same or less.

If you're still not quite getting it, the figure below shows the different multithreading relationships.

http://www.itinfopoint.com/contentsimages/20130609-model-multithreading.png
             


Advantages and Disadvantages of Multithreading


Advantages:
1. Memory locations and resources can be shared which means that less resources are used.
2. Application responsiveness is improved. If an application can be launched on its own thread then it will not freeze up.
3. Simplification of the program structure. Simple routines can be created for each activity which makes intricate programs much more easier to design and code. This will in turn make the application suitable for a variety of user commands.

Disadvantages
1. Changes in application programs as well as the operating system are required hardware support for multithreading is more visible to software.
2. Even though shared resources are seen as a plus, threads may interfere with each other while this is happening.
3. Difficulty debugging. Replication of an error is harder to do than in a single threaded application so identifying and verifying the root of the cause will be more troublesome.

4 comments:

  1. Emily: "Firstly I would like commend you on a job well done, and make it clear that I do not want you to think that I am trying to be harsh or critical, but I have a few comments to make and questions to ask:
    1. In what particular instance would large amounts of matematical operations be necessary?
    2. The first Disadvantage is not clear. Are you trying to say that changes have to be made to application programs and the OS to support multi-threading or that it is platform dependent??
    3. For the third disadvantage, what do you mean by replication of an error?"

    ReplyDelete
  2. I must commend you. This page is well put together and explained quite well. keep up the good work.

    ReplyDelete
  3. Very informative... I like the use of the diagrams to explain more about Multithreading and the comparison between a single threaded and multithreaded process..

    ReplyDelete
  4. Firstly I would like to say that the information was put together very well......simple and clear for any user to understand. Thumbs up on the use of pictures to illustrate Multithreading. However as stated by Emily, I am not to clear on what is meant by Replication of an error. Also, not related to the topic, the background image plays a bit on the eyes. Once again.....a job well done!

    ReplyDelete