|
1 | | -## StrStr |
2 | | -#### Usage |
| 1 | +## Simple One-layer Timeing Wheel |
| 2 | +### Usage |
3 | 3 | ``` |
4 | 4 | make |
5 | | -./strstr |
| 5 | +./timer |
6 | 6 | ``` |
7 | 7 |
|
8 | | -#### Analysis |
| 8 | +### Analysis |
9 | 9 |
|
10 | | -#### Code |
| 10 | +This is a simple one layer timing wheel design where the wheel contains 10 slices of wheel bin, with each bin it maintains a linked lists of callback function to call. These function will trigger once the timing wheel tick happens and deadline is due. |
| 11 | + |
| 12 | +### Code |
11 | 13 | ```c |
12 | | -#include <stdio.h> |
13 | | - |
14 | | -// returns true if X and Y are same |
15 | | -int compare(const char *X, const char *Y) |
16 | | -{ |
17 | | - while (*X && *Y) |
18 | | - { |
19 | | - if (*X != *Y) |
20 | | - return 0; |
21 | | - |
22 | | - X++; |
23 | | - Y++; |
| 14 | +#include<stdio.h> |
| 15 | +#include<stdint.h> |
| 16 | +#include<stdlib.h> |
| 17 | +#include<time.h> |
| 18 | +#include<unistd.h> |
| 19 | + |
| 20 | +#define WHEEL_BIN_NUMBER 10 |
| 21 | +#define GRANULARITY 1000000 |
| 22 | + |
| 23 | +typedef void (*timeout_handler)(); |
| 24 | + |
| 25 | +typedef struct node { |
| 26 | + struct node *next; |
| 27 | + int timestamp; |
| 28 | + timeout_handler timeout_cb; |
| 29 | +} Node, *pNode; |
| 30 | + |
| 31 | +typedef struct timing_wheel { |
| 32 | + int cur_slot; |
| 33 | + int granularity; |
| 34 | + Node nodes[WHEEL_BIN_NUMBER]; |
| 35 | +} TWheel, *pTWheel; |
| 36 | + |
| 37 | +pTWheel init_time_wheel(int gran) { |
| 38 | + pTWheel new_wheel = (pTWheel) malloc(sizeof(TWheel)); |
| 39 | + new_wheel->granularity = gran; |
| 40 | + new_wheel->cur_slot = 0; |
| 41 | + int i; |
| 42 | + for (i; i < WHEEL_BIN_NUMBER; i++) { |
| 43 | + new_wheel->nodes[i].next = NULL; |
24 | 44 | } |
25 | | - |
26 | | - return (*Y == '\0'); |
| 45 | + |
| 46 | + return new_wheel; |
27 | 47 | } |
28 | | - |
29 | | -// Function to implement strstr() function |
30 | | -const char* strstr(const char* X, const char* Y) |
31 | | -{ |
32 | | - while (*X != '\0') |
33 | | - { |
34 | | - if ((*X == *Y) && compare(X, Y)) |
35 | | - return X; |
36 | | - X++; |
| 48 | + |
| 49 | +int install_handler(pTWheel twheel, int deadline, timeout_handler new_cb) { |
| 50 | + int ret = 0; |
| 51 | + if (deadline/twheel->granularity >= WHEEL_BIN_NUMBER) { |
| 52 | + printf("Deadline exceed timing wheel size\n"); |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + int index = (twheel->cur_slot + ((deadline/(twheel->granularity))))%WHEEL_BIN_NUMBER; |
| 57 | + pNode iterator = &(twheel->nodes[index]); |
| 58 | + |
| 59 | + while(iterator->next) { |
| 60 | + iterator = iterator->next; |
| 61 | + } |
| 62 | + |
| 63 | + pNode new_node = (pNode) malloc(sizeof(Node)); |
| 64 | + new_node->timeout_cb = new_cb; |
| 65 | + new_node->timestamp = deadline; |
| 66 | + new_node->next = NULL; |
| 67 | + iterator->next = new_node; |
| 68 | + |
| 69 | + return ret; |
| 70 | +} |
| 71 | + |
| 72 | +void tick(pTWheel twheel) { |
| 73 | + int i; |
| 74 | + pNode iterator = &twheel->nodes[twheel->cur_slot]; |
| 75 | + while(iterator->next) { |
| 76 | + pNode tmp = iterator->next; |
| 77 | + tmp->timeout_cb(); |
| 78 | + printf("Callback at %d deadline triggers\n", tmp->timestamp); |
| 79 | + iterator->next = iterator->next->next; |
| 80 | + free(tmp); |
37 | 81 | } |
38 | | - |
39 | | - return NULL; |
| 82 | + twheel->cur_slot = (twheel->cur_slot+1)%WHEEL_BIN_NUMBER; |
40 | 83 | } |
41 | | - |
42 | | -// Implement strstr function in C |
43 | | -int main() |
44 | | -{ |
45 | | - char *X = "Techie Delight - Coding made easy"; |
46 | | - char *Y = "Coding"; |
47 | | - |
48 | | - printf("%s\n", strstr(X, Y)); |
49 | | - |
| 84 | + |
| 85 | +void print_task() { |
| 86 | + printf("Hi1\n"); |
| 87 | +} |
| 88 | + |
| 89 | +void print_task2() { |
| 90 | + printf("Hi2\n"); |
| 91 | +} |
| 92 | + |
| 93 | +void print_task3() { |
| 94 | + printf("Hi3\n"); |
| 95 | +} |
| 96 | + |
| 97 | +int main(void) { |
| 98 | + int ret = 0; |
| 99 | + |
| 100 | + pTWheel new_wheel = init_time_wheel(GRANULARITY); |
| 101 | + timeout_handler cb = print_task; |
| 102 | + install_handler(new_wheel, 4*GRANULARITY, cb); |
| 103 | + install_handler(new_wheel, 4.25*GRANULARITY, cb); |
| 104 | + install_handler(new_wheel, 4.9*GRANULARITY, cb); |
| 105 | + |
| 106 | + cb = print_task2; |
| 107 | + install_handler(new_wheel, 8*GRANULARITY, cb); |
| 108 | + |
| 109 | + cb = print_task3; |
| 110 | + install_handler(new_wheel, 8*GRANULARITY, cb); |
| 111 | + |
| 112 | + install_handler(new_wheel, 12*GRANULARITY, cb); |
| 113 | + |
| 114 | + int count = 0; |
| 115 | + while(count < 15) { |
| 116 | + tick(new_wheel); |
| 117 | + usleep(GRANULARITY); |
| 118 | + printf("Time: %d\n", count++); |
| 119 | + if (count == 8) { |
| 120 | + install_handler(new_wheel, 4*GRANULARITY, cb); |
| 121 | + } |
| 122 | + } |
| 123 | + |
50 | 124 | return 0; |
51 | 125 | } |
52 | 126 | ``` |
53 | 127 |
|
54 | 128 | ### Reference |
55 | | -
|
56 | | -https://www.techiedelight.com/implement-strstr-function-c-iterative-recursive/ |
|
0 commit comments