// Cup boy simulation void* cup_boy(void){ void* ret_val = 0; cup_boy_lock = binary_semaphore_create(0); int i,n; for(;;){ if(finished_shift){ thread_exit(ret_val); } n =DBB->full->value; for(i = 1; i < n; i++){ struct Cup* current_cup = wash_dirty(); sleep(1); add_clean_cup(current_cup); //need to write to file intsead of screen TODO printf(1,"Cup boy added clean cup %d\n",current_cup->id); } printf(1,"Clean Boy- sleep\n"); binary_semaphore_down(cup_boy_lock); printf(1,"Clean Boy- awake\n"); } return ret_val; }
int main() { exit_lk=semaphore_create(0); lk=binary_semaphore_create(0); int err=0; if((err = read_config()) < 0){ printf(1, "error %d reading config file! exiting...\n", err); exit(); return -1; } printf(1, "config ok!\n\n"); printf(1, "making output file...\n"); err=open(OUTPUT, O_CREATE | O_RDWR); if(err < 0){ printf(1, "error %d opening output file! exiting...\n", err); exit(); return -1; } printf(1, "output file ok!\n\n"); printf(1, "creating data...\n"); create_data(); printf(1, "data created!\n\n"); printf(1, "creating students...\n"); if((err = create_students()) < 0){ printf(1, "error %d creating students! exiting...\n", err); exit(); return -1; } printf(1, "students created!\n\n"); printf(1, "creating bartenders...\n"); if((err = create_bartenders()) < 0){ printf(1, "error %d creating bartenders! exiting...\n", err); exit(); return -1; } printf(1, "bartenders created!\n\n"); printf(1, "creating cupboy...\n"); if((err = create_cupboy()) < 0){ printf(1, "error %d creating cupboy! exiting...\n", err); exit(); return -1; } printf(1, "cupboy created!\n\n"); //semaphore_down(exit_lk); //binary_semaphore_down(lk); sleep(100); printf(1, "\nEXIT!\n\n"); close(fileOut); exit(); return 0; }
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; }