美文网首页首页投稿(暂停使用,暂停投稿)
Linux内核--其它几个特殊的字符设备

Linux内核--其它几个特殊的字符设备

作者: shenyifu | 来源:发表于2016-05-16 14:11 被阅读91次

/dev/null

/dev/null是个黑洞设备,它丢弃一切写入其中的数据,空设备通常被用于丢弃不需要的输出流。任何写入该设备数据都会被丢弃掉。从这个里面读取数据返回空。

其他部分请参考我Linux内核--简单的字符设备scull

……
int scull_open(struct inode *inode , struct file *filp)
{
  return 0; /* success */
}
int scull_release(struct inode *inode , struct file *filp)
{
  return 0;
}
/*
* Data management: read and write
*/
ssize_t scull_read(struct file *filp , char __user *buf, size_t count, loff_t *f_pos)
{
  return 0;
}
ssize_t scull_write(struct file *filp , const char __user *buf,size_t count ,loff_t *f_pos)
{
  return count;
}
……

/dev/full

/dev/full(常满设备)是一个特殊设备文件,总是在向其写入时返回设备无剩余空间(错误码为ENOSPC),读取时则与/dev/zero相似,返回无限的空字符(NULL, ASCII NUL, 0x00)。这个设备通常被用来测试程序在遇到磁盘无剩余空间错误时的行为。
/dev/zero 是一个特殊的文件,当你读它的时候,它会提供无限的空字符(NULL, ASCII NUL, 0x00)。其中的一个典型用法是用它提供的字符流来覆盖信息,另一个常见用法是产生一个特定大小的空白文件。

……
int scull_open(struct inode *inode , struct file *filp)
{
  return 0; /* success */
}
int scull_release(struct inode *inode , struct file *filp)
{
  return 0;
}
/*
* Data management: read and write
*/
ssize_t scull_read(struct file *filp , char __user *buf, size_t count, loff_t *f_pos)
{
  size_t i;
  for(i = 0; i < count; i++){
    buf[i] = 0;
  }
  return count;
}
ssize_t scull_write(struct file *filp , const char __user *buf,size_t count ,loff_t *f_pos)
{
  return ENOSPC;
}
……

/dev/zero

/dev/zero 是一个特殊的文件,当你读它的时候,它会提供无限的空字符(NULL, ASCII NUL, 0x00)。其中的一个典型用法是用它提供的字符流来覆盖信息,另一个常见用法是产生一个特定大小的空白文件。

……
int scull_open(struct inode *inode , struct file *filp)
{
  return 0; /* success */
}
int scull_release(struct inode *inode , struct file *filp)
{
  return 0;
}
/*
* Data management: read and write
*/
ssize_t scull_read(struct file *filp , char __user *buf, size_t count, loff_t *f_pos)
{
  size_t i;
  for(i = 0; i < count; i++){
    buf[i] = 0;
  }
  return count;
}
ssize_t scull_write(struct file *filp , const char __user *buf,size_t count ,loff_t *f_pos)
{
  return count;
}
……

相关文章

网友评论

    本文标题:Linux内核--其它几个特殊的字符设备

    本文链接:https://www.haomeiwen.com/subject/xlebrttx.html