2013-10-13 实验之内核空间与用户空间拷贝结构体

实验描述:内核空间与用户空间拷贝结构体

注意事项:char *和char[]的不同

内核版本:Linux 2.6.38

开发板:    Mini 6410


驱动程序:

#include <linux/init.h>  
#include <linux/module.h>  
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/timer.h>
#include <asm/uaccess.h>
#include <asm/io.h> 
#include <linux/fs.h>


/*
      Kernel Version: Linux 2.6.38
      Arm Version: Mini 6410
*/


#define MyPrintk  printk


struct my_data_structure
{
	unsigned int number;
	char name[20];
	unsigned long count;
};


static dev_t  Leds_Major ;
static char * DEVICE_NAME = "DataTransfemission";
static struct class *leds_class;
static struct my_data_structure my_own_data[] = {
			{1,"Jack",0},
			{2,"Tom",0},
			{3,"Kobe",0},
			{4,"Xiaohong",0}
	};


static ssize_t data_read(struct file *file, char __user * buffer, size_t count, loff_t *pos)
{


	/*struct my_data_structure temp_data= 
			{6,"Jack",0};*/
	int ret = copy_to_user(buffer,(void *)&my_own_data[1], sizeof(struct my_data_structure));
	if (ret != 0){
		MyPrintk("read error\n");
	}
	return 0;
}


static ssize_t data_write(struct file *flie, const char __user * buffer, size_t count, loff_t *pos)
{
	struct my_data_structure user_data;
	int ret = copy_from_user((void *)&user_data, buffer, count);
	if (ret != 0){
		MyPrintk("write error\n");
	}
	MyPrintk (KERN_EMERG "Kernel from user data: %i, %s, %lu\n", user_data.number, 
			user_data.name, user_data.count);  
	return 0;
}


static int data_open(struct file *file, struct node *nodes)
{
	return 0;
}


static struct file_operations  s3c64XX_leds_fops = {
	.owner = THIS_MODULE,
	.read = data_read,
	.write = data_write,
	.open = data_open,
};


static int myleds_init(void)  
{   
	Leds_Major = register_chrdev(Leds_Major,DEVICE_NAME , &s3c64XX_leds_fops);
	if(Leds_Major < 0){
   	  	 MyPrintk (KERN_EMERG "Sorry, Can not register the data trsanmission device!\n");  
   	} 
	MyPrintk (KERN_EMERG " Register the data trsanmission leds device\n");   


	leds_class = class_create(THIS_MODULE, DEVICE_NAME); 
	device_create(leds_class, NULL , MKDEV(Leds_Major, 0), NULL,DEVICE_NAME);
	return 0;  
}  


static void myleds_exit(void)  
{   
	unregister_chrdev(Leds_Major, DEVICE_NAME);
  	device_destroy(leds_class,  MKDEV(Leds_Major, 0));	
	class_destroy(leds_class);
  	MyPrintk (KERN_EMERG "Data transmission Linux Byebye\n");  
}  
  
module_init(myleds_init);  
module_exit(myleds_exit);
MODULE_LICENSE("GPL");  

应用程序:

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include <stdlib.h>

struct my_data_structure
{
	unsigned int number;
	char name[20];
	unsigned long count;
};

int main(int argc, char **argv)
{
	int fd;
	int ret;
	fd = open("/dev/DataTransfemission", O_RDWR);
	if (fd < 0){
		printf("Sorry, can't open!\n");
	}
	
	struct my_data_structure my_write_data = {6,"weipeng",1};
	printf("struct sizeof %d, %d\n", sizeof(my_write_data ), sizeof(struct my_data_structure ));
	ret = write(fd, &my_write_data , sizeof(my_write_data));
	if(ret < 0){
		printf("Sorry, write2 error!\n");
	}

	struct my_data_structure *my_write_data2 = (struct my_data_structure*)malloc(sizeof(struct my_data_structure));
	printf("struct* sizeof %d, %d\n", sizeof(my_write_data2), sizeof(struct my_data_structure ));
	my_write_data2->number = 5;
	strcpy(my_write_data2->name ,"xiangwei");
	my_write_data2->count = 2;
	ret = write(fd, my_write_data2 , sizeof(struct my_data_structure));
	if(ret < 0){
		printf("Sorry, write2 error!\n");
	}

	struct my_data_structure my_read_data;
	ret = read(fd, &my_read_data , sizeof(my_read_data));
	if(ret < 0){
		printf("Sorry, read1 error!\n");
	}
	printf("my_read_data: %u, %s, %u\n", my_read_data.number, my_read_data.name, my_read_data.count);
	
	struct my_data_structure* my_read_data3 =(struct my_data_structure*)malloc(sizeof(struct my_data_structure));
	ret = read(fd, my_read_data3 , sizeof(struct my_data_structure));
	if(ret < 0){
		printf("Sorry, read3 error!\n");
	}
	printf("my_read_data3: %u, %s, %u\n", my_read_data3->number, my_read_data3->name, my_read_data3->count);

	free(my_write_data2);
	free(my_read_data3);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值