c - Is task->state deprecated after 5.6 Linux kernel versions? - Stack Overflow

Consider:#include <linuxinit.h>#include <linuxmodule.h>#include <linuxsched.h>

Consider:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/jiffies.h>

MODULE_LICENSE("GPL");

static int __init process_init(void)
{
    struct task_struct *task;

    printk(KERN_INFO "Listing process CPU time and waiting time:\n");

    for_each_process(task)
    {
        // Convert CPU time from clock ticks to seconds
        unsigned long user_time = task->utime / HZ;
        unsigned long system_time = task->stime / HZ;

        // Context switches
        unsigned long voluntary_switches = task->nvcsw;
        unsigned long involuntary_switches = task->nivcsw;

        // Process state (to determine if it's waiting)
        long state = task->state;

        printk(KERN_INFO "Process: %s | PID: %d | User Time: %lus | System Time: %lus | State: %ld | Voluntary CS: %lu | Involuntary CS: %lu\n",
               task->comm, task->pid, user_time, system_time, state, voluntary_switches, involuntary_switches);
    }

    return 0;
}

static void __exit process_exit(void)
{
    printk(KERN_INFO "Exiting process waiting time module\n");
}

module_init(process_init);
module_exit(process_exit);

I'm learning Linux device driver development, and when I try to check the process state then I get the below error message. When state was removed, the code correctly compiled. What should I do?

Error message:

/root/MJ/waiting_time.c:26:28: error: ‘struct task_struct’ has no member named ‘state’; did you mean ‘stats’?
26 | long state = task->state;

Consider:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/jiffies.h>

MODULE_LICENSE("GPL");

static int __init process_init(void)
{
    struct task_struct *task;

    printk(KERN_INFO "Listing process CPU time and waiting time:\n");

    for_each_process(task)
    {
        // Convert CPU time from clock ticks to seconds
        unsigned long user_time = task->utime / HZ;
        unsigned long system_time = task->stime / HZ;

        // Context switches
        unsigned long voluntary_switches = task->nvcsw;
        unsigned long involuntary_switches = task->nivcsw;

        // Process state (to determine if it's waiting)
        long state = task->state;

        printk(KERN_INFO "Process: %s | PID: %d | User Time: %lus | System Time: %lus | State: %ld | Voluntary CS: %lu | Involuntary CS: %lu\n",
               task->comm, task->pid, user_time, system_time, state, voluntary_switches, involuntary_switches);
    }

    return 0;
}

static void __exit process_exit(void)
{
    printk(KERN_INFO "Exiting process waiting time module\n");
}

module_init(process_init);
module_exit(process_exit);

I'm learning Linux device driver development, and when I try to check the process state then I get the below error message. When state was removed, the code correctly compiled. What should I do?

Error message:

/root/MJ/waiting_time.c:26:28: error: ‘struct task_struct’ has no member named ‘state’; did you mean ‘stats’?
26 | long state = task->state;

Share Improve this question edited Mar 22 at 22:23 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Mar 22 at 20:33 Thurunu MihirangaThurunu Mihiranga 454 bronze badges 1
  • 4 Have you read this commit 2f064a59a11f ("sched: Change task_struct::state")? – 0andriy Commented Mar 22 at 21:59
Add a comment  | 

1 Answer 1

Reset to default 3

Looking at the commit posted by 0andriy use:

long state = READ_ONCE(task->__state);

But you might be more interested in task_state_to_char function https://elixir.bootlin/linux/v6.13.7/source/include/linux/sched.h#L1663 .

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744304413a4567659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信