예제 #1
0
파일: dine.c 프로젝트: girum11/cpe453
void *cycle(void *arg) {
	/*
	 * This function will be executed as the body of each child thread.
	 * It expects a single parameter that is an int (an ID for the
	 * Philosopher).
	 *
	 * The parameter is a void * to comply with the prototype, but we know
	 * what's really in there.
	 */
	int philosopher_id = *(int *) arg;
	Philosopher *phil = &phils[philosopher_id];
	int i = 0;

	for (i = 0; i < number_of_times_to_cycle; i++) {
		/************************************************
		 * At first, I'm hungry. I want to EAT.
		 ************************************************/
		// Attempt to pick up forks.
		if (phil->id % 2 == 0) {
			// Evens pick up right-hand forks first.
			pick_up_fork(phil, &forks[phil->des_right_fork]);
			pick_up_fork(phil, &forks[phil->des_left_fork]);
		} else {
			// Odds pick up left-hand forks first.
			pick_up_fork(phil, &forks[phil->des_left_fork]);
			pick_up_fork(phil, &forks[phil->des_right_fork]);
		}

		// Switch to EATING state.
		change_state(phil, EATING);

		// Eat for a bit (sleep the thread).
		dawdle(phil);

		// Switch to CHANGING state.
		change_state(phil, CHANGING);

		/*************************************************
		 * Now that I've finished EATING, I want to THINK.
		 *************************************************/
		// Attempt to put down forks.
		put_down_fork(phil, &forks[phil->des_left_fork]);
		put_down_fork(phil, &forks[phil->des_right_fork]);

		// Switch to THINKING state.
		change_state(phil, THINKING);

		// Think for a bit (sleep the thread).
		dawdle(phil);

		// Switch to CHANGING state.
		change_state(phil, CHANGING);
	}

	// Kill the pthread.
	pthread_exit(NULL );
}
예제 #2
0
SUMOReal
MSCFModel_SmartSK::moveHelper(MSVehicle* const veh, SUMOReal vPos) const {
    const SUMOReal oldV = veh->getSpeed(); // save old v for optional acceleration computation
    const SUMOReal vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
    // we need the acceleration for emission computation;
    //  in this case, we neglect dawdling, nonetheless, using
    //  vSafe does not incorporate speed reduction due to interaction
    //  on lane changing
    const SUMOReal vMin = getSpeedAfterMaxDecel(oldV);
    const SUMOReal vMax = MIN3(veh->getLane()->getVehicleMaxSpeed(veh), maxNextSpeed(oldV, veh), vSafe);
#ifdef _DEBUG
    if (vMin > vMax) {
        WRITE_WARNING("Vehicle's '" + veh->getID() + "' maximum speed is lower than the minimum speed (min: " + toString(vMin) + ", max: " + toString(vMax) + ").");
    }
#endif
    updateMyHeadway(veh);
    SSKVehicleVariables* vars = (SSKVehicleVariables*)veh->getCarFollowVariables();
#ifdef _DEBUG
    if (vars->ggOld.size() > 1) {
        std::cout << "# more than one entry in ggOld list. Speed is " << vPos << ", corresponding dist is " << vars->ggOld[(int) vPos] << "\n";
        for (std::map<int, SUMOReal>::iterator I = vars->ggOld.begin(); I != vars->ggOld.end(); I++) {
            std::cout << "# " << (*I).first << ' ' << (*I).second << std::endl;
        }
    }
#endif

    vars->gOld = vars->ggOld[(int) vPos];
    vars->ggOld.clear();
    return veh->getLaneChangeModel().patchSpeed(vMin, MAX2(vMin, dawdle(vMax)), vMax, *this);
}
예제 #3
0
void think(int id) {
   state[id] = THINKING;

   printStatusLine();

   /* Sleep for a random amount of time */ 
   dawdle();
}
예제 #4
0
void eat(int id) {
   state[id] = EATING;

   printStatusLine();

   /* Sleep for a random amount of time */ 
   dawdle();
}
예제 #5
0
파일: u5.c 프로젝트: FrankSchmidt/EZS_1314
void *thread(void *args) {
  int i,val;
  int arg = *((int*)args);
  
  for (i= 0; i<NLOOP; i++) {
    pthread_mutex_lock(&mutex);
    val= counter;
    dawdle(arg);
    printf("Thread %lu: %d\n",pthread_self(),val+1);
    counter= val+1;
    pthread_mutex_unlock(&mutex);
  }
  pthread_exit(NULL); 
}
SUMOReal
MSCFModel_Daniel1::moveHelper(MSVehicle* const veh, SUMOReal vPos) const {
    const SUMOReal oldV = veh->getSpeed(); // save old v for optional acceleration computation
    const SUMOReal vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
    // we need the acceleration for emission computation;
    //  in this case, we neglect dawdling, nonetheless, using
    //  vSafe does not incorporate speed reduction due to interaction
    //  on lane changing
    const SUMOReal vMin = getSpeedAfterMaxDecel(oldV);
    const SUMOReal vMax = MIN3(veh->getLane()->getVehicleMaxSpeed(veh), maxNextSpeed(oldV, veh), vSafe);
#ifdef _DEBUG
    if (vMin > vMax) {
        WRITE_WARNING("Vehicle's '" + veh->getID() + "' maximum speed is lower than the minimum speed (min: " + toString(vMin) + ", max: " + toString(vMax) + ").");
    }
#endif
    return veh->getLaneChangeModel().patchSpeed(vMin, MAX2(vMin, dawdle(vMax)), vMax, *this);
}
SUMOReal
MSCFModel_KraussOrig1::moveHelper(MSVehicle * const veh, const MSLane * const lane, SUMOReal vPos) const throw() {
    SUMOReal oldV = veh->getSpeed(); // save old v for optional acceleration computation
    SUMOReal vSafe = MIN2(vPos, veh->processNextStop(vPos)); // process stops
    // we need the acceleration for emission computation;
    //  in this case, we neglect dawdling, nonetheless, using
    //  vSafe does not incorporate speed reduction due to interaction
    //  on lane changing
    veh->setPreDawdleAcceleration(SPEED2ACCEL(vSafe-oldV));
    //
    SUMOReal vNext = dawdle(MIN3(lane->getMaxSpeed(), maxNextSpeed(oldV), vSafe));
    vNext =
        veh->getLaneChangeModel().patchSpeed(
            MAX2((SUMOReal) 0, oldV-(SUMOReal)ACCEL2SPEED(myDecel)), //!!! reverify
            vNext,
            MIN3(vSafe, veh->getLane().getMaxSpeed(), maxNextSpeed(oldV)),//vaccel(myState.mySpeed, myLane->maxSpeed())),
            vSafe);
    return MIN4(vNext, vSafe, veh->getLane().getMaxSpeed(), maxNextSpeed(oldV));
}