在linux内核代码里面,看到有for_each_process()和for_each_process_thread()两个函数,不太明白怎样回事,就做了个代码实验
1. 验证代码
/*****************************************************************
* Copyright (C) 2015 Intel Technology Co.,Ltd.*
******************************************************************
* lsitprocess.c
*
* DESCRIPTION:
* 列出所有的进程/线程
* AUTHOR:
* 刘峰 11579502
*
* CREATED DATE:
* 2015年09月17日
* REVISION:
* 1.0
*
* MODIFICATION HISTORY
* --------------------
* $Log:$
* 刘峰 2015年09月17日 Creation
*****************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#include <linux/hrtimer.h>
#include <linux/kvm_para.h>
#include <linux/seq_file.h>
#include <asm/u
access.h>
#include <stdbool.h>
#include <asm/msr.h>
#include <linux/slab.h>
#include <asm/page_64_types.h>
#include <linux/sched.h>
#define LOCAL static
#define KMOD_DBGDUMP(fmt, args...) printk(KERN_DEBUG "LISTPROCESS[%d] "fmt"
", __LINE__, args);
#define KMOD_RAWDUMP(args...) printk(args); /* RAW 信息输出 */
LOCAL struct proc_dir_entry *proc_file = NULL;
LOCAL int32_t kmod_action_sw = 8;
#define TEST_CASE_1 (1)
#define TEST_CASE_2 (2)
#define TEST_CASE_3 (3)
#define KMODSEPC
/******************************************************************************
* DESCRIPTION:
* 利用list_for_each_entry进行打印
* INPUTS:
* none
* OUTPUTS:
* none
* RETURNS:
* none
******************************************************************************/
void listprocess_test_case_1(void){
struct task_struct *task = NULL;
struct task_struct *p = NULL;
struct list_head *pos;
int count = 0;
printk("list_for_each
");
printk("PID COMM
");
task = &init_task;
list_for_each(pos, &task->tasks ) {
p = list_entry(pos, struct task_struct, tasks);
count++;
printk("%d %s
", p->pid, p->comm);
}
printk("Total process %d
", count);
return;
}
/******************************************************************************
* DESCRIPTION:
* 利用for_each_process进行打印
* INPUTS:
* none
* OUTPUTS:
* none
* RETURNS:
* none
******************************************************************************/
void listprocess_test_case_2(void){
struct task_struct *tsk;
int count = 0;
printk("for_each_process
");
printk("PID COMM
");
for_each_process(tsk){
count++;
printk("%d %s
", tsk->pid, tsk->comm);
}
printk("Total process %d
", count);
return;
}
/******************************************************************************
* DESCRIPTION:
* 利用for_each_process_thread进行打印
* INPUTS:
* none
* OUTPUTS:
* none
* RETURNS:
* none
******************************************************************************/
void listprocess_test_case_3(void){
struct task_struct *p;
struct task_struct *t;
int count = 0;
printk("for_each_process_thread
");
printk("PID COMM
");
for_each_process_thread(p,t){
count++;
printk("%d %s
", t->pid, t->comm);
}
printk("Total thread %d
", count);
return;
}
#if 1
KMODSEPC
#endif
/******************************************************************************
* DESCRIPTION:
* PROC 文件读方法
* INPUTS:
* none
* OUTPUTS:
* none
* RETURNS:
* none
******************************************************************************/
LOCAL int kmod_proc_read(struct seq_file * m, void * v)
{
seq_printf(m, "Entering kmod_proc_read
");
return 0;
}
/******************************************************************************
* DESCRIPTION:
* PROC 文件写方法
* INPUTS:
* none
* OUTPUTS:
* none
* RETURNS:
* none
******************************************************************************/
static ssize_t kmod_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos)
{
char debug_string[sizeof("4294967295")];
kmod_action_sw = 0;
KMOD_DBGDUMP("%s","Entering kmod_proc_write");
if (count >= sizeof(debug_string))
return -EINVAL;
if (copy_from_user(debug_string, buffer, count))
return -EFAULT;
debug_string[count] = '