20211105李宜时信息安全系统设计与基础学习笔记八

发布时间 2023-11-04 12:34:38作者: 李宜时

Ubuntu中的定时器及时钟服务学习笔记

基础概念

在Ubuntu系统中,定时器和时钟服务是操作系统时间管理的基础。定时器用于在特定时间点或经过特定时间间隔后触发事件。时钟服务则提供当前时间和日期信息。

硬件定时器

硬件定时器是由计算机硬件提供的计时设备,它可以在不同时间间隔发出信号。

  • 个人计算机定时器:这些定时器通常与系统的实时时钟(RTC)一起工作,提供定时功能。
  • CPU定时器:大多数现代CPU提供内置的定时器,如程序性间隔定时器(PIT),高精度事件定时器(HPET),以及其他高级定时器。

软件定时器

软件定时器是由操作系统内核管理的,它利用硬件定时器的功能来实现。

  • 中断处理:当定时器到时,硬件定时器会产生一个中断,由操作系统内核处理。
  • 时钟服务函数:操作系统提供的API,如 gettimeofday()clock_gettime(),可以用于获取当前时间或计算时间差。

定时器的类型

  • 间隔定时器:在固定时间间隔重复触发的定时器。
  • PEAL模式间隔定时器:一种高级的定时器,可以更精确地控制时间间隔和持续时间。

理解和代码实现

接下来,我们将讨论如何在Ubuntu中实现和使用这些概念。

系统基本代码

系统级编程常常需要使用C语言和系统调用。下面是一个简单的例子,演示如何获取当前时间。

#include <stdio.h>
#include <time.h>

int main() {
    time_t now;
    time(&now);
    printf("The current time is: %s", ctime(&now));
    return 0;
}

定时器中断

在C语言中,我们可以设置一个定时器,当时间到达时,操作系统会向我们的程序发送一个SIGALRM信号。

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void alarm_handler(int sig) {
    printf("Timer expired\n");
}

int main() {
    signal(SIGALRM, alarm_handler);
    alarm(5); // Set a timer for 5 seconds
    pause(); // Wait for any signal
    return 0;
}

定时器队列

在更复杂的应用中,可能需要管理多个定时器。这可以通过使用定时器队列来完成。

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>

#define NUM_TIMERS 3

void timer_handler(int signum) {
    static int count = 0;
    printf("timer expired %d times\n", ++count);
}

int main() {
    struct itimerval timers[NUM_TIMERS];
    signal(SIGALRM, timer_handler);

    for (int i = 0; i < NUM_TIMERS; ++i) {
        timers[i].it_value.tv_sec = (i+1) * 2; // first timer expiration
        timers[i].it_value.tv_usec = 0;
        timers[i].it_interval.tv_sec = (i+1) * 2; // subsequent expirations
        timers[i].it_interval.tv_usec = 0;

        // Set the timer. Note that ITIMER_REAL decrements in real time.
        if (setitimer(ITIMER_REAL, &timers[i], NULL) == -1) {
            perror("setitimer error");
            exit(EXIT_FAILURE);
        }
    }

    // An infinite loop to keep the program running while timers are ticking
    while (1) {
        pause(); // Wait for any signal
    }
    return 0;
}

临界区

当定时器中断发生时,可能会访问共享资源。在这种情况下,需要确保这些资源的访问是同步的,这通常涉及到临界区的概念。

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void* thread_function(void* arg) {
    // Lock the mutex before accessing the shared variable
    pthread_mutex_lock(&lock);

    // Critical section starts
    // ... (access shared variables)
    // Critical section ends

    // Unlock the mutex after accessing the shared variables
    pthread_mutex_unlock(&lock);

    return NULL;
}

int main() {
    pthread_t thread1, thread2;

    pthread_create(&thread1, NULL, &thread_function, NULL);
    pthread_create(&thread2, NULL, &thread_function, NULL);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    pthread_mutex_destroy(&lock);

    return 0;
}```

### 高级主题

对于高级用户,可能需要深入研究内核级别的时钟服务实现,如内核定时器和高分辨率定时器(hrtimer)。

```c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/timer.h>

MODULE_LICENSE("GPL");

struct timer_list my_timer;

void my_timer_callback(struct timer_list *t) {
    printk("my_timer_callback called (%ld).\n", jiffies);
}

int init_module(void) {
    printk("Module init: Hello, world\n");

    // Setup the timer
    timer_setup(&my_timer, my_timer_callback, 0);

    // Set timer expiration time (in jiffies)
    my_timer.expires = jiffies + HZ; // 1 second delay

    // Add the timer to the list of active timers
    add_timer(&my_timer);

    return 0;
}

void cleanup_module(void) {
    printk("Module cleanup: Goodbye, world\n");

    // Remove the timer if it's still active
    del_timer(&my_timer);
}

module_init(init_module);
module_exit(cleanup_module);

总结

Ubuntu系统中的定时器和时钟服务是对操作系统时间管理能力的体现。了解和掌握这些工具不仅可以帮助我们更好地理解操作系统的工作原理,还可以在需要进行时间控制或度量时提供必要的工具。

以下是我的苏格拉底挑战