Study the code below (obtained from the Cilk manual - so call it
cel-ex1.c).
Assignment 3 (due 2/1): Now run it on a uniprocessor, then a multiprocessor. You
may have to run it multiple times. Did you get it to fail on a
multiprocessor? Explain.
Post a brief summary.
#include <pthread.h>
int aa=0, bb=0, zz=0;
int c1=0, c2=0;
void * thread_routine1(void * arg)
{ do {
c1++;
aa = 1;
if (!bb) zz++;
zz = 0;
bb = 0;
} while(1);
}
void * thread_routine2(void * arg)
{ do {
c2++;
bb = 1;
if (!aa) zz++;
zz = 0;
aa = 0;
} while (1);
}
void * thread_routine3(void * arg)
{ while(1)
if (zz > 1) printf("Caught violation, c1=%d, c2=%d, zz=%d\n", c1, c2, zz);
}
int main()
{ pthread_t t1, t2, t3; pthread_create(&t3, 0, thread_routine3, 0);
pthread_create(&t1, 0, thread_routine1, 0); pthread_create(&t2, 0, thread_routine2, 0);
pthread_join(t1, 0); pthread_join(t2, 0); pthread_join(t3, 0); return 0;
}