/* * Rick van der Zwet * 0433373 * OS Assigment 3 - Part A * Licence: BSD * $Id: assignment4a.c 539 2008-04-01 20:23:55Z rick $ */ #include #include #include #include #include #define MAXCOUNTER 999999 struct thread_data { int *account1; int *account2; int threadid; }; static int go_quit = 0; void *balance(void *threadarg) { struct thread_data *my_data; my_data = (struct thread_data *) threadarg; int counter, temp1, temp2, amount; int *account1, *account2; account1 = my_data->account1; account2 = my_data->account2; counter = 0; do { temp1 = *account1; temp2 = *account2; amount = random() % 100; // printf("%i - %i: Temp1 %i, temp2 %i, amount %i\n", my_data->threadid, // counter, temp1, temp2, amount); *account1 = temp1 - amount; *account2 = temp2 + amount; counter++; } while (*account1+*account2 == 0 && counter < MAXCOUNTER && go_quit == 0); if (go_quit == 0) { printf("counter: %i\n", counter); go_quit = 1; } pthread_exit(NULL); } int main (int argc, char * argv[]) { pthread_t thread0, thread1; int account1, account2; struct thread_data thread0_data, thread1_data; thread0_data.account1 = &account1; thread0_data.account2 = &account2; thread0_data.threadid = 0; thread1_data.account1 = &account1; thread1_data.account2 = &account2; thread1_data.threadid = 1; account1 = 0; account2 = 0; pthread_create(&thread0, NULL, balance, (void *) &thread0_data); pthread_create(&thread1, NULL, balance, (void *) &thread1_data); pthread_exit(NULL); return(EX_OK); }