Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1277875
  • 博文数量: 213
  • 博客积分: 7590
  • 博客等级: 少将
  • 技术积分: 2185
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-31 17:31
个人简介

热爱开源,热爱linux

文章分类

全部博文(213)

文章存档

2018年(4)

2017年(1)

2015年(1)

2014年(5)

2013年(2)

2012年(2)

2011年(21)

2010年(82)

2009年(72)

2008年(23)

分类: LINUX

2010-04-19 22:21:06


前几天写了一个简单的内核模块编程,在此基础上我对起进行了一些内容的增加,代码如下,(Makefile文件与上次一样,地址:http://blog.chinaunix.net/u2/77929/showart_2193865.html)


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/pid_namespace.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/rculist.h>
#include <linux/init_task.h>
#include <linux/syscalls.h>
#include <linux/slab.h>
#include <linux/bootmem.h>
#include <linux/pid_namespace.h>
#include <linux/list.h>

static pid_t pid=1;
int add(int i,int j)
{
    return (i+j);    
}
int fun()
{
    printk(KERN_INFO "this is fun() function\n");
      return 0;
}
static int __init hello_init(void)
{
      struct task_struct *taskss,*cur_task;
    taskss = pid_task(find_pid_ns(pid, current->nsproxy->pid_ns), PIDTYPE_PID);//获取进程块    

    printk("Current pid is %d,Parent pid is %d\n",taskss->parent->pid,taskss->pid);
    
    cur_task=pid_task(find_get_pid(pid),PIDTYPE_PID); //获取进程快


    if(cur_task) {
     printk("The current process info:\n");
     printk("pid=%d,state=%ld\n",cur_task->pid,cur_task->state);
    }

    for_each_process(cur_task) {
        printk("Each process info:\n");
        printk("pid=%d,state=%ld\n",cur_task->pid,cur_task->state);
    }
    fun();
    printk("5+6=%d",add(5+6));
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "Leaving the kernel!Byebye!\n");
}

MODULE_LICENSE("GPL");//Check if the code support the protocol

module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("email:ceaglechina@gmail.com");


阅读(1272) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~