//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(); }
int fn1() { int n = 10; printf("\nExecuting F1, ready queue id: %d\n", ready_queue->id); printf("\n N = %d", n); printf("\n &N = %d", &n); sem1 = MySemaphoreInit(2); sem2 = MySemaphoreInit(0); printf("CPAfterSemaphoreCreation"); printQueues(); printf("\nGetting SemaphoreId: %d",sem1.semaphoreId); temp = getSemaphoreFromId(sem1.semaphoreId); printf("\nF2 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); temp = getSemaphoreFromId(sem2.semaphoreId); printf("\nF2 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); child2 = MyThreadCreate((void*)fn2, &n); child2 = MyThreadCreate((void*)fn2, &n); child2 = MyThreadCreate((void*)fn3, &n); child2 = MyThreadCreate((void*)fn2, &n); printf("\nthis is from 1_1\n"); printf("\n Joining the threads: parent %d(won't run again), child %d", ready_queue->id, child2.id); MyThreadJoinAll(); printf("\nthis is from 1_2\n"); printf("If printed, %d has joined successfully", ready_queue->id); }
// 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 }
void merge(int start, int end) { if (end - start < 2) return; else { mid = (start+end)/2; /* split list in half */ MyThreadCreate(merge_unpack, merge_pack(start, mid)); // sort 1st half MyThreadCreate(merge_unpack, merge_pack(mid, end)); // sort 2nd half MyThreadJoinAll(); k = start; j = mid; // merge the lists while (k < j) { if (j == end) break; if (List[k] <= List[j]) k++; else { tmp = List[k]; List[k] = List[j]; // insert tmp in the proper place // assume this can be done... } } } }
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(); }