zeus
4 years ago
10 changed files with 198 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,69 @@ |
|||||
|
#include<stdio.h> |
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
|
||||
|
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; |
||||
|
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER; |
||||
|
pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER; |
||||
|
|
||||
|
void *functionCount1(); |
||||
|
void *functionCount2(); |
||||
|
int count = 0; |
||||
|
#define COUNT_DONE 10 |
||||
|
#define COUNT_HALT1 3 |
||||
|
#define COUNT_HALT2 6 |
||||
|
|
||||
|
int main() |
||||
|
{ |
||||
|
pthread_t thread1, thread2; |
||||
|
|
||||
|
pthread_create( &thread1, NULL, &functionCount1, NULL); |
||||
|
pthread_create( &thread2, NULL, &functionCount2, NULL); |
||||
|
pthread_join( thread1, NULL); |
||||
|
pthread_join( thread2, NULL); |
||||
|
|
||||
|
exit(0); |
||||
|
} |
||||
|
|
||||
|
void *functionCount1() |
||||
|
{ |
||||
|
for(;;) |
||||
|
{ |
||||
|
pthread_mutex_lock( &condition_mutex ); |
||||
|
while( count >= COUNT_HALT1 && count <= COUNT_HALT2 ) |
||||
|
{ |
||||
|
pthread_cond_wait( &condition_cond, &condition_mutex ); |
||||
|
} |
||||
|
pthread_mutex_unlock( &condition_mutex ); |
||||
|
|
||||
|
pthread_mutex_lock( &count_mutex ); |
||||
|
count++; |
||||
|
printf("Counter value functionCount1: %d\n",count); |
||||
|
pthread_mutex_unlock( &count_mutex ); |
||||
|
|
||||
|
if(count >= COUNT_DONE) return(NULL); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void *functionCount2() |
||||
|
{ |
||||
|
for(;;) |
||||
|
{ |
||||
|
pthread_mutex_lock( &condition_mutex ); |
||||
|
if( count < COUNT_HALT1 || count > COUNT_HALT2 ) |
||||
|
{ |
||||
|
pthread_cond_signal( &condition_cond ); |
||||
|
} |
||||
|
pthread_mutex_unlock( &condition_mutex ); |
||||
|
|
||||
|
pthread_mutex_lock( &count_mutex ); |
||||
|
count++; |
||||
|
printf("Counter value functionCount2: %d\n",count); |
||||
|
pthread_mutex_unlock( &count_mutex ); |
||||
|
|
||||
|
if(count >= COUNT_DONE) return(NULL); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1 @@ |
|||||
|
gcc -pthread -o Example_cond1 Example_cond1.c |
@ -0,0 +1,42 @@ |
|||||
|
#include<stdio.h> |
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
|
||||
|
void *functionC(); |
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
int counter = 0; |
||||
|
|
||||
|
void *function1() |
||||
|
{ |
||||
|
//...
|
||||
|
pthread_mutex_lock(&lock1); // - Execution step 1
|
||||
|
pthread_mutex_lock(&lock2); // - Execution step 3 DEADLOCK!!!
|
||||
|
//...
|
||||
|
//...
|
||||
|
pthread_mutex_lock(&lock2); |
||||
|
pthread_mutex_lock(&lock1); |
||||
|
... |
||||
|
} |
||||
|
|
||||
|
void *function2() |
||||
|
{ |
||||
|
//...
|
||||
|
pthread_mutex_lock(&lock2); // - Execution step 2
|
||||
|
pthread_mutex_lock(&lock1); |
||||
|
//...
|
||||
|
//...
|
||||
|
pthread_mutex_lock(&lock1); |
||||
|
pthread_mutex_lock(&lock2); |
||||
|
//...
|
||||
|
} |
||||
|
|
||||
|
int main() |
||||
|
{ |
||||
|
//...
|
||||
|
pthread_create(&thread1, NULL, function1, NULL); |
||||
|
pthread_create(&thread2, NULL, function1, NULL); |
||||
|
//...
|
||||
|
} |
||||
|
|
Binary file not shown.
@ -0,0 +1,40 @@ |
|||||
|
#include<stdio.h> |
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
|
||||
|
#define NTHREADS 10 |
||||
|
void *thread_function(void *); |
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
int counter = 0; |
||||
|
|
||||
|
int main() |
||||
|
{ |
||||
|
pthread_t thread_id[NTHREADS]; |
||||
|
int i, j; |
||||
|
|
||||
|
for(i=0; i < NTHREADS; i++) |
||||
|
{ |
||||
|
pthread_create( &thread_id[i], NULL, thread_function, NULL ); |
||||
|
} |
||||
|
|
||||
|
for(j=0; j < NTHREADS; j++) |
||||
|
{ |
||||
|
pthread_join( thread_id[j], NULL); |
||||
|
} |
||||
|
|
||||
|
/* Now that all threads are complete I can print the final result. */ |
||||
|
/* Without the join I could be printing a value before all the threads */ |
||||
|
/* have been completed. */ |
||||
|
|
||||
|
printf("Final counter value: %d\n", counter); |
||||
|
} |
||||
|
|
||||
|
void *thread_function(void *dummyPtr) |
||||
|
{ |
||||
|
printf("Thread number %ld\n", pthread_self()); |
||||
|
pthread_mutex_lock( &mutex1 ); |
||||
|
counter++; |
||||
|
pthread_mutex_unlock( &mutex1 ); |
||||
|
} |
@ -0,0 +1 @@ |
|||||
|
gcc -pthread -o Example_join1 Example_join1.c |
Binary file not shown.
@ -0,0 +1,44 @@ |
|||||
|
#include<stdio.h> |
||||
|
#include<string.h> |
||||
|
#include<pthread.h> |
||||
|
#include<stdlib.h> |
||||
|
#include<unistd.h> |
||||
|
|
||||
|
void *functionC(); |
||||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
||||
|
int counter = 0; |
||||
|
|
||||
|
int main() |
||||
|
{ |
||||
|
int rc1, rc2; |
||||
|
pthread_t thread1, thread2; |
||||
|
|
||||
|
/* Create independent threads each of which will execute functionC */ |
||||
|
|
||||
|
if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) ) |
||||
|
{ |
||||
|
printf("Thread creation failed: %d\n", rc1); |
||||
|
} |
||||
|
|
||||
|
if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) ) |
||||
|
{ |
||||
|
printf("Thread creation failed: %d\n", rc2); |
||||
|
} |
||||
|
|
||||
|
/* Wait till threads are complete before main continues. Unless we */ |
||||
|
/* wait we run the risk of executing an exit which will terminate */ |
||||
|
/* the process and all threads before the threads have completed. */ |
||||
|
|
||||
|
pthread_join( thread1, NULL); |
||||
|
pthread_join( thread2, NULL); |
||||
|
|
||||
|
exit(0); |
||||
|
} |
||||
|
|
||||
|
void *functionC() |
||||
|
{ |
||||
|
pthread_mutex_lock( &mutex1 ); |
||||
|
counter++; |
||||
|
printf("Counter value: %d\n",counter); |
||||
|
pthread_mutex_unlock( &mutex1 ); |
||||
|
} |
@ -0,0 +1 @@ |
|||||
|
gcc -pthread -o Example_sync1 Example_sync1.c |
Loading…
Reference in new issue