int main() { int i; mythread_t thread[NR_THREADS]; mythread_setconcurrency(3); mythread_mutex_init(&mutex, NULL); mythread_cond_init(&okToProceed, NULL); for (i = 0; i < NR_THREADS; i++) mythread_create(&thread[i], NULL, runner, NULL); sleep(2); printf("Main: sending broadcast signal to all threads\n"); fflush(stdout); mythread_cond_broadcast(&okToProceed); printf("Main: joining all threads\n"); fflush(stdout); for (i = 0; i < NR_THREADS; i++) mythread_join(thread[i], NULL); printf("Main: about to exit\n"); fflush(stdout); mythread_exit(NULL); mythread_cond_destroy(&okToProceed); mythread_mutex_destroy(&mutex); return 1; }
int main (void) { int test; // initialize mutex, condition variable and barrier. mythread_mutex_init (&mut, NULL); mythread_cond_init (&con, NULL); // 4 threads will be calling wait. mythread_barrier_init (&barr, NULL, 4); mythread_setconcurrency (8); // create first 3 threads and assign them functions. mythread_create (&t1, NULL, foo1, NULL); mythread_create (&t2, NULL, foo2, NULL); mythread_create (&t3, NULL, foo3, NULL); // stall main until all threads have finished. if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) PASS--; // after this stage, mutex and condition variables are not required, destroy them. if ( mythread_cond_destroy (&con) != 0) PASS --; if ( mythread_mutex_destroy (&mut) != 0) PASS --; // create next 3 threads and assign functions. This is to test reusability of barriers. mythread_create (&t4, NULL, foo3, NULL); mythread_create (&t5, NULL, foo4, NULL); mythread_create (&t6, NULL, foo5, NULL); // let main also wait on the barrier if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) PASS--; // at this point, we are sure that all the threads have finished running. Now check value of PASS for any errors. // If PASS is 0, means all function executed successfully. If PASS is less than 0, atleast one function returned error. // print PASS or FAIL accordingly. if (PASS == 0) printf ("PASS\n"); else printf ("FAIL\n"); //destroy the barrier and exit. mythread_barrier_destroy (&barr); mythread_exit(NULL); return 0; }
int main(int argc, char **argv) { int i; char buffer[1024]; futex_init(&printFutex, 1); mythread_t mythread1[NTHREADS]; mythread_t signalingThread[NTHREADS/2]; mythread_setconcurrency(4); mythread_mutex_init(&mutex,NULL); mythread_cond_init(&condition,NULL); mythread_barrier_init(&barrier,NULL,NTHREADS+1); /* Create Threads */ for(i=0;i<NTHREADS;i++){ sprintf(buffer, "Created thread : %d\n", i+1); printToConsole(buffer); mythread_create(&mythread1[i],NULL,&thread_func, NULL); } /*Signal threads waiting on cond var*/ while(count<NTHREADS){ /* Special case for testing broadcast*/ if(count == NTHREADS/2){ mythread_cond_broadcast(&condition); }else{ mythread_cond_signal(&condition); } } /* Waiting on barrier. Last thread, or this main thread will unblock the barrier depending on the execution sequence */ mythread_barrier_wait(&barrier); sprintf(buffer, "Out of barrier, main thread exiting..\n"); printToConsole(buffer); /* Destroy mutex, barrier and cond*/ mythread_cond_destroy(&condition); mythread_mutex_destroy(&mutex); mythread_barrier_destroy(&barrier); sprintf(buffer, "Finished Execution\n"); printToConsole(buffer); }
int main(void){ int rc1, rc2; int i; mythread_setconcurrency(4); /* Creating 10 threads */ for ( i = 0; i<10; i++) { rc1 = mythread_create(&threads[i], NULL, thread_function, (void *)NULL); if (rc1) ("ERROR; return code from pthread_create() is %d\n", rc1); } /* Main will now wait for 10 threads */ for(i = 0; i<10; i++) { rc2 = mythread_join(threads[i], NULL); if (rc2) ("ERROR; return code from pthread_join() is %d\n", rc2); } return 0; }
int main() { int i; mythread_t thread[NTHREADS]; mythread_setconcurrency(NTHREADS+1); mythread_mutex_init(&mut, NULL); mythread_cond_init(&cond, NULL); for(i = 0; i < NTHREADS; i++) mythread_create(&thread[i], NULL, fun, NULL); for(i = 0; i < INT_MAX/10; i++); printf("Broadcast\n"); fflush(stdout); mythread_cond_broadcast(&cond); printf("Now join\n"); fflush(stdout); for(i = 0; i < NTHREADS; i++) mythread_join(thread[i], NULL); printf("main exit\n"); fflush(stdout); mythread_exit(NULL); mythread_cond_destroy(&cond); mythread_mutex_destroy(&mut); return 1; }
int main() { mythread_setconcurrency(4); mythread_mutex_init(&lock,NULL); mythread_mutex_init(&lock1,NULL); mythread_cond_init(&cond,NULL); mythread_cond_init(&cond1,NULL); glval=0; glval1=0; mythread_t tid1,tid2,tid3,tid4,tid5; mythread_queue_t head; tid1 = malloc(sizeof(struct mythread)); tid2 = malloc(sizeof(struct mythread)); tid3 = malloc(sizeof(struct mythread)); tid4 = malloc(sizeof(struct mythread)); tid5 = malloc(sizeof(struct mythread)); /**************** Test cases for testing all functions except destroy functions ********************/ mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal) mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal) mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal) printOut("Sleeping for 1 second so that threads calling condition waits starts before their condition variables are signaled\n"); sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent mythread_create(&tid1,NULL,thread1,NULL); mythread_create(&tid2,NULL,thread2,NULL); mythread_join(tid1,NULL); mythread_join(tid2,NULL); printOut("Value of global variable incremented 200 times in two threads : "); printOut(itoa(glval,10)); printOut("\n"); /*************************************************************************************************/ mythread_join(tid3,NULL); mythread_join(tid4,NULL); mythread_join(tid5,NULL); printOut("=================================================================\n"); printOut("Repeat the entire test after destroying(mythread_mutex_destroy,mythread_cond_destroy) and reinitializing 2 locks and 2 condition variables\n"); printOut("=================================================================\n"); /********* Destroying all mmutexes and locks and reinitialising again ****************************/ mythread_mutex_destroy(&lock); mythread_mutex_destroy(&lock1); mythread_cond_destroy(&cond); mythread_cond_destroy(&cond1); mythread_mutex_init(&lock,NULL); mythread_mutex_init(&lock1,NULL); mythread_cond_init(&cond,NULL); mythread_cond_init(&cond1,NULL); /**************************************************************************************************/ /**************** Test cases for testing all functions except destroy functions ********************/ glval=0; mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal) mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal) mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal) printOut("Sleeping for 1 second so that threads calling 'condition wait' starts before their condition variables are signaled\n"); sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent mythread_create(&tid1,NULL,thread1,NULL); mythread_create(&tid2,NULL,thread2,NULL); mythread_join(tid1,NULL); mythread_join(tid2,NULL); printOut("Value of global variable incremented in the repeated test 200 times in two threads : "); printOut(itoa(glval,10)); printOut("\n"); /**************************************************************************************************/ mythread_exit(NULL); }
int main(int argc, char *argv[]) { int rc = 0; mythread_t th[THREAD_COUNT]; mythread_attr_t *attr[THREAD_COUNT]; struct sched_param sparam1; long i; mythread_setconcurrency(CONCURRENCY_COUNT); //printf("concurrency is %d\n", mythread_getconcurrency()); mythread_mutex_init(&mutex, NULL); for (i = 0; i < THREAD_COUNT; i++) { attr[i] = malloc(sizeof(mythread_attr_t)); mythread_attr_init(attr[i]); switch (i) { case 2: sparam1.__sched_priority = 9; mythread_attr_setschedparam(attr[i], &sparam1); break; case 3: sparam1.__sched_priority = 8; mythread_attr_setschedparam(attr[i], &sparam1); break; case 4: sparam1.__sched_priority = 7; mythread_attr_setschedparam(attr[i], &sparam1); break; case 5: sparam1.__sched_priority = 6; mythread_attr_setschedparam(attr[i], &sparam1); break; case 6: sparam1.__sched_priority = 5; mythread_attr_setschedparam(attr[i], &sparam1); break; default: break; } mythread_create(&th[i], attr[i], thread_func, (void*) i); } for (i = 0; i < THREAD_COUNT; i++) { mythread_join(th[i], NULL); } /* Test: mythread_attr_getschedparam()*/ for (i = 0; i < THREAD_COUNT; i++) { int rc = 0; struct sched_param sparam2; rc = mythread_attr_getschedparam(attr[i], &sparam2); if(!rc){ switch (i) { case 2: sparam2.__sched_priority = 9; score = score + 0.7; break; case 3: sparam2.__sched_priority = 8; score = score + 0.7; break; case 4: sparam2.__sched_priority = 7; score = score + 0.7; break; case 5: sparam2.__sched_priority = 6; score = score + 0.7; break; case 6: sparam2.__sched_priority = 5; score = score + 0.7; break; default: break; } } // printf("sparam2.sched_priority = %d\n", // sparam2.__sched_priority); //show_sched_param(attr[i]); } /* Test 1*/ printf("# Test Case 1:"); //printf("Expected Output: %s\n", EXPECTED_OUTPUT1); //printf("Actual Output: %s\n", actualOutput); if (!strcmp(EXPECTED_OUTPUT1, actualOutput) || !strcmp(EXPECTED_OUTPUT2, actualOutput)) { printf("PASS\n"); score = score + 25; } else { printf("FAIL\n"); } /*Test: mythread_attr_destroy() */ int deduction = 0; for (i = 0; i < THREAD_COUNT; i++) { rc = mythread_attr_destroy(attr[i]); if(rc){ deduction = deduction + 0.5; } free(attr[i]); } if(rc){ score = score - deduction; printf("Deduction occurred.\n"); } mythread_mutex_destroy(&mutex); printf("## Score: %d\n", score); return 0; }