POSIX Threads Programming Notes

What is progress

Processes contain information about program resources and program execution state, including:

  • Process ID, process group ID, user ID, and group ID
  • Environment
  • Working directory.
  • Program instructions
  • Registers
  • Stack
  • Heap
  • File descriptors
  • Signal actions
  • Shared libraries
  • Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory).

What is thread

Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code.

This independent flow of control is accomplished because a thread maintains its own:

  • Stack pointer
  • Registers
  • Scheduling properties (such as policy or priority)
  • Set of pending and blocked signals
  • Thread specific data.

So, in summary, in the UNIX environment a thread:

  • Exists within a process and uses the process resources
  • Has its own independent flow of control as long as its parent process exists and the OS supports it
  • Duplicates only the essential resources it needs to be independently schedulable
  • May share the process resources with other threads that act equally independently (and dependently)
  • Dies if the parent process dies - or something similar
  • Is “lightweight” because most of the overhead has already been accomplished through the creation of its process.

What are Pthreads

Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library - though this library may be part of another library, such as libc, in some implementations.

Why Pthreads but not Progress

  • a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes.
    在這裏插入圖片描述
  • Efficient Communications/Data Exchange.
    – no intermediate memory copy required because threads share the same address space within a single process. There is no data transfer, per se. It can be as efficient as simply passing a pointer.
    – In the worst case scenario, Pthread communications become more of a cache-to-CPU or memory-to-CPU bandwidth issue
    在這裏插入圖片描述
  • Threaded applications offer potential performance gains and practical advantages over non-threaded applications in several other ways:
    – Overlapping CPU work with I/O: For example, a program may have sections where it is performing a long I/O operation. While one thread is waiting for an I/O system call to complete, CPU intensive work can be performed by other threads.
    – Priority/real-time scheduling: tasks which are more important can be scheduled to supersede or interrupt lower priority tasks.
    – Asynchronous event handling: tasks which service events of indeterminate frequency and duration can be interleaved. For example, a web server can both transfer data from previous requests and manage the arrival of new requests.

Several common models for threaded programs

  • Manager/worker: a single thread, the manager assigns work to other threads, the workers. Typically, the manager handles all input and parcels out work to the other tasks. At least two forms of the manager/worker model are common: static worker pool and dynamic worker pool.
  • Pipeline: a task is broken into a series of suboperations, each of which is handled in series, but concurrently, by a different thread. An automobile assembly line best describes this model.
  • Peer: similar to the manager/worker model, but after the main thread creates other threads, it participates in the work.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章