Friday, May 21, 2021

Multi thread and Concurrency - Context Switch , Thread scheduling and Thread vs Process

In simple scenario

  • We have lots of processes
  • Each process having one or more thread
  • with One CPU


Threads are fighting to get CPU

stop one thread and allow another thread to run, this switch between thread in simple Context switch


The price for context switch --> price for concurrency( multi task)

  • store data for one thread
  • restore data for resume thread

Important

  • Too many thread -> spend more time in management than real productive work
  • Thread consume less resources compare to process
  • context switch between two thread in same process is cheaper than context switch between two thread in different process


Thread scheduling 

  • if we give queue (FIFO) --> 

    longer thread comes first, then other thread has to wait long time - starvation


  • How about scheduling shorter one first --> 

    then longer tasks may not get a chance

Now we have little understanding of trade-off and challenges in scheduling Thread


In general OS divide time into moderately sized pieces called 'epochs'

In each epochs -> OS allocate different time slice for each thread

Note: not each thread can complete in epochs

decision to allocate time for thread --> based on dynamic priority ( OS maintain for each thread)

static priority -> developer set ahead of time

bonus -> adjusted by OS


This way OS will give preference to interactive and  real time threads ( these need immediate attention)

will give preference to computational threads that did not complete or |  did not get enough time to run in previous epics to prevent "starvation"


Dynamic Priority = Static Priority + Bonus

bonus - can be negative


when to use multi thread in single program

when to create a new program an run differently 


When to preference multi thread architecture

  • when task share lots of data
  • Threads are much faster create and destroy
  • switch between threads of same process is much faster

lastly we create separate process and run 

If Security and stability are higher importance

Tasks are unrelated to each other -> no point putting them in same process


https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html

https://developer.ibm.com/tutorials/l-completely-fair-scheduler/

No comments:

Post a Comment