Friday, May 21, 2021

Multi thread and Concurrency - Questions

Q1.Why do we want to use multiple threads in an application?


  1. The more threads in one application, the faster the application will be.
  2. By using multiple threads we can execute multiple related tasks simultaneously, making the application more responsive. And executing multiple tasks in parallel may achieve higher performance.
  3. Having multiple threads is always a better software design.
  4.  Creating more threads in one process, is always better than creating a new application.


Q2. Multiple threads in a single process share:


  1. The stack , heap , instruction pointer, Code,  The process's open files
  2. Only application code
  3. The heap, Code, The process's open files, The process's meta data
  4. Only heap


Q3. How does the Operating System design what thread to schedule?

  1. The Operating System maintains a dynamic priority for each thread to prioritize interactive threads and to avoid starvation for any particular thread in the system.
  2. The Operating System is going to choose the interactive threads ahead of any other threads at all times to make the users happy.
  3. The Operating System will always pick the shortest job at any given moment.
  4. The Operating System will simply pick a random thread to execute, which on average will be fair to everybody.

 

Q1 - 2

Q2 - 3

Q3 - 1

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/

Multi thread and Concurrency - Basic Understanding

 Multi thread - Why we need

  1.  Responsiveness
  2.  Performance

Multi thread, Synchronous and Asynchronous, one of the words you will here a lot.

let's take a simple example

In a supermarket people come and buy product and go to queue and purchase. In a simple scenario, there is one person who buys a large number of items and one with few items. If the customer with large items comes, other has to wait for a huge time. 

So can we reduce this ? IF yes --> How ?

In an application we have basically two parts. UI and backend

Looking at that point Responsiveness is more important to UI and Performance in backend. This is where threads become handy


Concurrency -  Multi tasking

  • Achieved by multitasking between threads
  • Concurrency == Multi tasking

Note: Even with one core we can achieve multitasking

One core -> multiple tasks concurrently

more cores -> multiple tasks parallelly


Performance Impact

  • Completing a complex task much faster
  • Finish more work in the same period of time
  • For high scale services
    • Fewer machines
    • Less money spent on hardware
    • More money save | money in pocket


Thread is a part of process and Process may have few components like

  • Process Specific
    • PID - process Id
    • Mode
    • Priority  etc
  • Files
  • Data (Heap)
  • Code
  • Thread (  there can be one or more threads)

each Thread will have

  • Stack
  • Instruction Pointer

Note: Thread will have its unique stack and Instruction pointer , but rest of the items are shared with in process context


Stack -> Region in memory, where local variables are stored, and passed in to functions

Instruction pointer -> Address of the next instruction to execute


Why we have its own stack and Instruction pointer ?

at given time each thread is executing different instructions in a different function 


Summary up to now

Why multi thread ?

Responsiveness achieved by concurrency

Performance achieved by parallelism

Thread

Stack

Instruction pointer

What thread share

files

heap(data)

code

Thursday, May 20, 2021

GIT - cherry-pick

you have two  branches. make a hot fix. apply that hot fix to other branches

cherry pick commit.

Here we create some changes in branch 'develop'.

Cherry-pick a commit from it and apply  to master branch


git status -> clean

git checkout -b develop

mate sample.txt -> 'cherry pick sample'

git commit -am "my cherry pick commit"

mate sample.txt -> "This won't be cherry picked"

git commit -am "non cherry pick commit"

git log --oneline -> will non cherry pick and cherry pick commit


copy the commit id of the 'cherry pick item'

git checkout master 

verify with

git status

git log --oneline --graph --all

mate sample.txt -> 'update away from develop branch'


git log --oneline --graph --all -> you can see last commit on master and previous two on develop branch'

-----------

*d4de234 update away from develop branch

| * c123452 non cherry pick commit

| * 1c23452 my cherry pick commit

|/

*6534123 express commit on README file


d4de234 -> on master

c123452 , 1c23452 -> on develop branch


let's do cherry pick '1c23452 my cherry pick commit'

git cherry-pick 1c23452


git status -> clean

Let's go and see the change

mate sample.txt -> you can see the change we did under 'my cherry pick commit' is in the 'sample.txt'. change is cherry-picked from develop branch to master branch

Wednesday, May 19, 2021

GIT Basics - Using tags with Github

git status -> clean

git tag --list

--------------

v-0.8-alpha

v-0.9-beta

v-1.0

v-1.1

v-1.2


git log --online --graph --decorate -- all --> you can see where each of tags are associated with


But if you got Github via browser and tags section --> there aren't any tags

Why? --> we did not push

let's see how to push  a tag

git push origin v-0.9-beta


Now if you browse Github --> 

you can see newly pushed tag "v-0.9-beta". 

same commit id.

git push origin v-1.1 -> not only tag, commit associated with also being pushed


Still there are few tags in local

push all tag to remote/Github

git push origin {branch name} --tags

git push origin master --tags --> synchronize master branch and push any tags there


Browse Github Release / Tags

you can see tags and download options (zip, tar.gz)


What if you pushed a tag accidentally - which should not be there ?

git tag --list -> can see all tags

Need to remove 'v-0.8-alpha' with colon(:) -> this will message "deleted" with your tag name 'v-0.8-alpha'

git push origin :v-0.8-alpha


you can verify by browsing Github

GIT Basics - Updating tags

git status -> clean

git log --online --graph --decorate -- all -> several commits associated with the tags


Scenario 1: we need to update a tag "v-0.8-alpha" with new commit id

you can delete and create a tag

else update/ force a tag

git tag -a v-0.8-alpha -f bd4523

-------------------------------------

Updated tag 'v-0.8-alpha' (was 96a2324)


you can verify with history| log command that tag has been moved to correct command

git log --online --graph --decorate -- all

GIT Basics - Tagging specific commit

git status -> clean

git log --online --graph --decorate -- all -> last three commits are tagged. Refer "Comparing Tags"


I need to tag one of the commits from the list - use commit id (96a234f)

git tag {annotate} {version name} {commit id where tag should apply}

git tag -a v-0.9-beta 96a234f


we can create further tags as per need

git tag -a v-0.8-alpha 96a2324


git log --online --graph --decorate -- all -> you can see the newly created tag names are associated with those commit ids respectively in the list