Home > Articles

  • Print
  • + Share This
This chapter is from the book

4.2 The Anatomy of a Thread

The layout of a thread is embedded in the layout of a process. As discussed in Chapter 3, a process has a code, data, and stack segment. The thread shares the code and data segment with the other threads of the process. Each thread has its own stack allocated in the stack segment of the process's address space. The thread's stack size is set when the thread is created. If the creator of the thread does not specify the size of the thread's stack, a default size is assigned by the system. The default size of the stack is system dependent and will depend on the maximum number of threads a process can have, the allotted size of a process's address space, and the space used by system resources. The thread's stack size must be large enough for any functions called by the thread, any code that is external to the process like library code, and local variable storage. A process with multiple threads should have a stack segment large enough for all of its thread's stacks. The address space allocated to the process limits the stack size, thus limiting the size possible for each thread's stack. Figure 4-3 shows the layout of a process that contains multiple threads.

04fig03.gifFigure 4-3. Layout of a process that contains multiple threads.

In Figure 4-3, the process contains two threads and the thread's stacks are located in the stack segment of the process. Each thread executes different functions: thread A executes function 1 and thread B executes function 2.

4.2.1 Thread Attributes

The attributes of a process are what describe the process to the operating system. The operating system uses this information to manage processes and distinguish one process from another. The process shares almost everything with its thread including its resources and environment variables. The data segment, text segment, and all resources are associated with the process and not the threads. Everything a thread needs to operate is supplied and defined by the process. What distinguishes threads from one another is the id, the set of registers that defines the state of the thread, its priority, and its stack. These attributes are what give each thread their identity. Like the process, the information about a thread is stored in data structures and returned by functions supplied by the operating system. Some information about a thread is contained in a structure called the thread information block, created at the time the thread is created.

The thread id is a unique value that identifies each thread during its lifetime in a process. The priority of a thread determines which threads are given preferential access to an available processor at a given time. The state of the thread is the condition a thread is in at any given time. The set of registers for a thread includes the program counter and the stack pointer. The program counter contains the address of the instruction the thread is to execute and the stack pointer points to the top of the thread's stack.

The POSIX thread library defines a thread attribute object that encapsulates the properties of the thread accessible and modifiable to the creator of the thread. The thread attribute defines the following attributes:

  • scope

  • stack size

  • stack address

  • priority

  • detached state

  • scheduling policy and parameters

A thread attribute object can be associated with one thread or multiple threads. When an attribute object is used, it is a profile that defines the behavior of a thread or group of threads. All the threads that use the attribute object take on all the properties defined by the attribute object. Figure 4-3 also shows the attributes associated with each thread. As you can see, both threads A and B share an attribute object but they maintain their separate thread ids and set of registers. Once the attribute object is created and initialized, it can be referenced in any calls to the thread creation function. Therefore, a group of threads can be created that has a "small stack, low priority" or "large stack, high priority and detached." Detached threads are threads that are not synchronized with other threads in the process. In other words, there are no threads waiting for the detached thread to exit. Therefore, once the thread exits, its resources, namely thread id, can be instantly reused. Several methods can be invoked to set and retrieve the values of these attributes. Once a thread is created, its attributes cannot be changed while the thread is in use.

The scope attribute describes which threads a particular thread will compete with for resources. Threads contend for resources within two contention scopes: process scope (threads of the same process) and system scope (all threads in the system). Threads compete with threads within the same process for file descriptors while threads with system-wide contention scope compete for resources that are allocated across the system (e.g., real memory). Threads compete with threads that have process scope and threads from other processes for processor usage depending on the contention scope and the allocation domains (the set of processors to which it is assigned). A thread that has system scope will be prioritized and scheduled with respect to all of the system-wide threads. Table 4-3 lists the settable properties for the POSIX thread attribute object with a brief description.

  • + Share This
  • 🔖 Save To Your Account