static void bridge_init_game(void) { seat_t s; game.specific = ggz_malloc(sizeof(bridge_game_t)); set_num_seats(4); set_num_teams(2); for (s = 0; s < game.num_seats; s++) { assign_seat(s, s); /* one player per seat */ assign_team(s % 2, s); } game.cumulative_scores = 0; /* TODO: for now we won't use bridge scoring */ BRIDGE.declarer = -1; }
/* Method for sellers to run */ void *ProcessSellers(void *threadarg) { struct seller_data *my_data; my_data = (struct seller_data *) threadarg; while (time_elapsed() <= SECS_TO_RUN) { // Only run if time isn't up if (is_empty(my_data->next)) { sleep(1); // Sleep for 1 second if no one in line } else { pthread_mutex_lock(&mutex_seating); int assigned = assign_seat(my_data); // Assign seating pthread_mutex_unlock(&mutex_seating); char *str = time_elapsed_string(); if (assigned) { int completion_time = 0; if (my_data->type == H) { completion_time = rand() % 2 + 1; // Sleep for 1 - 2 } else if (my_data->type == M) { completion_time = rand() % 3 + 2; // Sleep for 2 - 4 } else { completion_time = rand() % 4 + 4; // Sleep for 4 - 7 } pthread_mutex_lock(&my_data->mutex); remove_head(my_data); // Remove first person from the line // Increment wait timer on all people in line and get amount of buyers who left int cust_left = increment_wait_timer(my_data, completion_time, str); printf("%s: A customer was assigned a seat at %s\n", str, my_data->name); pthread_mutex_unlock(&my_data->mutex); // Increments ticket sold if (my_data->type == H) { h_sold++; h_left += cust_left; } else if (my_data->type == M) { m_sold++; m_left += cust_left; } else { l_sold++; l_left += cust_left; } // Sleep for random time to generate minutes to complete sale sleep(completion_time); free(str); char *str2 = time_elapsed_string(); printf("%s: A customer has completed their ticket purchase at %s\n", str2, my_data->name); free(str2); } else { pthread_mutex_lock(&my_data->mutex); printf("%s: Remaining %d customer(s) at %s are being told to leave since tickets are sold out\n", str, my_data->cust_count, my_data->name); free(str); // Remove and count everyone from the line while (my_data->next != NULL) { remove_head(my_data); if (my_data->type == H) h_leave++; else if (my_data->type == M) m_leave++; else l_leave++; } my_data->closed = 1; // Set seller to closed pthread_mutex_unlock(&my_data->mutex); pthread_exit(NULL); // Exit seller thread } } } pthread_mutex_lock(&my_data->mutex); my_data->closed = 1; // Set seller to closed int rem = my_data->cust_count; if (rem != 0) { char *str = time_elapsed_string(); printf("%s: Remaining %d customer(s) at %s are being told to leave since the selling time is up\n", str, my_data->cust_count, my_data->name); free(str); // Remove and count everyone from the line while (my_data->next != NULL) { remove_head(my_data); if (my_data->type == H) h_closing++; else if (my_data->type == M) m_closing++; else l_closing++; } } pthread_mutex_unlock(&my_data->mutex); }
int main(void) { struct planestats plane[SEATS]; int choice; int i; size_t size = sizeof(struct planestats); FILE *fp; if((fp = fopen("air.dat","rb")) == NULL) { fprintf(stderr, "Can't open file %s","air.dat\n"); if((fp = fopen("air.dat","wb")) == NULL) { exit(1); } for(i = 0; i < SEATS; i++) { plane[i].seat_id = i + 1; plane[i].status = 0; } fwrite(plane, size , SEATS, fp); puts("We created the file air.dat now."); exit(1); } for(i = 0; i < SEATS; i++) { plane[i].seat_id = i + 1; plane[i].status = 0; } fread(plane, size , SEATS, fp); fclose(fp); while((choice = getmenu()) != 'f') { switch(choice) { case 'a': printf("%d seat is empty!\n",empty_seat(plane,SEATS)); break; case 'b': show_empty(plane,SEATS); break; case 'c': show_seats(plane,SEATS); break; case 'd': assign_seat(plane,SEATS); break; case 'e': delet_seat(plane,SEATS); break; } } if((fp = fopen("air.dat","wb")) == NULL) { fprintf(stderr, "Can't open file %s","air.dat"); exit(1); } fwrite(plane, size , SEATS, fp); fclose(fp); puts("Bye!"); return 0; }