What is the Difference Between a Process and a Thread

Introduction

We all have a very common question about threads, processes and their execution.

In this article I try to explain the common queries about threads and processes.

This article explains the following:

  • What a thread and a process is
  • What threads and processes actually do
  • The similarities between threads and processes
  • The differences between threads and processes
  • Whether threads are faster

Explanations of the preceding will reveal very much about System Programming features and how the operating system works, I will explain each of the preceding one by one.

  1. What a thread and a process is

    A computer system normally has many active processes and threads. An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more threads run in the context of the process.

    A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread. The processes and threads are independent sequences of execution.

  2. What threads and processes actually do

    Processes and threads provide an execution environment. Ultimately, they form a system, a collection of threads and processes that does something.

    A system consists of a number of processes. Each process is responsible for providing a service of some nature, whether it's a filesystem, a display driver, data acquisition module, control module, or whatever.

    Within each process, there may be a number of threads. The number of threads varies. One designer using only one thread may accomplish the same functionality as another designer using five threads.

  3. The similarities between threads and processes

    · Both threads and processes have sequential execution.

    · Both threads and processes create their child threads and processes.

    · Both threads and processes share the common CPU.

    · If one thread is blocked, then another can execute just like processes.

  4. The differences between threads and processes

    · Threads will, by default, share memory, but processes by default do not share memory.

    · A thread cannot exist by itself, a process must start a thread. A process can start multiple threads in other words “threads are not independent like processes”.

    · Threads will share file descriptors, but among processes more file descriptors are not shared.

    · Changes to the main thread (cancellation, priority change and so on) may affect the behavior of the other threads of the process; changes to the parent process do not affect the child processes.

    · Threads will share file system context, but processes don't share file system context.

    · Threads will share signal handling, but processes don't share signal handling.

  5. Are threads faster than processes?

    Use of a process means you also need Inter Process Communication (IPC) to get data in and out of the process whereas threads on the same process are much more lightweight. They reside in the same memory space and they can share handles and security context. Switching among threads in the same process is much faster because the OS only switches registers, not memory mapping.

    Threads are basically an optimization to avoid too many transitions or too much communication among memory spaces.

    I hope this article clarifies many things regarding threads and processes and will also be helpful for interviewing purposes.

Next Recommended Readings