首先通过自定义数据结构,对缓冲区做几个基本的指针和参数进行定义:
char * buffer_start, *buffer_end 指向buffer起始端和结束端的指针
char *wp ,*rp 数据的读写指针
int buffersize buffer大小
调用内存分配函数kmalloc函数,为该数据结构申请内存空间,初始化结束后,数据的读写指针都指向char *buffer_star,对于缓冲区,我们可以做一下几个rules:
1. *wp = *rp :这个数据缓冲区是空的。对于读操作,遇到这种情况读操作应该会被阻塞,无数据可读,读进程进入睡眠等待状态;对于写操作,写睡眠将被唤醒,可写入的大小为整个buffer空间的大小
2. *wp > *rp :缓冲区有数据可读,可读大小为wp-rp,读进程不会不会被阻塞,而wp-rp=buffersize时,写进程被阻塞进入睡眠,若wp-rp<buffersize时,写进程不会被阻塞,buffer还有空间可以写入
3. *wp< *rp: 如果wp rp指向buffer_end的时候,会自动反转到buffer_start位置,可写空间为rp-wp-1
通过阻塞和睡眠机制,我们可以实现对这个buffer的读写的同步,下面还是以代码的方式讲解一下读写同步的原理:
static ssize_t scull_p_read (struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
struct scull_pipe *dev = filp->private_data;
if (down_interruptible(&dev->sem)) 锁定信号量
return -ERESTARTSYS;
while (dev->rp == dev->wp) { /* nothing to read */ 此时缓冲区为空,无数据可读
up(&dev->sem); /* release the lock */ /*解锁信号量,注意:必须在进入阻塞睡眠之前解 锁信号量,准备进入睡眠*/
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
PDEBUG("/"%s/" reading: going to sleep/n", current->comm);
if (wait_event_interruptible(dev->inq, (dev->rp != dev->wp))) /* 阻塞,进入睡眠,当dev->rp != dev->wp这个条件被满足的时候,唤醒睡眠,这个睡眠应 该 在 写操作中被唤醒*/
return -ERESTARTSYS; /* signal: tell the fs layer to handle it */
/* otherwise loop, but first reacquire the lock */
if (down_interruptible(&dev->sem)) /* 如果被唤醒,则重新锁定信号量,进行数据读取*/
return -ERESTARTSYS;
}
/* ok, data is there, return something */
if (dev->wp > dev->rp)
count = min(count, (size_t)(dev->wp - dev->rp));
else /* the write pointer has wrapped, return data up to dev->end */
count = min(count, (size_t)(dev->end - dev->rp));
if (copy_to_user(buf, dev->rp, count)) { /*i在rp>wp情况下,本次操作不能一次性读取buffer里面所有的数据*/
up (&dev->sem); /*必须分两次读取,第一次只读到end-rp,第二次读到wp-start*/
return -EFAULT;
}
dev->rp += count; /*count值已经被处理过,保证dev->rp += count不会超过buffer_end*/
if (dev->rp == dev->end)
dev->rp = dev->buffer; /* wrapped */
up (&dev->sem);
/* finally, awake any writers and return */
wake_up_interruptible(&dev->outq); /*读取结束后完成指针的更新,唤醒写睡眠*/
PDEBUG("/"%s/" did read %li bytes/n",current->comm, (long)count);
return count;
}
static ssize_t scull_p_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct scull_pipe *dev = filp->private_data;
int result;
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
/* Make sure there's space to write */
result = scull_getwritespace(dev, filp); /*测试是否还有可写入的空间*/
if (result)
return result; /* scull_getwritespace called up(&dev->sem) */
/* ok, space is there, accept something */
count = min(count, (size_t)spacefree(dev)); /*如果有,察看还有多少空间可写*/
if (dev->wp >= dev->rp)
count = min(count, (size_t)(dev->end - dev->wp)); /* to end-of-buf */ /*似乎还有一小段空间没有写入*/
else /* the write pointer has wrapped, fill up to rp-1 */
count = min(count, (size_t)(dev->rp - dev->wp - 1));
PDEBUG("Going to accept %li bytes to %p from %p/n", (long)count, dev->wp, buf);
if (copy_from_user(dev->wp, buf, count)) {
up (&dev->sem);
return -EFAULT;
}
dev->wp += count;
if (dev->wp == dev->end)
dev->wp = dev->buffer; /* wrapped */ /*更新写指针*/
up(&dev->sem);
/* finally, awake any reader */
wake_up_interruptible(&dev->inq); /*写完之后必定有数据可读,唤醒读睡眠*/
/* and signal asynchronous readers, explained late in chapter 5 */
if (dev->async_queue)
kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
PDEBUG("/"%s/" did write %li bytes/n",current->comm, (long)count);
return count;
}