/**
 * fellowship - Fellowship synch problem driver routine.
 *
 * You may modify this function to initialize any synchronization primitives
 * you need; however, any other data structures you need to solve the problem
 * must be handled entirely by the forked threads (except for some freeing at
 * the end).  Feel free to change the thread forking loops if you wish to use
 * the same entrypoint routine to implement multiple Middle-Earth races.
 *
 * Make sure you don't leak any kernel memory!  Also, try to return the test to
 * its original state so it can be run again.
 */
int
fellowship(int nargs, char **args)
{
  int i, n;

  (void)nargs;
  (void)args;
  
  print_lock = lock_create("print");
  done_sem = sem_create("done", 0);
  
  fotr.warlock = lock_create("wizard");
  init_members(fotr.men, 2, "men");
  init_members(&fotr.elf, 1, "elf");
  init_members(&fotr.dwarf, 1, "dwarf");
  init_members(fotr.hobbits, 4, "hobbits");
  fotr.generation = 0;

  for (i = 0; i < NFOTRS; ++i) {
    thread_fork_or_panic("wizard", wizard, NULL, i, NULL);
  }
  for (i = 0; i < NFOTRS; ++i) {
    thread_fork_or_panic("elf", elf, NULL, i, NULL);
  }
  for (i = 0; i < NFOTRS; ++i) {
    thread_fork_or_panic("dwarf", dwarf, NULL, i, NULL);
  }
  for (i = 0, n = NFOTRS * MEN_PER_FOTR; i < n; ++i) {
    thread_fork_or_panic("man", man, NULL, i, NULL);
  }
  for (i = 0, n = NFOTRS * HOBBITS_PER_FOTR; i < n; ++i) {
    thread_fork_or_panic("hobbit", hobbit, NULL, i, NULL);
  }
  
  for (i = 0, n = NFOTRS * FOTR_SIZE; i < n; i++)
  {
    P(done_sem);
  }
  
  lock_destroy(fotr.warlock);
  destroy_members(fotr.men);
  destroy_members(&fotr.elf);
  destroy_members(&fotr.dwarf);
  destroy_members(fotr.hobbits);
  
  lock_destroy(print_lock);
  sem_destroy(done_sem);

  return 0;
}
Beispiel #2
0
/**
 * piazza - Piazza synch problem driver routine.
 *
 * You may modify this function to initialize any synchronization primitives
 * you need; however, any other data structures you need to solve the problem
 * must be handled entirely by the forked threads (except for some freeing at
 * the end).
 *
 * Make sure you don't leak any kernel memory!  Also, try to return the test to
 * its original state so it can be run again.
 */
int
piazza(int nargs, char **args)
{
  int i;

  (void)nargs;
  (void)args;

  // Initalize locks and globals
  for(i=0; i<NANSWERS; i++)
    creation_lock[i] = lock_create("creation lock");
  driver_sem = sem_create("driver semaphore", 0);
  thread_count_lock = lock_create("thread count lock");
  finished_thread_count = 0;

  for (i = 0; i < NSTUDENTS; ++i) {
    thread_fork_or_panic("student", student, NULL, i, NULL);
  }
  for (i = 0; i < NINSTRUCTORS; ++i) {
    thread_fork_or_panic("instructor", instructor, NULL, i, NULL);
  }

  // Wait for task to finish
  P(driver_sem);

  // Cleanup
  for(i=0; i<NANSWERS; i++){
    lock_destroy(questions[i]->mutex);
    cv_destroy(questions[i]->readerQ);
    cv_destroy(questions[i]->writerQ);
    kfree(questions[i]->pq_answer);
    kfree(questions[i]);
    questions[i] = NULL;

    lock_destroy(creation_lock[i]);
  }
  sem_destroy(driver_sem);
  lock_destroy(thread_count_lock);

  return 0;
}
Beispiel #3
0
int
airballoon(int nargs, char **args) {
	unsigned i;

	(void) nargs;
	(void) args;
	init_mappings();
	num_deleted = 0;

	for (i = 0; i < NROPES; ++i) {
		ground_stakes[i].lk = lock_create("ground lock");
		KASSERT( ground_stakes[i].lk != NULL );
	}

	num_deleted_lk = lock_create("num_deleted lock");
	KASSERT( num_deleted_lk != NULL );
	exit_sem = sem_create("exit_sem",0);
	KASSERT( exit_sem != NULL );
	// Spawn FlowerKiller thread.
	thread_fork_or_panic("FlowerKiller", NULL, KillerFlower, NULL, 0);

	// Spawn Dandelion's and Marigold's threads
	for (i = 0; i < NTHREADS; i++) {
        	thread_fork_or_panic("Dandelion", NULL, dandelion, NULL, 0);
        	thread_fork_or_panic("Marigold", NULL, marigold, NULL, 0);
	}

	for (i = 0; i < 2*NTHREADS + 1; ++i)
		P(exit_sem);

	// cleanup
	num_deleted = 0;
	sem_destroy(exit_sem);
	lock_destroy(num_deleted_lk);
	for (i = 0; i < NROPES; ++i) {
		lock_destroy(ground_stakes[i].lk);
	}

	return 0;
}