void giveUpForks(int i) { int left, right; //gtthread_mutex_lock(&monitor); philosopherState[i] = THINKING; left = (i + NO_OF_PHILOSOPHERS - 1)% NO_OF_PHILOSOPHERS; right = (i) % NO_OF_PHILOSOPHERS; if(i % 2 == 0) { gtthread_mutex_unlock(&chopstickLock[right]); gtthread_mutex_unlock(&chopstickLock[left]); } else { gtthread_mutex_unlock(&chopstickLock[left]); gtthread_mutex_unlock(&chopstickLock[right]); } //gtthread_mutex_unlock(&monitor); attemptToEat(left); attemptToEat(right); }
int main() { gtthread_t t1, t2, t3, t4, t5; gtthread_init(1000); gtthread_mutex_init(>tm); gtthread_create(&t1, func1, (void*) 1); gtthread_create(&t2, func1, (void*) 2); gtthread_join(t1, NULL); gtthread_join(t2, NULL); gtthread_mutex_lock(>tm); gtthread_create(&t3, func2, (void*) 3); gtthread_create(&t4, func2, (void*) 4); printf("This should print before threads 3 and 4 finish\n"); gtthread_mutex_unlock(>tm); gtthread_join(t3, NULL); gtthread_join(t4, NULL); printf("Testing yield with one schedulable thread... "); gtthread_yield(); gtthread_yield(); gtthread_yield(); printf("done!\n"); printf("Deadlocking with thread 5...\n"); gtthread_mutex_lock(>tm); gtthread_create(&t5, func3, NULL); gtthread_join(t5, NULL); gtthread_mutex_unlock(>tm); printf("!!! ERROR !!! Failed to deadlock with thread 5\n"); return 0; }
void *philosopher(void *arg){ short i; gtthread_mutex_t f1,f2; switch((int)arg){ case 1: f1=m5; f2=m1;break; case 2: f1=m2; f2=m1;break; case 3: f1=m3; f2=m2;break; case 4: f1=m4; f2=m3;break; case 5: f1=m5; f2=m4;break; } while(1){ printf("Philosopher #%d tries to acquire fork %lu\n",(int)arg,f1); gtthread_mutex_lock(&f1); printf("Philosopher #%d tries to acquire fork %lu\n",(int)arg,f2); gtthread_mutex_lock(&f2); printf("Philosopher #%d eating with fork %lu and %lu\n",(int)arg,f1,f2); gtthread_yield(); i=rand()%9999; while(--i>0); printf("Philosopher #%d releasing fork %lu\n",(int)arg,f2); gtthread_mutex_unlock(&f2); printf("Philosopher #%d releasing fork %lu\n",(int)arg,f1); gtthread_mutex_unlock(&f1); printf("Philosopher #%d thinking\n",(int)arg); gtthread_yield(); i=rand()%9999; while(--i>0); } }
void eat(int id) { int c[2]; int r = rand() % 4; if(id == 0) { c[0] = gtthread_mutex_lock(chopstick[4]); c[1] = gtthread_mutex_lock(chopstick[0]); } else if(id % 2 == 1) { c[0] = gtthread_mutex_lock(chopstick[id]); c[1] = gtthread_mutex_lock(chopstick[(id - 1) % 5]); } else if(id % 2 == 0) { c[0] = gtthread_mutex_lock(chopstick[(id - 1) % 5]); c[1] = gtthread_mutex_lock(chopstick[id]); } if (c[0] == 0 && c[1] == 0) { printf("Philosopher %d is eating for %d seconds.\n", id, r); sleep(r); } if(id == 0) { c[0] = gtthread_mutex_unlock(chopstick[4]); c[1] = gtthread_mutex_lock(chopstick[0]); } else if(id % 2 == 1) { c[0] = gtthread_mutex_unlock(chopstick[id]); c[1] = gtthread_mutex_unlock(chopstick[(id - 1) % 5]); } else if(id % 2 == 0) { c[0] = gtthread_mutex_unlock(chopstick[(id - 1) % 5]); c[1] = gtthread_mutex_unlock(chopstick[id]); } printf("Philosopher %d is full.\n", id); }
void down_chopsticks (int c1, int c2) { gtthread_mutex_unlock (&chopstick[c1]); gtthread_mutex_unlock (&chopstick[c2]); }
int main() { int i; gtthread_t t1, t2, t3, t6; void *ret; gtthread_mutex_init(&count_lock); gtthread_init(5); gtthread_create(&t1, thr1, (void *) 1); gtthread_create(&t2, thr2, (void *) 2); gtthread_create(&t3, thr3, (void *) 3); gtthread_create(&t6, thr6, (void *) 6); for (i = 0; i < LOOP_MAX; i++) { printf("Main thread!\n"); fflush(stdout); gtthread_mutex_lock(&count_lock); count++; gtthread_mutex_unlock(&count_lock); if (i % YIELD_VAL == 0) { gtthread_yield(); } } printf("main waiting for thr1...\n"); fflush(stdout); gtthread_join(t1, &ret); printf("thr1 joined main with value %d!\n", (int) ret); fflush(stdout); gtthread_cancel(t2); printf("thr2 cancelled!\n"); fflush(stdout); printf("main waiting for thr3...\n"); fflush(stdout); gtthread_join(t3, &ret); printf("thr3 joined main with value %d!\n", (int) ret); fflush(stdout); printf("main waiting for thr6...\n"); fflush(stdout); gtthread_join(t6, &ret); printf("thr6 joined main with value %d!\n", (int) ret); fflush(stdout); gtthread_mutex_lock(&count_lock); printf("Final count value: %lu\n", count); fflush(stdout); gtthread_mutex_unlock(&count_lock); gtthread_mutex_destroy(&count_lock); return EXIT_SUCCESS; }
void* thr2(void *in) { gtthread_mutex_lock(&g_mutex); gtthread_mutex_unlock(&g_mutex); int k = 0; for (k = 0; k < 5; ++k) { gtthread_mutex_lock(&g_mutex); printf("thr2 hello\n"); gtthread_mutex_unlock(&g_mutex); gtthread_yield(); } return NULL; }
void release_chopsticks (int id) { if (id%2 == 0) { gtthread_mutex_unlock(&chopstick[(id+1)%5]); printf("Philosopher #%d released right chopstick.\n", id); gtthread_mutex_unlock(&chopstick[id]); printf("Philosopher #%d released left chopstick.\n", id); } else { gtthread_mutex_unlock(&chopstick[id]); printf("Philosopher #%d released left chopstick.\n", id); gtthread_mutex_unlock(&chopstick[(id+1)%5]); printf("Philosopher #%d released right chopstick.\n", id); } }
void* worker(void* arg) { gtthread_mutex_lock(&g_mutex); ++g_num; gtthread_mutex_unlock(&g_mutex); return NULL; }
void *thr4(void *in) { int i, j = 0; printf("thr4 started with parameter %d\n", (int) in); fflush(stdout); for (i = 0; i < LOOP_MAX; i++) { printf("thr4!\n"); fflush(stdout); j++; gtthread_mutex_lock(&count_lock); count++; gtthread_mutex_unlock(&count_lock); if (i % YIELD_VAL == 0) { gtthread_yield(); } } printf("thr4 finished with parameter %d\n", (int) in); fflush(stdout); return (void *) j + 4; }
void think(int phil) { gtthread_mutex_lock(&g_mutex); if(state[phil] == EATING) { printf("Philosopher %d is putting chopsticks %d and %d down \n", (phil+1), (phil+5)%N, (phil+1)%N); } state[phil] = THINKING; printf("Philosopher %d is thinking \n", phil+1); gtthread_mutex_unlock(&g_mutex); }
void hungry(int phil) { gtthread_mutex_lock(&g_mutex); state[phil] = HUNGRY; printf("Philosopher %d is hungry \n", phil+1); for(i = 0; i<99999999; i++); eat(phil); gtthread_mutex_unlock(&g_mutex); }
void *diner(int *i) { int philId, eat, think, tTime, eTime; //int eating = 0; printf("I'm Philosopher %d\n",*i); philId = *i; int forwardCycle=0; if(philId%2==0){ forwardCycle=1; } while (1) { philosopher_status[philId]=THINKING; printf("Philosopher %d is thinking\n", philId); think=rand()%10000000; for(tTime=0;tTime<think;tTime++); philosopher_status[philId]=HUNGRY; printf("Philosopher %d is hungry\n", philId); while(philosopher_status[philId+CHOPSTICKS-1]==EATING || philosopher_status[philId+1]==EATING); if(forwardCycle){ gtthread_mutex_lock(&chopstick_mutex[philId]); gtthread_mutex_lock(&chopstick_mutex[(philId+1)%CHOPSTICKS]); } else{ gtthread_mutex_lock(&chopstick_mutex[(philId+1)%CHOPSTICKS]); gtthread_mutex_lock(&chopstick_mutex[philId]); } philosopher_status[philId]=EATING; printf("Philosopher %d is eating\n", philId); eat=rand()%10000000; for(eTime=0;eTime<eat;eTime++); printf("Philosopher %d is done eating\n", philId); if(forwardCycle){ gtthread_mutex_unlock(&chopstick_mutex[(philId+1)%CHOPSTICKS]); gtthread_mutex_unlock(&chopstick_mutex[philId]); } else{ gtthread_mutex_unlock(&chopstick_mutex[philId]); gtthread_mutex_unlock(&chopstick_mutex[(philId+1)%CHOPSTICKS]); } } gtthread_exit(NULL); }
void attemptToEat(int i) { gtthread_mutex_lock(monitor); philosopherState[i] = HUNGRY; checkIfCanEat(i); gtthread_mutex_unlock(monitor); gtthread_mutex_lock(chopstickLock[i]); }
void* worker(void* arg) { int i; gtthread_mutex_lock(&g_mutex); for(i=0; i < 3; i++) { printf("thread %ld\n", (long)arg); gtthread_yield(); } gtthread_mutex_unlock(&g_mutex); return NULL; }
int food_on_table () { static int food = FOOD; int myfood; gtthread_mutex_lock (&food_lock); if (food > 0) { food--; } myfood = food; gtthread_mutex_unlock (&food_lock); return myfood; }
void *thrR(void *in) { int ndx; int th = (int)(long)in; printf("thrR: enter\n"); fflush(stdout); for(ndx=0; ndx<100; ++ndx) { gtthread_mutex_lock(&m[0]); printf("thrR: lock %d\n",th); fflush(stdout); usleep(100*1000); printf("thrR: unlock %d\n",th); fflush(stdout); gtthread_mutex_unlock(&m[0]); } printf("thrR: exit\n"); fflush(stdout); return in; }
void giveUpForks(int i) { int left, right; gtthread_mutex_lock(monitor); philosopherState[i] = THINKING; left = (i + NO_OF_PHILOSOPHERS - 1)% NO_OF_PHILOSOPHERS; right = (i) % NO_OF_PHILOSOPHERS; checkIfCanEat(left); checkIfCanEat(right); gtthread_mutex_unlock(monitor); }
void checkIfCanEat(int i) { int left, right; right =(i + 1) % NO_OF_PHILOSOPHERS; left = (i + NO_OF_PHILOSOPHERS - 1)% NO_OF_PHILOSOPHERS; if(philosopherState[i] == HUNGRY && philosopherState[left] != EATING && philosopherState[right] != EATING) { philosopherState[i] == EATING; gtthread_mutex_unlock(chopstickLock[i]); } }
void *thr3(void *in) { printf("Hello World! from thread 3\n"); fflush(stdout); double sum; gtthread_mutex_lock(&g_mutex); g_count = g_count + 3; printf("global count from thread 3: %d\n", g_count); gtthread_mutex_unlock(&g_mutex); sum = killCycle(3, CYCLE_NUM); // gtthread_exit(NULL); printf("returning from thread 3 %f \n", sum); return NULL ; // gtthread_yield(); }
int main() { printf("test6b. Should print 'main hello' then 'thr1 hello' and 'thr2 hello' for 5 times interleaving (but no mixed up prints e.g. 'th1 thr2 hello hello')\n"); gtthread_init(50000L); gtthread_mutex_init(&g_mutex); gtthread_mutex_lock(&g_mutex); gtthread_create( &t1, thr1, NULL); gtthread_create( &t2, thr2, NULL); gtthread_yield(); printf("main hello\n"); gtthread_mutex_unlock(&g_mutex); gtthread_join(t2, NULL); gtthread_join(t1, NULL); return EXIT_SUCCESS; }
void *func2(void *arg) { long x = (long) arg; volatile int y; for(int i = 5; i; --i) { gtthread_mutex_lock(>tm); gtthread_yield(); char *dst = buffer, *src = "\tThis is a locked message from thread "; while((*dst++ = *src++)) { y = 5000000; while(--y); } printf("%s%ld\n", buffer, x); memset(buffer, ' ', 50); gtthread_mutex_unlock(>tm); } return NULL; }
void *thrX(void *in) { int ndx; int th = (int)(long)in; int dur = (int)hold[th]; printf("thrX: enter\n"); fflush(stdout); for(ndx=0; ndx<10; ++ndx) { gtthread_mutex_lock(&m[0]); printf("thrX: lock %d\n",th); fflush(stdout); gtthread_yield(); usleep(dur*1000); printf("thrX: unlock %d\n",th); fflush(stdout); gtthread_mutex_unlock(&m[0]); gtthread_yield(); } printf("thrX: exit\n"); fflush(stdout); gtthread_exit(in); return in; }
void pickChopsticks(int i) { int left, right; left = (i+1)%NO_OF_PHILOSOPHERS; right = (i+NO_OF_PHILOSOPHERS - 1)%NO_OF_PHILOSOPHERS; if(i%2 == 1) { gtthread_mutex_unlock(&chopsticks[left]); gtthread_mutex_lock(&chopsticks[right]); } else { gtthread_mutex_lock(&chopsticks[right]); gtthread_mutex_lock(&chopsticks[left]); } fprintf(stderr, "\nPhilosopher %d is eating", i); }
void *thrH(void *in) { int ndx; int th = (int)in; int dur = (int)hold[th]; gtthread_mutex_t* mtx = m[0]; printf("thrH: enter\n"); fflush(stdout); for(ndx=0; ndx<10; ++ndx) { gtthread_mutex_lock(mutex); printf("thrH: lock %d\n",th); fflush(stdout); gtthread_yield(); usleep(dur*1000); gtthread_yield(); printf("thrH: unlock %d\n",th); fflush(stdout); gtthread_mutex_unlock(mutex); gtthread_yield(); } printf("thrH: exit\n"); fflush(stdout); return in; }
double* thr1(void *in) { printf("Hello World! from thread 1\n"); fflush(stdout); double sum; gtthread_mutex_lock(&g_mutex); printf("%p,%p\n",g_mutex,&g_mutex); g_count = g_count + 1; printf("global count from thread 1: %d\n", g_count); sum = killCycle(1, CYCLE_NUM); gtthread_mutex_unlock(&g_mutex); // gtthread_exit(NULL); printf("returning from thread 1 %f \n", sum); // gtthread_exit(NULL); double *ret_ptr = malloc(sizeof(double)); *ret_ptr = sum; printf("returning from thread with value= %f \n", sum); return ret_ptr; // gtthread_yield(); // return NULL; }
int main() { int rc; int rep; int ndx; printf("test mutex init, lock, unlock, destroy\n"); for( ndx=0; ndx<100; ++ndx ) { hold[ndx] = 100+(ndx%10)*10; } printf("main: enter\n"); gtthread_init(period); for( ndx=0; ndx<10; ++ndx ) { rc = gtthread_mutex_init(&m[ndx]); printf("init %d\n",ndx); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); } for( rep=0; rep<10; ++rep ) { for( ndx=0; ndx<10; ++ndx ) { rc = gtthread_mutex_lock(&m[ndx]); printf("lock(%d)\n",ndx); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); usleep(10*1000); rc = gtthread_mutex_unlock(&m[ndx]); printf("unlock(%d)\n",ndx); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); } } for( ndx=0; ndx<10; ++ndx ) { rc = gtthread_mutex_destroy(&m[ndx]); printf("destroy(%d)\n",ndx,rc); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); } printf("main: exit\n"); fflush(stdout); return EXIT_SUCCESS; }
void *func3(void *arg) { gtthread_mutex_lock(>tm); printf("Printing from thread 5... which shouldn\'t happen\n"); gtthread_mutex_unlock(>tm); return NULL; }