Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.88 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include "header.h"
  6.  
  7.  
  8.  
  9. //timelog function
  10.  
  11. char *timelog() {
  12. t = time(NULL);
  13. Time = *localtime(&t);
  14.  
  15. snprintf(timenow, sizeof(timenow), "\n%d:%d:%d ",Time.tm_hour, Time.tm_min, Time.tm_sec);
  16. return timenow;
  17. }
  18.  
  19.  
  20.  
  21. //code - init time - takeoff time
  22.  
  23. int create_departure(char *c, int i, int t) {
  24.  
  25.  
  26.  
  27. Lista_departure novo;
  28.  
  29. novo = (Lista_departure) malloc(sizeof(node_struct_departure));
  30.  
  31. novo->departure = malloc(sizeof(departure_struct));
  32.  
  33.  
  34.  
  35. if(novo!=NULL){
  36.  
  37. strcpy(novo->departure->flight_code, c);
  38.  
  39. novo->departure->initial_time = i;
  40.  
  41. novo->departure->takeoff_time = t;
  42.  
  43. }
  44.  
  45.  
  46.  
  47. sem_wait(&semafores->insert_queue);
  48.  
  49. departures = insert_departure(departures,novo);
  50.  
  51. sem_post(&semafores->insert_queue);
  52.  
  53.  
  54.  
  55. write_log_file("Departure introduzida na fila com sucesso!");
  56.  
  57. return 0;
  58.  
  59. }
  60.  
  61.  
  62.  
  63. //code - init time - time to runway - init fuel
  64.  
  65. int create_arrival(char *c, int i, int t, int f) {
  66.  
  67.  
  68.  
  69. Lista_arrival novo;
  70.  
  71. novo = (Lista_arrival) malloc(sizeof(node_struct_arrival));
  72.  
  73. novo->arrival = malloc(sizeof(arrival_struct));
  74.  
  75.  
  76.  
  77. if(novo!=NULL){
  78.  
  79. strcpy(novo->arrival->flight_code, c);
  80.  
  81. novo->arrival->initial_time = i;
  82.  
  83. novo->arrival->time_runway = t;
  84.  
  85. novo->arrival->initial_fuel = f;
  86.  
  87. }
  88.  
  89. sem_wait(&semafores->insert_queue);
  90.  
  91. arrivals = insert_arrival(arrivals, novo);
  92.  
  93. sem_post(&semafores->insert_queue);
  94.  
  95.  
  96.  
  97. write_log_file("Arrival introduzida na fila com sucesso!");
  98.  
  99. return 0;
  100.  
  101. }
  102.  
  103.  
  104.  
  105. Lista_departure insert_departure(Lista_departure departures, Lista_departure novo){
  106.  
  107. Lista_departure ant, act;
  108.  
  109.  
  110.  
  111. if(departures == NULL){
  112.  
  113. departures = novo;
  114.  
  115. novo->next = NULL;
  116.  
  117. return departures;
  118.  
  119. }
  120.  
  121.  
  122.  
  123. //(departures->departure->initial_time)>=(novo->departure->initial_time)
  124.  
  125. act = departures->next;
  126.  
  127. ant = departures;
  128.  
  129.  
  130.  
  131. while((act->next!=NULL) && (act->departure->initial_time)<=(novo->departure->initial_time)){
  132.  
  133. ant = ant->next;
  134.  
  135. act = act->next;
  136.  
  137. }
  138.  
  139.  
  140.  
  141. novo->next = act;
  142.  
  143. ant->next = novo;
  144.  
  145. return departures;
  146.  
  147. }
  148.  
  149.  
  150.  
  151. Lista_arrival insert_arrival(Lista_arrival arrivals, Lista_arrival novo){
  152.  
  153. Lista_arrival ant, act;
  154.  
  155.  
  156.  
  157. if(arrivals == NULL){
  158.  
  159. arrivals = novo;
  160.  
  161. novo->next = NULL;
  162.  
  163. return arrivals;
  164.  
  165. }
  166.  
  167.  
  168.  
  169. act = arrivals->next;
  170.  
  171. ant = arrivals;
  172.  
  173.  
  174.  
  175. while((act->next!=NULL) && (act->arrival->initial_time)<=(novo->arrival->initial_time)){
  176.  
  177. ant = ant->next;
  178.  
  179. act = act->next;
  180.  
  181. }
  182.  
  183.  
  184.  
  185. novo->next = act;
  186.  
  187. ant->next = novo;
  188.  
  189. return arrivals;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. Lista_departure remove_departure(){
  196.  
  197. Lista_departure no;
  198.  
  199. Lista_departure removido;
  200.  
  201.  
  202.  
  203. removido = (Lista_departure) malloc(sizeof(node_struct_departure));
  204.  
  205. removido->departure = malloc(sizeof(departure_struct));
  206.  
  207.  
  208.  
  209. if(departures == NULL){
  210.  
  211. printf("Fila vazia.");
  212.  
  213. }else if(departures->next == NULL){
  214.  
  215. strcpy(removido->departure->flight_code,departures->departure->flight_code);
  216.  
  217. removido->departure->initial_time = departures->departure->initial_time;
  218.  
  219. removido->departure->takeoff_time = departures->departure->takeoff_time;
  220.  
  221. free(departures);
  222.  
  223. departures = NULL;
  224.  
  225. }else{
  226.  
  227. no = departures->next;
  228.  
  229. strcpy(removido->departure->flight_code,departures->departure->flight_code);
  230.  
  231. removido->departure->initial_time = departures->departure->initial_time;
  232.  
  233. removido->departure->takeoff_time = departures->departure->takeoff_time;
  234.  
  235. free(departures);
  236.  
  237. departures=no;
  238.  
  239. }
  240.  
  241. return removido;
  242.  
  243. }
  244.  
  245.  
  246.  
  247. void remove_arrival(){
  248.  
  249. Lista_arrival no;
  250.  
  251.  
  252.  
  253. if(arrivals == NULL){
  254.  
  255. printf("Fila vazia.");
  256.  
  257. }else if(arrivals->next == NULL){
  258.  
  259. free(arrivals);
  260.  
  261. arrivals = NULL;
  262.  
  263. }else{
  264.  
  265. no = arrivals->next;
  266.  
  267. free(arrivals);
  268.  
  269. arrivals = no;
  270.  
  271. }
  272.  
  273. }
  274.  
  275.  
  276.  
  277. void create_pipe(){
  278.  
  279. unlink(PIPE_NAME);
  280.  
  281. if ((mkfifo(PIPE_NAME, O_CREAT|O_EXCL|0600)<0) && (errno!=EEXIST)){
  282.  
  283. perror("Cannot create pipe. ");
  284.  
  285. exit(0);
  286.  
  287. }
  288.  
  289. write_log_file("Pipe created.");
  290.  
  291. }
  292.  
  293.  
  294.  
  295.  
  296.  
  297. void *workerthread_d(){
  298.  
  299. Lista_departure no = remove_departure();
  300.  
  301. message_struct_d message_d;
  302.  
  303.  
  304.  
  305. message_d.mtype = 3;
  306.  
  307. message_d.takeoff_time = no->departure->takeoff_time;
  308.  
  309. message_d.slot = 0;
  310.  
  311.  
  312.  
  313. if(msgsnd(mq_id,&message_d,sizeof(message_struct_d)-sizeof(long),0)!=0){
  314.  
  315. perror("Error - msgsnd() of mq");
  316.  
  317. }else{
  318.  
  319. printf("Enviado para message queue\n");
  320.  
  321. }
  322.  
  323.  
  324.  
  325. printf("A iniciar\n");
  326.  
  327. usleep(1000*5000);
  328.  
  329. printf("A terminar...\n");
  330.  
  331. pthread_exit(NULL);
  332.  
  333. }
  334.  
  335.  
  336.  
  337. void *workerthread_a(){
  338.  
  339. Lista_arrival no = remove_arrival();
  340.  
  341. message_struct_a message_a;
  342.  
  343.  
  344.  
  345. if(no->arrival->initial_fuel > 4+no->arrival->time_runway+running_config->L)
  346.  
  347. message_a.mtype =1;
  348.  
  349. else
  350.  
  351. message_a.mtype = 2;
  352.  
  353. message_a.initial_fuel = no->departure->initial_fuel;
  354.  
  355. message_a.time_runway = no->departure->time_runway;
  356.  
  357.  
  358.  
  359. message_a.slot = 0;
  360.  
  361.  
  362.  
  363. if(msgsnd(mq_id,&message_a,sizeof(message_struct_a)-sizeof(long),0)!=0){
  364.  
  365. perror("Error - msgsnd() of mq");
  366.  
  367. }else{
  368.  
  369. printf("Enviado para message queue\n");
  370.  
  371. }
  372.  
  373.  
  374.  
  375. printf("A iniciar\n");
  376.  
  377. usleep(1000*5000);
  378.  
  379. printf("A terminar...\n");
  380.  
  381. pthread_exit(NULL);
  382.  
  383. }
  384.  
  385.  
  386.  
  387.  
  388.  
  389. void *time_counter(){
  390.  
  391. while(1){
  392.  
  393. if((departures != NULL) && (departures->departure->initial_time) == mem->t){
  394.  
  395. pthread_create(&id_thread,NULL,workerthread_d,NULL);
  396.  
  397. }
  398.  
  399. if((arrivals != NULL) && (arrivals->arrival->initial_time) == mem->t){
  400.  
  401. pthread_create(&id_thread,NULL,workerthread_d,NULL);
  402.  
  403. }
  404.  
  405.  
  406.  
  407. usleep(1000*running_config->ut);
  408.  
  409. sem_wait(&semafores->mem);
  410.  
  411. mem->t++;
  412.  
  413. sem_post(&semafores->mem);
  414.  
  415. printf("Tempo %d\n",mem->t);
  416.  
  417.  
  418.  
  419. }
  420.  
  421. }
  422.  
  423.  
  424.  
  425. int init_shm_mem(){
  426.  
  427. if((mem_shmid= shmget(IPC_PRIVATE, sizeof(mem_struct*), IPC_CREAT|0700))<0){
  428.  
  429. perror("Error - shmget() of mem struct");
  430.  
  431. return -1;
  432.  
  433. }
  434.  
  435. if((mem = (mem_struct*) shmat(mem_shmid, NULL, 0)) == (mem_struct*)-1){
  436.  
  437. perror("Error - shmat() of mem struct");
  438.  
  439. return -1;
  440.  
  441. }
  442.  
  443. mem->t = 0;
  444.  
  445. return 0;
  446.  
  447. }
  448.  
  449.  
  450.  
  451. int init_shm_stats(){
  452.  
  453. if((stats_shmid = shmget(IPC_PRIVATE, sizeof(stats_struct*), IPC_CREAT|0700))<0){
  454.  
  455. perror("Error - shmget() of statistics struct");
  456.  
  457. return -1;
  458.  
  459. }
  460.  
  461. if((stats = (stats_struct*) shmat(stats_shmid, NULL, 0)) == (stats_struct*)-1){
  462.  
  463. perror("Error - shmat() of statistics struct");
  464.  
  465. return -1;
  466.  
  467. }
  468.  
  469.  
  470.  
  471. stats->n_voos_criados=0;
  472.  
  473. stats->n_voos_aterrados=0;
  474.  
  475. stats->t_aterrar=0.0;
  476.  
  477. stats->n_voos_descolados=0;
  478.  
  479. stats->t_descolados=0.0;
  480.  
  481. stats->n_holding_aterragem=0.0;
  482.  
  483. stats->n_holding_urgencia=0.0;
  484.  
  485. stats->n_voos_redirecionados=0;
  486.  
  487. stats->voos_rejeitados=0;
  488.  
  489.  
  490.  
  491. return 0;
  492.  
  493. }
  494.  
  495.  
  496.  
  497. //Create departure queue
  498.  
  499. int create_queue(){
  500.  
  501. if((mq_id = msgget(IPC_PRIVATE, IPC_CREAT|0700)) < 0) {
  502.  
  503. perror("Error - msgget()");
  504.  
  505. return -1;
  506.  
  507. }
  508.  
  509. else {
  510.  
  511. write_log_file("Message Queue created.");
  512.  
  513. }
  514.  
  515. return 0;
  516.  
  517. }
  518.  
  519.  
  520.  
  521. void close_named_pipe(){
  522.  
  523. close(fd);
  524.  
  525. unlink(PIPE_NAME);
  526.  
  527. }
  528.  
  529.  
  530.  
  531. //Init queues
  532.  
  533. int init_queues() {
  534.  
  535. departures = NULL;
  536.  
  537. arrivals = NULL;
  538.  
  539.  
  540.  
  541. d_ord = NULL;
  542.  
  543. a_ord = NULL;
  544.  
  545.  
  546.  
  547. write_log_file("Queues ready.");
  548.  
  549. return 0;
  550.  
  551. }
  552.  
  553.  
  554.  
  555. //Ficheiro LOG
  556.  
  557. int init_log_file() {
  558.  
  559. printf("Creating \"log.txt\"...\n");
  560.  
  561.  
  562.  
  563. if((flog = fopen(LOG_FILE_NAME, "a+")) == NULL){
  564.  
  565. perror("Error - fopen(\"log.txt\", \"a\")");
  566.  
  567. return -1;
  568.  
  569. }
  570.  
  571. write_log_file("File \"log.txt\" created successfully.");
  572.  
  573.  
  574.  
  575. return 0;
  576.  
  577. }
  578.  
  579.  
  580.  
  581. // Inicializar semáforos em memória partilhada
  582.  
  583. // Todos os semáforos declaram-se na struct respectiva e inicializam-se aqui
  584.  
  585. int init_shm_semafores() {
  586.  
  587. if((sem_shmid = shmget(IPC_PRIVATE, sizeof(sem_struct*), IPC_CREAT|0700))<0){
  588.  
  589. perror("Error - shmget() of semafores struct");
  590.  
  591. return -1;
  592.  
  593. }
  594.  
  595.  
  596.  
  597. if((semafores = (sem_struct*) shmat(sem_shmid, NULL, 0)) == (sem_struct*)-1){
  598.  
  599. perror("Error - shmat() of semafores struct");
  600.  
  601. return -1;
  602.  
  603. }
  604.  
  605.  
  606.  
  607. sem_init(&semafores->write_log, 1, 1);
  608.  
  609. sem_init(&semafores->insert_queue, 1, 1);
  610.  
  611. sem_init(&semafores->remove_queue, 1, 1);
  612.  
  613. sem_init(&semafores->mem,1,1);
  614.  
  615.  
  616.  
  617. return 0;
  618.  
  619. }
  620.  
  621.  
  622.  
  623.  
  624.  
  625. // Inicializar estutura de configuração de acordo com o ficheiro respectivo
  626.  
  627. int read_config(){
  628.  
  629.  
  630.  
  631. FILE *fp;
  632.  
  633. if(!(fp = fopen("config.txt","r"))) {
  634.  
  635. printf("Erro ao abrir o ficheiro config.txt!\n");
  636.  
  637. exit(0);
  638.  
  639. }
  640.  
  641.  
  642.  
  643. running_config = (config_struct*) malloc(sizeof(config_struct));
  644.  
  645.  
  646.  
  647. fscanf(fp,"%d",&running_config->ut);//unidade de tempo
  648.  
  649. fscanf(fp,"%d, %d",&running_config->T,&running_config->dt);//duracao descolagem, intervalo entre descolagem
  650.  
  651. fscanf(fp,"%d, %d",&running_config->L,&running_config->dl);//duracao aterragem, intervalo entre aterragens
  652.  
  653. fscanf(fp,"%d, %d",&running_config->Min,&running_config->Max); //holding duracao minima, holding duracao maxima
  654.  
  655. fscanf(fp,"%d",&running_config->D); //quantidade maxima partidas
  656.  
  657. fscanf(fp,"%d",&running_config->A); //quantidade maxima chegadas
  658.  
  659.  
  660.  
  661. //printf("%d\n",running_config->ut);
  662.  
  663. //printf("%d, %d\n",running_config->T,running_config->dt);
  664.  
  665. //printf("%d, %d\n",running_config->L,running_config->dl);
  666.  
  667. //printf("%d, %d\n",running_config->Min,running_config->Max);
  668.  
  669. //printf("%d\n",running_config->D);
  670.  
  671. //printf("%d\n",running_config->A);
  672.  
  673.  
  674.  
  675. write_log_file("Configuration file \"config.txt\" successfully loaded.");
  676.  
  677.  
  678.  
  679. return 0;
  680.  
  681. }
  682.  
  683.  
  684.  
  685. void delay(int number_of_seconds){
  686.  
  687. int milli_seconds = 1000 * number_of_seconds;
  688.  
  689.  
  690.  
  691. clock_t start_time = clock();
  692.  
  693. while(clock() < start_time + milli_seconds)
  694.  
  695. ;
  696.  
  697. }
  698.  
  699.  
  700.  
  701. void le_pipe(){
  702.  
  703. if((fd = open(PIPE_NAME, O_RDWR)) < 0) {
  704.  
  705. perror("Cannot open pipe for reading:\n");
  706.  
  707. exit(0);
  708.  
  709. }
  710.  
  711. else{
  712.  
  713. write_log_file("Open pipe for reading.");
  714.  
  715. }
  716.  
  717.  
  718.  
  719. char aux[BUFFER_SIZE];
  720.  
  721. char str[BUFFER_SIZE];
  722.  
  723. char all_str[50];
  724.  
  725. char flight_code[50];
  726.  
  727. int initial_time,takeoff_time, eta, fuel;
  728.  
  729.  
  730.  
  731. while(1){
  732.  
  733.  
  734.  
  735. read(fd, str, BUFFER_SIZE);
  736.  
  737. strcpy(all_str, str);
  738.  
  739. token = strtok(str," ");
  740.  
  741. if(strcmp(token,"DEPARTURE")==0){
  742.  
  743. token = strtok(NULL," ");
  744.  
  745. if(verifica_codigo(token)==0){
  746.  
  747. sprintf(aux, "WRONG COMMAND => %s", all_str);
  748.  
  749. write_log_file(aux);
  750.  
  751. strncpy(aux, "", sizeof(aux));
  752.  
  753. continue;
  754.  
  755. }
  756.  
  757. strcpy(flight_code, token);
  758.  
  759.  
  760.  
  761. token = strtok(NULL," ");
  762.  
  763. token = strtok(NULL," ");
  764.  
  765. if(verifica_inteiro(token)==0){
  766.  
  767. sprintf(aux, "WRONG COMMAND => %s", all_str);
  768.  
  769. write_log_file(aux);
  770.  
  771. strncpy(aux, "", sizeof(aux));
  772.  
  773. continue;
  774.  
  775. }
  776.  
  777. initial_time = atoi(token);
  778.  
  779.  
  780.  
  781. token = strtok(NULL," ");
  782.  
  783. token = strtok(NULL," ");
  784.  
  785. if(verifica_inteiro(token)==0){
  786.  
  787. sprintf(aux, "WRONG COMMAND => %s", all_str);
  788.  
  789. write_log_file(aux);
  790.  
  791. strncpy(aux, "", sizeof(aux));
  792.  
  793. continue;
  794.  
  795. }
  796.  
  797. takeoff_time = atoi(token);
  798.  
  799. sprintf(aux, "NEW COMMAND => %s", all_str);
  800.  
  801.  
  802.  
  803. write_log_file(aux);
  804.  
  805. strncpy(aux, "", sizeof(aux));
  806.  
  807.  
  808.  
  809. create_departure(flight_code, initial_time, takeoff_time);
  810.  
  811.  
  812.  
  813. }
  814.  
  815.  
  816.  
  817. else if(strcmp(token,"ARRIVAL")==0){
  818.  
  819. token = strtok(NULL," ");
  820.  
  821. if(verifica_codigo(token)==0){
  822.  
  823. sprintf(aux, "WRONG COMMAND => %s", all_str);
  824.  
  825. write_log_file(aux);
  826.  
  827. strncpy(aux, "", sizeof(aux));
  828.  
  829. continue;
  830.  
  831. }
  832.  
  833. strcpy(flight_code, token);
  834.  
  835.  
  836.  
  837. token = strtok(NULL," ");
  838.  
  839. token = strtok(NULL," ");
  840.  
  841. if(verifica_inteiro(token)==0){
  842.  
  843. sprintf(aux, "WRONG COMMAND => %s", all_str);
  844.  
  845. write_log_file(aux);
  846.  
  847. strncpy(aux, "", sizeof(aux));
  848.  
  849. continue;
  850.  
  851. }
  852.  
  853. initial_time = atoi(token);
  854.  
  855.  
  856.  
  857. token = strtok(NULL," ");
  858.  
  859. token = strtok(NULL," ");
  860.  
  861. if(verifica_inteiro(token)==0){
  862.  
  863. sprintf(aux, "WRONG COMMAND => %s", all_str);
  864.  
  865. write_log_file(aux);
  866.  
  867. strncpy(aux, "", sizeof(aux));
  868.  
  869. continue;
  870.  
  871. }
  872.  
  873. eta = atoi(token);
  874.  
  875.  
  876.  
  877. token = strtok(NULL," ");
  878.  
  879. token = strtok(NULL," ");
  880.  
  881. if(verifica_inteiro(token)==0){
  882.  
  883. sprintf(aux, "WRONG COMMAND => %s", all_str);
  884.  
  885. write_log_file(aux);
  886.  
  887. strncpy(aux, "", sizeof(aux));
  888.  
  889. continue;
  890.  
  891. }
  892.  
  893. fuel = atoi(token); //FUEL <eta nao da wrong command
  894.  
  895. if(fuel<eta){
  896.  
  897. sprintf(aux, "WRONG COMMAND => %s", all_str);
  898.  
  899. write_log_file(aux);
  900.  
  901. strncpy(aux, "", sizeof(aux));
  902.  
  903. continue;
  904.  
  905. }
  906.  
  907.  
  908.  
  909. sprintf(aux, "NEW COMMAND => %s", all_str);
  910.  
  911. write_log_file(aux);
  912.  
  913. strncpy(aux, "", sizeof(aux));
  914.  
  915.  
  916.  
  917. create_arrival(flight_code, initial_time, eta,fuel);
  918.  
  919.  
  920.  
  921. printf("Flitght Code: %s \n", flight_code);
  922.  
  923. printf("Initial Time: %d\n", initial_time);
  924.  
  925. printf("Time to runway: %d\n", eta);
  926.  
  927. printf("Initial Fuel: %d\n", fuel);
  928.  
  929. }
  930.  
  931.  
  932.  
  933. else{
  934.  
  935. sprintf(aux, "WRONG COMMAND => %s", all_str);
  936.  
  937. write_log_file(aux);
  938.  
  939. strncpy(aux, "", sizeof(aux));
  940.  
  941. }
  942.  
  943. }
  944.  
  945. }
  946.  
  947.  
  948.  
  949. int verifica_codigo(char *token1){
  950.  
  951. char token[50];
  952.  
  953. strcpy(token, token1);
  954.  
  955.  
  956.  
  957. if(token[0]=='T' && token[1]=='P'){
  958.  
  959. for(int i=2; i<strlen(token);i++){
  960.  
  961. if(!(isdigit(token[i])==0))
  962.  
  963. return 1;
  964.  
  965. }
  966.  
  967. }
  968.  
  969. return 0;
  970.  
  971. }
  972.  
  973.  
  974.  
  975. int verifica_inteiro(char* token1){
  976.  
  977. char token[50];
  978.  
  979. strcpy(token, token1);
  980.  
  981.  
  982.  
  983. for(int i=0; i<strlen(token);i++){
  984.  
  985. if(isdigit(token[i])==0)
  986.  
  987. return 0;
  988.  
  989. }
  990.  
  991. return 1;
  992.  
  993. }
  994.  
  995.  
  996.  
  997. void init(){
  998.  
  999. init_shm_semafores();
  1000.  
  1001. init_log_file();
  1002.  
  1003. read_config();
  1004.  
  1005. init_shm_stats();
  1006.  
  1007. init_shm_mem();
  1008.  
  1009. create_pipe();
  1010.  
  1011. create_queue();
  1012.  
  1013. init_queues();
  1014.  
  1015.  
  1016.  
  1017. write_log_file("PROGRAM STARTED.");
  1018.  
  1019. fflush(stdout);
  1020.  
  1021. }
  1022.  
  1023.  
  1024.  
  1025. void stats_shm_shutdown(){
  1026.  
  1027. if (shmdt(stats)==-1){ // detaching da memoria partilhada
  1028.  
  1029. perror("Error at detaching");
  1030.  
  1031. }
  1032.  
  1033. if (shmctl(stats_shmid, IPC_RMID, NULL)==-1) { //limpar memoria partilhada
  1034.  
  1035. perror("Error unmapping shared memory\n");
  1036.  
  1037. }
  1038.  
  1039. write_log_file("Shared memory for statistics cleaned");
  1040.  
  1041. }
  1042.  
  1043.  
  1044.  
  1045. void mq_shutdown(){
  1046.  
  1047. if(msgctl(mq_id, IPC_RMID, NULL)==-1) {
  1048.  
  1049. perror("Error destroying message queue\n");
  1050.  
  1051. }
  1052.  
  1053. write_log_file("Message queue for departure cleaned");
  1054.  
  1055. }
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061. void mem_shm_shutdown(){
  1062.  
  1063. if (shmdt(mem)==-1){ // detaching da memoria partilhada
  1064.  
  1065. perror("Error at detaching");
  1066.  
  1067. }
  1068.  
  1069. if (shmctl(mem_shmid, IPC_RMID, NULL)==-1) { //limpar memoria partilhada
  1070.  
  1071. perror("Error unmapping shared memory\n");
  1072.  
  1073. }
  1074.  
  1075. write_log_file("Shared memory for mem cleaned");
  1076.  
  1077. }
  1078.  
  1079.  
  1080.  
  1081. void mutexes_shutdown(){
  1082.  
  1083. sem_destroy(&semafores->write_log);
  1084.  
  1085. sem_destroy(&semafores->insert_queue);
  1086.  
  1087. sem_destroy(&semafores->remove_queue);
  1088.  
  1089. sem_destroy(&semafores->mem);
  1090.  
  1091.  
  1092.  
  1093. write_log_file("Semafores cleaned");
  1094.  
  1095. }
  1096.  
  1097.  
  1098.  
  1099. void show_stats(){
  1100.  
  1101. //Escrever estatisticas - sigusr1
  1102.  
  1103. printf("\n\n-----|| ESTATISTICAS ||-----\n");
  1104.  
  1105. printf("Numero total de voos criados - %d\n", stats->n_voos_criados);
  1106.  
  1107. printf("Numero total de voos que aterraram - %d\n", stats->n_voos_aterrados);
  1108.  
  1109. printf("Tempo medio de espera para aterrar - %f\n", stats->t_aterrar);
  1110.  
  1111. printf("Numero total de voos que descolaram - %d\n", stats->n_voos_descolados);
  1112.  
  1113. printf("Tempo medio de espera para descolar - %f\n", stats->t_descolados);
  1114.  
  1115. printf("Numero medio de manobras de holding por voo de aterragem - %f\n", stats->n_holding_aterragem);
  1116.  
  1117. printf("Numero medio de manobras de holding por voo em estado de urgencia - %f\n", stats->n_holding_urgencia);
  1118.  
  1119. printf("Numero de voos redirecionados para outro aeroporto - %d\n", stats->n_voos_redirecionados);
  1120.  
  1121. printf("Voos rejeitados pela Torre de Controlo - %d\n", stats->voos_rejeitados);
  1122.  
  1123. }
  1124.  
  1125.  
  1126.  
  1127. void shutdown(int sig){
  1128.  
  1129. stats_shm_shutdown();
  1130.  
  1131. mem_shm_shutdown();
  1132.  
  1133. mq_shutdown();
  1134.  
  1135. close_named_pipe();
  1136.  
  1137.  
  1138.  
  1139. free(running_config);
  1140.  
  1141. fclose(flog);
  1142.  
  1143.  
  1144.  
  1145. mutexes_shutdown();
  1146.  
  1147. write_log_file("PROGRAM ENDED.");
  1148.  
  1149.  
  1150.  
  1151. printf("\n\n");
  1152.  
  1153. exit(0);
  1154.  
  1155. }
  1156.  
  1157.  
  1158.  
  1159. void write_log_file(char* str){
  1160.  
  1161. printf("%s%s",timelog(), str);
  1162.  
  1163. fflush(stdout);
  1164.  
  1165.  
  1166.  
  1167. sem_wait(&semafores->write_log);
  1168.  
  1169. fprintf(flog,"%s%s",timelog(), str);
  1170.  
  1171. fflush(flog);
  1172.  
  1173. sem_post(&semafores->write_log);
  1174.  
  1175. }
  1176.  
  1177.  
  1178.  
  1179. Departures_ord insert_departure_ord(message_struct_d departure){
  1180.  
  1181. Departures_ord novo;
  1182.  
  1183. novo = (Departures_ord) malloc(sizeof(node_struct_departure_ord));
  1184.  
  1185. novo->departure = malloc(sizeof(message_struct_d));
  1186.  
  1187.  
  1188.  
  1189. novo->departure->takeoff_time = departure.takeoff_time;
  1190.  
  1191.  
  1192.  
  1193. Departures_ord ant, act;
  1194.  
  1195.  
  1196.  
  1197. if(d_ord == NULL){
  1198.  
  1199. d_ord = novo;
  1200.  
  1201. novo->next = NULL;
  1202.  
  1203. return d_ord;
  1204.  
  1205. }
  1206.  
  1207.  
  1208.  
  1209. act = d_ord->next;
  1210.  
  1211. ant = d_ord;
  1212.  
  1213.  
  1214.  
  1215. while((act->next!=NULL) && (act->departure->takeoff_time)<=(novo->departure->takeoff_time)){
  1216.  
  1217. ant = ant->next;
  1218.  
  1219. act = act->next;
  1220.  
  1221. }
  1222.  
  1223.  
  1224.  
  1225. novo->next = act;
  1226.  
  1227. ant->next = novo;
  1228.  
  1229. return d_ord;
  1230.  
  1231. }
  1232.  
  1233.  
  1234.  
  1235. Arrivals_ord insert_arrival_ord(message_struct_a *arrival){
  1236.  
  1237. Arrivals_ord novo;
  1238.  
  1239. novo = (Arrivals_ord) malloc(sizeof(node_struct_arrival_ord));
  1240.  
  1241. novo->arrival = malloc(sizeof(message_struct_a));
  1242.  
  1243.  
  1244.  
  1245. novo->arrival = arrival;
  1246.  
  1247. Arrivals_ord ant, act;
  1248.  
  1249.  
  1250.  
  1251. if(a_ord == NULL){
  1252.  
  1253. a_ord = novo;
  1254.  
  1255. novo->next = NULL;
  1256.  
  1257. return a_ord;
  1258.  
  1259. }
  1260.  
  1261.  
  1262.  
  1263. while((act->next!=NULL) && (act->arrival->time_runway)<=(novo->arrival->time_runway)){
  1264.  
  1265. ant = ant->next;
  1266.  
  1267. act = act->next;
  1268.  
  1269. }
  1270.  
  1271.  
  1272.  
  1273. novo->next = act;
  1274.  
  1275. ant->next = novo;
  1276.  
  1277.  
  1278.  
  1279. return a_ord;
  1280.  
  1281. }
  1282.  
  1283.  
  1284.  
  1285. void *waiting_message_d(){
  1286.  
  1287. message_struct_d message_d;
  1288.  
  1289.  
  1290.  
  1291. while(1){
  1292.  
  1293. if(msgrcv(mq_id, &message_d, sizeof(message_struct_d)-sizeof(long), 3, 0) >0){
  1294.  
  1295.  
  1296.  
  1297. printf("ahahhhhhhhh %d\n",message_d.takeoff_time);
  1298.  
  1299. d_ord = insert_departure_ord(message_d);
  1300.  
  1301. }
  1302.  
  1303. }
  1304.  
  1305. }
  1306.  
  1307.  
  1308.  
  1309. void *waiting_message_a(){
  1310.  
  1311. message_a = (message_struct_a*) malloc(sizeof(message_struct_a));
  1312.  
  1313. while(1){
  1314.  
  1315. msgrcv(mq_id, &message_a, sizeof(message_a), -1, IPC_NOWAIT);
  1316.  
  1317. a_ord = insert_arrival_ord(message_a);
  1318.  
  1319. }
  1320.  
  1321. }
  1322.  
  1323.  
  1324.  
  1325. void torre_de_controlo(){
  1326.  
  1327. char res[100];
  1328.  
  1329. sprintf(res,"[%d] Torre de controlo ativa.",getpid());
  1330.  
  1331. write_log_file(res);
  1332.  
  1333.  
  1334.  
  1335. pthread_create(&thread_receive_d,NULL,waiting_message_d,NULL);
  1336.  
  1337. pthread_create(&thread_receive_a,NULL,waiting_message_a,NULL);
  1338.  
  1339. pthread_join(thread_receive_d,NULL);
  1340.  
  1341. pthread_join(thread_receive_a,NULL);
  1342.  
  1343.  
  1344.  
  1345. /*if(sizeof(mem->threads_departures)>running_config ->D){
  1346.  
  1347. sem_wait(&semafores->stats);
  1348.  
  1349. stats->voos_rejeitados++;
  1350.  
  1351. sem_post(&semafores->stats);
  1352.  
  1353. //terminar thread
  1354.  
  1355. write_log_file("Voo rejeitado. Numero maximo atingido.");
  1356.  
  1357. }*/
  1358.  
  1359. }
  1360.  
  1361.  
  1362.  
  1363. int main(){
  1364.  
  1365. signal(SIGINT, shutdown);
  1366.  
  1367. signal(SIGUSR1, show_stats);
  1368.  
  1369. init();
  1370.  
  1371.  
  1372.  
  1373. pid_t id = fork();
  1374.  
  1375. if(id == 0){
  1376.  
  1377. torre_de_controlo();
  1378.  
  1379. exit(0);
  1380.  
  1381. }
  1382.  
  1383. else if(id == -1){
  1384.  
  1385. write_log_file("Erro na criacao da torre de controlo.");
  1386.  
  1387. exit(0);
  1388.  
  1389. }
  1390.  
  1391. else{
  1392.  
  1393. pthread_create(&thread_time,NULL,time_counter,&id);
  1394.  
  1395. le_pipe();
  1396.  
  1397. exit(0);
  1398.  
  1399. }
  1400.  
  1401.  
  1402.  
  1403. return 0;
  1404.  
  1405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement