diff --git a/BreakDownManager.c b/BreakDownManager.c index b3061d5..a98cd34 100644 --- a/BreakDownManager.c +++ b/BreakDownManager.c @@ -15,15 +15,15 @@ #include "TeamManager.h" struct config_fich_struct *inf_fich; - +struct ids *msgid; struct team *team_list; struct semaphoreStruct *semaphore_list; int start_breakdown = 0; void endBreakDown(int signum){ - free(semaphore_list); - free(inf_fich); + msgctl(msgid->msg_queue_id,IPC_RMID,NULL); + start_breakdown =0 ; #ifdef DEBUG printf("Break Down Manager is out!\n"); #endif @@ -31,56 +31,61 @@ void endBreakDown(int signum){ } +void waitSig(int signum){ + pause(); +} + void raceStartBreakdown(int signum){ start_breakdown = 1; } -void createBreakdowns(struct ids *idsP){ +void createBreakdowns(struct ids *idsP, sigset_t mask, sigset_t new_mask){ + char log[MAX]; + + sigprocmask(SIG_BLOCK,&new_mask, NULL); sem_wait(semaphore_list->writingMutex); srand((unsigned) time(NULL)); + for(int i= 0; i < inf_fich->number_of_teams; i++){ + for(int j = 0; j < team_list[i].number_of_cars; j++){ - int r = rand()%100; - if(r >= team_list[i].cars[j].reliability && team_list[i].cars[j].has_breakdown != 1){ + int r = rand()%100; - char problem_string[200]=""; - char car_number[3]=""; + if(r >= team_list[i].cars[j].reliability && team_list[i].cars[j].has_breakdown != 1 && strcmp(team_list[i].cars[j].current_state,"TERMINADO") != 0 && strcmp(team_list[i].cars[j].current_state,"DESISTENCIA") != 0){ - strcpy(problem_string,"NEW PROBLEM IN CAR "); - sprintf(car_number,"%2d",team_list[i].cars[j].car_number); - strcat(problem_string,car_number); + sprintf(log, "NEW PROBLEM IN CAR %02d",team_list[i].cars[j].car_number); + //printf("%s\n",log); + writeLog(log,semaphore_list->logMutex,inf_fich->fp); - printf("NEW PROBLEM IN CAR %2d\n",team_list[i].cars[j].car_number); - writeLog(problem_string,semaphore_list->logMutex,inf_fich->fp); struct messageQ msg; msg.mtype=i*inf_fich->number_of_cars+j+1; msg.response = 1; - #ifdef DEBUG + #ifdef DEBUG printf("Breakdown Sent(%s)\n", team_list[i].team_name); #endif - msgsnd(idsP->msg_queue_id, &msg, sizeof(msg)-sizeof(long), 0); + msgsnd(idsP->msg_queue_id, &msg, sizeof(msg)-sizeof(long), IPC_NOWAIT); } } } sem_post(semaphore_list->writingMutex); - + sigprocmask(SIG_UNBLOCK,&new_mask, NULL); } void BreakDownManager(struct config_fich_struct *inf_fichP, struct team *team_listP, struct semaphoreStruct *semaphore_listP, struct ids *idsP){ sigset_t mask, new_mask; - + printf("Breakdown id: %d\n", getpid()); //Ignore all unwanted signals! sigfillset(&mask); sigprocmask(SIG_SETMASK, &mask, NULL); @@ -89,11 +94,13 @@ void BreakDownManager(struct config_fich_struct *inf_fichP, struct team *team_li sigemptyset(&new_mask); sigaddset(&new_mask, SIGUSR2); sigaddset(&new_mask, SIGTERM); + sigaddset(&new_mask, SIGUSR1); sigprocmask(SIG_UNBLOCK,&new_mask, NULL); signal(SIGUSR2,endBreakDown); signal(SIGTERM,raceStartBreakdown); + signal(SIGUSR1, waitSig); #ifdef DEBUG printf("Breakdown Manager created(%ld)\n",(long)getpid()); @@ -102,21 +109,19 @@ void BreakDownManager(struct config_fich_struct *inf_fichP, struct team *team_li inf_fich = inf_fichP; team_list = team_listP; semaphore_list = semaphore_listP; - + msgid = idsP; pause(); - + //Gives time to threads to be created + sleep(1); while(start_breakdown == 1) { - sleep(inf_fich->T_Avaria); - createBreakdowns(idsP); + sleep(1/inf_fich->time_units_per_second * inf_fich->T_Avaria); + + createBreakdowns(idsP, mask ,new_mask); } - #ifdef DEBUG - printf("Breakdown Manager is out!\n"); - #endif - exit(0); } diff --git a/BreakDownManager.o b/BreakDownManager.o index efb36a7..592447e 100644 Binary files a/BreakDownManager.o and b/BreakDownManager.o differ diff --git a/MultipleProcessActions.c b/MultipleProcessActions.c index 741eb52..13480a1 100644 --- a/MultipleProcessActions.c +++ b/MultipleProcessActions.c @@ -17,14 +17,16 @@ void writeLog(char * string, sem_t *mutex,FILE *fp){ char buffer[550]=""; time_t rawtime; - struct tm * timeinfo; + struct tm timeinfo; time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - sprintf(buffer,"%.2d:%.2d:%.2d %s\n",timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,string); + localtime_r ( &rawtime, &timeinfo); + sprintf(buffer,"%.2d:%.2d:%.2d %s\n",timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,string); sem_wait(mutex); + printf("%s",buffer); + fprintf(fp,"%s",buffer); fflush(fp); sem_post(mutex); @@ -51,7 +53,7 @@ int writingNewCarInSharedMem(struct team *team_list, struct car *new_car, struct team_list[i].cars[j] = *new_car; sem_post(semaphore_list->writingMutex); - sprintf(carLog,"NEW CAR LOADED => TEAM: %s, CAR: %d, SPEED: %d, CONSUMPTION: %.2f, RELIABILITY: %d",team_name,new_car->car_number, new_car->speed,new_car->consumption,new_car->reliability); + sprintf(carLog,"NEW CAR LOADED => TEAM: %s, CAR: %02d, SPEED: %d, CONSUMPTION: %.2f, RELIABILITY: %d",team_name,new_car->car_number, new_car->speed,new_car->consumption,new_car->reliability); writeLog(carLog,semaphore_list->logMutex, inf_fich->fp); return 0; } @@ -70,7 +72,7 @@ int writingNewCarInSharedMem(struct team *team_list, struct car *new_car, struct team_list[i].number_of_cars = 1; sem_post(semaphore_list->writingMutex); - sprintf(carLog,"NEW CAR LOADED => TEAM: %s, CAR: %d, SPEED: %d, CONSUMPTION: %.2f, RELIABILITY: %d",team_name,new_car->car_number, new_car->speed,new_car->consumption,new_car->reliability); + sprintf(carLog,"NEW CAR LOADED => TEAM: %s, CAR: %02d, SPEED: %d, CONSUMPTION: %.2f, RELIABILITY: %d",team_name,new_car->car_number, new_car->speed,new_car->consumption,new_car->reliability); writeLog(carLog,semaphore_list->logMutex, inf_fich->fp); return 1; } @@ -184,19 +186,17 @@ void updateState(struct team *team_list, struct semaphoreStruct *semaphore_list, //Prints the statistics of a race (could be midway or at the end). This has priority over writing actions void readStatistics(struct config_fich_struct *inf_fich, struct team *team_list, struct semaphoreStruct *semaphore_list){ - + printf("entered here\n"); sem_wait(semaphore_list->writingMutex); - + printf("yo\n"); //First line for team index second line for car index int top5Teams[5][2] = {{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}; int lastTeam[1][2] = {{-1,-1}}; - getTop5Teams(inf_fich, team_list, top5Teams); getLastTeam(inf_fich, team_list, lastTeam); int total_breakdowns = amountBreakdowns(inf_fich, team_list); int total_reffils = amountReffil(inf_fich, team_list); int total_racing = amountRacing(inf_fich, team_list); - for(int i = 0; i<5; i++){ printf("Lugar: %d, Número Carro: %d, Nome Equipa: %s, Número Voltas: %d, Número de Paragens na Box: %d\n", i+1, team_list[top5Teams[i][0]].cars[top5Teams[i][1]].car_number , team_list[top5Teams[i][0]].team_name diff --git a/MultipleProcessActions.o b/MultipleProcessActions.o index 48178ac..c84a6e1 100644 Binary files a/MultipleProcessActions.o and b/MultipleProcessActions.o differ diff --git a/RaceManager.c b/RaceManager.c index da228cd..60de312 100644 --- a/RaceManager.c +++ b/RaceManager.c @@ -29,27 +29,64 @@ struct ids *ids_proc; int *pids; int *pipes; -void my_handler(int signum) -{ - - if (signum == SIGUSR1) - { - #ifdef DEBUG - printf("Received SIGUSR1! => Interromper corrida\n"); - #endif +int number_of_teams_stopped; +int number_added_this_iteration; +int race_has_paused; +int start = 0; + +void skip(int signum){ + + number_of_teams_stopped++; + + //means all the teams have finished + if(race_has_paused == 0 && number_of_teams_stopped == inf_fich->number_of_teams){ + writeLog("EVERY SINGLE CAR GAVE UP", semaphore_list->logMutex, inf_fich->fp); + kill(getppid(), SIGINT); } } +void pauseRace(int signum){ + + signal(SIGUSR1, SIG_IGN); + + if(start == 1){ + + race_has_paused =1; + writeLog("SIMULATOR PAUSING", semaphore_list->logMutex, inf_fich->fp); + + for(int i = 0; i < inf_fich->number_of_teams; i++){ + kill(pids[i], SIGUSR1); + } + + kill(ids_proc->pid_breakdown, SIGUSR1); + + start = 0; + + number_added_this_iteration = 0; + + writeLog("WAITING FOR ALL CARS TO PAUSE", semaphore_list->logMutex, inf_fich->fp); + while(number_of_teams_stopped != inf_fich->number_of_teams){ + pause(); + number_added_this_iteration++; + } + + writeLog("ALL CARS PAUSED", semaphore_list->logMutex, inf_fich->fp); + printf("IF YOU WANT TO RESTART RACE TYPE: START RACE!\n"); + } + +} void endRaceManager(int signum){ - pid_t wpid; - int status = 0; + for(int i = 0; i < inf_fich->number_of_teams; i++){ kill(pids[i], SIGUSR2); } free(pids); - while ((wpid = wait(&status)) > 0); + + for(int i = 0; i < inf_fich->number_of_teams; i++){ + wait(NULL); + } #ifdef DEBUG printf("RaceManeger is out.\n"); @@ -61,12 +98,10 @@ void endRaceManager(int signum){ void endRace(){ kill(ids_proc->pid_breakdown, SIGUSR2); + kill(getppid(), SIGUSR2); endRaceManager(0); } -void interruptRace(int signum){ - printf("TO DO\n"); -} int getFreeChannel(int n, int pipes[n]){ for(int i=0;i 0 ) { //NAO FAÇO -1 PQ O "PIPES" TEM NUMBER_OF_TEAMS + 1 (NAMED) if(FD_ISSET(pipes[0], &read_set)){ - + sigprocmask(SIG_BLOCK,&new_mask, NULL); read(pipes[0],received,sizeof(received)); #ifdef DEBUG printf("[RaceManager] (NP) Received: %s\n",received); @@ -171,34 +207,51 @@ void Race_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, //char received[512] ="ADDCAR TEAM: C, CAR: 21, SPEED: 30, CONSUMPTION: 0.04, RELIABILITY: 95"; //char received[512] ="START RACE!"; if(strcmp(received,"START RACE!")==0){ + printf("here!\n"); //verificar se o numero de equipas é suficiente => erro no ecra e nos logs if(start == 0){ - int n_equipas=0; - for(int i=0; inumber_of_teams; i++){ - if(strcmp(team_list[i].team_name,"")!=0){ - n_equipas++; - } - } - if(n_equipas!=inf_fich->number_of_teams){ - printf("CANNOT START, NOT ENOUGH TEAMS\n"); - writeLog("CANNOT START, NOT ENOUGH TEAMS",semaphore_list->logMutex,inf_fich->fp); + + if(firstStart == 0){ + int n_equipas=0; + for(int i=0; inumber_of_teams; i++){ + if(strcmp(team_list[i].team_name,"")!=0){ + n_equipas++; + } + } + if(n_equipas!=inf_fich->number_of_teams){ + writeLog("CANNOT START, NOT ENOUGH TEAMS",semaphore_list->logMutex,inf_fich->fp); + } + else{ + //Notificar os TeamManagers do inicio da corrida + for(int i = 0; inumber_of_teams; i++){ + kill(pids[i], SIGTERM); + + } + kill(getppid(), SIGTERM); + kill(idsP->pid_breakdown, SIGTERM); + start = 1; + firstStart = 1; + + } } else{ - //Notificar os TeamManagers do inicio da corrida + //Notificar os TeamManagers do inicio da corrida for(int i = 0; inumber_of_teams; i++){ - kill(pids[i], SIGTERM); - } + kill(pids[i], SIGTERM); + } kill(idsP->pid_breakdown, SIGTERM); + writeLog("RACE UNPAUSED",semaphore_list->logMutex,inf_fich->fp); start = 1; + race_has_paused =0; + number_of_teams_stopped -= number_added_this_iteration; + signal(SIGUSR1, pauseRace); } } else if(start == 1){ - strcpy(wrong_command_string,received); - strcat(wrong_command_string," => Rejected, race already started!"); - printf("%s\n",wrong_command_string); - writeLog(wrong_command_string,semaphore_list->logMutex,inf_fich->fp); + sprintf(log,"%s => Rejected, race already started!",received); + writeLog(log,semaphore_list->logMutex,inf_fich->fp); } } //is START RACE! else{ // Check if it is ADDCAR @@ -213,16 +266,13 @@ void Race_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, newCar->consumption > inf_fich->fuel_capacity || newCar->reliability < 0 ){ - strcat(wrong_command_string,"WRONG COMMAND => "); - strcat(wrong_command_string,received); - printf("%s\n",wrong_command_string); - writeLog(wrong_command_string,semaphore_list->logMutex,inf_fich->fp); + sprintf(log,"WRONG COMMAND => %s",received); + writeLog(log,semaphore_list->logMutex,inf_fich->fp); } else{ if(start == 1){ //Race already started - printf("%s => ",received); - printf("Rejected, race already started!\n"); - writeLog("Rejected, race already started!",semaphore_list->logMutex,inf_fich->fp); + sprintf(log,"%s => Rejected, race already started!",received); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); } else{ //ADD CAR @@ -261,11 +311,13 @@ void Race_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, } } } //ISNt START RACE! + sigprocmask(SIG_UNBLOCK,&new_mask, NULL); }//isset //else{ for(int channel=1;channel<(inf_fich->number_of_teams+1);channel++){ if(FD_ISSET(pipes[channel], &read_set)){ + sigprocmask(SIG_BLOCK,&new_mask, NULL); struct message data; @@ -276,8 +328,12 @@ void Race_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, printf("MENSAGEM RECEBIDA %s\n",data.message); if(strcmp(data.message,"TERMINADO") == 0){ + signal(SIGUSR1, SIG_IGN); + sigprocmask(SIG_UNBLOCK,&new_mask, NULL); endRace(); + } + sigprocmask(SIG_UNBLOCK,&new_mask, NULL); } } //} @@ -286,16 +342,5 @@ void Race_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, } //select } //while - //the father process waits for all his children to die :( - while ((wpid = wait(&status)) > 0); - -for(int i=0;inumber_of_teams+1;i++){ - close(pipes[i]); -} - free(pipes); - #ifdef DEBUG - printf("Race Manager is out!\n"); - #endif - exit(0); } diff --git a/RaceManager.o b/RaceManager.o index eaa5656..c0003f6 100644 Binary files a/RaceManager.o and b/RaceManager.o differ diff --git a/RaceSimulator.c b/RaceSimulator.c index 76193be..598aa67 100644 --- a/RaceSimulator.c +++ b/RaceSimulator.c @@ -49,9 +49,11 @@ struct ids *idsP; int shmid; int named_pipe_fd; pid_t pids[2]; +int race_started =0 ; //Only for debug purposes will be deleted/changed later +/* void leituraParaTeste(){ for(int i = 0; i< inf_fich->number_of_teams; i++){ @@ -63,7 +65,7 @@ void leituraParaTeste(){ break; } printf("Amount cars: %d\n Box State %s\n", team_list[i].number_of_cars, team_list[i].box_state ); - /*printf("Team name:%s\n Box state:%s\n Car number: %d\n Car speed: %d\n Car consumption: %.2f\n Car reliability: %d\n Number laps: %d\n Amount Breakdown %d\n Amount reffil:%d\n Car state:%s", team_list[i].team_name + printf("Team name:%s\n Box state:%s\n Car number: %d\n Car speed: %d\n Car consumption: %.2f\n Car reliability: %d\n Number laps: %d\n Amount Breakdown %d\n Amount reffil:%d\n Car state:%s", team_list[i].team_name , team_list[i].box_state , team_list[i].cars[j].car_number , team_list[i].cars[j].speed @@ -72,7 +74,7 @@ void leituraParaTeste(){ , team_list[i].cars[j].number_of_laps , team_list[i].cars[j].amount_breakdown , team_list[i].cars[j].times_refill - , team_list[i].cars[j].current_state);*/ + , team_list[i].cars[j].current_state); printf("Team name: %s\nBox state: %s\nCar number: %d\nCar speed: %d\nCar consumption: %.2f\nCar reliability: %d\nNumber of laps: %d\n", team_list[i].team_name , team_list[i].box_state @@ -87,17 +89,18 @@ void leituraParaTeste(){ printf("-----------\n"); } -} +}*/ //cleans active memory void clean(){ free(inf_fich); shmdt(team_list); - + msgctl(idsP->msg_queue_id,IPC_RMID,NULL); shmctl(shmid, IPC_RMID, NULL); sem_close(semaphore_list->logMutex); sem_close(semaphore_list->writingMutex); sem_close(semaphore_list->readingMutex); + sem_unlink("MUTEX"); sem_unlink("WRITING_MUTEX"); sem_unlink("READING_MUTEX"); @@ -107,32 +110,51 @@ void clean(){ unlink(PIPE_NAME); } + + +void endRaceSim(int signum){ + pid_t wpid; + int status = 0; + + while ((wpid = wait(&status)) > 0); + + //leituraParaTeste(); + if (race_started == 1) { + readStatistics(inf_fich, team_list, semaphore_list); + } + + clean(); + exit(0); +} + + void sigint(int signum){ - printf("SIGNAL SIGINT RECEIVED\n"); - writeLog("SIGNAL SIGINT RECEIVED", semaphore_list->logMutex, inf_fich->fp); + printf("\n"); + writeLog("SIMULATOR CLOSING", semaphore_list->logMutex, inf_fich->fp); signal(SIGINT, SIG_IGN); kill(pids[0],SIGUSR2); kill(pids[1],SIGUSR2); - pid_t wpid; - int status = 0; - while ((wpid = wait(&status)) > 0); - leituraParaTeste(); - readStatistics(inf_fich, team_list, semaphore_list); - clean(); - exit(0); + endRaceSim(0); } + void sigtstp(int signum){ signal(SIGTSTP, SIG_IGN); - readStatistics(inf_fich, team_list, semaphore_list); + if(race_started == 1) readStatistics(inf_fich, team_list, semaphore_list); signal(SIGTSTP, sigtstp); } +void raceStartRaceSim(int signum){ + race_started =1; +} + + + //Main function. Here the RaceManager and the MalfunctionManager processes will be created int main(int argc, char* argv[]){ @@ -145,15 +167,15 @@ int main(int argc, char* argv[]){ //Create the wanted signals sigemptyset(&new_mask); sigaddset(&new_mask,SIGINT); - sigaddset(&new_mask,SIGTSTP); + sigaddset(&new_mask,SIGTSTP); + sigaddset(&new_mask,SIGTERM); + sigaddset(&new_mask, SIGUSR2); sigprocmask(SIG_UNBLOCK,&new_mask, NULL); signal(SIGINT, sigint); signal(SIGTSTP, sigtstp); - - - - + signal(SIGUSR2, endRaceSim); + signal(SIGTERM, raceStartRaceSim); semaphore_list = (struct semaphoreStruct*) malloc(sizeof(struct semaphoreStruct)); @@ -191,7 +213,7 @@ int main(int argc, char* argv[]){ for(int i = 0; i <= inf_fich->number_of_teams ; i++){ team_list[i].cars = (struct car*)(team_list + inf_fich->number_of_teams +1 + i * (inf_fich->number_of_cars)); } - printf("SIMULATOR STARTING\n"); + writeLog("SIMULATOR STARTING", semaphore_list->logMutex, inf_fich->fp); idsP = (struct ids*) malloc(sizeof(struct ids)); @@ -219,9 +241,6 @@ int main(int argc, char* argv[]){ } - - - unlink(PIPE_NAME); #ifdef DEBUG printf("Creating named pipe.\n"); @@ -271,24 +290,6 @@ int main(int argc, char* argv[]){ } } - //The main process waits for its child (BreakDownManager) to die - pid_t wpid; - int status = 0; - - while ((wpid = wait(&status)) > 0); - - #ifdef DEBUG - printf("---SHARED MEMORY BEFORE THE SIMULATOR ENDED---\n"); - //Only for debugging purposes. We know that there are no protections to the shared memory here, but they - //do not need to exist because it is in a controled envirnoment. In future work there will be a mutex - //stoping any readers in case someone is changing the shared memory. - leituraParaTeste(); - #endif - - printf("SIMULATOR CLOSING\n"); - writeLog("SIMULATOR CLOSING", semaphore_list->logMutex, inf_fich->fp); - clean(); - return 0; } diff --git a/RaceSimulator.h b/RaceSimulator.h index 90eb377..876b685 100644 --- a/RaceSimulator.h +++ b/RaceSimulator.h @@ -3,7 +3,7 @@ #define SIZE 50 #define DEBUG #define PIPE_NAME "pipe" -#define MAX 200 +#define MAX 550 #include #include diff --git a/RaceSimulator.o b/RaceSimulator.o index 6945659..eb9dddf 100644 Binary files a/RaceSimulator.o and b/RaceSimulator.o differ diff --git a/TeamManager.c b/TeamManager.c index 1fc1df6..94bae04 100644 --- a/TeamManager.c +++ b/TeamManager.c @@ -34,6 +34,7 @@ sem_t *reservation; sem_t *update_waiting; sem_t *counter_mutex; sem_t *wait_box; +sem_t *interrupt_race_mutex; int team_index; @@ -49,13 +50,68 @@ int interrupting = 0; int all_exited = 1; int amount_terminated = 0; int out = 0; +int firstStart = 0; sigset_t mask, new_mask; struct ids *idsT; //Ends the team + + +void cleanT(){ + sem_close(car_in_box); + sem_close(reservation); + sem_close(update_waiting); + sem_close(counter_mutex); + sem_close(wait_box); + sem_close(interrupt_race_mutex); + pthread_cond_destroy(&interruption_cond); + pthread_mutex_destroy(&mutex_interruption); + pthread_mutex_destroy(&mutex_stop); + pthread_cond_destroy(&stop_cond); + sem_unlink("BOX_MUTEX"); + sem_unlink("RESERVATION_MUTEX"); + sem_unlink("UPDATE_MUTEX"); + sem_unlink("END COUNTER"); + sem_unlink("WAIT BOX"); + sem_unlink("INTERRUPT MUTEX"); + +} + + + +void pauseRaceTeams(int signum){ + + interrupting = 1; + + + sigprocmask(SIG_BLOCK,&new_mask, NULL); + + + //Makes sure it waits for all of the cars + sem_wait(interrupt_race_mutex); + + + //Sends a signal to the race manager that this team has stopped all cars! + kill(getppid(), SIGALRM); + + sigprocmask(SIG_UNBLOCK,&new_mask, NULL); + + printf("waiting for signal\n"); + //waits for a signal + pause(); + + interrupting = 0; + printf("unpaused\n"); + pthread_cond_broadcast(&interruption_cond); +} + +/*This function is for when the team manager prepares to die. It waits for all the cars to +end their lap and, when they all do, the team manager cleans all of the resources it uses +and exits. This function can be called by SIGINT or when a car in the team ends the race.*/ void teamEnd(int signum){ + sigprocmask(SIG_BLOCK,&new_mask, NULL); //This code exists in case a SIGINT is called after a SIGUSR1 if(start_teams == 0){ for(int i =0 ; i < team_list[team_index].number_of_cars; i++){ @@ -65,6 +121,7 @@ void teamEnd(int signum){ } terminating = 1; + interrupting = 0; pthread_cond_broadcast(&interruption_cond); @@ -83,22 +140,7 @@ void teamEnd(int signum){ pthread_join(cars[j], NULL); } - - free(cars); - sem_close(car_in_box); - sem_close(reservation); - sem_close(update_waiting); - sem_close(counter_mutex); - sem_close(wait_box); - pthread_cond_destroy(&interruption_cond); - pthread_mutex_destroy(&mutex_interruption); - pthread_mutex_destroy(&mutex_stop); - pthread_cond_destroy(&stop_cond); - sem_unlink("BOX_MUTEX"); - sem_unlink("RESERVATION_MUTEX"); - sem_unlink("UPDATE_MUTEX"); - sem_unlink("END COUNTER"); - sem_unlink("WAIT BOX"); + cleanT(); #ifdef DEBUG printf("Team %ld out\n",(long)getpid()); @@ -109,33 +151,64 @@ void teamEnd(int signum){ -void waitForEnd(){ +void everyoneGaveUp(){ + printf("EVERYONE IS DEAD!\n"); + + for(int i =0 ; i < team_list[team_index].number_of_cars; i++){ + pthread_cancel(cars[i]); + } + kill(getppid(), SIGALRM); + + exit(0); - sem_wait(counter_mutex); - amount_terminated++; - printf("Team %s Amount terminated : %d\n",team_list[team_index].team_name, amount_terminated); - sem_post(counter_mutex); +} + +void waitForEnd(){ pthread_mutex_lock(&mutex_stop); - printf("enterr\n"); while(out == 0){ pthread_cond_wait(&stop_cond,&mutex_stop); } pthread_mutex_unlock(&mutex_stop); - printf("exittt\n"); pthread_exit(NULL); } +void gaveUp(){ + sem_wait(counter_mutex); + amount_terminated++; + printf("Team %s Amount terminated : %d\n",team_list[team_index].team_name, amount_terminated); + + if(amount_terminated == team_list[team_index].number_of_cars && terminating == 0){ + kill(getpid(),SIGALRM); + } + sem_post(counter_mutex); + waitForEnd(); + + +} +//This funcion is where threads wait for the race to end +void anouncingEnd(){ + + sem_wait(counter_mutex); + amount_terminated++; + printf("Team %s Amount terminated : %d\n",team_list[team_index].team_name, amount_terminated); + sem_post(counter_mutex); + + waitForEnd(); +} + + + //Cars are racing void racing(int arrayNumber){ - sigprocmask(SIG_BLOCK,&new_mask, &mask); + sigprocmask(SIG_BLOCK,&new_mask, NULL); + char log[MAX]; //Variables - char logMessage[MAX]; char update[MAX]; float current_fuel = inf_fich->fuel_capacity; float distance_in_lap = 0; @@ -148,15 +221,13 @@ void racing(int arrayNumber){ data.car_index = arrayNumber; data.team_index = team_index; - usleep(500); - printf("entrei\n"); + while(1){ - printf("Car %d while1 \n",team_list[team_index].cars[arrayNumber].car_number); if(terminating != 1 && team_list[team_index].cars[arrayNumber].has_breakdown != 1){ msgrcv(idsT->msg_queue_id, &msg, sizeof(msg)-sizeof(long), team_index*inf_fich->number_of_cars+arrayNumber+1, IPC_NOWAIT); } - printf("Car %d while2 \n",team_list[team_index].cars[arrayNumber].car_number); + if(msg.response == 1){ #ifdef DEBUG printf("Breakdown Received (%d) => Security Mode\n",team_list[team_index].cars[arrayNumber].car_number); @@ -165,7 +236,11 @@ void racing(int arrayNumber){ team_list[team_index].cars[arrayNumber].has_breakdown = 1; strcpy(team_list[team_index].cars[arrayNumber].current_state, "SEGURANCA"); + sprintf(log,"CAR %02d IS NOW IN STATE: SEGURANCA",team_list[team_index].cars[arrayNumber].car_number); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); + sem_wait(update_waiting); + if(strcmp(team_list[team_index].box_state, "LIVRE") == 0){ is_reserved = 1; #ifdef DEBUG @@ -193,18 +268,20 @@ void racing(int arrayNumber){ current_fuel -= 0.4 * team_list[team_index].cars[arrayNumber].consumption; distance_in_lap += 0.3 * team_list[team_index].cars[arrayNumber].speed; } - printf("Car %d while3 \n",team_list[team_index].cars[arrayNumber].car_number); + //The car has no more fuel. This means that the car is giving up on the race if(current_fuel <= 0){ strcpy(team_list[team_index].cars[arrayNumber].current_state,"DESISTENCIA"); - printf("Desisti\n"); - break; + sprintf(log,"CAR %02d IS NOW IN STATE: DESISTENCIA",team_list[team_index].cars[arrayNumber].car_number); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); + + gaveUp(); } - printf("Car %d while4 \n",team_list[team_index].cars[arrayNumber].car_number); + //If the car does not have enough fuel for the next 4 laps. The car enters security mode if(interrupting != 1 && terminating != 1 && current_fuel < 2 * ((inf_fich->lap_distance / team_list[team_index].cars[arrayNumber].speed)*team_list[team_index].cars[arrayNumber].consumption )){ @@ -212,6 +289,9 @@ void racing(int arrayNumber){ if(strcmp(team_list[team_index].cars[arrayNumber].current_state,"SEGURANCA") != 0){ strcpy(team_list[team_index].cars[arrayNumber].current_state, "SEGURANCA"); + sprintf(log,"CAR %02d IS NOW IN STATE: SEGURANCA",team_list[team_index].cars[arrayNumber].car_number); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); + } sem_wait(update_waiting); @@ -230,7 +310,7 @@ void racing(int arrayNumber){ } - printf("Car %d while5 \n",team_list[team_index].cars[arrayNumber].car_number); + //Passes a lap. Sees if the car has a breakdown or need a refill, if it does then it goes, else it continues the race if(distance_in_lap >= inf_fich->lap_distance){ team_list[team_index].cars[arrayNumber].number_of_laps++; @@ -252,13 +332,14 @@ void racing(int arrayNumber){ sem_wait(counter_mutex); amount_terminated++; sem_post(counter_mutex); - + printf("Stopped %s\n", team_list[team_index].team_name); #ifdef DEBUG printf("(%d) Interrupted.\n", team_list[team_index].cars[arrayNumber].car_number); #endif //all of the cars are now on stand by if(amount_terminated == team_list[team_index].number_of_cars){ start_teams = 0; + sem_post(interrupt_race_mutex); } pthread_mutex_lock(&mutex_interruption); @@ -286,14 +367,17 @@ void racing(int arrayNumber){ //If the race has no interruptions else{ - printf("Car %d while2 \n",team_list[team_index].cars[arrayNumber].car_number); - printf("Number of laps, name car (%d, %s)\n", team_list[team_index].cars[arrayNumber].number_of_laps, team_list[team_index].team_name); + printf("Number of laps, car number, team (%d, %d, %s)\n", team_list[team_index].cars[arrayNumber].number_of_laps, team_list[team_index].cars[arrayNumber].car_number, team_list[team_index].team_name); //The car ended the race! if(team_list[team_index].cars[arrayNumber].number_of_laps >= inf_fich->number_of_laps){ strcpy(team_list[team_index].cars[arrayNumber].current_state, "TERMINADO"); + + sprintf(log,"CAR %02d IS NOW IN STATE: TERMINADO",team_list[team_index].cars[arrayNumber].car_number); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); + strcpy(data.message, "TERMINADO"); write(channel[1], &data, sizeof(struct message)); #ifdef DEBUG @@ -305,11 +389,12 @@ void racing(int arrayNumber){ distance_in_lap = distance_in_lap - inf_fich->lap_distance; - printf("antes de esperar"); + sem_wait(update_waiting); - printf("Car %d while7 \n",team_list[team_index].cars[arrayNumber].car_number); - if((strcmp(team_list[team_index].cars[arrayNumber].current_state, "SEGURANCA") == 0) && ((strcmp(team_list[team_index].box_state, "RESERVADO") == 0) || (strcmp(team_list[team_index].box_state, "LIVRE") == 0))){ + + + if((strcmp(team_list[team_index].cars[arrayNumber].current_state, "SEGURANCA") == 0) && (strcmp(team_list[team_index].box_state, "RESERVADO") == 0)){ car_index = arrayNumber; @@ -323,11 +408,14 @@ void racing(int arrayNumber){ strcpy(team_list[team_index].cars[arrayNumber].current_state, "BOX"); + sprintf(log,"CAR %02d IS NOW IN STATE: BOX",team_list[team_index].cars[arrayNumber].car_number); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); + //Waits for the everything to get sorted in the car - sem_wait(wait_box); - current_fuel = team_list[team_index].cars[car_index].consumption; + current_fuel = inf_fich->fuel_capacity; + sem_wait(wait_box); //if the race has terminated while he was in the box if(terminating == 1){ @@ -336,15 +424,25 @@ void racing(int arrayNumber){ } else if(interrupting == 1){ - printf("Car %d while8 \n",team_list[team_index].cars[arrayNumber].car_number); + sem_wait(counter_mutex); amount_terminated++; + + printf("Stopped %s\n", team_list[team_index].team_name); + if(amount_terminated == team_list[team_index].number_of_cars){ + + start_teams = 0; + sem_post(interrupt_race_mutex); + } + sem_post(counter_mutex); #ifdef DEBUG printf("(%d) Interrupted.\n", team_list[team_index].cars[arrayNumber].car_number); #endif pthread_mutex_lock(&mutex_interruption); + + while (interrupting == 1) { pthread_cond_wait(&interruption_cond, &mutex_interruption); @@ -367,11 +465,14 @@ void racing(int arrayNumber){ //TO-DO Add a semaphore here strcpy(team_list[team_index].cars[arrayNumber].current_state, "CORRIDA"); + sprintf(log,"CAR %02d IS NOW IN STATE: CORRIDA",team_list[team_index].cars[arrayNumber].car_number); + writeLog(log, semaphore_list->logMutex, inf_fich->fp); + } else if(current_fuel < 4 * ((inf_fich->lap_distance / team_list[team_index].cars[arrayNumber].speed)*team_list[team_index].cars[arrayNumber].consumption) && strcmp(team_list[team_index].box_state, "LIVRE") == 0){ - printf("Car %d while9 \n",team_list[team_index].cars[arrayNumber].car_number); + //car reserves and immediatly goes into box //TO-DO add semaphore is_reserved = 0; @@ -385,16 +486,24 @@ void racing(int arrayNumber){ #ifdef DEBUG printf("(%s) Stopped in box.\n", team_list[team_index].team_name); #endif + + current_fuel = inf_fich->fuel_capacity; sem_wait(wait_box); - printf("Car %d while10 \n",team_list[team_index].cars[arrayNumber].car_number); + if(terminating == 1){ strcpy(team_list[team_index].cars[arrayNumber].current_state,"TERMINADO"); break; } else if( interrupting == 1){ + printf("Stopped %s\n", team_list[team_index].team_name); sem_wait(counter_mutex); amount_terminated++; + if(amount_terminated == team_list[team_index].number_of_cars){ + start_teams = 0; + sem_post(interrupt_race_mutex); + } + sem_post(counter_mutex); #ifdef DEBUG printf("(%d) Interrupted.\n", team_list[team_index].cars[arrayNumber].car_number); @@ -426,7 +535,6 @@ void racing(int arrayNumber){ //Normal time else{ - printf("Car %d while11 \n",team_list[team_index].cars[arrayNumber].car_number); sem_post(update_waiting); sleep(1/inf_fich->time_units_per_second); } @@ -439,7 +547,7 @@ void racing(int arrayNumber){ } - waitForEnd(); + anouncingEnd(); } @@ -453,48 +561,49 @@ void *carThread(void* team_number){ #ifdef DEBUG //printf("I %ld created car %d.\n",(long)getpid(),number); #endif - //printf("i am waiting\n"); - //Have a condition variable racing(number); - pthread_exit(NULL); - - } //Signals the race manager that the race has started void raceStart(int signum){ - start_teams = 1; + + if(firstStart == 0 ){ + start_teams = 1; + firstStart = 1; + } + else{ + start_teams = 1; + pthread_cond_broadcast(&interruption_cond); + } + } -void interruptRaceTeam(){ - interrupting = 1; -} //Team manager. Will create the car threads void Team_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, struct semaphoreStruct *semaphore_listP, int channelP[2],int team_indexP, struct ids *idsP){ - - //Ignore all unwanted signals! sigfillset(&mask); sigprocmask(SIG_SETMASK, &mask, NULL); - sigemptyset(&new_mask); sigaddset(&new_mask, SIGUSR1); sigaddset(&new_mask, SIGUSR2); sigaddset(&new_mask, SIGTERM); + sigaddset(&new_mask, SIGALRM); + sigprocmask(SIG_UNBLOCK,&new_mask, NULL); signal(SIGUSR2, teamEnd); - signal(SIGUSR1, interruptRaceTeam); + signal(SIGUSR1, pauseRaceTeams); signal(SIGTERM, raceStart); + signal(SIGALRM, everyoneGaveUp); sem_unlink("BOX_MUTEX"); @@ -507,6 +616,8 @@ void Team_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, counter_mutex = sem_open("END COUNTER", O_CREAT|O_EXCL,0700,1); sem_unlink("WAIT BOX"); wait_box = sem_open("WAIT BOX", O_CREAT|O_EXCL,0700,0); + sem_unlink("INTERRUPT MUTEX"); + interrupt_race_mutex = sem_open("INTERRUPT MUTEX", O_CREAT|O_EXCL,0700,0); #ifdef DEBUG printf("Team Manager created (%ld)\n", (long)getpid()); @@ -525,7 +636,6 @@ void Team_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, pause(); } - sigdelset(&new_mask, SIGTERM); sigprocmask(SIG_BLOCK,&new_mask, NULL); @@ -577,42 +687,32 @@ void Team_Manager(struct config_fich_struct *inf_fichP, struct team *team_listP, if(team_list[team_index].cars[car_index].has_breakdown == 1){ //Sleep for a random amount of time - sleep((rand() % (inf_fich->T_Box_Min - inf_fich->T_Box_Max + 1)) + inf_fich->T_Box_Min); + printf("Entrei aqui\n"); + //sleep(((rand() % (inf_fich->T_Box_Min - inf_fich->T_Box_Max + 1)+ inf_fich->T_Box_Min) * 1/inf_fich->time_units_per_second)); + printf("ehhehehe\n"); team_list[team_index].cars[car_index].has_breakdown = 0; team_list[team_index].cars[car_index].amount_breakdown++; + printf("epowierpoweirpow\n"); } //refill - sleep(2); + printf("Entrei aqui2\n"); + sleep(2 * 1/inf_fich->time_units_per_second); team_list[team_index].cars[car_index].times_refill++; #ifdef DEBUG printf("(%d) Left box.\n", team_list[team_index].cars[car_index].car_number); #endif is_reserved = 0; - strcpy(team_list[team_index].box_state, "LIVRE"); sem_post(wait_box); - sigprocmask(SIG_UNBLOCK,&new_mask, &mask); - } + sem_wait(update_waiting); + strcpy(team_list[team_index].box_state, "LIVRE"); + sem_post(update_waiting); - /* - //Waits for all the cars to die - for(int j=0; j TEAM: A, CAR: 1, SPEED: 120, CONSUMPTION: 0.10, RELIABILITY: 89 -23:49:24 NEW CAR LOADED => TEAM: B, CAR: 1, SPEED: 90, CONSUMPTION: 2.00, RELIABILITY: 80 -23:49:24 NEW CAR LOADED => TEAM: C, CAR: 1, SPEED: 200, CONSUMPTION: 0.10, RELIABILITY: 70 -23:49:24 NEW CAR LOADED => TEAM: A, CAR: 2, SPEED: 122, CONSUMPTION: 0.30, RELIABILITY: 99 -23:49:24 NEW CAR LOADED => TEAM: B, CAR: 2, SPEED: 95, CONSUMPTION: 0.70, RELIABILITY: 80 -23:49:24 NEW CAR LOADED => TEAM: C, CAR: 2, SPEED: 300, CONSUMPTION: 1.00, RELIABILITY: 90 -23:49:24 NEW CAR LOADED => TEAM: A, CAR: 3, SPEED: 100, CONSUMPTION: 2.10, RELIABILITY: 60 -23:49:24 NEW CAR LOADED => TEAM: B, CAR: 3, SPEED: 90, CONSUMPTION: 3.20, RELIABILITY: 80 -23:49:24 NEW CAR LOADED => TEAM: C, CAR: 3, SPEED: 200, CONSUMPTION: 1.00, RELIABILITY: 70 -23:49:24 NEW CAR LOADED => TEAM: A, CAR: 4, SPEED: 80, CONSUMPTION: 2.10, RELIABILITY: 80 -23:49:24 NEW CAR LOADED => TEAM: B, CAR: 4, SPEED: 190, CONSUMPTION: 3.20, RELIABILITY: 90 -23:49:25 NEW CAR LOADED => TEAM: C, CAR: 4, SPEED: 100, CONSUMPTION: 1.00, RELIABILITY: 50 -23:49:29 NEW PROBLEM IN CAR 4 -23:49:29 SIGNAL SIGINT RECEIVED +01:18:35 SIMULATOR STARTING +01:18:39 NEW CAR LOADED => TEAM: A, CAR: 01, SPEED: 120, CONSUMPTION: 0.10, RELIABILITY: 100 +01:18:39 NEW CAR LOADED => TEAM: B, CAR: 01, SPEED: 100, CONSUMPTION: 0.20, RELIABILITY: 50 +01:18:39 NEW CAR LOADED => TEAM: C, CAR: 01, SPEED: 200, CONSUMPTION: 0.30, RELIABILITY: 70 +01:18:39 NEW CAR LOADED => TEAM: A, CAR: 02, SPEED: 122, CONSUMPTION: 1.00, RELIABILITY: 90 +01:18:39 NEW CAR LOADED => TEAM: B, CAR: 02, SPEED: 95, CONSUMPTION: 0.30, RELIABILITY: 10 +01:18:39 NEW CAR LOADED => TEAM: C, CAR: 02, SPEED: 300, CONSUMPTION: 0.10, RELIABILITY: 100 +01:18:39 NEW CAR LOADED => TEAM: A, CAR: 03, SPEED: 100, CONSUMPTION: 2.00, RELIABILITY: 100 +01:18:39 NEW CAR LOADED => TEAM: B, CAR: 03, SPEED: 90, CONSUMPTION: 0.50, RELIABILITY: 80 +01:18:39 NEW CAR LOADED => TEAM: C, CAR: 03, SPEED: 200, CONSUMPTION: 0.10, RELIABILITY: 90 +01:18:39 NEW CAR LOADED => TEAM: A, CAR: 04, SPEED: 100, CONSUMPTION: 0.80, RELIABILITY: 50 +01:18:39 NEW CAR LOADED => TEAM: B, CAR: 04, SPEED: 190, CONSUMPTION: 1.50, RELIABILITY: 70 +01:18:40 NEW CAR LOADED => TEAM: C, CAR: 04, SPEED: 100, CONSUMPTION: 1.20, RELIABILITY: 80 +01:19:12 NEW PROBLEM IN CAR 02 +01:19:12 NEW PROBLEM IN CAR 04 +01:19:12 NEW PROBLEM IN CAR 02 +01:19:12 NEW PROBLEM IN CAR 04 +01:19:12 CAR 02 IS NOW IN STATE: SEGURANCA +01:19:12 CAR 04 IS NOW IN STATE: SEGURANCA +01:19:12 CAR 04 IS NOW IN STATE: SEGURANCA +01:19:12 CAR 02 IS NOW IN STATE: SEGURANCA +01:19:14 CAR 03 IS NOW IN STATE: SEGURANCA +01:19:15 NEW PROBLEM IN CAR 01 +01:19:15 NEW PROBLEM IN CAR 01 +01:19:15 NEW PROBLEM IN CAR 03 +01:19:15 CAR 01 IS NOW IN STATE: SEGURANCA +01:19:15 CAR 01 IS NOW IN STATE: SEGURANCA +01:19:15 CAR 03 IS NOW IN STATE: SEGURANCA +01:19:15 CAR 02 IS NOW IN STATE: BOX +01:19:17 CAR 02 IS NOW IN STATE: CORRIDA +01:19:18 SIMULATOR PAUSING +01:19:18 WAITING FOR ALL CARS TO PAUSE +01:19:31 ALL CARS PAUSED +01:19:40 CAR 04 IS NOW IN STATE: BOX +01:19:40 CAR 02 IS NOW IN STATE: BOX +01:19:40 CAR 04 IS NOW IN STATE: BOX +01:19:40 RACE UNPAUSED +01:19:42 CAR 04 IS NOW IN STATE: CORRIDA +01:19:42 CAR 04 IS NOW IN STATE: CORRIDA +01:19:42 CAR 02 IS NOW IN STATE: CORRIDA +01:19:43 NEW PROBLEM IN CAR 04 +01:19:43 NEW PROBLEM IN CAR 02 +01:19:43 NEW PROBLEM IN CAR 04 +01:19:43 CAR 04 IS NOW IN STATE: SEGURANCA +01:19:43 CAR 04 IS NOW IN STATE: SEGURANCA +01:19:43 CAR 02 IS NOW IN STATE: SEGURANCA +01:19:45 CAR 02 IS NOW IN STATE: TERMINADO +01:19:50 CAR 03 IS NOW IN STATE: DESISTENCIA diff --git a/test b/test index e800909..7b88fc0 100755 Binary files a/test and b/test differ diff --git a/test.c b/test.c index 8ac3d35..a2dab11 100644 --- a/test.c +++ b/test.c @@ -4,174 +4,70 @@ #include #include #include -#include + #include #include +#include +#include #include #include -#include -#include -#include -#include #include -#include + int i = 0; -sem_t *sem; +sem_t *car_in_box; pthread_mutex_t mutex_cond = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t variavel_cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex_interruption = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t interruption_cond = PTHREAD_COND_INITIALIZER; pthread_t *cars; -/*pthread_mutex_t mutex_cond = PTHREAD_MUTEX_INITIALIZER; -pthread_cond_t variavel_cond = PTHREAD_COND_INITIALIZER; -sem_t *comunicate; -sem_t *reserva;*/ -/* -void carEnd(){ - printf("Car from team %ld killed.\n",(long)getpid()); - pthread_exit(NULL); -} -void teamEnd(int signum){ - for(int j=0; j<3; j++){ +sem_t *semaphore; - pthread_join(cars[j],NULL); - } - printf("Team %ld killed\n",(long)getpid()); - exit(0); -} - -void *carThread(void* team_number){ - signal(SIGINT, carEnd); - sleep(10); - pthread_exit(NULL); - return NULL; - -} -*/ -//void *thread(void *cancro){ - -/* while(1){ +void child2(){ + sem_unlink("BOX_MUTEX"); + car_in_box = sem_open("BOX_MUTEX", O_CREAT|O_EXCL,0700,2); sleep(1); - sem_post(comunicate); - pthread_cond_wait(&variavel_cond, &mutex_cond); - - printf("It worked :O\n"); - } -}*/ -/* -void weee(int signum){ - printf("I received it clear and loud\n"); -}*/ - -void *thread(void *yeet){ - printf("brah"); - pthread_exit(NULL); -} - -void interrupt(int singum){ - printf("wow\n"); -} - -void func(){ - signal(SIGINT,SIG_IGN); - printf("vou dormir zzz\n"); - sleep(5); - sem_post(sem); - printf("bazei"); -} - -void sigint(int signum){ - - i = 0; - while (i < 5){ - printf("yoooooooo\n"); - sleep(1); - i++; - } + sem_wait(car_in_box); + printf("2 - I am out :)"); } -int main(){ -/* signal(SIGINT, teamEnd); - - int workerId[3]; - - pthread_t cars[3]; - - //Create the car threads - for(int i=0; i<3;i++){ - workerId[i] = i+1; - pthread_create(&cars[i], NULL, carThread,&workerId[i]); - }*/ - //Waits for all the cars to die - - - - /*signal(SIGINT, weee); - int workerId = 0; - - pthread_t cars[1]; - - pthread_create(&cars[0], NULL, thread,&workerId); +void child1(){ - pause(); - for(int j=0; j<1; j++){ - pthread_join(cars[j],NULL); - }*/ -/* - sem_unlink("MUTEX"); - comunicate = sem_open("MUTEX", O_CREAT|O_EXCL,0700,0); - sem_unlink("ATUATIA"); - reserva= sem_open("A TUA TIA", O_CREAT|O_EXCL,0700,0); + sem_unlink("BOX_MUTEX"); + car_in_box = sem_open("BOX_MUTEX", O_CREAT|O_EXCL,0700,2); - int workerId = 0; - pthread_t cars[1]; - pthread_create(&cars[0], NULL, thread,&workerId); + sem_wait(car_in_box); + printf("1-waiting for 2 seconds\n"); + sleep(2); - while(1){ + sem_wait(car_in_box); - sem_wait(reserva); - printf("está reservado!"); - sem_wait(comunicate); - printf("yoooo\n"); - sleep(1); - pthread_cond_signal(&variavel_cond); +} - } - */ - sigset_t mask, new_mask; - //Ignore all unwanted signals! - sigfillset(&mask); - sigprocmask(SIG_SETMASK, &mask, NULL); - - //Create the wanted signals - sigemptyset(&new_mask); - sigaddset(&new_mask,SIGINT); - - sigprocmask(SIG_UNBLOCK,&new_mask, NULL); - signal(SIGINT, sigint); - - sigprocmask(SIG_BLOCK,&new_mask, NULL); - printf("tou dormindo blockeado\n"); - sleep(5); - - sigprocmask(SIG_UNBLOCK, &new_mask, NULL); - - printf("tou dormindo nao blockeado\n"); - sleep(5); - - - printf("3etipoji jio \n"); - +int main(){ + if(fork() == 0){ + child1(); + printf("1-I am leaving\n"); + exit(0); + } + if(fork() == 0){ + child2(); + printf("2-I am leaving\n"); + exit(0); + } + + for(int i = 0; i < 2 ; i++){ + wait(NULL); + } }