Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <time.h>
- #define EVER ;;
- /*
- Faça um programa para monitorar e informar o
- preço de chamadas telefônicas. O programa deverá
- ser executado em background.
- .O início e o término de uma chamada são
- informados através dos sinais SIGUSR1 e
- SIGUSR2, respectivamente.
- .O custo da ligação é de 2 centavos por segundo,
- para ligações de até 1 minuto ou de 1 centavo por
- segundo a partir do 2o minuto, ou seja, uma ligação
- de 1m30s custa R$1,50.
- */
- void CallHandler (int signal);
- float CalculateCallCost (int callDuration);
- double CalculateElapsedTime (clock_t startTime,clock_t endTime);
- int main (void)
- {
- //void (*p)(int);
- // Setting up Signal Handlers
- signal (SIGUSR1,CallHandler);
- signal (SIGUSR2,CallHandler);
- printf("\n PROCESS ID : %d \n",getpid());
- for(EVER);
- return 0;
- }
- // Handles the Signals for Starting and Ending a call
- void CallHandler (int signal)
- {
- switch(signal)
- {
- static clock_t begin = 0;
- clock_t end;
- static int elapsedSeconds ;
- static int callStarted = 0;
- case SIGUSR1:
- // Starting timer
- begin = clock();
- printf("\nStarted Call. Ring Ring !");
- fflush(NULL);
- // Setting call started flag
- callStarted = 1;
- break;
- case SIGUSR2:
- // Checking for a call first
- if (!callStarted)
- {
- printf("\nNo call was started. Start a call first");
- fflush(NULL);
- return;
- }
- // Getting end time
- end = clock();
- elapsedSeconds = CalculateElapsedTime (end,begin);
- printf ("\nCall Ended. Call time in seconds : %.2f - END %f BEGIN %f", elapsedSeconds, end, begin);
- printf ("\nTotal Cost of the Call : R$: %.2f\n\n", CalculateCallCost(elapsedSeconds));
- fflush(NULL);
- callStarted = 0;
- break;
- }
- }
- // Calculates time interval between the start and the end of a call
- double CalculateElapsedTime (clock_t endTime,clock_t startTime)
- {
- double diffticks = endTime - startTime;
- double diffms = (diffticks*1000) / CLOCKS_PER_SEC;
- // Returning time in seconds
- return diffms;
- }
- // Calculates the cost of a call based on the elapsed
- // time in seconds received as argument
- float CalculateCallCost (int callDuration)
- {
- float totalCost;
- if (callDuration <= 60)
- {
- totalCost = callDuration * 2;
- }
- else
- {
- }
- return totalCost / 100;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement