Basic Programming - LogeshVel/learning_resources GitHub Wiki

Concurrency

Consider you are given a task of singing and eating at the same time. At a given instance of time either you would sing or you would eat as in both cases your mouth is involved. So in order to do this, you would eat for some time and then sing and repeat this until your food is finished or song is over. So you performed your tasks concurrently.

image

Concurrency means executing multiple tasks at the same time but not necessarily simultaneously. In a concurrent application, two tasks can start, run, and complete in overlapping time periods i.e Task-2 can start even before Task-1 gets completed.

In the computer science world, the way how concurrency is achieved in various processors is different. In a single core environment (i.e your processor is having a single core), concurrency is achieved via a process called context-switching. If its a multi-core environment, concurrency can be achieved through parallelism.

image

Execution of tasks in a single core environment. Tasks are context switched between one another.

Parallelism

Consider you are given two tasks of cooking and speaking to your friend over the phone. You could do these two things simultaneously. You could cook as well as speak over the phone. Now you are doing your tasks parallelly.

Parallelism means performing two or more tasks simultaneously. Parallel computing in computer science refers to the process of performing multiple calculations simultaneously.

image

How is concurrency related to parallelism?

  • Concurrency and Parallelism refer to computer architectures which focus on how our tasks or computations are performed.

  • In a single core environment, concurrency happens with tasks executing over same time period via context switching i.e at a particular time period, only a single task gets executed.

  • In a multi-core environment, concurrency can be achieved via parallelism in which multiple tasks are executed simultaneously.

Threads

Threads are a sequence of execution of code which can be executed independently of one another. It is the smallest unit of tasks that can be executed by an OS. A program can be single threaded or multi-threaded.

Process

A process is an instance of a running program. A program can have multiple processes. A process usually starts with a single thread i.e a primary thread but later down the line of execution it can create multiple threads.

image

Synchronous

Imagine you were given to write two letters one to your mom and another to your best friend. You can not at the same time write two letters unless you are a pro ambidextrous.

In a synchronous programming model, tasks are executed one after another. Each task waits for any previous task to complete and then gets executed.

Asynchronous

Imagine you were given to make a sandwich and wash your clothes in a washing machine. You could put your clothes in the washing machine and without waiting for it to be done, you could go and make the sandwich. Here you performed these two tasks asynchronously.

In an asynchronous programming model, when one task gets executed, you could switch to a different task without waiting for the previous to get completed.

image

image

image

image

Call by value

While calling the function, the entire value of the argument is copied to the function. The original (caller value) is not modified

Call by reference

While calling the function, the pointer to the original value is get passed so no copy process is required. This can modify the original value. No need to copy the values.

Method Signature

Only method name, parameters, return types are defined

Implementation is not defined.