Computing process, Thread and Scheduler

https://en.wikipedia.org/wiki/Process_(computing)

Process

  1. When we create a jar for an application, the jar file in the computer’s disk.
  2. When we launch the application (e.g. a jar file), the OS creates an instance of that application.
    1. The application is loaded into the memory (RAM).
    2. That instance is called a process.
  3. A process is an instance of a computer program.
  4. The process is completely isolated (code, data, memory, sockets, other resources allocated by the OS) from any other process running on that same computer (RAM).
  5. A process is heavy-weight. It is expensive to create and destroy processes.
  6. Just because the application is sitting in the RAM, it doesn’t mean that it is executing instructions. It is the CPU that is executing the instructions.

Decoupling of processes

Decoupling is achieved if each process is running on a different machine.

Thread

The basic unit of program execution. A process can have several threads running concurrently, each performing a different job, such as waiting for events or performing a time-consuming job that the program doesn’t need to complete before going on. When a thread has finished its job, the thread is suspended or destroyed.

Threading is a facility to allow multiple tasks to run concurrently within a single process. Threads are independent, concurrent execution through a program, and each thread has its own stack.

  1. Thread is part of a process.
    1. A process can contain one or more threads.
    2. Threads within a process can share memory space.
  2. A process is a unit of resources. A thread is a unit of execution.
  3. More threads do not mean better performance.
  4. 1 CPU can run only 1 thread at any given time.

Scheduler

https://en.wikipedia.org/wiki/Scheduling_(computing)

  1. The scheduler assigns a thread to the CPU for execution.
  2. When multiple processes are running on a computer, the threads from all of those processes will compete for processor (CPU) capacity.
  3. When there is only one processor (CPU) and multiple threads, the scheduler will do the allocation for all of those threads on the CPU. The scheduler determines how long a thread can execute.
  4. If there are two processors (CPU) and multiple threads, the scheduler try to assign different threads to each of these processors. The scheduler determines how long a thread can execute.
  5. We call these threads kernel threads or OS threads.
  6. Modern CPUs come with multiple cores. Each core can be seen as a separate processor.
  7. When the allocation of the CPU is switched from one thread to another, the execution state of that particular thread needs to be stored. When that thread is picked up by the scheduler later, that thread can resume from the point where it was stopped.
    1. If that thread is running a Java program (in other words, if that thread is a Java thread), when the OS scheduler switches this thread with some other thread on the CPU, the OS has to store some details like the methods’ local variables, function call definitions, etc. That is called stack memory. See Memory Allocation and Garbage Collections
    2. Each and every thread will have it’s own stack memory.
    3. The size of the stack memory for a thread is determined when the process is created. It cannot be modified once the thread is created.

Problem statement

  1. CPU is very expensive.
    1. But using as much CPU is available makes the program run somewhat faster.
    2. Applications try to use as much CPU as they can.
    3. People create too many threads to make use of CPU.
  2. Often times, in Microservices architecture, we have too many network calls. But network calls are slow. Threads will be idle.
  3. Thread is an expensive resource. It is heavy and consumes memory.
  4. We need a mechanism to make these network calls more efficient without wasting system resources.

Tags

  1. Java Threads
  2. Killing a CPU process

Links to this note