�腋崭湛�始��写linux���������������冉��烦�����撕枚����禄���没�薪��������指��忙���一�掳伞�谢谢���浅���谢��源�������拢�
//�������目��:
//����:
//����:2005��
#ifndef _KERNEL_
#define _KERNEL_
#endif 
#ifndef MODULE
#define MODULE
#endif
#include<linux/config.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
#ifdef CONFIG_SMP
#define _SMP_
#endif
#define _NO_VERSION_
#include<linux/version.h>
#include<linux/kernel.h>

#include<linux/types.h>
#include<linux/fs.h>
#include<linux/mm.h>
#include<linux/errno.h>
#include<asm/segment.h>
char kernel_version[]=UTS_RELEASE;

unsigned int test_major=0;

int init_module(void);
void cleanup_module(void);
int read_test(struct file * ,char * ,size_t ,loff_t * );
int write_test(struct file *,const char * ,size_t ,loff_t * );
int open_test(struct inode * ,struct file* );
void release_test(struct inode * ,struct file*);

int read_test(struct file * file,char * buf,size_t count,loff_t * test)
{
    int i;
    for(i=0;i<10;i++)
        printk("<0>This is the %d times\n",i);
    return 0;
};

int write_test(struct file *file,const char * buf ,size_t count,loff_t * test)
{
    printk("<0>This is inside the write_test function:%d\n",count);
    return 0;
};


int open_test(struct inode * node,struct file* file)
{
    //MOD_INC_USE_COUNT;
    printk("<0>This is inside the open_test function\n");
    return 0;
};

void release_test(struct inode * node ,struct file* file)
{
    //MOD_DEC_USE_COUNT;
    printk("<0>This is inside the release_test function\n");    
};

struct file_operations test_ops=
{
    NULL,
    NULL,
    read_test,
    write_test,
    NULL,
    NULL,
    NULL,
    NULL,
    open_test,
    NULL,
    release_test
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
};

int init_module(void)
{
    printk("<0>This is the init part!\n");
    int result;
    result=register_chrdev(0,"great",&test_ops);
    if(result<0)
    {
        printk("<0>test :Can't get major number\n");
        return result;
    }
    if(test_major==0) test_major=result;
    return 0;
}

void cleanup_module(void)
{
    printk("<0>This is inside the cleanup_module function!\n");
    unregister_chrdev(test_major,"great");
}
注��
�诤�帽9��������������:gcc -c test.c -I/usr/src/linux-2.4.20-8/include
������示���拢�In file included from /usr/src/linux-2.4.20-8/include/linux/sched.h:16,
                 from /usr/src/linux-2.4.20-8/include/linux/mm.h:22,
                 from org.c:22:
/usr/src/linux-2.4.20-8/include/linux/timex.h:173: field `time' has incomplete type
In file included from /usr/src/linux-2.4.20-8/include/linux/sched.h:23,
                 from /usr/src/linux-2.4.20-8/include/linux/mm.h:22,
                 from org.c:22:
/usr/src/linux-2.4.20-8/include/asm/mmu.h:12: field `sem' has incomplete type
In file included from /usr/src/linux-2.4.20-8/include/linux/sched.h:31,
                 from /usr/src/linux-2.4.20-8/include/linux/mm.h:22,
                 from org.c:22:
/usr/src/linux-2.4.20-8/include/linux/pid.h:18: field `task_list' has incomplete type
/usr/src/linux-2.4.20-8/include/linux/pid.h:19: field `hash_chain' has incomplete type
/usr/src/linux-2.4.20-8/include/linux/pid.h:24: field `pid_chain' has incomplete type
/usr/src/linux-2.4.20-8/include/linux/pid.h:36: parse error before '(' token
/usr/src/linux-2.4.20-8/include/linux/pid.h:38: parse error before '(' token
/usr/src/linux-2.4.20-8/include/linux/pid.h:43: parse error before '(' token
/usr/src/linux-2.4.20-8/include/linux/pid.h:49: parse error before '(' token
/usr/src/linux-2.4.20-8/include/linux/pid.h:52: parse error before '(' token
org.c:31: warning: `struct file' declared inside parameter list
org.c:31: warning: its scope is only this definition or declaration, which is probably not what you want
org.c:32: warning: `struct file' declared inside parameter list
org.c:33: warning: `struct file' declared inside parameter list
org.c:33: warning: `struct inode' declared inside parameter list
org.c:34: warning: `struct file' declared inside parameter list
org.c:34: warning: `struct inode' declared inside parameter list
org.c:36: warning: `struct file' declared inside parameter list
org.c:37: conflicting types for `read_test'
org.c:31: previous declaration of `read_test'
org.c:44: warning: `struct file' declared inside parameter list
org.c:45: conflicting types for `write_test'
org.c:32: previous declaration of `write_test'
org.c:51: warning: `struct file' declared inside parameter list
org.c:51: warning: `struct inode' declared inside parameter list
org.c:52: conflicting types for `open_test'
org.c:33: previous declaration of `open_test'
org.c:58: warning: `struct file' declared inside parameter list
org.c:58: warning: `struct inode' declared inside parameter list
org.c:59: conflicting types for `release_test'
org.c:34: previous declaration of `release_test'
org.c:64: variable `test_ops' has initializer but incomplete type
org.c:66: warning: excess elements in struct initializer
org.c:66: warning: (near initialization for `test_ops')
org.c:67: warning: excess elements in struct initializer
org.c:67: warning: (near initialization for `test_ops')
org.c:68: warning: excess elements in struct initializer
org.c:68: warning: (near initialization for `test_ops')
org.c:69: warning: excess elements in struct initializer
org.c:69: warning: (near initialization for `test_ops')
org.c:70: warning: excess elements in struct initializer
org.c:70: warning: (near initialization for `test_ops')
org.c:71: warning: excess elements in struct initializer
org.c:71: warning: (near initialization for `test_ops')
org.c:72: warning: excess elements in struct initializer
org.c:72: warning: (near initialization for `test_ops')
org.c:73: warning: excess elements in struct initializer
org.c:73: warning: (near initialization for `test_ops')
org.c:74: warning: excess elements in struct initializer
org.c:74: warning: (near initialization for `test_ops')
org.c:75: warning: excess elements in struct initializer
org.c:75: warning: (near initialization for `test_ops')
org.c:77: too few arguments to function `release_test'
org.c:77: warning: excess elements in struct initializer
org.c:77: warning: (near initialization for `test_ops')
org.c:78: warning: excess elements in struct initializer
org.c:78: warning: (near initialization for `test_ops')
org.c:79: warning: excess elements in struct initializer
org.c:79: warning: (near initialization for `test_ops')
org.c:80: warning: excess elements in struct initializer
org.c:80: warning: (near initialization for `test_ops')
org.c:81: warning: excess elements in struct initializer
org.c:81: warning: (near initialization for `test_ops')
org.c:82: warning: excess elements in struct initializer
org.c:82: warning: (near initialization for `test_ops')
org.c:83: warning: excess elements in struct initializer
org.c:83: warning: (near initialization for `test_ops')
org.c:64: storage size of `test_ops' isn't known
����指��伞��巍��������������������浅���谢��
--

�� ��源:��BBS 水木�华站 http://smth.org��[FROM: 210.72.13.*]