コード例 #1
0
void *sellTickets(void *param) {
	// threaded method for seller

	Person *p;
	int tl, th;
	Seller *s = (Seller *) param;
	while (!(hall.hasStarted || hall.isSoldOut)) {
		if(isSoldOut(&hall)){

			LOCK(&outputLock);
			printf("%6s |           |             ", getTime());
			printf("| Event: Tickets Sold Out\n");
			UNLOCK(&outputLock);
			LOCK(hall.lock);
			hall.isSoldOut = TRUE;
			UNLOCK(hall.lock);
		}
		if (s->que != NULL) {
			p = removePerson(s->que);

			LOCK(hall.lock);
			getSeat(&hall, p, s->price);
			UNLOCK(hall.lock);

			switch (s->price) {
			case HIGH:
				tl = 1;
				th = 2;
				break;
			case MEDIUM:
				tl = 2;
				th = 4;
				break;
			case LOW:
				tl = 4;
				th = 7;
				break;
			default: // broken code
				printf("Error, we've been hacked Price is %d", s->price);
				exit(-1);
			}
			PRINTEVENT(s,p,"Customer Served");

			LOCK(&outputLock);
			output();
			UNLOCK(&outputLock);

			sleep(getRandomTime(tl, th)); // random time based on price
			// this code needed to handle frustrated customers safely

			LOCK(garbage->lock);
			queAdd(garbage, p);
			UNLOCK(garbage->lock);
			PRINTEVENT(s,p,"Customer Purchase Complete - Leaves Seller");
		}
	}
	s->open = FALSE;
	return NULL;
	// thread finished
}
コード例 #2
0
ファイル: StructsIntro.c プロジェクト: pandarah/deepsea
int main()
{
	int option;
	
	while(1)
	{
		printf("What would you like to do?\n");
		printf("(1)Input a person.\n(2)Remove a person.\n(3)List people.\n(4)Quit.\n");

		if(scanf("%d", &option)!=1)
		{
			printf("Wrong entry.\n");
			return 0;
		}

		switch(option)
		{
			case 1:
				inputPerson(); //insert name
				break;
			case 2:
				removePerson(); //copy last element in array to index being removed
				break;
			case 3:
				listPeople(); //display all people in list
				break;
			default:
				return 0; //quit
		}
	
	}
}
コード例 #3
0
void frustratedPerson(Person *person) {
	// this will be threaded... does it need a return ?
	if (person->inQ) {
		person = removePerson(person);
		free(person);
	}
	// can explicitly kill thread if we want
}
コード例 #4
0
	bool System::removePerson(const Pessoa &person)
	{
		return removePerson(person.getNome());
	}
コード例 #5
0
void mkTest() {

	printf(" Seller\n");
	srand(0);
	Seller *s;
	s = createSeller(HIGH, 1);
	assert(checkT(s->price==HIGH,"Seller is HIGH"));
	assert(checkT(s->que==NULL,"Seller starts with empty que"));
	assert(checkN(s->lastRow,1,"HIGH seller starts in row 1"));
	free(s);
	s = createSeller(MEDIUM, 1);
	assert(checkN(s->lastRow,5,"MEDIUM seller starts in row 5"));
	free(s);
	s = createSeller(LOW, 1);
	assert(checkN(s->lastRow,10,"LOW seller starts in row 10"));

	printf(" Person\n");
	Person *p = createPerson(s);
	assert(checkN(p->arrival,43,"p->arrival"));
	assert(checkT(s==p->seller,"checkT"));
	assert(checkT((p->next==p->prev) && (p->prev==p),"p is self referential"));

	// add person to seller
	//   we need 4 people to test que management
	//   for our testing set arrival to 0 and do not thread
	printf(" Add Person\n");
	int i;
	for(i=0; i<4;i++) {
		p=createPerson(s);
		p->arrival = 0;
		addPerson(p);
	}
	assert(checkN(s->que->id,1,"First customer id"));
	assert(checkN(s->que->next->id,2,"Second customer id"));
	assert(checkN(s->que->next->next->id,3,"Third customer id"));
	assert(checkN(s->que->next->next->next->id,4,"Fourth/Last customer id"));
	assert(checkN(s->que->next->next->next->next->id,4,"Fifth customer id (tail points to self"));

	assert(checkT(s->que->inQ==TRUE,"First customer id"));
	assert(checkT(s->que->next->inQ==TRUE,"Second customer id"));
	assert(checkT(s->que->next->next->inQ==TRUE,"Third customer id"));
	assert(checkT(s->que->next->next->next->inQ==TRUE,"Fourth/Last customer id"));

	printf(" Remove Person\n");
	// test removals
	p = removePerson(s->que);// remove person 1
	assert(checkN(p->id,1,"First person on que removed"));
	assert(checkN(s->que->id,2,"First person on que now number 2"));

	p = removePerson(s->que->next); // remove person 3
	assert(checkN(p->id,3,"Second(middle) person on que removed"));
	assert(checkN(s->que->id,2,"First person on que now number 2"));
	assert(checkN(s->que->next->id,4,"Second person should now be #4"));

	p = removePerson(s->que->next); // remove person 4
	assert(checkN(p->id,4,"Second(tail) person on que removed"));
	assert(checkN(s->que->id,2,"First person on que now number 2"));
	assert(checkN(s->que->id,s->que->next->id,"Only one person left on que #2"));
	assert(checkT((p->next==p->prev) && (p->prev==p),"p(2) is self referential"));

	p = removePerson(s->que);// remove person 2 (single person left)
	assert(checkN(p->id,2,"Last person on que removed"));
	assert(checkT(s->que==NULL,"Seller que now empty"));

	p = removePerson(s->que);// remove person from empty que
	assert(checkT(p==NULL,"NULL pointer expected"));

	//
	printf(" getSeat()\n");
	p=createPerson(s); // current low price seller
	p->id = 42;
	// check if row full algorithm even works
	assert(checkT(!isRowFull(&hall,8),"Is row 8 full NO"));

	printf("   LOW\n");
	getSeat(&hall,p,LOW);
	// first call to low assigns 10,10
	Person *cp = hall.seats[10][10];
	assert(checkT(cp!=NULL,"Seat 10,10 occupied"));
	assert(checkN(cp->id,42,"Customer 42 has the seat"));

	printf("   HIGH\n");
	p->seller->lastRow=1;
	getSeat(&hall,p,HIGH);
	// first call to low assigns 1,1
	cp = hall.seats[1][1];
	assert(checkT(cp!=NULL,"Seat 1,1 occupied"));
	assert(checkN(cp->id,42,"Customer 42 has the seat"));

	printf("   MEDIUM\n");
	p->seller->lastRow=5;
	getSeat(&hall,p,MEDIUM);
	// first call to low assigns 5,10
	cp = hall.seats[5][10];
	assert(checkT(cp!=NULL,"Seat 5,10 occupied"));
	assert(checkN(cp->id,42,"Customer 42 has the seat"));

	p->seller->lastRow=3;
	getSeat(&hall,p,MEDIUM);
	// first call to low assigns 3,10
	cp = hall.seats[3][10];
	assert(checkT(cp!=NULL,"Seat 3,10 occupied"));
	assert(checkN(cp->id,42,"Customer 42 has the seat"));

	p->seller->lastRow=6;
	getSeat(&hall,p,MEDIUM);
	// first call to low assigns 6,1
	cp = hall.seats[6][1];
	assert(checkT(cp!=NULL,"Seat 6,1 occupied"));
	assert(checkN(cp->id,42,"Customer 42 has the seat"));

	// need non-threaded test for sellTickets method


	// non-threaded test for frustratedPerson()

	printf(" frustratedPerson()\n");
	s = createSeller(HIGH,103);
	p=createPerson(s);
	p->arrival=0;
	addPerson(p);
	frustratedPerson(p);
	assert(checkT(s->que==NULL,"Person has left the building"));
}