Sunday, April 7, 2013

Multitasking

Before learning about multithreading, we should know what necessitated multithreading for that. we need to know about multithreading.

Considering the following program.

Class MSD
{
fun1()  fun2() fun3()
{
----
}
public static void main(String args[])
{
MSD s1 = new MSD();
S1.fun1();
S1.fun2();
S3.fun3();
}
}

Normally, if the class is compiled and JVM executes the program, then the order of executing is that, first fun1() is called and ofter complete execution of fun1(), then control comes back to main() and then fun2() is called.After complete execution of fun2(), then fun3() is called and executed this is 

how the flow goes on.

Now, let us assume that fun1() has some statements which involve data transfer.

We know that data trandfer is not the job of the processor.

It is the duty of a separate individual circuit (DMA) which functions under the control of the processor.

Since fun1() has data transfer statements, processor assign that job to DMA. During this the processor should runtime idle.

The state of CPU where CPU waits for DMA circuit to transfer the data is known as Idle state.

This made software developers to think that, efficiency of the processor would be increased if processor is made to do some other useful work during this idle state. The useful work is nothing but, executing other functions present in the program.

This need led to the concepts of multitasking.

Remember that processor is also an electrical circuit and at any instant of time, it can execute only one statement. It can not execute multiple statements simultaneously.

Multitasking : The concept of executing multiple functionalities simultaneously is known as multitasking.

If we apply the concept of multitasking to our program, then control flow would be as follows.

A part of fun1() will be executed and then control switches to func2(). Now, here also, a part of fun2() is executed and then control again comes back to fun1(). Now it contains executing fun1() from where it had stopped earlier. Similarly, for fun2() and fun3(). This is not guaranteed. The sequence may change)

Now when we see the output, we feel that the processor is executing the three functionalities simultaneously avoid the Idle States of CPU.

In multitasking we said that, a part of a functionality is executed one at a time. Now, the question is ‘part’ means how many statements.

This conflict is resolved by Scheduling.

Scheduling:  It is the process in which a specific time period is allocated to a functionality where the control remains in that particular functionality for that specific time period.

Once the time period is lapsed, control switches to another functionality with another time slice so on.

Scheduling is supervised by the scheduler.

The time slices allocated will be in the order off nano seconds.

Thus, by the time our eye recognizes the execution of one part, control switches to another part of the program.

Now, it is clear that, without applying multitasking,one function will depend upon another function. i.e: after execution of fun1(), fun2() will be executed and so on.

Advantages of multitasking:

i)                    Multitasking was invented to avoid Idle states of CPU.
ii)                   Butt interestingly, it is used to make the functions get executed independently.

Note : Actually, in realtime we use multitasking to make the most of the second advantage rather than the first, because in case of small projects, idle state of CPU is not a big concern.

Animation along with from submission is a very good example of multitasking.
Multitasking is two types

i)                     Thread based multitasking

ii)                   Process based multitasking

Java supports aonly thread based multitasking.

Process based multitasking:

Concept of executing more than program simultaneously which are presents in different locations of RAM is known as process based multitasking.

Thread based multitasking:

Concept of executing more than one functionality simultaneously belonging to the same memory(i.e same program) is known as thread based multitasking.

Thread : Thread is a functionality (group of statements) which would be getting executed simultaneously with the other part of the program.

Interview Q: What is the difference between a process and a thread?

Process is any program under execution thread is a part of the process.

In other words, thread is the smallest part of a process.




No comments:

Post a Comment