static void free_session(wrt_api_session_t session, void *data) { wrt_wtc_sop_session_t *wtc_sop_session = (wrt_wtc_sop_session_t*)data; BB_free(&wtc_sop_session->request_data); BB_free(&wtc_sop_session->reply_data); free(wtc_sop_session); return; }
int main(int argc, char** argv) { //variables int A; // number of slots to Actions that can be received int B; // number of bartenders int C; // number of cups int S; // number of students int M; // maximum number of students that can be at the bar at once int fconf; int conf_size; struct stat bufstat; fconf = open("con.conf",O_RDONLY); fstat(fconf,&bufstat); conf_size = bufstat.size; char bufconf[conf_size]; read(fconf,bufconf,conf_size); int inputs_parsed[5]; //{Aval, Bval, Cval, Sval, Mval} parse_buffer(bufconf, inputs_parsed); A = inputs_parsed[0]; B = inputs_parsed[1]; C = inputs_parsed[2]; S = inputs_parsed[3]; M = inputs_parsed[4]; printf(1,"A: %d B: %d C: %d S: %d M: %d\n",A,B,C,S,M); void* students_stacks[S]; void* bartenders_stacks[B]; void* cup_boy_stack; int i; int student_tids[S]; //int bartender_tids[B]; finished_shift = 0; // cup_boy changes it to 1 if all students left the bar and sends Action => GO_HOME to bartenders file_to_write = open("out.txt",(O_CREATE | O_WRONLY)); // if(file_to_write == -1){ printf(1,"There was an error opening out.txt\n"); exit(); } //Databases bouncer = semaphore_create(M); //this is the bouncer to the Beinstein ABB = BB_create(A); //this is a BB for student actions: drink, ans for a dring DrinkBB = BB_create(A); //this is a BB holding the drinks that are ready to be drinking CBB = BB_create(C); //this is a BB hold clean cups DBB = BB_create(C); //this is a BB hold dirty cups cup_boy_lock = binary_semaphore_create(0); // initial cup_boy with 0 so he goes to sleep imidietly on first call to down general_mutex = binary_semaphore_create(1); //initialize C clean cups struct Cup* cup_array[C]; for(i = 0; i < C; i++){ cup_array[i] = malloc(sizeof(struct Cup)); //TODO free cups //memset(cup_array[i],0,sizeof(void*)*STACK_SIZE); cup_array[i]->id = i; add_clean_cup(cup_array[i]); } //initialize cup_boy cup_boy_stack = (void*)malloc(sizeof(void*)*STACK_SIZE); memset(cup_boy_stack,0,sizeof(void*)*STACK_SIZE); if(thread_create((void*)cup_boy,cup_boy_stack,sizeof(void*)*STACK_SIZE) < 0){ printf(2,"Failed to create cupboy thread. Exiting...\n"); exit(); } //initialize B bartenders for(i = 0; i < B; i++){ bartenders_stacks[i] = (void*)malloc(sizeof(void*)*STACK_SIZE); memset(bartenders_stacks[i],0,sizeof(void*)*STACK_SIZE); thread_create((void*)bartender,bartenders_stacks[i],sizeof(void*)*STACK_SIZE);//TODO test //bartender_tids[i] = } //initialize S students for(i = 0; i < S; i++){//TODO test for fail students_stacks[i] = malloc(sizeof(void*)*STACK_SIZE); memset(students_stacks[i],0,sizeof(void*)*STACK_SIZE); student_tids[i] = thread_create((void*)student,students_stacks[i],sizeof(void*)*STACK_SIZE); } join_peoples(student_tids,S); //join students finished_shift = 1; //join_peoples(bartender_tids,B); //join bartenders sleep(2); // delay so exit will not come before threads finished TODO (need better soloution) if(finished_shift){ binary_semaphore_up(cup_boy_lock); } if(close(file_to_write) == -1){ printf(1,"There was an error closing out.txt\n"); exit(); } //free cup_boy_stack free(cup_boy_stack); //after all students have finished need to exit all bartenders and cup boy, and free all memory allocation //free cups for(i = 0; i < C; i++){ free(cup_array[i]); } //free bartenders_stacks for(i = 0; i < B; i++){ free(bartenders_stacks[i]); } //free students_stacks for(i = 0; i < S; i++){ free(students_stacks[i]); } semaphore_free(bouncer); BB_free(ABB); BB_free(DrinkBB); BB_free(CBB); BB_free(DBB); exit(); return 0; }