Systems programming
Thread management (Cont.)
Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash
Systems programming Thread management (Cont.) Most of the slides in - - PowerPoint PPT Presentation
Systems programming Thread management (Cont.) Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash Creation of Unix processes vs. Pthreads Example of Pthreads #include <pthread.h>
Thread management (Cont.)
Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash
#include <pthread.h> #include <stdio.h> void *PrintHello(void * id){ printf(“Thread %d: Hello World!\n", id); } void main (){ pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); }
#include <pthread.h> #include <stdio.h> void *PrintHello(void * id){ printf(“Hello from thread %d\n", id); } void main (){ pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); pthread_join(thread0, NULL); pthread_join(thread1, NULL); }
gcc −g −Wall −o hello hello.c −lpthread
Linking the Pthreads library
./hello ./hello
Hello from thread 0 Hello from thread 1 Hello from thread 1 Hello from thread 0
the pthread_cancel() routine
subroutines.
Description: terminates running a thread. Syntax: void pthread_exit(void *value_ptr);
Syntax: pthread_t pthread_self(void);
invoked.
Syntax: int pthread_equal (pthread_t t1, pthread_t t2);
and non zero value. If it is equal then return non zero value else return 0.
By having main() explicitly call pthread_exit() as the last thing it does, main() will block and be kept alive to support the threads it created until they are done. for(t=0;t < NUM_THREADS; t++) { pthread_join( threads[t], NULL); }
thread id. This function send a cancellation request to the thread.
int pthread_cancel(pthread_t thread);
created, so that the system can release all resources associated with the thread.
leaks until the process ends.
void *processfd(void *arg); int error; int fd pthread_t tid; if (error = pthread_create(&tid, NULL, processfd, &fd)) { fprintf(stderr, "Failed to create thread: %s\n", strerror(error)); } else if (error = pthread_detach(tid)){ fprintf(stderr, "Failed to detach thread: %s\n", strerror(error)); }
function description pthread_cancel terminate another thread pthread_create create a thread pthread_detach set thread to release resources pthread_equal test two thread IDs for equality pthread_exit exit a thread without exiting process pthread_join wait for a thread pthread_self find out own thread ID
pthread.h pthread_t
int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) ( void ) /* in */ , void* arg_p /* in */ ) ;
On One obj bject t for eac ach thread ad.
creation
#include <pthread.h> int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr);
In all the previous examples in which we used pthread_create, we passed in a null pointer instead of passing in a pointer to a pthread_attr_t structure
property function attribute objects pthread_attr_destroy pthread_attr_init
detach state
pthread_attr_getdetachstate pthread_attr_setdetachstate
stack
pthread_attr_getguardsize pthread_attr_setguardsize pthread_attr_getstack pthread_attr_setstack
scheduling
pthread_attr_getinheritsched pthread_attr_setinheritsched pthread_attr_getschedparam pthread_attr_setschedparam pthread_attr_getschedpolicy pthread_attr_setschedpolicy pthread_attr_getscope pthread_attr_setscope
#include <pthread.h> pthread_t tid; void * myfunction(void *); void *myptr; pthread_attr_t tattr; pthread_attr_init( &tattr ); pthread_attr_setscope( &tattr, PTHREAD_SCOPE_SYSTEM ); thread_create( &tid, &tattr, myfunction, &myptr );
PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM.
pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
process or with other processes in the system.
A global Variable If proper synchronization techniques are NOT applied, it may cause a RACE CONDITION where the values of variables may be unpredictable and vary depending on the timings of context switches of the processes or threads.
Thread 0 for i = 0, n/2-1 s = s + f(A[i]) Thread 1 for i = n/2, n-1 s = s + f(A[i]) int s = 0;
least one does a write.
happen simultaneously
Mutex = Mutual Exclusion One of the primary means of implementing thread synchronization and for protecting shared data when multiple writes occur. Acts like a lock protecting access to a shared data resource Only one thread can lock (or own) a mutex variable at any given time.
int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) int pthread_mutex_lock(pthread_mutex_t *mutex) int pthread_mutex_unlock(pthread_mutex_t *mutex)
Indivisibly waits for mutex to be unlocked and then locks it. Unlocks a mutex.