POSIX THREADS
WHAT IS THREAD?
 An independent stream of instructions
 A "procedure" that runs independently from its main program
 A thread maintains its own:
Stack pointer
Registers
Thread specific data, etc,.
Example: Imagine all of
these procedures being able
to be scheduled to run
simultaneously and/or
independently by the
operating system. That
would describe a "multi-
threaded" program.
 Exists within a process and uses the process
resources
 Changes made by one thread to shared system
resources (modifying a file) will be affected by
all other threads.
Windows does not support
pthreads
Standard natively, therefore
pthreads-w32 project seek to
provide a portable and open
WHAT ARE P-THREADS?
 Portable threaded applications.
 A standardized programming interface
 Pthreads are defined as a set of C language programming types and
procedure calls, implemented with a pthread.h
 For UNIX systems, this interface has been specified by the IEEE
POSIX 1003.1c standard (1995).
 Implementations adhering to this standard are referred to as POSIX
threads, or Pthreads.
WHY P-THREAD?
 Light Weight Efficient Communications/Data Exchange
 Other Common Resources
Overlapping CPU work with I/O
Priority/real-time scheduling
Asynchronous event handling
DESIGNING THREADED PROGRAMS
1) PARALLEL PROGRAMMING:-
 On modern, multi-core machines, pthreads are ideally suited for parallel
programming, and whatever applies to parallel programming in general, applies to
parallel pthreads programs.
 There are many considerations for designing parallel programs, such as:
What type of parallel programming model to use?
Problem partitioning Load balancing
Communications Data dependencies
Synchronization Race conditions
Memory issues I/O issues
Program complexity Programmer effort/costs/time
DESIGNING THREADED PROGRAMS
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 process is more important than other
process (priority interrupts)
DESIGNING THREADED PROGRAMS
2)SHARED MEMORY MODEL:
 All threads have access to the same global, shared
memory
 Threads also have their own private data
 Programmers are responsible for synchronizing
access (protecting) globally shared data.
DESIGNING THREADED PROGRAMS
Thread-safeness:
 Thread-safeness: in a nutshell, refers an
application's ability to execute multiple threads
simultaneously without "clobbering" shared data
or creating "race" conditions.
Thread Limits:
 Although the Pthreads API is an ANSI/IEEE
standard, implementations can, and usually do,
vary in ways not specified by the standard.
 Because of this, a program that runs fine on one
platform, may fail or produce wrong results on
P-THREADS API
Thread management: Routines that
work directly on threads - creating,
detaching, joining, etc. They also
include functions to set/query thread
attributes (joinable, scheduling etc.)
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.
Synchronization: Routines that
manage read/write locks and
barriers.
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.
P-THREAD PROGRAM TO FIND OUT THE
PRIME NUMBERS BETWEEN 1 TO N
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *runner(void *param); The Thread
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.
int main(int argc, char *argv[]) {
if(argc < 2) {
fprintf(stderr, "USAGE: ./prime.out <Integer value>n");
exit(1);
}
if(atoi(argv[1]) < 2) {
fprintf(stderr, "USAGE: %d must be >= 2n", atoi(argv[1]));
exit(1);
}
pthread_t tid;
pthread_attr_t attr;
printf("Prime Numbers: ");
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("nCompleten");
}
Verify the argument
passed
Verify whether the
input is greater
than or equal to
two
Setting up the ID and
attributes.
Retrieving the default
attributes
Creating the threads.
Waiting for the thread to
exit.
void *runner(void *param) {
int i,j,upper = atoi(param);
for(i = 2; i < upper; i++) {
int trap = 0;
for(j = 2; j < i; j++) {
int result = i % j;
if(result == 0) {
trap = 1;
break; }
}
if(trap == 0) {
printf("[%d] ", i);
} }
pthread_exit(0);
}
Starting the thread
Checking a number
Verifying the divisibility of a
number
If it is undivided, it’s a prime
Exit.
SAMPLE OUTPUT
Compiling pthreads using gcc requires a command line
option.
stark21@ubuntu:~$ gcc-lpthread -o a prime.c
stark21@ubuntu:~$ ./a 100
Prime Numbers: [2] [3] [5] [7] [11] [13] [17] [19] [23]
[29] [31] [37] [41] [43] [47] [53] [59] [61] [67] [71] [73]
[79] [83] [89] [97]

P-Threads

  • 1.
  • 2.
    WHAT IS THREAD? An independent stream of instructions  A "procedure" that runs independently from its main program  A thread maintains its own: Stack pointer Registers Thread specific data, etc,. Example: Imagine all of these procedures being able to be scheduled to run simultaneously and/or independently by the operating system. That would describe a "multi- threaded" program.  Exists within a process and uses the process resources  Changes made by one thread to shared system resources (modifying a file) will be affected by all other threads. Windows does not support pthreads Standard natively, therefore pthreads-w32 project seek to provide a portable and open
  • 3.
    WHAT ARE P-THREADS? Portable threaded applications.  A standardized programming interface  Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h  For UNIX systems, this interface has been specified by the IEEE POSIX 1003.1c standard (1995).  Implementations adhering to this standard are referred to as POSIX threads, or Pthreads.
  • 4.
    WHY P-THREAD?  LightWeight Efficient Communications/Data Exchange  Other Common Resources Overlapping CPU work with I/O Priority/real-time scheduling Asynchronous event handling
  • 5.
    DESIGNING THREADED PROGRAMS 1)PARALLEL PROGRAMMING:-  On modern, multi-core machines, pthreads are ideally suited for parallel programming, and whatever applies to parallel programming in general, applies to parallel pthreads programs.  There are many considerations for designing parallel programs, such as: What type of parallel programming model to use? Problem partitioning Load balancing Communications Data dependencies Synchronization Race conditions Memory issues I/O issues Program complexity Programmer effort/costs/time
  • 6.
    DESIGNING THREADED PROGRAMS Programshaving 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 process is more important than other process (priority interrupts)
  • 7.
    DESIGNING THREADED PROGRAMS 2)SHAREDMEMORY MODEL:  All threads have access to the same global, shared memory  Threads also have their own private data  Programmers are responsible for synchronizing access (protecting) globally shared data.
  • 8.
    DESIGNING THREADED PROGRAMS Thread-safeness: Thread-safeness: in a nutshell, refers an application's ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" conditions. Thread Limits:  Although the Pthreads API is an ANSI/IEEE standard, implementations can, and usually do, vary in ways not specified by the standard.  Because of this, a program that runs fine on one platform, may fail or produce wrong results on
  • 9.
    P-THREADS API Thread management:Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.) 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. Synchronization: Routines that manage read/write locks and barriers. 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.
  • 10.
    P-THREAD PROGRAM TOFIND OUT THE PRIME NUMBERS BETWEEN 1 TO N #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *runner(void *param); The Thread 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.
  • 11.
    int main(int argc,char *argv[]) { if(argc < 2) { fprintf(stderr, "USAGE: ./prime.out <Integer value>n"); exit(1); } if(atoi(argv[1]) < 2) { fprintf(stderr, "USAGE: %d must be >= 2n", atoi(argv[1])); exit(1); } pthread_t tid; pthread_attr_t attr; printf("Prime Numbers: "); pthread_attr_init(&attr); pthread_create(&tid,&attr,runner,argv[1]); pthread_join(tid,NULL); printf("nCompleten"); } Verify the argument passed Verify whether the input is greater than or equal to two Setting up the ID and attributes. Retrieving the default attributes Creating the threads. Waiting for the thread to exit.
  • 12.
    void *runner(void *param){ int i,j,upper = atoi(param); for(i = 2; i < upper; i++) { int trap = 0; for(j = 2; j < i; j++) { int result = i % j; if(result == 0) { trap = 1; break; } } if(trap == 0) { printf("[%d] ", i); } } pthread_exit(0); } Starting the thread Checking a number Verifying the divisibility of a number If it is undivided, it’s a prime Exit.
  • 13.
    SAMPLE OUTPUT Compiling pthreadsusing gcc requires a command line option. stark21@ubuntu:~$ gcc-lpthread -o a prime.c stark21@ubuntu:~$ ./a 100 Prime Numbers: [2] [3] [5] [7] [11] [13] [17] [19] [23] [29] [31] [37] [41] [43] [47] [53] [59] [61] [67] [71] [73] [79] [83] [89] [97]