Threads Items Flashcards
What is a thread of execution (a.k.a. just thread)?
A thread of execution, or simply a thread, is the smallest unit of processing that can be performed in an operating system. It represents a single sequence of instructions within a program. A thread is a component of a process and can run concurrently with other threads within the same process.
Why might we want to create new threads of execution instead of new processes?
Efficiency: Creating and managing threads is typically less resource-intensive and faster than creating and managing processes because threads share the same memory space and resources of the parent process.
Shared State: Threads can easily share data and communicate with each other through the process’s shared memory space without the need for inter-process communication (IPC) mechanisms.
Parallelism: Using multiple threads can improve performance through parallelism, especially on multi-core processors, by allowing different parts of a program to run concurrently.
Why might we want to create new processes instead of new threads?
Isolation: Processes provide better isolation and fault tolerance because they have separate memory spaces. If one process crashes, it does not affect the others.
Security: Processes are more secure because they do not share memory by default, reducing the risk of unintended data corruption or security vulnerabilities.
Resource Management: Processes can be given distinct resources, permissions, and scheduling priorities, making it easier to manage different tasks with varying requirements.
What do threads belonging to the same process share?
Memory Space: Threads within the same process share the same address space, including the heap and global variables.
File Descriptors: Threads share file descriptors, meaning they can access the same files and I/O devices.
Code Segment: Threads share the same code segment, so they can execute the same instructions.
Data Segment: Threads share static and global variables.
Other Resources: Threads share other process-wide resources, such as open sockets and environment variables.
What don’t threads belonging to the same process share?
Thread Stack: Each thread has its own stack for local variables, function calls, and return addresses.
Thread-specific Data: Each thread maintains its own set of registers, including the instruction pointer and stack pointer.
Thread-local Storage: Threads can have their own thread-local storage (TLS) to store data that should be unique to each thread.