Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting kernel linux linked list

I have a C program that is to simulate different scheduling algorithms. Process information is read from a file. Each process' information in the file is stored in the struct below:

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;
};

I have a "queue" structure which will hold the list of tasks/processes

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

I somewhat understand how kernel linked lists work and have a user version of the list.h. It seems as if most interactions with the list are defined in list.h. Anyone have an idea as how to go about trying to implement a sorting algorithm (merge probably) using the functions in that file?

like image 961
Seapoe Avatar asked Dec 26 '22 19:12

Seapoe


1 Answers

Why not just use list_sort defined in linux/list_sort.h? The syntax is:

/**
 * 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))
like image 65
Subhasis Das Avatar answered Jan 02 '23 05:01

Subhasis Das