| 1 | /***********************************************************************************
|
|---|
| 2 | C-DAC Tech Workshop : hyPACK-2013
|
|---|
| 3 | October 15-18,2013
|
|---|
| 4 | Example : pthread-demo-datarace.c
|
|---|
| 5 |
|
|---|
| 6 | Objective : Write Pthread code to illustrate Data Race Condition
|
|---|
| 7 | and its solution using MUTEX.
|
|---|
| 8 |
|
|---|
| 9 | Input : Nothing.
|
|---|
| 10 |
|
|---|
| 11 | Output : Value of Global variable with and without using Mutex.
|
|---|
| 12 |
|
|---|
| 13 | Created :MAY-2013
|
|---|
| 14 |
|
|---|
| 15 | E-mail : hpcfte@cdac.in
|
|---|
| 16 |
|
|---|
| 17 | ****************************************************************************/
|
|---|
| 18 |
|
|---|
| 19 | #include <pthread.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 | #include <stdio.h>
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | int myglobal; // declaration of global variable
|
|---|
| 25 | pthread_mutex_t mymutex = {0,-1,0,0,{0,0,0,0,0}}; // initialization of MUTEX variable
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | void *thread_function_datarace(void *arg) // Function which operates on myglobal without using mutex
|
|---|
| 29 | {
|
|---|
| 30 | int i,j;
|
|---|
| 31 | for ( i=0; i<20; i++ )
|
|---|
| 32 | {
|
|---|
| 33 | j=myglobal;
|
|---|
| 34 | j=j+1;
|
|---|
| 35 | printf("\nIn thread_function_datarace..\t"); // Incrementing j and assign myglobal value of j
|
|---|
| 36 | // fflush(stdout);
|
|---|
| 37 | sleep(1);
|
|---|
| 38 | myglobal=j;
|
|---|
| 39 | }
|
|---|
| 40 | return NULL;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void *thread_function_mutex(void *arg) // Function which operates on myglobal using mutex
|
|---|
| 44 | {
|
|---|
| 45 | int i,j;
|
|---|
| 46 | for ( i=0; i<20; i++ )
|
|---|
| 47 | {
|
|---|
| 48 | pthread_mutex_lock(&mymutex);
|
|---|
| 49 | j=myglobal;
|
|---|
| 50 | j=j+1;
|
|---|
| 51 | printf("\nIn thread_function_mutex..\t");
|
|---|
| 52 | // fflush(stdout);
|
|---|
| 53 | sleep(0.1);
|
|---|
| 54 | myglobal=j;
|
|---|
| 55 | pthread_mutex_unlock(&mymutex);
|
|---|
| 56 | }
|
|---|
| 57 | return NULL;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | int main(void)
|
|---|
| 61 | {
|
|---|
| 62 |
|
|---|
| 63 | pthread_t mythread;
|
|---|
| 64 | int i;
|
|---|
| 65 |
|
|---|
| 66 | if ( pthread_create( &mythread, NULL, thread_function_datarace, NULL) ) // calling thread_function_datarace
|
|---|
| 67 | {
|
|---|
| 68 | printf("error creating thread.");
|
|---|
| 69 | abort();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | printf("\n\t\t---------------------------------------------------------------------------");
|
|---|
| 73 | printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
|
|---|
| 74 | printf("\n\t\t Email : hpcfte@cdac.in");
|
|---|
| 75 | printf("\n\t\t---------------------------------------------------------------------------");
|
|---|
| 76 | printf("\n\t\t Objective : Pthread code to illustrate data race condition and its solution \n ");
|
|---|
| 77 | printf("\n\t\t..........................................................................\n");
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | for ( i=0; i<20; i++)
|
|---|
| 82 | {
|
|---|
| 83 | myglobal=myglobal+1;
|
|---|
| 84 | printf("\nIn main..\t");
|
|---|
| 85 | // fflush(stdout);
|
|---|
| 86 | sleep(1);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | if ( pthread_join ( mythread, NULL ) )
|
|---|
| 90 | {
|
|---|
| 91 | printf("error joining thread.");
|
|---|
| 92 | abort();
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | printf("\nValue of myglobal in thread_function_datarace is : %d\n",myglobal);
|
|---|
| 96 |
|
|---|
| 97 | printf("\n ----------------------------------------------------------------------------------------------------\n");
|
|---|
| 98 |
|
|---|
| 99 | myglobal = 0;
|
|---|
| 100 |
|
|---|
| 101 | if ( pthread_create( &mythread, NULL, thread_function_mutex, NULL) ) // calling thread_function_mutex
|
|---|
| 102 | {
|
|---|
| 103 | printf("error creating thread.");
|
|---|
| 104 | abort();
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | for ( i=0; i<20; i++)
|
|---|
| 108 | {
|
|---|
| 109 | pthread_mutex_lock(&mymutex);
|
|---|
| 110 | myglobal=myglobal+1;
|
|---|
| 111 | pthread_mutex_unlock(&mymutex);
|
|---|
| 112 | printf("\nIn main..\t");
|
|---|
| 113 | // fflush(stdout);
|
|---|
| 114 | sleep(1);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | if ( pthread_join ( mythread, NULL ) )
|
|---|
| 118 | {
|
|---|
| 119 | printf("error joining thread.");
|
|---|
| 120 | abort();
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | printf("\n");
|
|---|
| 124 | printf("\nValue of myglobal in thread_function_mutex is : %d\n",myglobal);
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | exit(0);
|
|---|
| 128 |
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|