DEV Community

suvhotta
suvhotta

Posted on

Processes and Threads - Part 1

Ask anyone What is the difference between a Process and a Thread?. Most of them would first take a short pause and reply you "Ummmn they're actually the same thing."(Meanwhile deep-down there would be this inner battle to find out the exact answer)

As a beginner, I was also using both the terms interchangeably, till I realized it was a Cardinal Sin to do that.
Hence decided to make a two part series on what both of them do in our OS.

Process

Whenever any program is running on our computer, it can have one or multiple processes associated with it.

Thread

Thread can be thought of as the basic unit of execution of any process. Thus one process can consist of one or many threads.

Threads within the same Process run within a shared memory space while different Processes are allocated separate memory spaces.

Process Management and scheduling:

When a process is being scheduled, it may appear due to the Multi-Programming aspect that we are able to switch between typing a document, simultaneously while playing some music. This is something that happens by the help of the time sharing. Objective of time sharing is to switch the CPU between the programs so fast that the users can interact with each program while it is running and it would feel as if everything is happening at the same time. But if we dig deep into it, its the CPU which is actually switching at a very high rate between all these programs, to give a real time feel and run all of them in parallel without any lag.

This luxury isn't available in a Single Processor System. There will never be more than one running process in such systems.

In a Multi Processor System, there is a process scheduler, which takes care of the end to end scheduling process of various programs. If there are multiple processes queued up, then the job of the process/job scheduler is to allot the CPU to the processes depending on the priority. If there is a low priority process running and the OS gets a command to run a higher priority task, then the lower priority one is pushed to the waiting queue and the CPU resources are prioritized towards the higher priority process in hand.

So, Kudos to your OS,arranging the execution of applications so that you believe that there are several things happening at once. This is complicated because the CPU can only do one thing at a time. Today's multi-core processors and multi-processor machines can handle more work, but each processor core is still capable of managing one task at a time.

In order to give the appearance of lots of things happening at the same time, the operating system has to switch between different processes thousands of times a second. Here's how it happens:

  • A process occupies a certain amount of RAM. It also makes use of registers, stacks and queues within the CPU and operating-system memory space.
  • When two processes are multi-tasking, the operating system allots a certain number of CPU execution cycles to one program.
  • After that number of cycles, the operating system makes copies of all the registers, stacks and queues used by the processes, and notes the point at which the process paused in its execution.
  • It then loads all the registers, stacks and queues used by the second process and allows it a certain number of CPU cycles.
  • When those are complete, it makes copies of all the registers, stacks and queues used by the second program, and loads the first program.

Interprocess Communication:

When there are several dependent processes which need to pass data among themselves, then there is a clear need for internal communication. This is done by the Inter process Communication.

Two Methods are widely used:

  • Shared-Memory:
    There would be a portion of memory shared by the processes and in that space, all the values to be passed on are written and communicated.

  • Message-Passing:
    In the message passing model, the processes communicate with each other through messages.
    Markdown Monster icon

There are also other options to distribute resources among different processes and help in inter-process communication - Semaphores, signals etc.

Top comments (0)