atuhappy
驱动老牛
驱动老牛
  • 注册日期2002-03-15
  • 最后登录2009-09-09
  • 粉丝0
  • 关注0
  • 积分8分
  • 威望21点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1378回复:0

以前编的一个内存共享的驱动,在redhat7.3下可以用,而在9下就不能编译了

楼主#
更多 发布于:2005-04-19 20:25
如题,本人是个新手,不知道哪里的设置不对
这是用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);


在一回首间,才忽然发现,原来,我一生的种种努力,不过只是为了要使周遭的人都对我满意而已。为了要博得他人的称许和微笑,我战战兢兢得将自己套入所有得模式,所有的桎梏。走到中途,才忽然发现,我只剩下一副模糊得面目,和一条不能回头的路...
游客

返回顶部