【问题标题】:add another field to user_struct向 user_struct 添加另一个字段
【发布时间】:2014-12-21 14:17:29
【问题描述】:

我想在文件 linux-source/kernel/user.c 中的 user_struct 中添加新字段(存储该用户的就绪进程数)

struct user_struct {
    atomic_t ready_processes; /* I add this field */
    /* not important fields */
}

在哪里正确初始化这个字段?

【问题讨论】:

    标签: kernel linux-kernel


    【解决方案1】:

    为了给user_struct添加一个新字段,你需要做3件事:

    1. user_struct 的定义在文件 sched.h(include/linux/sched.h)
      您应该在 struct 中添加您的字段。

      struct user_struct {
          atomic_t ready_processes; /* I added this line! */
          /*Other fields*/
      };
      
    2. 在 user.c (kernel/user.c) 第 51 行,user_struct 被全局实例化为 root_user。在此处为您的字段赋值。

      struct user_struct root_user = {
          .ready_processes = ATOMIC_INIT(1), /* I added this line! */
          .__count    = ATOMIC_INIT(2),
          .processes  = ATOMIC_INIT(1),
          .files      = ATOMIC_INIT(0),
          .sigpending = ATOMIC_INIT(0),
          .locked_shm     = 0,
          .user_ns    = &init_user_ns,    
      };
      
    3. 您已经为 root 用户初始化了您的字段,但您还应该为其他用户初始化它。
      为此,在 user.c 中,转到函数alloc_uid,新用户在此分配和初始化。例如,您看到有一行 atomic_set(&new->__count, 1); 初始化了 __count。在此旁边添加您的初始化代码。

      atomic_set(&new->__count, 1);
      atomic_set(&new->ready_processes, 1); /* I added this line! */
      

    注意:它适用于 linux 2.6.32.62。我不确定其他版本,但我认为应该不会有太大不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 2022-01-20
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      相关资源
      最近更新 更多