예제 #1
0
void woman_enter()
{
	clock_t start, stop;
	double waittime;
	start = clock();
   int id = get_simple_tid(pthread_self());
   //Man entry code here
   /* */
   printf("Thread f%d trying to get in the restroom\n", id);
   pthread_mutex_lock(&female_mutex);

   while(femaleCount == RR_CAP){

       pthread_cond_wait(&female_full, &female_mutex);
   }
   //  printf("%d\n", value);
   		sem_wait(&female_semaphore);
   		stop = clock();
   		waittime = (double) (stop-start)/CLOCKS_PER_SEC;
   		++femaleCount;
   		pthread_mutex_unlock(&female_mutex);
   		printf("Thread f%d got in!\n", id);

	printf("Wait time for thread f%d was %f\n", id, waittime);

}
예제 #2
0
void do_other_stuff()
{
   struct timespec req, rem;
   double worktime;
   worktime = OTHER_SLEEP * (random() / (1.0*(double) ((unsigned long) RAND_MAX)));
   req.tv_sec = (int) floor(worktime);
   req.tv_nsec = (unsigned int) ( (worktime - (int) floor(worktime)) * 1000000000);
   printf("Thread %d working for %lf time\n", get_simple_tid(pthread_self()), worktime);
   nanosleep(&req,&rem);
}
예제 #3
0
void use_rr()
{
   struct timespec req, rem;
   double usetime;
   usetime = RR_MAXSLEEP * (random() / (1.0*(double) ((unsigned long) RAND_MAX)));
   req.tv_sec = (int) floor(usetime);
   req.tv_nsec = (unsigned int) ( (usetime - (int) floor(usetime)) * 1000000000);
   printf("Thread %d using restroom for %lf time\n", get_simple_tid(pthread_self()), usetime);
   nanosleep(&req,&rem);
}
예제 #4
0
//Males leaves restroom after using it
//If last male, switch sign to empty
void man_leave()
{
	int value;
	int id = get_simple_tid(pthread_self());
	pthread_mutex_lock(&semcheck); //Lock resrources
	sem_post(&capacity_semaphore); //"Exit res
	sem_getvalue(&capacity_semaphore, &value);
	if (value == 2) //If no more men in the restroom
		gender = 0; //Sign says empty - either gender may enter first
	printf("Man %d leaving, there are %d men left in the restroom\n", id, (2 - value));
	pthread_mutex_unlock(&semcheck); //Unlock resources
}
예제 #5
0
void woman_leave()
{
   int id = get_simple_tid(pthread_self());

   //Exit code here
   /* */
   pthread_mutex_lock(&female_mutex);
   sem_post(&female_semaphore);
   --femaleCount;
   pthread_cond_broadcast(&female_full);
   pthread_mutex_unlock(&female_mutex);
   printf("thread f %d left!\n",id);
}
예제 #6
0
//After using the restroom, female will exit
//If last one to leave, switch sign to say empty
void woman_leave()
{
	int value;
	int id = get_simple_tid(pthread_self());

	pthread_mutex_lock(&semcheck);	//Lock resources
	sem_post(&capacity_semaphore); //"Exit restroom"
	sem_getvalue(&capacity_semaphore, &value);
	if (value == 2) //Zero people in the semaphore
		gender = 0; //Restroom is up for grabs
	printf("Woman %d leaving, there are %d women left in the restroom\n", id, (2 - value));
	pthread_mutex_unlock(&semcheck); //Unlock resources
}
예제 #7
0
void man_leaveRR2()
{
   int id = get_simple_tid(pthread_self());

   //Exit code here
   /* */
   pthread_mutex_lock(&mutex);
   sem_post(&rr2_semaphore);
   --maleCountRR2;
   pthread_cond_broadcast(&full);
   pthread_mutex_unlock(&mutex);

   printf("Thread m%d left!\n", id);

}
예제 #8
0
//Male tries to enter restroom
//Waits in line if restroom belongs to females or if male restroom is full
//Enters otherwise
void man_enter()
{
	int value;
	int id = get_simple_tid(pthread_self());

	int shouldQuit = 0;
	while (shouldQuit == 0) { //Keep checking until males allowed in restroom
		pthread_mutex_lock(&semcheck); //Lock resources
		if (gender != -1) { //If the restroom does not belong to females
			if (gender == 0) {
				gender = 1; //restroom now belongs to males
			}
			shouldQuit = 1;
		}
		pthread_mutex_unlock(&semcheck); //Unlock resources
	}
	sem_wait(&capacity_semaphore); //Enter restroom
	sem_getvalue(&capacity_semaphore, &value);
	printf("Man %d entering, there are %d men now in the restroom\n", id, (2 - value));
}
예제 #9
0
//Female tries to enter restroom
//Waits in line if restroom for men only or if female restroom is full
//Enters otherwise
void woman_enter()
{
	int value;
	int id = get_simple_tid(pthread_self());

	int shouldQuit = 0;
	while (shouldQuit == 0) { //Keep checking until gender is allowed in restroom
		pthread_mutex_lock(&semcheck); //Lock resources
		if (gender != 1) { 	// Wait until men are out of the restroom
			if (gender == 0){
				gender = -1; //restroom now belongs to females
			}
			shouldQuit = 1;
		}
		pthread_mutex_unlock(&semcheck); //Unlock resources
	}
	sem_wait(&capacity_semaphore); //"Enter restroom"
	sem_getvalue(&capacity_semaphore, &value); //Get people in restroom
	printf("Woman %d entering, there are %d women now in the restroom\n", id, (2 - value));
}
예제 #10
0
void woman_enter()
{
	clock_t start, stop;
	double waittime;
	start = clock();
   int id = get_simple_tid(pthread_self());
   //Man entry code here
   /* */
   printf("Thread f%d trying to get in the restroom\n", id);
   pthread_mutex_lock(&mutex);

   while((maleCountRR1 > 0 || femaleCountRR1 == RR_CAP) && (maleCountRR2 > 0 || femaleCountRR2 == RR_CAP)){

       pthread_cond_wait(&full, &mutex);
   }
   //  printf("%d\n", value);
   if(maleCountRR1 ==0 && femaleCountRR1 !=RR_CAP){
   		sem_wait(&rr1_semaphore);
   		stop = clock();
   		waittime = (double) (stop-start)/CLOCKS_PER_SEC;
   		++femaleCountRR1;
   		pthread_mutex_unlock(&mutex);
   		printf("Thread f%d got in to restroom 1!\n", id);
   		use_rr();
   		woman_leaveRR1();
   }
   else if(maleCountRR2 ==0 && femaleCountRR2 !=RR_CAP){
   		sem_wait(&rr2_semaphore);
   		stop = clock();
   		waittime = (double) (stop-start)/CLOCKS_PER_SEC;
   		++femaleCountRR2;
   		pthread_mutex_unlock(&mutex);
   		printf("Thread f%d got in to restroom 2!\n", id);
   		use_rr();
   		woman_leaveRR2();
	}
	printf("Wait time for thread f%d was %f\n", id, waittime);

}