字符设备驱动之循环缓冲队列+读写等待

本文介绍了一段使用Linux内核实现的缓冲区读写操作代码,包括初始化、读取和写入等功能,并通过两个示例程序演示了如何在用户空间和内核空间进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 buffer.c

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/mach/irq.h>
#include <asm/arch/regs-gpio.h>


static int major = 0;
static struct class *cls;

#define BUF_LEN   10
static char kern_buf[BUF_LEN];
static volatile int r = 0, w = 0;

static wait_queue_head_t read_waitq;
static wait_queue_head_t write_waitq;


static int isEmpty(void)
{
 return (r == w);
}

static int isFull(void)
{
 return (r == ((w+1)%BUF_LEN));
}

static int putData(char val)
{
 if (isFull())
 {
  return -1;
 }
 else
 {
  kern_buf[w] = val;
  w = (w+1)%BUF_LEN;
  return 0;
 }
}


static int getData(char *p)
{
 if (isEmpty())
 {
  return -1;
 }
 else
 {
  *p = kern_buf[r];
  r = (r+1)%BUF_LEN;
  return 0;
 }
}


ssize_t buffer_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
 int i = 0;
 char val;
 
 do {

  wait_event_interruptible(read_waitq, !isEmpty());

  while ((i < size) && getData(&val) == 0)
  {
   copy_to_user(buf+i, &val, 1);
   wake_up_interruptible(&write_waitq);
   i++;
  }  
  
 } while (i < size);
 
 return size;
}

ssize_t buffer_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
 int i = 0;
 char val;
 
 do {

  wait_event_interruptible(write_waitq, !isFull());

  while ((i < size) && !isFull())
  {
   copy_from_user(&val, buf+i, 1);
   putData(val);
   i++;
   wake_up_interruptible(&read_waitq);
  }  
  
 } while (i < size);
 
 return size;
}

static struct file_operations buffer_fops = {
 .owner   =  THIS_MODULE,
 .read    =  buffer_read,
 .write   =  buffer_write,
};

int buffer_init(void)
{
 major = register_chrdev(0, "buffer", &buffer_fops);

 /* sysfs  ==> 挂接到/sys */
 cls = class_create(THIS_MODULE, "buffer_class");
 class_device_create(cls, NULL, MKDEV(major, 0), NULL, "buffer");

 // mdev会根据/sys下的这些内容创建/dev/buffer
 init_waitqueue_head(&read_waitq);
 init_waitqueue_head(&write_waitq);
 
 return 0;
}
void buffer_exit(void)
{
 unregister_chrdev(major, "buffer");

 class_device_destroy(cls, MKDEV(major, 0));
 class_destroy(cls);
}

module_init(buffer_init);
module_exit(buffer_exit);

MODULE_LICENSE("GPL"); 

reader.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv)
{
 int fd;
 char buf[10];
 int i;

 fd = open("/dev/buffer", O_RDONLY);

 read(fd, buf, 10);

 printf("data: ");
 for (i = 0; i < 10; i++)
  printf("%c ", buf[i]);
 printf("\n"); 
 
 return 0;
}

writer.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

/* writer string */
int main(int argc, char **argv)
{
 int fd;
 char buf[10];
 int i;

 if (argc != 2)
 {
  printf("Usage: \n");
  printf("%s string\n", argv[0]);
  return 0;
 }

 fd = open("/dev/buffer", O_WRONLY);

 write(fd, argv[1], strlen(argv[1])); 
 
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值