c - 排序内核linux链表

标签 c linux linked-list

我有一个 C 程序,用于模拟不同的调度算法。从文件中读取进程信息。文件中每个进程的信息存储在以下结构中:

struct task_struct {
  volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  unsigned int flags; /* per process flags, defined below */
  int on_rq;
  int prio, static_prio, normal_prio;
  const struct sched_class *sched_class;
  struct list_head tasks;
  pid_t pid;
  int arr;
  /* simplify accounting */
  int ticks;
  int start_tick;
  int end_tick;
  int burst;
};

我有一个“队列”结构,它将保存任务/进程列表

struct rq {
  struct task_struct *curr, *idle, *stop;
  struct list_head task_root;
};

我有点了解内核链表的工作原理并且有一个list.h 的用户版本。似乎大多数与列表的交互都是在 list.h 中定义的。任何人都知道如何使用该文件中的函数尝试实现排序算法(可能合并)?

最佳答案

为什么不直接使用 linux/list_sort.h 中定义的 list_sort?语法是:

/**
 * list_sort - sort a list
 * @priv: private data, opaque to list_sort(), passed to @cmp
 * @head: the list to sort
 * @cmp: the elements comparison function
 *
 * This function implements "merge sort", which has O(nlog(n))
 * complexity.
 *
 * The comparison function @cmp must return a negative value if @a
 * should sort before @b, and a positive value if @a should sort after
 * @b. If @a and @b are equivalent, and their original relative
 * ordering is to be preserved, @cmp must return 0.
 */
void list_sort(void *priv, struct list_head *head,
        int (*cmp)(void *priv, struct list_head *a,
            struct list_head *b))

关于c - 排序内核linux链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19282929/

相关文章:

c++ - 如何打开 icc/icpc 警告?

c - 打印的元素显示出意想不到的值

c - 线程是否应该始终使用 while 循环来保存 pthread_cond_wait 语句?

android - 为什么 Android Linux 需要 ANDROID_HOME 路径?

c - 为什么在尝试填充结构时出现段错误(核心转储)或总线错误(核心转储)?

c - 错误 : dereferencing pointer to incomplete type but everything seems fine to me

linux - Bash 语句在此处 doc 中时挂起

linux - 存储网络数据包

java - 在java中的递归链表中搜索项目的索引

c - 在 C 链表中,为什么节点也是指针?