diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1 new file mode 100755 index 0000000..5e83bb7 Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1 differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c new file mode 100644 index 0000000..d6fff5b --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include + +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); + } + +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh new file mode 100755 index 0000000..55dd026 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o Example_cond1 Example_cond1.c diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c new file mode 100644 index 0000000..0c226d0 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +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); + //... + } + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1 new file mode 100755 index 0000000..0c216b8 Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1 differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c new file mode 100644 index 0000000..8ee2023 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +#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 ); +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh new file mode 100755 index 0000000..4d88c29 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o Example_join1 Example_join1.c diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1 b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1 new file mode 100755 index 0000000..80d46ed Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1 differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c new file mode 100644 index 0000000..333bb1a --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +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 ); +} diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh new file mode 100755 index 0000000..284bcbd --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh @@ -0,0 +1 @@ +gcc -pthread -o Example_sync1 Example_sync1.c