我在 linux-3.7.6/kernel/sched/core.c 上工作,在 schedule() 函数中我必须记录进程的 pid 和 tgid,并且必须向用户空间显示记录的值。我在存储 tgid 和 pid 的内核空间中获取了全局结构数组,我在想是否可以将数组的地址传递到用户空间,然后在用户空间访问 tgid 和 pid 的值。
typedef struct process{
int pid;
int tgid;
}p;
p proc[100];
有没有办法一次性将存储在结构数组中的所有数据发送到用户空间? 我以前使用过 copy_to_user 但只是停留在这里,因为如何发送这些整组值作为 copy_to_user 以 block 的形式复制数据?如果有人能给我指示如何继续,我将不胜感激。谢谢!
最佳答案
我假设您希望在将数组复制到用户级别时保持原子性。
一个简单的方法是:
p local_array[100];
preemption_disable(); //disable preemption so you array content will not change,
//because no schedule() can be executed at this time.
memcpy(local_array, array, sizeof(array)); //then we get the consistent snapshot of
//array.
preemption_enable();
copy_to_user(user_buff_ptr, local_array, sizeof(array));
关于linux - 从内核空间向用户空间传递地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15676185/