Advertisement
MarcelloGrechi

Clock Sistemas Computacionais

Apr 24th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8.  
  9. #define EVER ;;
  10.  
  11. /*
  12.  Faça um programa para monitorar e informar o
  13.  preço de chamadas telefônicas. O programa deverá
  14.  ser executado em background.
  15.  
  16.   .O início e o término de uma chamada são
  17.    informados através dos sinais SIGUSR1 e
  18.    SIGUSR2, respectivamente.
  19.  
  20.   .O custo da ligação é de 2 centavos por segundo,
  21.    para ligações de até 1 minuto ou de 1 centavo por
  22.    segundo a partir do 2o minuto, ou seja, uma ligação
  23.    de 1m30s custa R$1,50.
  24.  
  25. */
  26.  
  27. void CallHandler (int signal);
  28. float CalculateCallCost (int callDuration);
  29. double CalculateElapsedTime (clock_t startTime,clock_t endTime);
  30.  
  31.  
  32. int main (void)
  33. {
  34.  
  35.    //void (*p)(int);
  36.  
  37.    // Setting up Signal Handlers
  38.    signal (SIGUSR1,CallHandler);
  39.    signal (SIGUSR2,CallHandler);
  40.  
  41.    printf("\n PROCESS ID : %d \n",getpid());
  42.    
  43.    for(EVER);
  44.  
  45.   return 0;
  46.  
  47. }
  48.  
  49.  
  50. // Handles the Signals for Starting and Ending a call
  51. void CallHandler (int signal)
  52. {
  53.  
  54.   switch(signal)
  55.   {
  56.     static clock_t begin = 0;
  57.     clock_t end;
  58.     static int elapsedSeconds ;
  59.     static int callStarted = 0;
  60.  
  61.     case SIGUSR1:
  62.       // Starting timer  
  63.       begin = clock();
  64.  
  65.           printf("\nStarted Call. Ring Ring !");
  66.       fflush(NULL);
  67.  
  68.       // Setting call started flag
  69.       callStarted = 1;
  70.    
  71.     break;
  72.  
  73.     case SIGUSR2:
  74.    
  75.     // Checking for a call first
  76.     if (!callStarted)
  77.         {
  78.         printf("\nNo call was started. Start a call first");
  79.         fflush(NULL);
  80.         return;
  81.         }
  82.  
  83.     // Getting end time
  84.     end = clock(); 
  85.  
  86.     elapsedSeconds = CalculateElapsedTime (end,begin);
  87.  
  88.     printf ("\nCall Ended. Call time in seconds : %.2f - END %f BEGIN %f", elapsedSeconds, end, begin);
  89.  
  90.     printf ("\nTotal Cost of the Call : R$: %.2f\n\n", CalculateCallCost(elapsedSeconds));
  91.     fflush(NULL);
  92.  
  93.     callStarted = 0;
  94.     break;
  95.  
  96.   }
  97.  
  98. }
  99.  
  100. // Calculates time interval between the start and the end of a call
  101. double CalculateElapsedTime (clock_t endTime,clock_t startTime)
  102. {
  103.     double diffticks = endTime - startTime;
  104.     double diffms    = (diffticks*1000) / CLOCKS_PER_SEC;
  105.  
  106.     // Returning time in seconds
  107.     return diffms;
  108. }
  109.  
  110.  
  111. // Calculates the cost of a call based on the elapsed
  112. // time in seconds received as argument
  113. float CalculateCallCost (int callDuration)
  114. {
  115.    float totalCost;
  116.  
  117.    if (callDuration <= 60)
  118.    {
  119.     totalCost = callDuration * 2;
  120.    }
  121.    else
  122.    {
  123.    
  124.    }
  125.  
  126.  
  127.   return totalCost / 100;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement