Linux SIGALRM

HELLOW,
I need to do a program in c ++ under linux:
alling the alarm function (sec) will bring the SIGALRM signal to the ordering party after a second.
A program that calculates how much a computer is capable of performing increments of the integer variable of type long in 1 second. The program generates an integer result for the standard output.

I have no idea how to do it.
OK.

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
long int z = 0;
alarm(1);
while (1)
{
printf("Liczba:");
printf("%u\n" , z);
z++;
printf("pid %d ppid %d\n ",getpid(),getppid());


}
return 0;
}

How to add to with "z" only for 1 second and at the same time show the number of current processes?
Last edited on
alarm() raises a software interrupt, or signal. You must install a signal handler to handle the signal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>

int t = time(NULL);

void sigalarm_handler(int)
{
  printf("alarm fired at t=%ld\n", time(NULL) - t); // write a message when called
}

int main(void)
{
  // install our interrupt handler
  signal(SIGALRM, sigalarm_handler);

  // we want an alarm in 3 seconds
  printf("alarm requested t=%ld\n", time(NULL) - t); // write start time
  alarm(3);

  // sleep for 10 seconds or until signal arives
  sleep(10);
  printf("ending at t=%ld\n", time(NULL) - t); // write signalled/sleep time
  return 0;
}
Last edited on
Topic archived. No new replies allowed.