1

I am planning to use kthread_run API in my kernel module.
As kthread_run, returns a struct task_struct *, which i want to store in a variable global across my module.
However, i want one thread each for a cpu and i obtain no of cpus using num_online_cpus.
However, when i write following code:

    //outside init_module function
    int cpus = num_online_cpus();
    static struct task_struct *my_tasks[cpus];  

    static int __init init_module(){
           for(int j = 0; j < cpus; ++j){
                  my_tasks[j] = kthread_run(...);  
           }  
    }

However, i get following error on this:
error: variably modified ‘tasks’ at file scope
How can i achieve this ???

5
  • Your loop variable is j, but the array subscript is i? Anyway, use kmalloc Commented Mar 5, 2015 at 18:22
  • I think the problem is declaring *my_tasks[cpus]. When using global variables, which by the way is usually a bad choice, the sizes of your arrays must be compile time constants. Commented Mar 5, 2015 at 19:58
  • @Miline: Thats exactly the point. But how can i declare the array with exact size as num_of_cpus ? Commented Mar 6, 2015 at 6:59
  • Dont create array. Create dynamic linked list of items with each node as task_struct Commented Mar 6, 2015 at 10:01
  • @Vikas, You have the answer already below. Try to invest your time into your problem. Commented Mar 6, 2015 at 17:56

1 Answer 1

1

If your variables are actually one per cpu, you might want to use the per_cpu macros. The gist is, you declare such a variable with:

DEFINE_PER_CPU(struct task_struct, my_tasks);

and then access the variable using

get_cpu_var(my_tasks).foo = bar;

You can get more info at See http://www.makelinux.net/ldd3/chp-8-sect-5 (or percpu.h) for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

That's good but not usable in my situation. I need to access all variables together in main process and then create threads for each.
You can access the variable of another cpu using per_cpu. (i.e.: per_cpu(my_tasks, other_cpu).foo = bar).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.