//void cancel(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority) void cancel(char* buf, int bufsize, int seat_id, int customer_id) { seat_t* curr = seat_header; while(curr != NULL) { if(curr->id == seat_id) { if(curr->state == PENDING && curr->customer_id == customer_id ) { pthread_mutex_lock(&seat_mutex[curr->id]); snprintf(buf, bufsize, "Seat request cancelled: %d %c\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = AVAILABLE; pthread_mutex_unlock(&seat_mutex[curr->id]); } else if(curr->customer_id != customer_id ) { snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n"); } else if(curr->state != PENDING) { snprintf(buf, bufsize, "No pending request\n\n"); } return; } curr = curr->next; } snprintf(buf, bufsize, "Seat not found\n\n"); return; }
void confirm_seat(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority) { seat_t* curr = seat_header; while(curr != NULL) { if(curr->id == seat_id) { pthread_mutex_lock(curr->lock); if(curr->state == PENDING && curr->customer_id == customer_id ) { snprintf(buf, bufsize, "Seat confirmed: %d %c\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = OCCUPIED; } else if(curr->customer_id != customer_id ) { snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n"); } else if(curr->state != PENDING) { snprintf(buf, bufsize, "No pending request\n\n"); } pthread_mutex_unlock(curr->lock); return; } curr = curr->next; } snprintf(buf, bufsize, "Requested seat not found\n\n"); return; }
//void view_seat(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority) void view_seat(char* buf, int bufsize, int seat_id, int customer_id) { seat_t* curr = seat_header; while(curr != NULL) { if(curr->id == seat_id) { if(curr->state == AVAILABLE || (curr->state == PENDING && curr->customer_id == customer_id)) { pthread_mutex_lock(&seat_mutex[curr->id]); snprintf(buf, bufsize, "Confirm seat: %d %c ?\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = PENDING; curr->customer_id = customer_id; pthread_mutex_unlock(&seat_mutex[curr->id]); } else { snprintf(buf, bufsize, "Seat unavailable\n\n"); } return; } curr = curr->next; } snprintf(buf, bufsize, "Requested seat not found\n\n"); return; }
void list_seats(char* buf, int bufsize, pool_t* p) { seat_t* curr = seat_header; int index = 0; int i = 0; while(curr != NULL && index < bufsize+ strlen("%d %c,")) { int length; if(pthread_mutex_trylock(&p->seat_locks[i])==0){ length = snprintf(buf+index, bufsize-index, "%d %c,", curr->id, seat_state_to_char(curr->state)); pthread_mutex_unlock(&p->seat_locks[i]); } else{ length = snprintf(buf+index, bufsize-index, "%d P,", curr->id); } if (length > 0) index = index + length; curr = curr->next; i++; } if (index > 0) snprintf(buf+index-1, bufsize-index-1, "\n"); else snprintf(buf, bufsize, "No seats not found\n\n"); }
void view_seat(char *buf, int bufsize, int seat_id, int customer_id, int customer_priority) { seat_t *curr = seat_header; while (curr != NULL) { if (curr->id == seat_id) { pthread_mutex_lock(&curr->lock); if (curr->state == AVAILABLE || (curr->state == PENDING && curr->customer_id == customer_id)) { if (curr->state == AVAILABLE) { sem_wait_pseudo(&seat_sem); } snprintf(buf, bufsize, "Confirm seat: %d %c ?\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = PENDING; curr->customer_id = customer_id; curr->timestamp = clock(); } else { if (sem_empty(&seat_sem)) { if (standby_list_empty(&stdby)) { snprintf(buf, bufsize, "Seat unavailable, waiting list full\n\n"); } else { standby_list_push(&stdby, customer_id); snprintf(buf, bufsize, "You are in the waiting list\n\n"); } } else { snprintf(buf, bufsize, "Seat unavailable\n\n"); } } pthread_mutex_unlock(&curr->lock); return; } curr = curr->next; } snprintf(buf, bufsize, "Requested seat not found\n\n"); return; }
void list_seats(char *buf, int bufsize) { seat_t *curr = seat_header; int index = 0; while (curr != NULL && index < bufsize + strlen("%d %c,")) { int length = snprintf(buf + index, bufsize - index, "%d %c,", curr->id, seat_state_to_char(curr->state)); if (length > 0) { index = index + length; } curr = curr->next; } if (index > 0) { snprintf(buf + index - 1, bufsize - index - 1, "\n"); } else { snprintf(buf, bufsize, "No seats found\n\n"); } }
void list_seats(char* buf, int bufsize) { seat_t* curr = seat_header; int index = 0; while(curr != NULL && index < bufsize+ strlen("%d %c,")) { /*LOCK*/ pthread_mutex_lock(&(curr->lock)); int length = snprintf(buf+index, bufsize-index, "%d %c,", curr->id, seat_state_to_char(curr->state)); /*UNLOCK*/ pthread_mutex_unlock(&(curr->lock)); if (length > 0) index = index + length; curr = curr->next; } if (index > 0) snprintf(buf+index-1, bufsize-index-1, "\n"); else snprintf(buf, bufsize, "No seats not found\n\n"); }
void cancel(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority) { seat_t* curr = seat_header; while(curr != NULL) { if(curr->id == seat_id) { pthread_mutex_lock(&(curr->lock)); if(curr->state == PENDING && curr->customer_id == customer_id ) { snprintf(buf, bufsize, "Seat request cancelled: %d %c\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = AVAILABLE; curr->customer_id = -1; // //if possible, assign to someonefrom standby list assign_standby(buf,bufsize,seat_id); // // } else if(curr->customer_id != customer_id ) { snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n"); } else if(curr->state != PENDING) { snprintf(buf, bufsize, "No pending request\n\n"); } pthread_mutex_unlock(&(curr->lock)); return; } curr = curr->next; } snprintf(buf, bufsize, "Seat not found\n\n"); return; }
void view_seat(char* buf, int bufsize, int seat_id, int customer_id, int customer_priority) { seat_t* curr = seat_header; while(curr != NULL) { if(curr->id == seat_id) { pthread_mutex_lock(&(curr->lock)); if(curr->state == AVAILABLE || (curr->state == PENDING && curr->customer_id == customer_id)) { snprintf(buf, bufsize, "Confirm seat: %d %c ?\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = PENDING; curr->customer_id = customer_id; } else { snprintf(buf, bufsize, "Seat unavailable\n\n"); // // add to standby list sem_wait(&standby_sem); add_to_standby(buf,bufsize,seat_id,customer_id); sem_post(&standby_sem); // // } pthread_mutex_unlock(&(curr->lock)); return; } curr = curr->next; } snprintf(buf, bufsize, "Requested seat not found\n\n"); return; }
void cancel(char *buf, int bufsize, int seat_id, int customer_id, int customer_priority) { seat_t *curr = seat_header; while (curr != NULL) { if (curr->id == seat_id) { pthread_mutex_lock(&curr->lock); if (curr->state == PENDING && curr->customer_id == customer_id) { snprintf(buf, bufsize, "Seat request cancelled: %d %c\n\n", curr->id, seat_state_to_char(curr->state)); curr->state = AVAILABLE; curr->customer_id = -1; int cid = standby_list_pop(&stdby); if (cid > 0) { curr->state = PENDING; curr->customer_id = cid; } else { sem_post_pseudo(&seat_sem); } } else if (curr->customer_id != customer_id) { snprintf(buf, bufsize, "Permission denied - seat held by another user\n\n"); } else if (curr->state != PENDING) { snprintf(buf, bufsize, "No pending request\n\n"); } pthread_mutex_unlock(&curr->lock); return; } curr = curr->next; } snprintf(buf, bufsize, "Seat not found\n\n"); return; }