ericltw / notes

0 stars 1 forks source link

Difference between program, process and thread #8

Open ericltw opened 6 years ago

ericltw commented 6 years ago

What is a program

Program is an executable file containing the set of instructions written to perform a specific job on your computer. For example, chrome.exe is an executable file containing the set of instructions written so that we can view web pages. notpad.exe is an executable file containing the set of instructions which help us to edit and print the text files.

What is a process

Process is an executing instance of a program. For example, when you double click on Google Chrome icon on your computer, you start a process which will run the Google Chrome program. When you double click on a notepad icon on your computer, a process is started that will run the notepad program.

A process is sometimes refereed as active entity as it resides on the primary memory and leaves the memory if the system is rebooted. Several process may related to same program. For example, you can run multiple instance of a notepad program. Each instance is referred as a process.

What is a thread

Thread is the smallest executable unit of a process. For example, when you run a notepad program, operating system creates a process and starts the execution of main thread of the process.

A process can have multiple threads. Each thread will have their own task and own path of execution in a process. For example, in a notepad program, one thread will be taking user inputs and another thread will be printing a document.

All threads of the same process share memory of the that process. As threads of the same process share the same memory, communication between the threads is fast.

Processes and threads can be represented like below,

Process vs Thread

Process Thread
Process are heavy weight operations. Threads are light weight operations.
Every process has its own memory space. Threads use the memory of the process they belong to.
Inter process communication is slow as processes have different memory address. Inter thread communication is fast as threads of the same process share the same memory address of the process they belong to.
Context switching between the process is more expensive. Context switching between threads of the same process is less expensive.
Processes don’t share the memory with other processes. Threads share the memory with other threads of the same process.

Reference

ericltw commented 6 years ago

Single threading

In computer programming, single-threading is the processing of one command at a time. The opposite of single-threading is multithreading.

Multithreading

Multithreading is mainly found in multitasking operating system. Multithreading is widespread programming and execution model that allows multiple threads to exist within the context of one process. These threads share the process's resource, but are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. Multithreading can also be applied to one process to enable parallel execution on a multiprocessing system.

Multithreaded applications have the following advantages:

Multithreading has the following drawbacks:

Reference

ericltw commented 5 years ago

https://medium.com/swift-india/concurrency-parallelism-threads-processes-async-and-sync-related-39fd951bc61d

ericltw commented 5 years ago

Program

Process

Thread

Reference


Single thread

Multi-thread


Concurrency

實現Concurrency

Reference


Sync / Async