コード例 #1
0
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);

}
コード例 #2
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();
}
コード例 #3
0
main()  
{
	int p,c;	
	n =10;	
	produce_semaphore = MySemaphoreInit(2); 
	consume_semaphore = MySemaphoreInit(1);
 
	MyThreadInit(temp,(void *)&n);	
	if(MySemaphoreDestroy(produce_semaphore) == -1)
		printf("\n Error destroying producer semaphore");
	if(MySemaphoreDestroy(consume_semaphore))
		printf("\nError destroying consumer semaphore");
}
コード例 #4
0
//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();
}