Beispiel #1
0
void* client_thread(void* arg) {
    int i = 0;
    int id = *((int *) arg);
    srand(time(NULL));
    while (1) {
        initscr();
        start_color();
        if (pthread_mutex_lock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");
            draw_chair(out_of_salon_chairs[id], 2, "C", '%', id);
        if (pthread_mutex_unlock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");
        usleep((rand()%3 +1) * 3000000);
        if (pthread_mutex_lock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");
        draw_chair(out_of_salon_chairs[id], 3, "C", '%', id);
        if (pthread_mutex_unlock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");

        sem_wait(&access_waiting_room_seats);
        if(number_of_free_seats > 0){
            number_of_free_seats--;

            int tmp = number_of_free_seats;
            if (pthread_mutex_lock(&draw_out) != 0)
                print_error(errno, "mutex_unlock error");
            draw_chair(out_of_salon_chairs[id], 2, " ", ' ', id);
            draw_chair(waiting_chairs[tmp], 2, "C", '%', id);
            if (pthread_mutex_unlock(&draw_out) != 0)
                print_error(errno, "mutex_unlock error");
            usleep((rand()%3 +1) * 500000);
            sem_post(&customer_ready);
                sem_post(&access_waiting_room_seats);
            sem_wait(&barber_ready);
            if (pthread_mutex_lock(&draw_out) != 0)
                print_error(errno, "mutex_unlock error");
            draw_chair(waiting_chairs[tmp], 2, " ", ' ', id);
            draw_chair(haircut_chairs[1], 2, "C", '%', id);
            if (pthread_mutex_unlock(&draw_out) != 0)
                print_error(errno, "mutex_unlock error");
//            printf("Client %d is having hair cut\n", id);
            usleep(2000000);
            if (pthread_mutex_lock(&draw_out) != 0)
                print_error(errno, "mutex_unlock error");
            draw_chair(haircut_chairs[1], 2, " ", ' ', id);
            if (pthread_mutex_unlock(&draw_out) != 0)
                print_error(errno, "mutex_unlock error");

        } else {
            sem_post(&access_waiting_room_seats);
//            printf("Client %d left without hair cut\n", id);
            usleep((rand()%3 +1) *1000000);
        }

        endwin();
    }
    return NULL;
}
// Draw everything
void draw_all_objects(
	int* room_display_lists,
	float lid_degrees,
	float* dancer_angles, float dancer_angle,float dancer_y,
	float door_angle
) {
	// int* room_display_lists = all_display_lists[0];

	// Room walls and door
	draw_room(room_display_lists, door_angle);

	// Box and dancer
	glPushMatrix();
		// glTranslatef(0,2.6001,1.7);
		glTranslatef(-2,2.1+.001,-1);
		draw_box(room_display_lists ,lid_degrees);
	glPopMatrix();
	glPushMatrix();
		glTranslatef(-2,dancer_y,-2.7);
		draw_dancer(room_display_lists, dancer_angles, dancer_angle);
	glPopMatrix();

	// Furniture
	glPushMatrix();
		glTranslatef(-2,-1,-2.7);
		draw_table(room_display_lists);
	glPopMatrix();
	glPushMatrix();
		glTranslatef(5,-2.05,0);
		draw_one_legged_table(room_display_lists);
	glPopMatrix();
	glPushMatrix();
		glTranslatef(4.5,-2,-2.5);
		glRotatef(-90,0,1,0);
		draw_chair(room_display_lists);
	glPopMatrix();
	glPushMatrix();
		glTranslatef(0,-2,0);
		draw_stool(room_display_lists);
	glPopMatrix();

	// Lights
	// lamp
	glPushMatrix();
		glTranslatef(-3,-1.5,0);
		draw_lamp(room_display_lists);
	glPopMatrix();
	// wall light
	glPushMatrix();
		glTranslatef(4,2,-5);
		glRotatef(-90,0,1,0);
		draw_wall_light(room_display_lists);
	glPopMatrix();
}
Beispiel #3
0
void* barber_thread(void* arg) {
    int i = 0;
    int id = *((int *) arg);
    srand(time(NULL));
    while (1) {
        initscr();
        start_color();

        if (pthread_mutex_lock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");
            draw_chair(barber_chairs[id], 1, "B", '%', id);
        if (pthread_mutex_unlock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");

        sem_wait(&customer_ready);

        if (pthread_mutex_lock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");
            draw_chair(barber_chairs[id], 1, " ", ' ', id);
            draw_chair(haircut_chairs[0], 1, "B", '%', id);
        if (pthread_mutex_unlock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");

        sem_wait(&access_waiting_room_seats);

        number_of_free_seats++;

        sem_post(&barber_ready);
        sem_post(&access_waiting_room_seats);
//        printf("barber is cutting hair: ");
        usleep(2000000);
        if (pthread_mutex_lock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");
        draw_chair(haircut_chairs[0], 1, " ", ' ', id);
        if (pthread_mutex_unlock(&draw_out) != 0)
            print_error(errno, "mutex_unlock error");

        endwin();
    }
    return NULL;
}