diff --git a/testsuits_alpine/pthread-cond b/testsuits_alpine/pthread-cond new file mode 100755 index 0000000..f24440b Binary files /dev/null and b/testsuits_alpine/pthread-cond differ diff --git a/testsuits_alpine/pthread-cond.c b/testsuits_alpine/pthread-cond.c new file mode 100644 index 0000000..2a5f8a0 --- /dev/null +++ b/testsuits_alpine/pthread-cond.c @@ -0,0 +1,77 @@ +#include +#include +#include + +pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; + +void *functionCount1(); +void *functionCount2(); +int count = 0; +#define COUNT_DONE 10 +#define COUNT_HALT1 3 +#define COUNT_HALT2 6 + +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); + + printf("Final count: %d\n",count); + + exit(EXIT_SUCCESS); +} + +// Write numbers 1-3 and 8-10 as permitted by functionCount2() + +void *functionCount1() +{ + for(;;) + { + // Lock mutex and then wait for signal to relase mutex + pthread_mutex_lock( &count_mutex ); + + // Wait while functionCount2() operates on count + // mutex unlocked if condition varialbe in functionCount2() signaled. + pthread_cond_wait( &condition_var, &count_mutex ); + count++; + printf("Counter value functionCount1: %d\n",count); + + pthread_mutex_unlock( &count_mutex ); + + if(count >= COUNT_DONE) return(NULL); + } +} + +// Write numbers 4-7 + +void *functionCount2() +{ + for(;;) + { + pthread_mutex_lock( &count_mutex ); + + if( count < COUNT_HALT1 || count > COUNT_HALT2 ) + { + // Condition of if statement has been met. + // Signal to free waiting thread by freeing the mutex. + // Note: functionCount1() is now permitted to modify "count". + pthread_cond_signal( &condition_var ); + } + else + { + count++; + printf("Counter value functionCount2: %d\n",count); + } + + pthread_mutex_unlock( &count_mutex ); + + if(count >= COUNT_DONE) return(NULL); + } + +} diff --git a/testsuits_alpine/pthread-create b/testsuits_alpine/pthread-create new file mode 100755 index 0000000..d1fc403 Binary files /dev/null and b/testsuits_alpine/pthread-create differ diff --git a/testsuits_alpine/pthread-create.c b/testsuits_alpine/pthread-create.c new file mode 100644 index 0000000..21bc840 --- /dev/null +++ b/testsuits_alpine/pthread-create.c @@ -0,0 +1,48 @@ +#include +#include +#include + +void *print_message_function( void *ptr ); + +main() +{ + pthread_t thread1, thread2; + const char *message1 = "Thread 1"; + const char *message2 = "Thread 2"; + int iret1, iret2; + + /* Create independent threads each of which will execute function */ + + iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); + if(iret1) + { + fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1); + exit(EXIT_FAILURE); + } + + iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); + if(iret2) + { + fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2); + exit(EXIT_FAILURE); + } + + printf("pthread_create() for thread 1 returns: %d\n",iret1); + printf("pthread_create() for thread 2 returns: %d\n",iret2); + + /* 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(EXIT_SUCCESS); +} + +void *print_message_function( void *ptr ) +{ + char *message; + message = (char *) ptr; + printf("%s \n", message); +} diff --git a/testsuits_alpine/pthread-join b/testsuits_alpine/pthread-join new file mode 100755 index 0000000..bcf7f21 Binary files /dev/null and b/testsuits_alpine/pthread-join differ diff --git a/testsuits_alpine/pthread-join.c b/testsuits_alpine/pthread-join.c new file mode 100644 index 0000000..056ff32 --- /dev/null +++ b/testsuits_alpine/pthread-join.c @@ -0,0 +1,37 @@ +#include +#include + +#define NTHREADS 10 +void *thread_function(void *); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; +int counter = 0; + +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 ); +} diff --git a/testsuits_alpine/pthread-mutex b/testsuits_alpine/pthread-mutex new file mode 100755 index 0000000..b21743a Binary files /dev/null and b/testsuits_alpine/pthread-mutex differ diff --git a/testsuits_alpine/pthread-mutex.c b/testsuits_alpine/pthread-mutex.c new file mode 100644 index 0000000..8394e87 --- /dev/null +++ b/testsuits_alpine/pthread-mutex.c @@ -0,0 +1,42 @@ +#include +#include +#include + +void *functionC(); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; +int counter = 0; + +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(EXIT_SUCCESS); +} + +void *functionC() +{ + pthread_mutex_lock( &mutex1 ); + counter++; + printf("Counter value: %d\n",counter); + pthread_mutex_unlock( &mutex1 ); +}