Пример #1
0
void
PC_Init(PC *pc, int cap, int maxColor)
{
  Lock_Init(&pc->lock);
  Cond_Init(&pc->spaceAvail, &pc->lock);
  Cond_Init(&pc->stuffAvail, &pc->lock);
  List_Init(&(pc->list));
  pc->capacity = cap;
  pc->used = 0;
  pc->maxColor = maxColor;
  pc->waitingP = 0;
  pc->waitingC = 0;
  return;
}
Пример #2
0
/*
  PROGRAM: Symposium
  This program executes a "symposium" for a number of philosophers, by spawning
  a process per philosopher.
 */
int Symposium(int argl, void* args)
{
    int bites;			/* Bites (mpoykies) per philosopher */
    Pid_t pid;
    int i;

    assert(argl == sizeof(int[2]));
    N = ((int*)args)[0];		/* get the arguments */
    bites = ((int*)args)[1];

    /* Initialize structures */
    state = (PHIL*) malloc(sizeof(PHIL)*N);
    hungry = (CondVar*) malloc(sizeof(CondVar)*N);
    for(i=0; i<N; i++) {
        state[i] = NOTHERE;
        Cond_Init(&(hungry[i]));
    }

    /* Execute philosophers */
    for(i=0; i<N; i++) {
        int Arg[2];
        Arg[0] = i;
        Arg[1] = bites;
        pid = Exec(Philosopher, sizeof(Arg), Arg);
    }

    /* Wait for philosophers to exit */
    for(i=0; i<N; i++) {
        pid = WaitChild(NOPROC, NULL);
    }
    free(state);
    free(hungry);
    return 0;
}
Пример #3
0
/*
 * Create a cache of filesystem buffers.
 */
struct FS_Buffer_Cache *Create_FS_Buffer_Cache(struct Block_Device *dev,
                                               uint_t fsBlockSize) {
    struct FS_Buffer_Cache *cache;

    KASSERT(dev != 0);
    KASSERT(dev->inUse);

    /*
     * Currently, we don't allow filesystem blocks
     * larger than the hardware page size.
     */
    KASSERT(fsBlockSize <= PAGE_SIZE);

    cache = (struct FS_Buffer_Cache *)Malloc(sizeof(*cache));
    if (cache == 0)
        return 0;

    cache->dev = dev;
    cache->fsBlockSize = fsBlockSize;
    cache->numCached = 0;
    Clear_FS_Buffer_List(&cache->bufferList);
    Mutex_Init(&cache->lock);
    Cond_Init(&cache->cond);

    return cache;
}
Пример #4
0
int Create_Semaphore(char* name, int ival)
{
	struct Semaphore* sema = (&s_semaphoreList)->head;
	static ulong_t sid = 1;
	
	//find sem by sname
	while (sema != 0)
	{
		if (strcmp(sema->name, name) == 0)
		{
			//Print("exist sema->name: %s, %d\n", sema->name, sema->sid);
			return sema->sid;
		}
		sema = Get_Next_In_Semaphore_List(sema);
	}

	//if there is no sem, create sem
	sema = (struct Semaphore*)Malloc(sizeof(struct Semaphore));
	sema->sid = sid++;
	memcpy(sema->name, name, 25);
	sema->count = ival;
	//sema->refCount = 1;
	Mutex_Init(&(sema->mutex));
	Cond_Init(&(sema->cond));
	Add_To_Back_Of_Semaphore_List(&s_semaphoreList, sema);
	//Print("new sema->name: %s, %d\n", sema->name, sema->sid);
	return sema->sid;
	
}