Pthreads
Pthreads
Jayanti Prasad
http://www.iucaa.ernet.in/ jayanti/
Introduction
I Processes
I Threads
Posix threads
I What is pthreads
I Creating and joining pthreads
I Race condition and Mutex locks
Examples
Problems
Process
Program = Instruction + data
Process = Program in action
Jayanti Prasad (IUCAA-Pune) Programming with pthreads February 01, 2012 5 / 24
Processs address contains
The current status of the process (sleeping, stopped, runnable, etc.)
The execution priority of the process
Information about the resources the process has used
Information about the files and network ports the process has opened
The processs signal mask (a record of which signals are blocked)
The owner of the process
Compilation
gcc program.c -lpthread -lm
1
2 pt h re ad _ mutex_t mutex1 = P T H R E A D _ M U T E X _ I N I T I A L I Z E R ;
3 int counter = 0
4
5 void * functionC (){
6 p t h r e a d _ m ut e x_ lo c k (& mutex1 );
7 counter ++;
8 printf ( " Counter value : % d \ n " , counter );
9 p t h r e a d _ m u t e x _ u n l o c k ( & mutex1 );
10 }
11
12 int main ( int argc , char * argv []){
13
14
15 for ( l =0; l < num_threads ; l ++)
16 pthr ead_create (& thread [ l ] , NULL ,& functionC , NULL );
17
18 for ( l =0; l < num_threads ; l ++)
19 pthread_join ( thread [ l ] , NULL );
20
21 }