Parallel programming interview questions and answers in C#.Net


 
1)What is Thread?

Thread is flow of control or Thread light weight process.

One procees contains many threads.

2)What is multi threading program?

With help of threads we can multiple programs at a time this is called multi threading programing.

3)How many types of threads available?

Two types
1)Foreground threads
2) Background threads

4)Which thread executing main() method either foreground or background?

Foreground Thread.

5)Who is responsible for scheduling and maintaining the Threads?

Operating system kernal is responsible for scheduling threads according to a policy, Policy contains various factors like priority etc..

6)What is the difference between foreground and background threads ?

Foreground threads keeps application active.

Background threads working on background maintain GC activities etc

Once foreground threads finishes it's execution all background threads closed automatically.
If it is user created thread then user can schedule the thread as he want.

7)What is thread locking scenario's?

Your program contain two or more threads sharing common variable
Needs to keep this variables in critical section

8)What is criticalsection in threads?

Criticalsection  is nothing but block of code   sharing between multiple threads and these code contain variable assignment , conditional checking and increment.
Always try for criticalsection as much as short.

9)What is contextswitch?

Storing one thread state and restore another thread to execution is known as context switch.

10)What is deadlock?

A deadlock is when two threads wait for each other indefinitely by attempting to gain access  on two resources.

11)How to mitigate deadlocks?

Many way we can avoid deadlocks
Using Monitor.TryEnter( ) with timeout.
Using Sleep( ) method , other thread methods.
Using locks etc.
 
12) What is the use of Suspend( ) and Resume( ) methods in Threads?

Suspend( ) is used to suspend thread execution.
Resume( ) is used resume suspended thread.
Both are deprecated methods in threads sometimes these methods leads to deadlock situation.

13)What is the use of Interrupt( ) and Abort( ) methods in threads?

Using both methods release threads from waitsleepjoin states.

If we use Interrupt( ) method on a thread its throwing ThreadInterruptedException.

Thread interrupt doesn't interrupt the threads which are running unmanaged code.

For Abort( ) method thread throwing ThreadAbortException.

Exception happen unexpectedly with Abort().

Using Abort method terminate nonblocked threads also.

Both methods introduce a resource leak.

Both are not suitable if you have third party code inside.

14)What is AutiResetEvent class in Threads?

Well worked for two threads environment.After completion one thread instructing another thread.

15)What is ManualResetEvent in Threads?

Well worked for instructing  all the threads upon completion of work.

16)What is CountDownEvent in Threads?

Well worked for group of threads signals one thread.

17)What is ThreadLocal state?

Declaring the some of the data types which are used only specific threads.

18) What is [ThreadStatic] attribute in threads?

Usually static preserve state between multiple the threads so that sometimes getting un expected results. To avoid these situation, decorating static variables with this attribute so that this variable not shared with other threads.

19) What is ThreadLocal<T> class?

Using this class declaring the variables local to a  Thread. So that other Threads not sharing this variables. 

20)What are best debugging Technics in Threads?

Giving  name to thread using Name property.
Freezing periodically used threads like maintenance activity threads.
Using ID property.

21) What is UMS in threads?

UMS means User Mode Scheduling is used to reduced context switching between threads.

22)What is lock() keyword in Threads?

Lock keyword is used synchronize shared resource between threads.
Lock keyword take the object as parameter.
Lock internally represent monitor class.

Ex 
object door= new object();
Needs to use lock keyword before shared resource so that resource locked not shared with other thread.

lock(door)
//Shared resources name ...

23)What is the use mutex keyword in Threads?

mutex is used for writing thread safe code.

mutex.WaitOne():
//Shared resource
mutex.ReleaseMutex()

mutex.WaitOne(): is used for waiting for the mutex to be released by another Thread.
mutex.ReleaseMutex():is used release mutex on shared variable.

24) As we have lock (monitor) what is the need of mutex in threads?

The concept of lock sits on top of CLR but mutexes are lower level OS primitives.
Mutex allows co-ordination of resource access between different components of bigger system. 

25)What is Semaphores in Threads?

Semaphores are generalization of mutexes.
Those are built in OS primitives exposed to managed code.

Ex:    var bridgeSemaphore = new Semaphore(2,2);

bridgeSemaphore.WaitOne();
//Shared resource 
bridgeSemaphore.Release(); 

26)What is Spinlocks in Threads?

While working with classic Threads managing Heap , Garbage Collection biggest thing needs manage due to this Usage of Spinlocks come into picture.
Using Spinlocks spin the tight loop until lock becomes available but Thread consuming a lots of CPU cycles while spinning. So that Spinlock recommended for small tasks.


public static class Program 
    {
        public static void Main() 
        {
       var user ="shiva";
       var work=new Thread(()=>{ Console.WriteLine(user);});
        user="Gaurav"; 
        work.Start();
        user="Janhith";
        }
   }
Depend on child birth you will see the output as Janhith


Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment