[C++ job interview Q&A] What are the differences between process and thread?
Hi All! I’d like to start a new post series called C++ Job Interview Questions where – and it’s not surprising – We’ll discuss most-popular questions that are asked during job interviews for C++ developer position. Questions that might be asked to all junior, mid or senior programmers.
For the beginning, let’s discuss the following one: “What are the differences between process and thread?” which is most probably the most popular question to C++ programmers ever.
Let’s focus on the process first.
A process is an independent sequence of execution. It runs in its own virtual address space and is always started with a single thread – called the main thread. It’s able to create additional threads from any of its threads.
Each process provides resources needed for the program execution. It provides the executable code, open handles to the system resources, security context, its own process identifier, environment variables, a priority class, minimum/maximum working set sizes.
On a multiprocessor system, it is possible to run multiple processes in parallel. It’s called parallelism.
Also, it’s worth to notice that in some contexts the term “task” is used to refer to a process that is being executed.
What’s a thread?
A thread is also an independent sequence of execution, but it runs in a shared memory within a process. All threads of a process share the same virtual address space and resources.
Each thread maintains i.a. its own exception handlers, local storage, unique identifier, and a stack in the address space of the thread’s process.
If a process is made up of multiple threads, the code is executed concurrently. However, it’s absolutely crucial to synchronize the access to the data shared between threads of the process. There are several mechanisms that help programmers to solve the synchronization issues, e.g. semaphores, mutexes or monitors.
That’s it. Knowing what’s a process and what’s a thread, it’s quite clear what are the differences between them. It’s absolutely important to know that, not only in the job interview but also in our daily programmer’s work.