LKD: Chapter 6 Kernel Data Structures

  这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees.

Linked Lists:(<linux/list.h>)

  在linux中,并不是直接将某个结构体作为链表的节点,而是在该结构中插入一个链表的节点。借助container_of()这个宏,我们可以轻松的找到包含给定成员变量的父结构。

  Linux kernel中一般使用的是循环双链表。

  正常遍历使用的是list_for_each()宏,但是当遍历过程中删除某个表项时就有可能出错。解决办法是使用

list_for_each_entry_safe(pos, next, head, member)

  与原来的不同在于需要多提供一个next指针,这样就可以在遍历的过程中安全移除当前表项了。

  当我们能提供next和prev指针时,我们就直接使用内部函数而不是包装函数。比如删除某个节点,用__list_del(prev, next)而不是list_del(list),可以节约资源。

Queues:

  内核中的队列也称作kfifo,在<linux/kfifo.h>中声明,在kernel/kfifo.c中实现。因为先进先出的特性,所以常用于生产与消费的模型中。初始化中的size须是2的n次方。

Maps:

  Maps至少要能满足三种操作:

  Add(key, value)

  Remove(key)

  value=Lookup (key)

  Hash table是常见的map,但并不是所有maps都是hash table。

  在linux内核中,map也被称作idr。其中UID可以理解为key。

Binary Trees:

  某个节点的深度是指从根节点到该节点中有多少个父节点。而balanced binary tree就是指所有叶子节点的深度相差不超过1的二叉树。

  在linux kernel中,主要使用的是red-black树,它有6个属性:

1、所有节点非黑即红;

2、叶子节点全为黑;

3、叶子节点不含数据;

4、所有非叶子节点都有两个孩子节点;

5、如果某节点是红色的,则它的两个孩子都是黑色的;

6、从一个节点开始到它的某个叶子节点的路径上含有的黑节点数目与到它其他的叶子节点的数目相同。

  rbtree的查找和插入需要自己实现。

What Data Structure to Use, When:

  linked list: iterating over all your data;

  queue: producer/consumer pattern;

  map: map a UID to an object;

  red-black tree: store a large amount of data and look it up efficiently.

时间: 2024-12-17 06:39:33

LKD: Chapter 6 Kernel Data Structures的相关文章

Operating System: Three Easy Pieces --- Lock Concurrent Data Structures (Note)

Before moving beyong locks, we will first describe how to use locks in some common data structures. Adding locks to a data structure to make it usable by threads makes the structure thread safe. Of course, exactly how such locks are added determines

Operating system management of address-translation-related data structures and hardware lookasides

An approach is provided in a hypervised computer system where a page table request is at an operating system running in the hypervised computer system. The operating system determines whether the page table request requires the hypervisor to process.

Data Structures and Algorithms with JavaScript

Book Description As an experienced JavaScript developer moving to server-side programming, you need to implement classic data structures and algorithms associated with conventional object-oriented languages like C# and Java. This practical guide show

Lock-Free Data Structures

By Andrei Alexandrescu, October 01, 2004 Post a Comment Lock-free data structures guarantee the progress of at least one thread when executing mutlithreaded procedures, thereby helping you avoid deadlock. Andrei Alexandrescu is a graduate student in

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

【DataStructure】Linked Data Structures

Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. But if you want to maintain an ordered list that allows quick insertions and deletions, you should use a linked data structure. so inserting an elemen

2014 UESTC Training for Data Structures H - Cookies Test

H - Cookies Test Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status As chief programmer at a cookie production plant you have many responsibilities, one of them being that the cookies produced and packag

2014 UESTC Training for Data Structures K - 方师傅与栈

K - 方师傅与栈 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 方师傅有一个1?N的排列,排列的顺序是固定的,他想要把这个排列重新排列成他喜欢的顺序. 于是他买了一个栈,他会按顺序将排列扔进栈内,在某些时刻将栈顶元素取出,这样出栈后的排列就可以重新排序啦. 例如,原序列是1,2,他先将1入栈,再将2入栈,然后将2出栈,最后将1出栈,那么新序列就变