D
D
Denis2019-05-04 16:54:54
linux
Denis, 2019-05-04 16:54:54

How does program execution change when using an interval timer with different time intervals in Linux OS?

There is a certain program in which the interval timer is used. As I understand it, this timer is needed to count the time interval after which a certain signal (SIGALRM, SIGVTALRM, SIGPROF) is transmitted to the process. In the code, I change the time interval of the ITIMER_REAL type timer: 100ms, 300ms, 500ms, compile, execute with the output of the result to a file. In total, 3 files of the same size come out, respectively, with the same content (100 iterations of the rhyme).
Question: What should change in the output of the program? Or maybe the program execution time should change?

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
void timer_handler (int signum)
{
  static int count=0;
  printf("\nТаймер протикал %d раз\n\n",++count);
  if(count>=100)
  exit(0);
}
int main()
{
  int i;
  struct sigaction sa;
  struct itimerval timer;
  char stix1[]={"У попа была собака,"};
  char stix2[]={"он ее любил."};
  char stix3[]={"Она съела кусок мяса,"};
  char stix4[]={"он её убил."};
  char stix5[]={"Схоронил ее под камнем,"};
  char stix6[]={"а на камне написал:"};
  memset(&sa,0,sizeof(sa));
  sa.sa_handler=&timer_handler;
  sigaction(SIGALRM,&sa,NULL);
  /* Timer activate within 500 millisecond...*/
  timer.it_value.tv_sec=0;
  timer.it_value.tv_usec=500000;
  /*...and will be activated within other 500 milliseconds*/
  timer.it_interval.tv_sec=0;
  timer.it_interval.tv_usec=500000;
  /* Begin of virtual timer.*/
  setitimer(ITIMER_REAL,&timer,NULL);
  do
  {
    sleep(1);
    for(i=0;i<sizeof(stix1);i++)
    printf("%c",stix1[i]);
    printf("\n");
    for(i=0;i<sizeof(stix2);i++)
    printf("%c",stix2[i]);
    printf("\n");
    for(i=0;i<sizeof(stix3);i++)
    printf("%c",stix3[i]);
    printf("\n");
    for(i=0;i<sizeof(stix4);i++)
    printf("%c",stix4[i]);
    printf("\n");
    for(i=0;i<sizeof(stix5);i++)
    printf("%c",stix5[i]);
    printf("\n");
    for(i=0;i<sizeof(stix6);i++)
    printf("%c",stix6[i]);
    printf("\n");
  }
  while(1);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2019-05-06
@Griboks

Are you having problems with the compiler? Run and see. It's still a forum, not a debugging bureau.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question