zeus 4 years ago
parent
commit
b5ebe8f76e
  1. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1
  2. 69
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c
  3. 1
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh
  4. 42
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.c
  5. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1
  6. 40
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c
  7. 1
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh
  8. BIN
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1
  9. 44
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c
  10. 1
      install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh

BIN
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1

Binary file not shown.

69
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c

@ -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);
}
}

1
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_cond1.c.sh

@ -0,0 +1 @@
gcc -pthread -o Example_cond1 Example_cond1.c

42
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_deadlock1.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);
//...
}

BIN
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1

Binary file not shown.

40
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c

@ -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 );
}

1
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_join1.c.sh

@ -0,0 +1 @@
gcc -pthread -o Example_join1 Example_join1.c

BIN
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1

Binary file not shown.

44
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c

@ -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 );
}

1
install/usr/share/swarmlab.io/sec/project/courses/pthread/Example_sync1.c.sh

@ -0,0 +1 @@
gcc -pthread -o Example_sync1 Example_sync1.c
Loading…
Cancel
Save