Threads Versus Processes

Threads are a mechanism that permits an application to perform
multiple tasks concurrently. A single process can contain multiple threads.

All of these threads are independently executing the same
program, and they all share the same global memory, including the initialized data, uninitialized data, and heap segments.

some of the factors that might influence our choice of whether to implement an application as a group of threads or as a group of processes.

We begin by considering the advantages of a multithreaded approach:

Sharing data between threads is easy. By contrast, sharing data between processes requires more work (e.g., creating a shared memory segment or using a pipe).

Thread creation is faster than process creation; context-switch time may be
lower for threads than for processes.

Using threads can have some disadvantages compared to using processes:

When programming with threads, we need to ensure that the functions we call
are thread-safe or are called in a thread-safe manner.  Multiprocess applications don’t need to be
concerned with this.

A bug in one thread (e.g., modifying memory via an incorrect pointer) can dam-
age all of the threads in the process, since they share the same address space and other attributes. By contrast, processes are more isolated from one another.

Each thread is competing for use of the finite virtual address space of the host
process. In particular, each thread’s stack and thread-specific data (or thread-
local storage) consumes a part of the process virtual address space, which is
consequently unavailable for other threads.

No comments:

Post a Comment