Monday, June 1, 2015

Multi-threaded Programming

Process  
  • Program in execution.
  • It has
    • text section - program code
    • program counter - current activity
    • contents in the processor's registers -
    • stack - temporary data ( function parameters, local variables, return values)
    • data section - global variables
    • heap - memory which is dynamically allocated

  • Process states
    • new
    • running
    • waiting
    • Ready
    • Terminated

Thread
  • basic unit of CPU utilization.
  • It has
    • Thread id
    • program counter
    • register set
    • Stack
    • Signal mask
    • Priority
    • Return value

  • It shares with other threads belonging to the same process
    • code section
    • data section
    • other operating system resources (open files, signals)
    • Process instructions
    • Current workign directory
    • User and group id

Posix(pthreads) Threads

Why Pthreads
  • Light Weight - When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes.
  • Efficient Communications/Data Exchange- on a multi-processor architecture is to achieve optimum performance.

Programs having the following characteristics may be well suited for pthreads:
  • Work that can be executed, or data that can be operated on, by multiple tasks simultaneously:
  • Block for potentially long I/O waits
  • Use many CPU cycles in some places but not others
  • Must respond to asynchronous events
  • Some work is more important than other work (priority interrupts)

Thread-safeness: - an application's ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" conditions. A race condition occurs when two or more threads can access shared data and they try to change it at the same time

Thread Limits -the maximum number of threads permitted, and the default thread stack size are two important limits to consider when designing your program.


Pthread API

The subroutines which comprise the Pthreads API can be informally grouped into four major groups:

  • Thread management: Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.)
    • pthread_create - create a new thread
    • pthread_join - wait for termination of another thread
    • pthread_exit - terminate the calling thread
  • Mutexes: Routines that deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion". Mutex functions provide for creating, destroying, locking and unlocking mutexes. These are supplemented by mutex attribute functions that set or modify attributes associated with mutexes.
  • Condition variables: Routines that address communications between threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, destroy, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included.
  • Synchronization: Routines that manage read/write locks and barriers.The threads library provides three synchronization mechanisms:
    • mutexes - Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.
    • joins - Make a thread wait till others are complete (terminated).
    • condition variables - data type pthread_cond_t. The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true. A condition variable must always be associated with a mutex to avoid a race condition created by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on it resulting in a deadlock.

No comments:

Post a Comment