void fn3(void *args) { int *n = (int*)args; printQueues(); printf("\nExecuting F3, ready queue id: %d\n", ready_queue->id); printf("\n The Argument Obtained is: %d", args); MySemaphoreSignal(sem1); temp = getSemaphoreFromId(sem1.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); MySemaphoreSignal(sem2); temp = getSemaphoreFromId(sem2.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); MySemaphoreSignal(sem1); temp = getSemaphoreFromId(sem1.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); MySemaphoreSignal(sem2); temp = getSemaphoreFromId(sem2.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); MySemaphoreSignal(sem2); temp = getSemaphoreFromId(sem2.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); MySemaphoreSignal(sem2); temp = getSemaphoreFromId(sem2.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); MySemaphoreSignal(sem2); temp = getSemaphoreFromId(sem2.semaphoreId); printf("\nF3 Semaphore ID: %d has Value: %d", temp->semaphoreId,temp->semaphoreValue); printQueues(); }
//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(); }
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 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 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(); }