Exemplo n.º 1
0
/*
 * Function name: testitout
 * Description: This function is called from the main for calculating the  
 * fibonacci of the number represented by the input argument p.
 */
void
testitout(void* p) 
{
  fprintf(stderr, "main thread %p, %ld %x\n", mythread_myid(), (long)p,(unsigned int)pthread_self());
  /* Allocation heap space for storing input and output fields of fib(p) */
  Foo* a = (Foo *)calloc(1,sizeof(Foo));
  a->input = (long)p;
  /* Spawning a new thread for computing fib(p) */
  ThreadId id;
  mythread_create(&id, NULL, fibonacci, (void *)a);
  mythread_join(id);
  free(a);
  fprintf(stderr,"parent is done %x\n",(unsigned int)pthread_self());
  fprintf(stderr,"result is %ld %x\n",a->output,(unsigned int)pthread_self());
  //exit(0);
}
Exemplo n.º 2
0
int main()
{
    unsigned long thread_1;
    unsigned long thread_2;
    void *result;

    mythread_init();

    thread_1 = mythread_create(write_thread, (void *)1);
    mythread_detach(thread_1);
    thread_1 = mythread_create(write_thread, (void *)3);
    mythread_detach(thread_1);
    thread_2 = mythread_create(read_thread, (void *)10);

    mythread_join(thread_2, NULL);

    printf("main is going away\n");

    //mythread_cleanup();

    return 0;
}
Exemplo n.º 3
0
void prototype_os() {
	//thread initialized on creation..I want to bring back the initialize function
	ThreadControlBlock *newThread;
	initialize(mainThreadQueue);
	int i, j;
	for (i = 0; i < num_threads; i++) {
		// Here: do whatever you need
		// Here: call mythread_create so that the TCB for each thread is created
		//assembly calls
		newThread = mythread_create(i, 4096, mythread);
		// Here: call mythread_join to make each thread runnable/ready
		mythread_join(newThread);
	}
	// Here: initialize the timer and its interrupt handler as is done in Project I
	alt_alarm_start(&alarm, ALARMTICKS(x), mythread_handler, NULL);

	while (1) {
		alt_printf(
				"This is the OS prototype for my exciting CSE351 course projects!\n");
		for (j = 0; j < MAX; j++);
	}
}
Exemplo n.º 4
0
int main(void){
  
  int rc1, rc2;
  int i;
    
  mythread_setconcurrency(4);

	/*	Creating 10 threads */  
   for ( i = 0; i<10; i++)
    {
        rc1 = mythread_create(&threads[i], NULL, thread_function, (void *)NULL);
        if (rc1) ("ERROR; return code from pthread_create() is %d\n", rc1);
    }

   /* Main will now wait for 10 threads */
  for(i = 0; i<10; i++)
  {
   		rc2 = mythread_join(threads[i], NULL);
    	if (rc2) ("ERROR; return code from pthread_join() is %d\n", rc2);
  }
  
  return 0;

}
Exemplo n.º 5
0
int main()
{
	int i;
	mythread_t thread[NTHREADS];
	mythread_setconcurrency(NTHREADS+1);
	mythread_mutex_init(&mut, NULL);
	mythread_cond_init(&cond, NULL);

	for(i = 0; i < NTHREADS; i++)
		mythread_create(&thread[i], NULL, fun, NULL);
	for(i = 0; i < INT_MAX/10; i++);

	printf("Broadcast\n"); fflush(stdout);
	mythread_cond_broadcast(&cond);
	printf("Now join\n"); fflush(stdout);
	for(i = 0; i < NTHREADS; i++)
		mythread_join(thread[i], NULL);	
	printf("main exit\n"); fflush(stdout);

	mythread_exit(NULL);
	mythread_cond_destroy(&cond);
	mythread_mutex_destroy(&mut);
	return 1;
}
Exemplo n.º 6
0
int main() {

	mythread_setconcurrency(4);
	mythread_mutex_init(&lock,NULL);
	mythread_mutex_init(&lock1,NULL);

	mythread_cond_init(&cond,NULL);
	mythread_cond_init(&cond1,NULL);
	glval=0;
	glval1=0;
	mythread_t tid1,tid2,tid3,tid4,tid5;
	mythread_queue_t head;
	

	tid1 = malloc(sizeof(struct mythread));
	tid2 = malloc(sizeof(struct mythread));
	tid3 = malloc(sizeof(struct mythread));
	tid4 = malloc(sizeof(struct mythread));
	tid5 = malloc(sizeof(struct mythread));

/**************** Test cases for testing all functions except destroy functions ********************/
	mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal)  
	mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal)  
	mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal)  

	printOut("Sleeping for 1 second so that threads calling condition waits starts before their condition variables are signaled\n");
	sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent
	mythread_create(&tid1,NULL,thread1,NULL);
	mythread_create(&tid2,NULL,thread2,NULL);
	mythread_join(tid1,NULL);
	mythread_join(tid2,NULL);

	printOut("Value of global variable incremented 200 times in two threads : ");
	printOut(itoa(glval,10));
	printOut("\n");

/*************************************************************************************************/
	mythread_join(tid3,NULL);
	mythread_join(tid4,NULL);
	mythread_join(tid5,NULL);


	printOut("=================================================================\n");
	printOut("Repeat the entire test after destroying(mythread_mutex_destroy,mythread_cond_destroy) and reinitializing 2 locks and 2 condition variables\n");
	printOut("=================================================================\n");

/********* Destroying all mmutexes and locks and reinitialising again ****************************/
	mythread_mutex_destroy(&lock);
	mythread_mutex_destroy(&lock1);
	mythread_cond_destroy(&cond);
	mythread_cond_destroy(&cond1);

        mythread_mutex_init(&lock,NULL);
        mythread_mutex_init(&lock1,NULL);

        mythread_cond_init(&cond,NULL);
        mythread_cond_init(&cond1,NULL);
/**************************************************************************************************/

/**************** Test cases for testing all functions except destroy functions ********************/
	glval=0;
        mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal)  
        mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal)  
        mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal)  

	printOut("Sleeping for 1 second so that threads calling 'condition wait' starts before their condition variables are signaled\n");
	sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent
        mythread_create(&tid1,NULL,thread1,NULL);
        mythread_create(&tid2,NULL,thread2,NULL);
        mythread_join(tid1,NULL);
        mythread_join(tid2,NULL);

        printOut("Value of global variable incremented in the repeated test 200 times in two threads : ");
        printOut(itoa(glval,10));
        printOut("\n");

/**************************************************************************************************/


	mythread_exit(NULL);			

}
Exemplo n.º 7
0
int  mythread_wrapper(void * arg)
{
	void * fromthreadfunction ;
	node * thread_block;
	int temp;
	node * temptail,*temphead;
	int *i = (int *) malloc (sizeof(int));
	char buffer[10]="wrapper";
	void ** status;
	struct wrapper_arguments * arguments = (struct wrapper_arguments *) arg;
	void * (*functionpointer) (void *) = *(arguments->functionname);
	int jointargetcheck=0;

	/* creating a node and insertig the TCB into the queue */
        total_threads+=1;
	temptail=(node *)malloc(sizeof(node));
	temphead=(node *)malloc(sizeof(node));
	thread_block=createNode();
	
	thread_block->thread=arguments->tcb;
	temptail=queue_info->tail;
	addNode(thread_block);
	
	/* HAVE TO ADD getttid here to assign the threadid to the current block */
	
	if(isfirsttime==0)
	{
		isfirsttime+=1;
		futex_up(&f);
	}
	else
	{
		futex_up(&block_main_futex);
	}
	
	//if(haveyielded>0)
	{
	//futex_down(&(temptail->thread->thread_futex));
	}
	futex_down(&(queue_info->tail->thread->thread_futex));//locking its own futex rather than that of the previous ones
	futex_down(&yield_futex);

	// this is for the join condition check if the current threads target is in the execution queue if yes the yield the current thread
	
	if(queue_info->head->thread->join_target==NULL)
	{
		jointargetcheck=0;
	}
	else// check if the target is present in the queue if yes yield
	{
		jointargetcheck=findthread(queue_info->head->thread->join_target);
	}
	if(jointargetcheck==1)//added the futex down statements today latest
	{
		jointargetcheck=0;
		futex_down(&(queue_info->head->thread->thread_futex));//locking its own futex rather than that of the previous ones
        	futex_down(&yield_futex);
		temp=mythread_join(*(queue_info->head->thread->join_target),status);
	}
	//jointargetcheck=mythread_join(queue_info->head->thread->join_target,status);
	
	(*(functionpointer))(((void *)arguments->functionarguments));
	
	// when this thread is done move the head to the next position and free the previous nodes futex
      	temphead=queue_info->head;
	queue_info->head=queue_info->head->right;
	if(queue_info->head!=NULL)
	 	queue_info->head->left=NULL;

       futex_up(&(queue_info->head->thread->thread_futex));//changing this 
       futex_up(&yield_futex);
       //futex_down(&(queue_info->tail->left->thread->thread_futex));
	total_threads=total_threads-1;
}
Exemplo n.º 8
0
int main(int argc, char *argv[]) {
	int rc = 0;
	mythread_t th[THREAD_COUNT];
	mythread_attr_t *attr[THREAD_COUNT];
	struct sched_param sparam1;
	long i;

	mythread_setconcurrency(CONCURRENCY_COUNT);

	//printf("concurrency is %d\n", mythread_getconcurrency());

	mythread_mutex_init(&mutex, NULL);

	for (i = 0; i < THREAD_COUNT; i++) {
		attr[i] = malloc(sizeof(mythread_attr_t));
		mythread_attr_init(attr[i]);
		switch (i) {
		case 2:
			sparam1.__sched_priority = 9;
			mythread_attr_setschedparam(attr[i], &sparam1);
			break;
		case 3:
			sparam1.__sched_priority = 8;
			mythread_attr_setschedparam(attr[i], &sparam1);
			break;
		case 4:
			sparam1.__sched_priority = 7;
			mythread_attr_setschedparam(attr[i], &sparam1);
			break;
		case 5:
			sparam1.__sched_priority = 6;
			mythread_attr_setschedparam(attr[i], &sparam1);
			break;
		case 6:
			sparam1.__sched_priority = 5;
			mythread_attr_setschedparam(attr[i], &sparam1);
			break;
		default:
			break;
		}

		mythread_create(&th[i], attr[i], thread_func, (void*) i);
	}

	for (i = 0; i < THREAD_COUNT; i++) {
		mythread_join(th[i], NULL);
	}

	/* Test: mythread_attr_getschedparam()*/
	for (i = 0; i < THREAD_COUNT; i++) {
		int rc = 0;
		struct sched_param sparam2;
		rc = mythread_attr_getschedparam(attr[i], &sparam2);
		if(!rc){
			switch (i) {
			case 2:
				sparam2.__sched_priority = 9;
				score = score + 0.7;
				break;
			case 3:
				sparam2.__sched_priority = 8;
				score = score + 0.7;
				break;
			case 4:
				sparam2.__sched_priority = 7;
				score = score + 0.7;
				break;
			case 5:
				sparam2.__sched_priority = 6;
				score = score + 0.7;
				break;
			case 6:
				sparam2.__sched_priority = 5;
				score = score + 0.7;
				break;
			default:
				break;
			}
		}
//		printf("sparam2.sched_priority = %d\n",
//				sparam2.__sched_priority);
		//show_sched_param(attr[i]);

	}

	/* Test 1*/
	printf("# Test Case 1:");
	//printf("Expected Output: %s\n", EXPECTED_OUTPUT1);
	//printf("Actual Output: %s\n", actualOutput);
	if (!strcmp(EXPECTED_OUTPUT1, actualOutput) ||
			!strcmp(EXPECTED_OUTPUT2, actualOutput)) {
		printf("PASS\n");
		score = score + 25;
	} else {
		printf("FAIL\n");
	}


	/*Test: mythread_attr_destroy() */
	int deduction = 0;
	for (i = 0; i < THREAD_COUNT; i++) {
		rc = mythread_attr_destroy(attr[i]);
		if(rc){
			deduction = deduction + 0.5;
		}
		free(attr[i]);
	}
	if(rc){
		score = score - deduction;
		printf("Deduction occurred.\n");
	}

	mythread_mutex_destroy(&mutex);

	printf("## Score: %d\n", score);

	return 0;
}