Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.96 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: tiago
  4.  *
  5.  * Created on November 5, 2015, 4:25 PM
  6.  */
  7. #include "sema.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/shm.h>
  11.  
  12.  
  13.  
  14. #define LIFETIME      30        /* max. lifetime of a process   */
  15. #define KEYBALLSOFPLAYERS 110
  16. #define KEYGOALS 111
  17. #define KEYREMATES 113
  18. #define KEYPOSSEBOLA 117
  19. #define KEYFALTAS 118
  20. #define KEYBOLAS 11
  21.  
  22. int *bolasJogadores;
  23. int *golos;
  24. int *faltas;
  25. int *posseBola;
  26. int *bolas;
  27.  
  28. int NPlayers, NBolas;
  29. int *numeroRemates;
  30.  
  31. /*
  32.  *
  33.  */
  34. int main(int argc, char** argv) {
  35.     int i, j, k, child_stat, cc;
  36.  
  37.     /*#########################################################################################################################################
  38.     ############################################## TRATAMENTO DE VARIAVEIS NJOGADORES E NBOLAS#################################################  
  39.     ########################################################################################################################################### */
  40.     printf("Insira o numero de jogadores: "); //output para informar que se pretende saber o numero de jogadores
  41.     scanf("%d", &NPlayers); //input recebe o numero de jogadores
  42.     printf("Insira o numero de bolas: "); //output para informar que se pretende saber o numero de bolas
  43.     scanf("%d", &NBolas); //input recebe o numero de bolas
  44.  
  45.     /*#########################################################################################################################################
  46.     ############################################## TRATAMENTO DE MEMORIA PARTILHADA############################################################  
  47.     ########################################################################################################################################### */
  48.     int shmid = shmget(KEYBALLSOFPLAYERS, NPlayers, 0777 | IPC_CREAT);
  49.     char *addr = (char*) shmat(shmid, 0, 0);
  50.     bolasJogadores = (int*) addr;
  51.     int shmid1 = shmget(KEYGOALS, NPlayers, 0777 | IPC_CREAT);
  52.     char *addr1 = (char*) shmat(shmid1, 0, 0);
  53.     golos = (int*) addr1;
  54.     int shmid2 = shmget(KEYREMATES, NPlayers, 0777 | IPC_CREAT);
  55.     char *addr2 = (char*) shmat(shmid2, 0, 0);
  56.     numeroRemates = (int*) addr2;
  57.     int shmid3 = shmget(KEYFALTAS, NPlayers, 0777 | IPC_CREAT);
  58.     char *addr3 = (char*) shmat(shmid3, 0, 0);
  59.     faltas = (int*) addr3;
  60.     int shmid4 = shmget(KEYPOSSEBOLA, NPlayers, 0777 | IPC_CREAT);
  61.     char *addr4 = (char*) shmat(shmid4, 0, 0);
  62.     posseBola = (int*) addr4;
  63.     int shmid5 = shmget(KEYBOLAS, NBolas, 0777 | IPC_CREAT);
  64.     char *addr5 = (char*) shmat(shmid5, 0, 0);
  65.     bolas = (int*) addr5;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     /*#########################################################################################################################################
  73.     ############################################## ARRAY PARA OS FILHOS #######################################################################  
  74.     ########################################################################################################################################### */
  75.     int child_pid [NBolas + 1], /* process ID's         */
  76.             wait_pid;
  77.  
  78.  
  79.     /*#########################################################################################################################################
  80.     ############################################## SEMAFOROS ##################################################################################  
  81.     ########################################################################################################################################### */
  82.     //semaforo que controla os acessos á memoria
  83.     semaphore control = init_sem(1);
  84.  
  85.  
  86.  
  87.     srand(time(NULL));
  88.  
  89.  
  90.     /*#########################################################################################################################################
  91.     ############################################## INICIO DOS PROCESSOS FILHOS ################################################################  
  92.     ########################################################################################################################################### */
  93.     for (i = 0; i < NBolas + 1; i++) {
  94.         child_pid [i] = fork(); //criar filhos
  95.  
  96.  
  97.         /*#########################################################################################################################################
  98.         ############################################## VERIFICAR QUAL É O PID #####################################################################
  99.         ########################################################################################################################################### */
  100.         switch (child_pid [i]) {
  101.  
  102.  
  103.  
  104.             case -1: /* error: no process created   */
  105.                 perror("fork failed");
  106.                 exit(1);
  107.                 break;
  108.  
  109.  
  110.             case 0://estamos no filho
  111.  
  112.                 sleep(2);
  113.  
  114.                 alarm(LIFETIME);
  115.  
  116.  
  117.  
  118.                 while (1) {
  119.  
  120.                     /*#########################################################################################################################################
  121.                     ############################################## PROCESSO EM QUE i=NPlayers É RESPONSAVEL POR CONTROLAR POSSE DE BOLA #######################
  122.                     ########################################################################################################################################### */
  123.                     if (i == NBolas) {
  124.                         int k;
  125.                         for (k = 0; k < NPlayers; k++) {
  126.                             if (*(bolasJogadores + k) > 0) {
  127.                                 *(posseBola + k) = *(posseBola + k) + 100;
  128.                             }
  129.                         }
  130.                         usleep(100 * 1000);
  131.  
  132.                         /*#########################################################################################################################################
  133.                         ######################### PROCESSO EM QUE i=0 ATÉ i=NPlayers-1 REPRESENTA O COMPORTAMENTO DOS JOGADORES####################################  
  134.                         ########################################################################################################################################### */
  135.                     } else {
  136.  
  137.                         int tempoRemate = rand() % 601;
  138.                         if (tempoRemate > 500) {
  139.                             usleep(500 * 1000);
  140.                             int l = *(bolas + i);
  141.                             P(control);
  142.                             *(faltas + l - 1) = *(faltas + l - 1) + 1;
  143.  
  144.                             printf("###################Jogador %d cometeu uma falta ############################## \n", *(bolas + i));
  145.  
  146.                         } else {
  147.                             if (tempoRemate < 100) {
  148.                                 tempoRemate = 100;
  149.                             }
  150.                             usleep(tempoRemate * 1000);
  151.                             P(control);
  152.                         }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.                         rematar(i);
  159.                         int s;
  160.                         //  printf("Placar");
  161.                         //for (s = 0; s < NPlayers; s++) {
  162.                         //  printf("Jogador %d com %d golos num total de %d remates||", s + 1, *(golos + s), *(numeroRemates + s));
  163.                         //}
  164.                         printf("\n");
  165.  
  166.                         V(control);
  167.                     }
  168.  
  169.                 };
  170.  
  171.                 break;
  172.  
  173.  
  174.             default: /* parent process      */
  175.                 /*#########################################################################################################################################
  176.                ############################################## LIMPAR MEMORIA PARA PREVENIR ################################################################  
  177.                ########################################################################################################################################### */
  178.                 for (cc = 0; cc < NPlayers; cc++) {
  179.                     *(bolasJogadores + cc) = 0;
  180.                     *(posseBola + cc) = 0;
  181.                     *(faltas + cc) = 0;
  182.                     *(golos + cc) = 0;
  183.                     *(numeroRemates + cc) = 0;
  184.                 }
  185.  
  186.                 for (cc = 0; cc < NBolas; cc++) {
  187.                     *(bolas + cc) = 0;
  188.                 }
  189.  
  190.  
  191.  
  192.                 /*#########################################################################################################################################
  193.                 ############################################## DISTRIBUIR BOLAS ###########################################################################  
  194.                 ########################################################################################################################################### */
  195.                 atribuirBolas(NPlayers, NBolas);
  196.  
  197.  
  198.                 /*#########################################################################################################################################
  199.                 ############################################## PROCESSO PAI################################################################################
  200.                  * Esperar que os processos filhos acabem
  201.                  * Responsavel por tratar da memoria  
  202.                 ########################################################################################################################################### */
  203.  
  204.  
  205.  
  206.                 if (i == (NBolas)) /* all childs created ?      */ { /* yes -> wait for termination */
  207.                     for (j = 0; j < NBolas + 1; ++j) {
  208.                         wait_pid = wait(&child_stat);
  209.                         if (wait_pid == -1) {
  210.                             perror("wait failed");
  211.                         };
  212.                     };
  213.                     printf("All child processes have terminated.\n");
  214.                     //int lm;
  215.                     //for (lm = 0; lm < NPlayers; lm++) {
  216.                     //  printf("Jogador %d com %d golos em %d remates  tendo posse bola durante %1.2f minutos cometendo %d faltas \n", lm + 1, *(golos + lm), *(numeroRemates + lm)
  217.  
  218.                     //        , *(posseBola + lm) / ((float) 1000), *(faltas + lm));
  219.                     // }
  220.                     int podio [NPlayers][2];
  221.                     dataToArray(podio);
  222.                     selectionSort(podio, NPlayers);
  223.  
  224.                     imprimir(podio, NPlayers);
  225.  
  226.                     printf("verificando necesidade de prolongamento\n");
  227.  
  228.  
  229.                     for (cc = 0; cc < NPlayers; cc++) {
  230.                         *(bolasJogadores + cc) = 0;
  231.                         *(posseBola + cc) = 0;
  232.                         *(faltas + cc) = 0;
  233.                         *(golos + cc) = 0;
  234.                         *(numeroRemates + cc) = 0;
  235.  
  236.                     }
  237.  
  238.                     for (cc = 0; cc < NBolas; cc++) {
  239.                         *(bolas + cc) = 0;
  240.                     }
  241.  
  242.                     int nJogadoresAProlongamento = prolongamento(podio);
  243.  
  244.  
  245.  
  246.                     if (nJogadoresAProlongamento > 0) {
  247.                         int ii, jj;
  248.  
  249.                         for (i = 0; i < NBolas + 1; i++) {
  250.                             child_pid [i] = fork(); //criar filhos
  251.  
  252.  
  253.                             /*#########################################################################################################################################
  254.                             ############################################## VERIFICAR QUAL É O PID #####################################################################
  255.                             ########################################################################################################################################### */
  256.                             switch (child_pid [i]) {
  257.  
  258.  
  259.  
  260.                                 case -1: /* error: no process created   */
  261.                                     perror("fork failed");
  262.                                     exit(1);
  263.                                     break;
  264.  
  265.  
  266.                                 case 0://estamos no filho
  267.  
  268.                                     sleep(2);
  269.  
  270.                                     alarm(5);
  271.  
  272.  
  273.  
  274.                                     while (1) {
  275.  
  276.                                         /*#########################################################################################################################################
  277.                                         ############################################## PROCESSO EM QUE i=NPlayers É RESPONSAVEL POR CONTROLAR POSSE DE BOLA #######################
  278.                                         ########################################################################################################################################### */
  279.                                         if (i == NBolas) {
  280.                                             int k;
  281.                                             for (k = 0; k < NPlayers; k++) {
  282.                                                 if (*(bolasJogadores + k) > 0) {
  283.                                                     *(posseBola + k) = *(posseBola + k) + 100;
  284.                                                 }
  285.                                             }
  286.                                             usleep(100 * 1000);
  287.  
  288.                                             /*#########################################################################################################################################
  289.                                             ######################### PROCESSO EM QUE i=0 ATÉ i=NPlayers-1 REPRESENTA O COMPORTAMENTO DOS JOGADORES####################################  
  290.                                             ########################################################################################################################################### */
  291.                                         } else {
  292.  
  293.                                             int tempoRemate = rand() % 601;
  294.                                             if (tempoRemate > 500) {
  295.                                                 usleep(500 * 1000);
  296.                                                 int l = *(bolas + i);
  297.                                                 P(control);
  298.                                                 *(faltas + l - 1) = *(faltas + l - 1) + 1;
  299.  
  300.                                                 printf("###################Jogador %d cometeu uma falta ############################## \n", *(bolas + i));
  301.  
  302.                                             } else {
  303.                                                 if (tempoRemate < 100) {
  304.                                                     tempoRemate = 100;
  305.                                                 }
  306.                                                 usleep(tempoRemate * 1000);
  307.                                                 P(control);
  308.                                             }
  309.  
  310.  
  311.                                             int JogadorAQuemRematar;
  312.                                             do {
  313.                                                 JogadorAQuemRematar = podio[rand() % nJogadoresAProlongamento][1];
  314.                                             } while (JogadorAQuemRematar == *(bolas + i));
  315.  
  316.                                             rematarProlongamento(i, JogadorAQuemRematar - 1);
  317.                                             int s;
  318.  
  319.                                             printf("\n");
  320.  
  321.                                             V(control);
  322.                                         }
  323.  
  324.                                     };
  325.  
  326.                                     break;
  327.  
  328.  
  329.                                 default: /* parent process      */
  330.  
  331.  
  332.  
  333.                                     for (ii = 0; ii < NBolas; ii++) {
  334.                                         jj = rand() % nJogadoresAProlongamento;
  335.                                         *(bolas + ii) = podio[jj][1];
  336.                                         *(bolasJogadores + podio[jj][1] - 1) = *(bolasJogadores + podio[jj][1] - 1) + 1;
  337.  
  338.                                     }
  339.  
  340.                                     /*#########################################################################################################################################
  341.                                     ############################################## PROCESSO PAI################################################################################
  342.                                      * Esperar que os processos filhos acabem
  343.                                      * Responsavel por tratar da memoria  
  344.                                     ########################################################################################################################################### */
  345.  
  346.  
  347.  
  348.                                     if (i == (NBolas)) /* all childs created ?      */ { /* yes -> wait for termination */
  349.                                         for (j = 0; j < NBolas + 1; ++j) {
  350.                                             wait_pid = wait(&child_stat);
  351.                                             if (wait_pid == -1) {
  352.                                                 perror("wait failed");
  353.                                             };
  354.                                         };
  355.  
  356.  
  357.  
  358.  
  359.                                         int podio2[nJogadoresAProlongamento][2];
  360.  
  361.  
  362.                                         dataToProlongamento(podio2, podio, nJogadoresAProlongamento);
  363.                                         selectionSort(podio2, nJogadoresAProlongamento);
  364.  
  365.  
  366.                                         imprimir(podio2, nJogadoresAProlongamento);
  367.  
  368.  
  369.  
  370.                                     };
  371.                             }; /* end switch            */
  372.                         }; /* end for           */
  373.  
  374.  
  375.                         rel_sem(control);
  376.                         sleep(2);
  377.                         shmdt(addr);
  378.                         shmctl(shmid, IPC_RMID, NULL);
  379.                         shmdt(addr1);
  380.                         shmctl(shmid1, IPC_RMID, NULL);
  381.                         shmdt(addr2);
  382.                         shmctl(shmid2, IPC_RMID, NULL);
  383.                         shmdt(addr3);
  384.                         shmctl(shmid3, IPC_RMID, NULL);
  385.                         shmdt(addr4);
  386.                         shmctl(shmid4, IPC_RMID, NULL);
  387.                         shmdt(addr5);
  388.                         shmctl(shmid5, IPC_RMID, NULL);
  389.  
  390.  
  391.                     }
  392.  
  393.  
  394.                 };
  395.         }; /* end switch            */
  396.     }; /* end for           */
  397.  
  398.  
  399.     return (EXIT_SUCCESS);
  400. }
  401.  
  402. int prolongamento(int array[][2]) {
  403.     int maxGolo = 1, i;
  404.     for (i = 1; i < NPlayers; i++) {
  405.         if (array[i][0] == array[0][0]) {
  406.             maxGolo++;
  407.         }
  408.     }
  409.     if (maxGolo > 1) {
  410.         printf("É necessario prolongamento\n");
  411.  
  412.  
  413.         printf("vao %d jogadores a prolongamento", maxGolo);
  414.         return maxGolo;
  415.     } else {
  416.         printf("FIM DE JOGO\n");
  417.     }
  418.     return 0;
  419.  
  420. }
  421.  
  422. imprimir(int array[][2], int tamanho) {
  423.     int i;
  424.     float precisao;
  425.     printf("#########################################################################################################################################################\n");
  426.     for (i = 0; i < tamanho; i++) {
  427.         if (array[i][0] > 0) {
  428.             precisao = array[i][0] / ((float) *(numeroRemates + array[i][1] - 1));
  429.         } else {
  430.             precisao = 0;
  431.         }
  432.         printf("%d lugar- Jogador %d com %d golos em %d remates com uma precisao de remate %1.2f %% tendo posse bola durante %1.2f minutos cometendo %d faltas \n", i + 1, array[i][1], array[i][0], *(numeroRemates + array[i][1] - 1), precisao * 100
  433.  
  434.                 , *(posseBola + array[i][1] - 1) / ((float) 1000), *(faltas + array[i][1] - 1));
  435.         sleep(0.2);
  436.     };
  437.     printf("#########################################################################################################################################################\n");
  438. }
  439.  
  440. int dataToArray(int array[][2]) {
  441.     int i;
  442.     for (i = 0; i < NPlayers; i++) {
  443.  
  444.         array[i][0] = *(golos + i); //os golos dele
  445.         array[i][1] = i + 1; //numero do jogador
  446.     };
  447.  
  448. }
  449.  
  450. int dataToProlongamento(int array[][2], int array2[][2], int tamanho) {
  451.     int i;
  452.     for (i = 0; i < tamanho; i++) {
  453.         array[i][1] = array2[i][1]; //numero do jogador
  454.         array[i][0] = *(golos + array[i][1] - 1); //os golos dele
  455.  
  456.     };
  457.  
  458. }
  459.  
  460. selectionSort(int num[][2], int tam) {
  461.     int i, j, min, aux;
  462.     for (i = 0; i < (tam - 1); i++) {
  463.         min = i;
  464.         for (j = (i + 1); j < tam; j++) {
  465.             if (num[j][0] > num[min][0])
  466.                 min = j;
  467.         }
  468.         if (i != min) {
  469.             aux = num[i][0];
  470.             num[i][0] = num[min][0];
  471.             num[min][0] = aux;
  472.             aux = num[i][1];
  473.             num[i][1] = num[min][1];
  474.             num[min][1] = aux;
  475.         }
  476.     }
  477. }
  478.  
  479. rematarProlongamento(int i, int baliza) {
  480.  
  481.     int k = *(bolas + i); //k é o numero do jogador que vai rematar
  482.  
  483.  
  484.  
  485.     printf("###################################################\n");
  486.     if (rand() % 2 == 0) {//50% 50%
  487.  
  488.         printf("Jogador %d marcou golo\n", k);
  489.         *(golos + k - 1) = *(golos + k - 1) + 1;
  490.  
  491.     } else {//else falhou
  492.         printf("Jogador %d falhou \n", k);
  493.     }
  494.  
  495.  
  496.     *(bolas + i) = (baliza + 1);
  497.     *(bolasJogadores + baliza) = *(bolasJogadores + baliza) + 1;
  498.     printf("Jogador %d ganhou uma bola\n", baliza + 1);
  499.     *(numeroRemates + k - 1) = *(numeroRemates + k - 1) + 1;
  500.  
  501.     *(bolasJogadores + k - 1) = *(bolasJogadores + k - 1) - 1;
  502.     printf("###################################################\n");
  503.  
  504.  
  505.  
  506. }
  507.  
  508. rematar(int i) {
  509.  
  510.     int k = *(bolas + i); //k é o numero do jogador que vai rematar
  511.  
  512.     int baliza = getBalizaValida(k - 1); //obtem baliza que via rematar
  513.  
  514.     printf("###################################################\n");
  515.     if (rand() % 2 == 0) {//50% 50%
  516.  
  517.         printf("Jogador %d marcou golo\n", k);
  518.         *(golos + k - 1) = *(golos + k - 1) + 1;
  519.  
  520.     } else {//else falhou
  521.         printf("Jogador %d falhou \n", k);
  522.     }
  523.  
  524.  
  525.     *(bolas + i) = (baliza + 1);
  526.     *(bolasJogadores + baliza) = *(bolasJogadores + baliza) + 1;
  527.     printf("Jogador %d ganhou uma bola\n", baliza + 1);
  528.     *(numeroRemates + k - 1) = *(numeroRemates + k - 1) + 1;
  529.  
  530.     *(bolasJogadores + k - 1) = *(bolasJogadores + k - 1) - 1;
  531.     printf("###################################################\n");
  532.  
  533.  
  534.  
  535. }
  536.  
  537. int getBalizaValida(int i) {
  538.     int aux;
  539.     do {
  540.         aux = rand() % NPlayers;
  541.     } while (aux == i);
  542.     return aux;
  543. }
  544.  
  545. atribuirBolas(int numeroJogadores, int numeroBolas) {
  546.     int i, j;
  547.     for (i = 0; i < numeroBolas; i++) {
  548.         j = rand() % numeroJogadores;
  549.         *(bolas + i) = j + 1;
  550.         *(bolasJogadores + j) = *(bolasJogadores + j) + 1;
  551.  
  552.     }
  553.  
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement