一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Linux 編程之經(jīng)典多級(jí)時(shí)間輪定時(shí)器(上)

jf_78858299 ? 來(lái)源:CSDN ? 作者:叨陪鯉 ? 2023-04-21 14:45 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

圖片

上圖是5個(gè)時(shí)間輪級(jí)聯(lián)的效果圖。中間的大輪是工作輪,只有在它上的任務(wù)才會(huì)被執(zhí)行;其他輪上的任務(wù)時(shí)間到后遷移到下一級(jí)輪上,他們最終都會(huì)遷移到工作輪上而被調(diào)度執(zhí)行。

多級(jí)時(shí)間輪的原理也容易理解:就拿時(shí)鐘做說(shuō)明,秒針轉(zhuǎn)動(dòng)一圈分針轉(zhuǎn)動(dòng)一格;分針轉(zhuǎn)動(dòng)一圈時(shí)針轉(zhuǎn)動(dòng)一格;同理時(shí)間輪也是如此:當(dāng)?shù)图?jí)輪轉(zhuǎn)動(dòng)一圈時(shí),高一級(jí)輪轉(zhuǎn)動(dòng)一格,同時(shí)會(huì)將高一級(jí)輪上的任務(wù)重新分配到低級(jí)輪上。從而實(shí)現(xiàn)了多級(jí)輪級(jí)聯(lián)的效果。

1.1 多級(jí)時(shí)間輪對(duì)象

圖片

多級(jí)時(shí)間輪應(yīng)該至少包括以下內(nèi)容:

  • 每一級(jí)時(shí)間輪對(duì)象
  • 輪子上指針的位置

關(guān)于輪子上指針的位置有一個(gè)比較巧妙的辦法:那就是位運(yùn)算。比如定義一個(gè)無(wú)符號(hào)整型的數(shù):

圖片

通過(guò)獲取當(dāng)前的系統(tǒng)時(shí)間便可以通過(guò)位操作轉(zhuǎn)換為時(shí)間輪上的時(shí)間,通過(guò)與實(shí)際時(shí)間輪上的時(shí)間作比較,從而確定時(shí)間輪要前進(jìn)調(diào)度的時(shí)間,進(jìn)而操作對(duì)應(yīng)時(shí)間輪槽位對(duì)應(yīng)的任務(wù)。

為什么至少需要這兩個(gè)成員呢?

  • 定義多級(jí)時(shí)間輪,首先需要明確的便是級(jí)聯(lián)的層數(shù),也就是說(shuō)需要確定有幾個(gè)時(shí)間輪。
  • 輪子上指針位置,就是當(dāng)前時(shí)間輪運(yùn)行到的位置,它與真實(shí)時(shí)間的差便是后續(xù)時(shí)間輪需要調(diào)度執(zhí)行,它們的差值是時(shí)間輪運(yùn)作起來(lái)的驅(qū)動(dòng)力。

多級(jí)時(shí)間輪對(duì)象的定義

//實(shí)現(xiàn)5級(jí)時(shí)間輪 范圍為0~ (2^8 * 2^6 * 2^6 * 2^6 *2^6)=2^32
struct tvec_base
{
    unsigned long   current_index;   
    pthread_t     thincrejiffies;
    pthread_t     threadID;
    struct tvec_root  tv1; /*第一個(gè)輪*/
    struct tvec       tv2; /*第二個(gè)輪*/
    struct tvec       tv3; /*第三個(gè)輪*/
    struct tvec       tv4; /*第四個(gè)輪*/
    struct tvec       tv5; /*第五個(gè)輪*/
};

1.2 時(shí)間輪對(duì)象

圖片

我們知道每一個(gè)輪子實(shí)際上都是一個(gè)哈希表,上面我們只是實(shí)例化了五個(gè)輪子的對(duì)象,但是五個(gè)輪子具體包含什么,有幾個(gè)槽位等等沒有明確(即struct tvec和struct tvec_root)。

#define TVN_BITS   6
#define TVR_BITS   8
#define TVN_SIZE   (1<

此外,每一個(gè)時(shí)間輪都是哈希表,因此它的類型應(yīng)該至少包含兩個(gè)指針域來(lái)實(shí)現(xiàn)雙向鏈表的功能。這里我們?yōu)榱朔奖闶褂猛ㄓ玫膕truct list_head的雙向鏈表結(jié)構(gòu)。

1.3 定時(shí)任務(wù)對(duì)象

圖片

定時(shí)器的主要工作是為了在未來(lái)的特定時(shí)間完成某項(xiàng)任務(wù),而這個(gè)任務(wù)經(jīng)常包含以下內(nèi)容:

  • 任務(wù)的處理邏輯(回調(diào)函數(shù))
  • 任務(wù)的參數(shù)
  • 雙向鏈表節(jié)點(diǎn)
  • 到時(shí)時(shí)間

定時(shí)任務(wù)對(duì)象的定義

typedef void (*timeouthandle)(unsigned long );
 
struct timer_list{
    struct list_head entry;          //將時(shí)間連接成鏈表
    unsigned long expires;           //超時(shí)時(shí)間
    void (*function)(unsigned long); //超時(shí)后的處理函數(shù)
    unsigned long data;              //處理函數(shù)的參數(shù)
    struct tvec_base *base;          //指向時(shí)間輪
};

在時(shí)間輪上的效果圖:

圖片

1.4 雙向鏈表

在時(shí)間輪上我們采用雙向鏈表的數(shù)據(jù)類型。采用雙向鏈表的除了操作上比單鏈表復(fù)雜,多占一個(gè)指針域外沒有其他不可接收的問題。而多占一個(gè)指針域在今天大內(nèi)存的時(shí)代明顯不是什么問題。至于雙向鏈表操作的復(fù)雜性,我們可以通過(guò)使用通用的struct list結(jié)構(gòu)來(lái)解決,因?yàn)殡p向鏈表有眾多的標(biāo)準(zhǔn)操作函數(shù),我們可以通過(guò)直接引用list.h頭文件來(lái)使用他們提供的接口。

struct list可以說(shuō)是一個(gè)萬(wàn)能的雙向鏈表操作框架,我們只需要在自定義的結(jié)構(gòu)中定義一個(gè)struct list對(duì)象即可使用它的標(biāo)準(zhǔn)操作接口。同時(shí)它還提供了一個(gè)類似container_of的接口,在應(yīng)用層一般叫做list_entry,因此我們可以很方便的通過(guò)struct list成員找到自定義的結(jié)構(gòu)體的起始地址。

關(guān)于應(yīng)用層的log.h, 我將在下面的代碼中附上該文件。如果需要內(nèi)核層的實(shí)現(xiàn),可以直接從linux源碼中獲取。

1.5 聯(lián)結(jié)方式

多級(jí)時(shí)間輪效果圖:

圖片

二. 多級(jí)時(shí)間輪C語(yǔ)言實(shí)現(xiàn)

2.1 雙向鏈表頭文件: list.h

提到雙向鏈表,很多的源碼工程中都會(huì)實(shí)現(xiàn)一系列的統(tǒng)一的雙向鏈表操作函數(shù)。它們?yōu)殡p向鏈表封裝了統(tǒng)計(jì)的接口,使用者只需要在自定義的結(jié)構(gòu)中添加一個(gè)struct list_head結(jié)構(gòu),然后調(diào)用它們提供的接口,便可以完成雙向鏈表的所有操作。

這些操作一般都在list.h的頭文件中實(shí)現(xiàn)。Linux源碼中也有實(shí)現(xiàn)(內(nèi)核態(tài)的實(shí)現(xiàn))。他們實(shí)現(xiàn)的方式基本完全一樣,只是實(shí)現(xiàn)的接口數(shù)量和功能上稍有差別。可以說(shuō)這個(gè)list.h文件是學(xué)習(xí)操作雙向鏈表的不二選擇,它幾乎實(shí)現(xiàn)了所有的操作:增、刪、改、查、遍歷、替換、清空等等。這里我拼湊了一個(gè)源碼中的log.h函數(shù),終于湊夠了多級(jí)時(shí)間輪中使用到的接口。

#if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD)
#define _BLKID_LIST_H
#ifdef __cplusplus 
extern "C" {
#endif
/*
 * Simple doubly linked list implementation.
 *
 * Some of the internal functions ("__xxx") are useful when
 * manipulating whole lists rather than single entries, as
 * sometimes we already know the next/prev entries and we can
 * generate better code by using them directly rather than
 * using the generic single-entry routines.
 */
struct list_head {
 struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \\
 struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { \\
 (ptr)->next = (ptr); (ptr)->prev = (ptr); \\
} while (0)
static inline void
__list_add(struct list_head *entry,
                struct list_head *prev, struct list_head *next)
{
    next->prev = entry;
    entry->next = next;
    entry->prev = prev;
    prev->next = entry;
}
/**
 * Insert a new element after the given list head. The new element does not
 * need to be initialised as empty list.
 * The list changes from:
 *      head → some element → ...
 * to
 *      head → new element → older element → ...
 *
 * Example:
 * struct foo *newfoo = malloc(...);
 * list_add(&newfoo->entry, &bar->list_of_foos);
 *
 * @param entry The new element to prepend to the list.
 * @param head The existing list.
 */
static inline void
list_add(struct list_head *entry, struct list_head *head)
{
    __list_add(entry, head, head->next);
}
/**
 * Append a new element to the end of the list given with this list head.
 *
 * The list changes from:
 *      head → some element → ... → lastelement
 * to
 *      head → some element → ... → lastelement → new element
 *
 * Example:
 * struct foo *newfoo = malloc(...);
 * list_add_tail(&newfoo->entry, &bar->list_of_foos);
 *
 * @param entry The new element to prepend to the list.
 * @param head The existing list.
 */
static inline void
list_add_tail(struct list_head *entry, struct list_head *head)
{
    __list_add(entry, head->prev, head);
}
static inline void
__list_del(struct list_head *prev, struct list_head *next)
{
    next->prev = prev;
    prev->next = next;
}
/**
 * Remove the element from the list it is in. Using this function will reset
 * the pointers to/from this element so it is removed from the list. It does
 * NOT free the element itself or manipulate it otherwise.
 *
 * Using list_del on a pure list head (like in the example at the top of
 * this file) will NOT remove the first element from
 * the list but rather reset the list as empty list.
 *
 * Example:
 * list_del(&foo->entry);
 *
 * @param entry The element to remove.
 */
static inline void
list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}
static inline void
list_del_init(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    INIT_LIST_HEAD(entry);
}
static inline void list_move_tail(struct list_head *list,
      struct list_head *head)
{
 __list_del(list->prev, list->next);
 list_add_tail(list, head);
}
/**
 * Check if the list is empty.
 *
 * Example:
 * list_empty(&bar->list_of_foos);
 *
 * @return True if the list contains one or more elements or False otherwise.
 */
static inline int
list_empty(struct list_head *head)
{
    return head->next == head;
}
/**
 * list_replace - replace old entry by new one
 * @old : the element to be replaced
 * @new : the new element to insert
 *
 * If @old was empty, it will be overwritten.
 */
static inline void list_replace(struct list_head *old,
    struct list_head *new)
{
 new->next = old->next;
 new->next->prev = new;
 new->prev = old->prev;
 new->prev->next = new;
}
/**
 * Retrieve the first list entry for the given list pointer.
 *
 * Example:
 * struct foo *first;
 * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
 *
 * @param ptr The list head
 * @param type Data type of the list element to retrieve
 * @param member Member name of the struct list_head field in the list element.
 * @return A pointer to the first list element.
 */
#define list_first_entry(ptr, type, member) \\
    list_entry((ptr)->next, type, member)
static inline void list_replace_init(struct list_head *old,
     struct list_head *new)
{
 list_replace(old, new);
 INIT_LIST_HEAD(old);
}
/**
 * list_entry - get the struct for this entry
 * @ptr: the &struct list_head pointer.
 * @type: the type of the struct this is embedded in.
 * @member: the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \\
 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/**
 * list_for_each - iterate over elements in a list
 * @pos: the &struct list_head to use as a loop counter.
 * @head: the head for your list.
 */
#define list_for_each(pos, head) \\
 for (pos = (head)->next; pos != (head); pos = pos->next)
/**
 * list_for_each_safe - iterate over elements in a list, but don't dereference
 *                      pos after the body is done (in case it is freed)
 * @pos: the &struct list_head to use as a loop counter.
 * @pnext: the &struct list_head to use as a pointer to the next item.
 * @head: the head for your list (not included in iteration).
 */
#define list_for_each_safe(pos, pnext, head) \\
 for (pos = (head)->next, pnext = pos->next; pos != (head); \\
      pos = pnext, pnext = pos->next)
#ifdef __cplusplus
}
#endif
#endif /* _BLKID_LIST_H */

這里面一般會(huì)用到一個(gè)重要實(shí)現(xiàn):container_of, 它的原理這里不敘述

2.2 調(diào)試信息頭文件: log.h

這個(gè)頭文件實(shí)際上不是必須的,我只是用它來(lái)添加調(diào)試信息(代碼中的errlog(), log()都是log.h中的宏函數(shù))。它的效果是給打印的信息加上顏色,效果如下:

圖片

log.h的代碼如下:

#ifndef _LOG_h_
#define _LOG_h_
#include 
#define COL(x)  "\\033[;" #x "m"
#define RED     COL(31)
#define GREEN   COL(32)
#define YELLOW  COL(33)
#define BLUE    COL(34)
#define MAGENTA COL(35)
#define CYAN    COL(36)
#define WHITE   COL(0)
#define GRAY    "\\033[0m"
#define errlog(fmt, arg...) do{     \\
    printf(RED"[#ERROR: Toeny Sun:"GRAY YELLOW" %s:%d]:"GRAY WHITE fmt GRAY, __func__, __LINE__, ##arg);\\
}while(0)
#define log(fmt, arg...) do{     \\
    printf(WHITE"[#DEBUG: Toeny Sun: "GRAY YELLOW"%s:%d]:"GRAY WHITE fmt GRAY, __func__, __LINE__, ##arg);\\
}while(0)
#endif
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • Linux
    +關(guān)注

    關(guān)注

    87

    文章

    11508

    瀏覽量

    213645
  • C語(yǔ)言
    +關(guān)注

    關(guān)注

    180

    文章

    7632

    瀏覽量

    141537
  • 定時(shí)器
    +關(guān)注

    關(guān)注

    23

    文章

    3298

    瀏覽量

    118788
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    Linux編程之經(jīng)典多級(jí)時(shí)間定時(shí)器(C語(yǔ)言版)

    上圖是5個(gè)時(shí)間級(jí)聯(lián)的效果圖。中間的大是工作,只有在它的任務(wù)才會(huì)被執(zhí)行;其他輪上的任務(wù)時(shí)間
    發(fā)表于 11-08 14:06 ?1003次閱讀

    時(shí)間管理和定時(shí)器編程基本知識(shí)匯總

    Linux系統(tǒng)編程第08期:時(shí)間管理和定時(shí)器編程 6年嵌入式開發(fā)經(jīng)驗(yàn),在多家...
    發(fā)表于 12-23 08:32

    CHMOS可編程時(shí)間間隔定時(shí)器芯片82C54

    82C54是專為Intel系列微處理機(jī)而設(shè)計(jì)的一種可編程時(shí)間間隔定時(shí)器/計(jì)數(shù),它是一種通用芯片,在系統(tǒng)軟件中可以把多級(jí)
    發(fā)表于 06-12 22:13 ?84次下載

    Linux下一種高性能定時(shí)器池的實(shí)現(xiàn)

    提出Linux用戶空間下的一種高性能定時(shí)器池的實(shí)現(xiàn)方法。主要基于時(shí)間、紅黑樹及Linux內(nèi)核提供了一種利于管理的
    發(fā)表于 09-25 14:57 ?25次下載

    JAVA教程之定時(shí)器的使用

    JAVA教程之定時(shí)器的使用,很好的JAVA的資料,快來(lái)下載吧。
    發(fā)表于 04-13 11:41 ?14次下載

    編程控制器實(shí)驗(yàn)教程之定時(shí)器指令實(shí)驗(yàn)

    編程控制器實(shí)驗(yàn)教程之定時(shí)器指令實(shí)驗(yàn),很好的學(xué)習(xí)資料。
    發(fā)表于 04-19 13:45 ?0次下載

    單片機(jī)教程之定時(shí)器和計(jì)數(shù)原理及應(yīng)用

      本文檔的主要內(nèi)容詳細(xì)介紹的是單片機(jī)教程之定時(shí)器和計(jì)數(shù)原理及應(yīng)用主要內(nèi)容包括了:定時(shí)器、計(jì)數(shù)原理,Mega8
    發(fā)表于 01-14 17:20 ?4次下載
    單片機(jī)教<b class='flag-5'>程之</b><b class='flag-5'>定時(shí)器</b>和計(jì)數(shù)<b class='flag-5'>器</b>原理及應(yīng)用

    Linux時(shí)間子系統(tǒng)中的高精度定時(shí)器(HRTIMER)的原理和實(shí)現(xiàn)

    雖然大部分時(shí)間里,時(shí)間可以實(shí)現(xiàn)O(1)時(shí)間復(fù)雜度,但是當(dāng)有進(jìn)位發(fā)生時(shí),不可預(yù)測(cè)的O(N)定時(shí)器級(jí)聯(lián)遷移
    發(fā)表于 05-10 14:11 ?7911次閱讀

    定時(shí)器開關(guān)怎么設(shè)置時(shí)間

    定時(shí)器開關(guān):又稱定時(shí)器、定時(shí)開關(guān)、時(shí)間定時(shí)器開關(guān)等,是一種控制用電器定時(shí)自動(dòng)開啟、關(guān)閉的電氣裝置
    的頭像 發(fā)表于 09-08 15:32 ?3.8w次閱讀
    <b class='flag-5'>定時(shí)器</b>開關(guān)怎么設(shè)置<b class='flag-5'>時(shí)間</b>

    Linux內(nèi)核定時(shí)器

    Linux內(nèi)核中,也可以通過(guò)定時(shí)器來(lái)完成定時(shí)功能。但和單片機(jī)不同的是,Linux內(nèi)核定時(shí)器是一種基于未來(lái)
    的頭像 發(fā)表于 09-22 08:56 ?2483次閱讀
    <b class='flag-5'>Linux</b>內(nèi)核<b class='flag-5'>定時(shí)器</b>

    時(shí)間定時(shí)器開關(guān)怎樣接線?

    時(shí)間定時(shí)器:又稱時(shí)間定時(shí)器開關(guān)、定時(shí)器、定時(shí)控制
    的頭像 發(fā)表于 09-28 11:47 ?9141次閱讀

    Linux 編程之經(jīng)典多級(jí)時(shí)間定時(shí)器(下)

    多級(jí)時(shí)間的原理也容易理解:就拿時(shí)鐘做說(shuō)明,秒針轉(zhuǎn)動(dòng)一圈分針轉(zhuǎn)動(dòng)一格;分針轉(zhuǎn)動(dòng)一圈時(shí)針轉(zhuǎn)動(dòng)一格;同理時(shí)間也是如此:當(dāng)?shù)图?jí)輪轉(zhuǎn)動(dòng)一圈時(shí),高一
    的頭像 發(fā)表于 04-21 14:45 ?861次閱讀

    多級(jí)時(shí)間實(shí)現(xiàn)框架

    一. 多級(jí)時(shí)間實(shí)現(xiàn)框架 上圖是5個(gè)時(shí)間級(jí)聯(lián)的效果圖。中間的大是工作
    的頭像 發(fā)表于 06-22 14:57 ?884次閱讀
    <b class='flag-5'>多級(jí)</b><b class='flag-5'>時(shí)間</b><b class='flag-5'>輪</b>實(shí)現(xiàn)框架

    多級(jí)時(shí)間實(shí)現(xiàn)框架

    一. 多級(jí)時(shí)間實(shí)現(xiàn)框架 上圖是5個(gè)時(shí)間級(jí)聯(lián)的效果圖。中間的大是工作
    的頭像 發(fā)表于 11-11 15:49 ?1039次閱讀
    <b class='flag-5'>多級(jí)</b><b class='flag-5'>時(shí)間</b><b class='flag-5'>輪</b>實(shí)現(xiàn)框架

    三菱PLC編程實(shí)現(xiàn)讀出時(shí)間定時(shí)器

    的功能,而定時(shí)器是實(shí)現(xiàn)時(shí)間控制的關(guān)鍵組件。本文將詳細(xì)介紹如何使用三菱PLC編程實(shí)現(xiàn)讀出時(shí)間定時(shí)器。 1.
    的頭像 發(fā)表于 06-20 11:11 ?4633次閱讀