gpt4 book ai didi

c - 使用 setitimer() 和 ITIMER_VIRTUAL 时,是什么导致虚拟运行时间变慢?

转载 作者:行者123 更新时间:2023-11-30 14:50:40 26 4
gpt4 key购买 nike

为了研究 ITMER_REALITIMER_VIRTUAL 之间的差异,我编写了以下程序。

#include <stdlib.h>   // exit(), EXIT_FAILURE, EXIT_SUCCESS
#include <signal.h> // sigaction()
#include <stdio.h> // printf(), fprintf(), stdout, stderr, perror(), _IOLBF
#include <string.h> // memset()
#include <sys/time.h> // ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, struct itimerval, setitimer()
#include <stdbool.h> // true, false
#include <limits.h> // INT_MAX

#define TIMEOUT 50 // ms
#define TIMER_TYPE ITIMER_VIRTUAL // Type of timer.


/* The three types of timers causes different signals.

type: type of timer, one of ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF.

return value: the signal generated by the timer.

*/
int timer_signal(int timer_type) {
int sig;

switch (timer_type) {
case ITIMER_REAL:
sig = SIGALRM;
break;
case ITIMER_VIRTUAL:
sig = SIGVTALRM;
break;
case ITIMER_PROF:
sig = SIGPROF;
break;
default:
fprintf(stderr, "ERROR: unknown timer type %d!\n", timer_type);
exit(EXIT_FAILURE);
}

return sig;
}


/* Set a timer and a handler for the timer.

Arguments

type: type of timer, one of ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF.

handler: timer signal handler.

ms: time in ms for the timer.

*/
void set_timer(int type, void (*handler) (int), int ms) {
struct itimerval timer;
struct sigaction sa;

/* Install signal handler for the timer. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = handler;
sigaction (timer_signal(type), &sa, NULL);

/* Configure the timer to expire after ms msec... */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = ms*1000;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;

if (setitimer (type, &timer, NULL) < 0) {
perror("Setting timer");
exit(EXIT_FAILURE);
};
}

/* Timer signal handler. */
void timer_handler (int signum) {
static int count = 0;
fprintf (stderr, "======> timer (%03d)\n", count++);
set_timer(TIMER_TYPE, timer_handler, TIMEOUT);
}


/* Calculate the nth Fibonacci number using recursion. */
int fib(int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return fib(n-1) + fib(n-2);
}
}

/* Print the Fibonacci number sequence over and over again.

This is deliberately an unnecessary slow and CPU intensive
implementation where each number in the sequence is calculated recursively
from scratch.
*/

void fibonacci_slow() {
int n = 0;
while (true) {
printf(" fib(%d) = %d\n", n, fib(n));
n = (n + 1) % INT_MAX;
}
}

/* Print the Fibonacci number sequence over and over again.

This implementation is much faster than fibonacci_slow().
*/
void fibonacci_fast() {
int a = 0;
int b = 1;
int n = 0;
int next = a + b;

while(true) {
printf(" fib(%d) = %d\n", n, a);
next = a + b;
a = b;
b = next;
n++;
if (next < 0) {
a = 0;
b = 1;
n = 0;
}
}
}

int main () {
/* Flush each printf() as it happens. */
setvbuf(stdout, 0, _IOLBF, 0);
setvbuf(stderr, 0, _IOLBF, 0);

set_timer(TIMER_TYPE, timer_handler, TIMEOUT);

// Call fibonacci_fast() or fibonacci_fast()

fibonacci_fast();
// fibonacci_slow();

}

main() 中,我调用 fibonacci_fast()fibonacci_slow()

正如预期的那样,使用 ITMER_REAL 时,调用 fibonacci_fast()fibonacci_slow() 时,计时器滴答之间的挂钟时间没有差异> 来自 main()

当使用ITIMER_VIRTUAL设置计时器并main()调用fibonacci_fast()时,每个计时器滴答之间的挂钟时间确实很长,但是如果 main() 调用 fibonacci_slow(),则每个计时器滴答之间的挂钟时间要小得多。

我想了解为什么 fibonacci_fast() 使虚拟运行时比 fibonacci_slow() 慢得多。与使用 fibonacci_slow() 相比,使用 fibonacci_fast() 时 CPU 调度程序为进程提供的 CPU 时间是否少得多?如果是,为什么?如果不是,还有什么可以解释这种差异?

最佳答案

ITIMER_VIRTUAL 仅当进程在用户模式下运行时运行。在两个斐波那契例程中,都有一个 printf 调用涉及同步写入数据的系统调用,该调用不计入虚拟时间。 fibonacci_fast 实际上大部分时间都花在 printf 和这个系统调用上,因此计时器似乎运行得慢得多。

关于c - 使用 setitimer() 和 ITIMER_VIRTUAL 时,是什么导致虚拟运行时间变慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48876872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com