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
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...
      }
    }
  }
}
示例#3
0
// 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
}
示例#4
0
void temp(void *dummy)
{

	MyThread t_consumer;	
	t_consumer = MyThreadCreate(consumer,dummy);
	MyThreadCreate(producer,dummy);
	MyThreadJoin(t_consumer);


}
示例#5
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();
}
示例#6
0
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();
}
示例#7
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();
}
示例#8
0
void testjoinvalid(void *n)
{
    int x = 10;

    someThread = MyThreadCreate(donothing, (void *)&x);
    //Exit first thread
    MyThreadExit(); 
}
示例#9
0
void MyThreadInit(void(*f)(void *), void *args){
    q_running  = NULL;
    q_ready    = NULL;
    q_blocked  = NULL;
    q_reapable = NULL;
    MyThreadCreate(f, args);
    _MyThreadScheduler();
};
示例#10
0
//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();
}
示例#11
0
void temp(void *dummy)
{

	MyThread t_consumer;	
	t_consumer = MyThreadCreate(func,(void *)0);
//	MyThreadCreate(func,(void *)2);
	MyThreadJoin(t_consumer);


}
示例#12
0
void MyThreadInitExtra(){

    static int fork = 0;

    Thread* bootstrap = (Thread*) MyThreadCreate(NULL, NULL );
    bootstrap->ctx.uc_stack.ss_sp   = malloc(STACK_SIZE);
    bootstrap->ctx.uc_stack.ss_size = STACK_SIZE;
    bootstrap->ctx.uc_link          = &bootstrap->return_ctx;
    bootstrap->flags |= THREAD_IS_STARTED;

    getcontext(&bootstrap->ctx);
    if( fork == 0 ) { 
        fork++;
        swapcontext(&bootstrap->return_ctx, &bootstrap->ctx);
    } else if ( fork == 1 ) { 
        fork++;
        Thread* sched =  MyThreadCreate(_MyThreadScheduler, NULL);
        q_ready   = bootstrap;
        _MyThreadRun(sched);
    }
};
示例#13
0
int makeThreads(char *me, void (*t)(void *), int many)
{
  MyThread T;
  int i;
  for (i = 0; i < many; i++) {
    printf("\n%s create %d\n", me, i);
    T = MyThreadCreate(t, (void *)i);
    if (yield)
      MyThreadYield();      
  }
  if (join)
    MyThreadJoin(T);
}
示例#14
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();
}
示例#15
0
void t0(void * dummy)
{
  MyThreadCreate(t1, (void *)1);
  t1(0);
}
示例#16
0
void MyThreadInit(void(*start_funct)(void *), void *args)
{
	MyThreadCreate(start_funct, args);
}