Ejemplo n.º 1
0
//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 fn2(void *args)
{
 int *n = (int*)args;
 //printQueues();
 printf("\nExecuting F2, ready queue id: %d\n", ready_queue->id);
 printf("\n The Argument Obtained is: %d", args);
 MySemaphoreWait(sem1);
 MySemaphoreWait(sem2);
 //printQueues();
 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);
}
Ejemplo n.º 3
0
//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();
}
Ejemplo n.º 4
0
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();
}
Ejemplo n.º 5
0
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();
}
Ejemplo n.º 6
0
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();
}
Ejemplo n.º 7
0
//A consumer program
void consumer(MySemaphore sem){
MySemaphore test123/* = NULL*/;
MySemaphoreWait(sem);
MyThreadExit();
}