void t0(void * dummy) { printf("\nt0 start\n"); makeThreads("t0", t1, n); printf("\nt0 end\n"); MyThreadExit(); }
// evaluate a Fibonacci number: // fib(0) = 0 // fib(1) = 1 // fib(n) = fib(n-1) + fib(n-2) [n>1] // this function is messy because we have to pass everything as a // generic parameter (void*). // also, the function parameter is a value/result -- therefore it is a // pointer to an integer. // void fib(void *in) { int *n = (int *)in; /* cast input parameter to an int * */ if (*n == 0) /* pass */; /* return 0; it already is zero */ else if (*n == 1) /* pass */; /* return 1; it already is one */ else { int n1 = *n - 1; /* child 1 param */ int n2 = *n - 2; /* child 2 param */ // create children; parameter points to int that is initialized. // this is the location they will write to as well. printf("\n Parent %d, calling child: %d", *n, n1); MyThreadCreate(fib, (void*)&n1); printf("\n Parent %d, calling child: %d", *n, n2); MyThreadCreate(fib, (void*)&n2); // after creating children, wait for them to finish MyThreadJoinAll(); // write to addr n_ptr points; return results in addr pointed to // by input parameter printf("\n %d term = ", *n); *n = n1 + n2; printf("%d", *n); } MyThreadExit(); // always call this at end }
//A simple semaphore test that makes the child wait //on his/her parent's signal void semtest(MySemaphore sem) { printf("... Child waits on semaphore with value 0.\n"); MySemaphoreWait(sem); printf("... Child can now go forward. \n"); MyThreadExit(); }
// this unpacks two params from the void pointer that the interface // requires. // void merge_unpack(void *params) { int start, end; sscanf(params, "%d %d", &start, &end); merge(start, end); free(params); /* paired w/ malloc in merge_pack */ MyThreadExit(); }
void testjoinvalid(void *n) { int x = 10; someThread = MyThreadCreate(donothing, (void *)&x); //Exit first thread MyThreadExit(); }
//A recusion test that spawns up to "rcount" threads void recursion(int i) { int j; j=i+1; if (i < rcount) { MyThreadCreate(recursion,j); } MyThreadYield(); MyThreadExit(); }
void testjoininvalid(void *n) { int x = MyThreadJoin(someThread); if(x == -1) { printf("Invalid join\n"); } //Exit the second thread MyThreadExit(); }
void t1(void * who_) { char me[16]; int who = (int)who_; sprintf(me, "t1 %d", who); printf("\n%s start\n", me); makeThreads(me, t2, m); printf("\nt1 %d end\n", who); MyThreadExit(); }
void t1(void * who) { int i; printf("t%d start\n", (int)who); for (i = 0; i < n; i++) { printf("t%d yield\n", (int)who); MyThreadYield(); } printf("t%d end\n", (int)who); MyThreadExit(); }
//The main function. //Should be pretty simple to understand void start_func(void *j) { MyThread tmp[100]; MySemaphore sem[10]; printf("0. Root thread started.\n"); printf("...<MyThreadCreate>>\n"); tmp[1]=MyThreadCreate(printv,NULL ); printf("1. Created a Test Thread. \n"); printf("...<MyThreadYield>>\n"); MyThreadYield(); tmp[2]=MyThreadCreate(printa,2); printf("4. Return value from a valid join (0=%i)\n",MyThreadJoin(tmp[2])); printf("5. Return value from an expired join (-1=%i)\n",MyThreadJoin(tmp[2])); printf("...<MyThreadJoinAll>>\n"); printf("6. Create three threads & then joinall on them.\n"); tmp[3]=MyThreadCreate(printj,NULL); tmp[4]=MyThreadCreate(printj,NULL); tmp[5]=MyThreadCreate(printj,NULL); MyThreadJoinAll(); printf("7. Returned from Join All. \n"); printf("...<MySemaphoreInit>>\n"); sem[1]=MySemaphoreInit(1); sem[2]=MySemaphoreInit(2); sem[3]=MySemaphoreInit(3); printf("8. Created 3 Semaphores with initialvalues 1,2,3.\n"); printf("...<MySemaphoreWait>>\n"); MySemaphoreWait(sem[1]); MySemaphoreWait(sem[2]); MySemaphoreWait(sem[3]); printf("9. Waited on all three Semaphores should have values 0,1,2.\n"); tmp[6]=MyThreadCreate(semtest,sem[1]); MyThreadYield(); MySemaphoreSignal(sem[1]); printf("... Parent Signaled \n"); MyThreadYield(); MySemaphoreWait(sem[2]); tmp[7]=MyThreadCreate(semtest2,sem[2]); printf("10. Lets try it in reverse...\n"); printf("...parent waits on a semaphore with value 0.\n"); MySemaphoreWait(sem[2]); printf("...parent passes through now.\n"); printf("11. Recursively build %i threads ",rcount); MyThreadCreate(recursion,1); MyThreadJoinAll(); printf("... success.\n"); rcount=1000; printf("12. Create a producer/consumer scenario with %i threads",pcount*2); tmp[8]=MyThreadCreate(producer_consumer,NULL); MyThreadJoin(tmp[8]); printf("... success \n"); MyThreadExit(); }
void consumer(void *dummy) { int i,v; printf("I'm a consumer\n"); for (i=0;i<10;i++){ MySemaphoreWait(consume_semaphore); v = get_buffer(i); MySemaphoreSignal(produce_semaphore); printf("got %d ",v); } MyThreadExit(); }
//A producer/consumer program void producer_consumer(void){ MySemaphore sem[pcount]; MyThread producers[pcount]; MyThread consumers[pcount]; int i; for (i=1;i<pcount;i++){ sem[i]=MySemaphoreInit(0); consumers[i]=MyThreadCreate(consumer,sem[i]); producers[i]=MyThreadCreate(producer,sem[i]); } MyThreadExit(); }
void producer(void *dummy) { int i = 0; printf("I'm a producer\n"); for (i=0;i<10;i++){ MySemaphoreWait(produce_semaphore); add_buffer(i); //MyThreadExit(); MySemaphoreSignal(consume_semaphore); //i = i + 1; } MyThreadExit(); }
void testjoinmain(void * n) { MyThread t1 = NULL; MyThread t2 = NULL; //Create the 2 threads t1 = MyThreadCreate(testjoinvalid, "t1"); t2 = MyThreadCreate(testjoininvalid, "t2"); //Join on the first thread MyThreadJoinAll(); //Exit the parent thread MyThreadExit(); }
void func(void *dummy) { int i = (int) dummy,v; MyThread t; //for (i=0;i<10;i++){ MySemaphoreWait(consume_semaphore); printf("I'm a consumer in %d\n",i); //printf("\n In %d", i); if(i==0) t = MyThreadCreate(func,(void *)1); MyThreadJoin(t); // v = get_buffer(i); MySemaphoreSignal(consume_semaphore); printf("Exiting %d ",i); //} MyThreadExit(); }
//A producer program void producer(MySemaphore sem){ MySemaphore test123; MySemaphoreSignal(sem); MyThreadExit(); }
// A simple test function without an argument void printv(void) { int i; printf("2. Test with Void Arg (NULL = NULL)\n"); MyThreadExit(); }
void t0(void * dummy) { printf("t0 start\n"); MyThreadExit(); }
void mergesort(void *n) { merge(0, (int)n); MyThreadExit(); }
//A simple test function with an argument void printa(int n) { printf("3. Test Passing Integer (2 = %i)\n",n); MyThreadExit(); }
//A simple function that is created and then joined void printj(void) { printf("... Joinall Test\n"); MyThreadYield(); MyThreadExit(); }
void t2(void * who) { printf("\nt2 %d start\n", (int)who); MyThreadExit(); }
//A simple semaphore test that makes the parent wait //on his/her child's signal void semtest2(MySemaphore sem) { printf("...Child signals sempahore.\n"); MySemaphoreSignal(sem); MyThreadExit(); }
void donothing(void * n) { printf("Basically doing nothing\n"); MyThreadExit(); }
//A consumer program void consumer(MySemaphore sem){ MySemaphore test123/* = NULL*/; MySemaphoreWait(sem); MyThreadExit(); }