阅读:1366回复:0
以前编的一个内存共享的驱动,在redhat7.3下可以用,而在9下就不能编译了
如题,本人是个新手,不知道哪里的设置不对
这是用sys_shmat实现的内存共享,怎么实现访问互斥呢?dx指点一下 #ifndef __KERNEL__ # define __KERNEL__ #endif #ifndef MODULE # define MODULE #endif #define __KERNEL_SYSCALLS__ #include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/timer.h> #include <asm/byteorder.h> #include <asm/uaccess.h> #include <linux/shm.h> #include <linux/skbuff.h> #include <linux/netdevice.h> #include <linux/in.h> #include <linux/if_ether.h> //#include <sys/ipc.h> //#include <linux/types.h> //#include <unistd.h> typedef struct { unsigned int data; unsigned int flag; }_Packetstr; #define WRITE_DATA 1 #define READ_DATA 2 #define DEVICE_NAME \"/dev/mydev\" int shmid_a; _Packetstr *share_map; static int Major = 252; int init_sharemem(){ unsigned long pnull; pnull=0; shmid_a=sys_shmget((key_t)88088,sizeof(_Packetstr),SHM_R | SHM_W | IPC_CREAT); if (shmid_a==-1){ printk(\"create share memory faile.\\n\"); return 0; } share_map=(_Packetstr*)0; sys_shmat(shmid_a,(void *)pnull,SHM_RND,(unsigned long *)&share_map); if (share_map==(void *)0){ printk(\"map a share memory faile.\\n\"); return 0; } share_map->data = 1; share_map->flag = 2; return 1; } void free_sharemem(){ if (sys_shmdt((void *)share_map) == -1){ printk(\"free share memory faile.\\n\"); } if (sys_shmctl(shmid_a,IPC_RMID, 0) == -1){ printk(\"free share memory id faile.\\n\"); } } static int mydev_open(struct inode *inode, struct file *file) { return 0; } static int mydev_release(struct inode *inode, struct file *file) { return 0; } static int mydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { int retval; switch(cmd){ case WRITE_DATA: retval = 1; break; case READ_DATA: retval = 2; break; default: retval = 0; } return retval; } static struct file_operations mydev_fops = { open : mydev_open, release: mydev_release, ioctl: mydev_ioctl, }; static int mydev_init_module(void) { int rc; SET_MODULE_OWNER(&mydev_fops); rc = register_chrdev(Major, DEVICE_NAME, &mydev_fops); if (rc < 0) { printk(\"mydev: Failed to register device.\\n\"); return rc; } if(Major == 0) Major = rc; printk(\"mydev: Registration success.\\n\"); init_sharemem(); return 0; } static void mydev_exit_module(void) { int rc; rc = unregister_chrdev(Major, DEVICE_NAME); if (rc < 0) printk(\"mydev: unregister failed\\n\"); else printk(\"mydev: unregister success\\n\"); free_sharemem(); } module_init(mydev_init_module); module_exit(mydev_exit_module); |
|
|