当前位置: 技术问答>linux和unix
fasync 入门问题,大家指教
来源: 互联网 发布时间:2016-07-23
本文导语: #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME "chardev" static int chardev_open(struct inode *inodp, struct file *filp); static ssize_t chardev_read(struct file *filp, char __user *buff, size_t...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "chardev"
static int chardev_open(struct inode *inodp, struct file *filp);
static ssize_t chardev_read(struct file *filp, char __user *buff, size_t count, loff_t *f_pos);
static ssize_t chardev_write(struct file *filp, const char __user *buff, size_t count, loff_t *f_pos);
static int chardev_fasync(int fd, struct file *filp, int mode);
static int chardev_release(struct inode *inodp, struct file *filp);
static struct file_operations chardev_fops = {
.open = chardev_open,
.read = chardev_read,
.write = chardev_write,
.fasync = chardev_fasync,
.release = chardev_release,
};
static struct fasync_struct *async = NULL;
static struct semaphore sem;
static struct cdev *cdevp;
static dev_t devno;
static char buffer[8192];
static int chardev_open(struct inode *inodp, struct file *filp)
{
return 0;
}
static ssize_t chardev_read(struct file *filp, char __user *buff, size_t count, loff_t *f_pos)
{
if (down_interruptible(&sem))
goto err;
count = strlen(buffer);
copy_to_user(buff, buffer, count);
printk("""read buff is %sn",buffer);
up(&sem);
return count;
err:
return -ERESTARTSYS;
}
static ssize_t chardev_write(struct file *filp, const char __user *buff, size_t count, loff_t *f_pos)
{
if (down_interruptible(&sem))
goto err;
memset(buffer, '', sizeof(buffer));
copy_from_user(buffer, buff, count);
printk("""write buff is %sn",buffer);
up(&sem);
if (async)
kill_fasync(&async, SIGIO, POLL_IN);
return count;
err:
return -ERESTARTSYS;
}
static int chardev_fasync(int fd, struct file *filp, int mode)
{
//printk("""nin chardev_fasyncn");
dump_stack();
return fasync_helper(fd, filp, mode, &async);
}
static int chardev_release(struct inode *inodp, struct file *filp)
{
//printk("""nin chardev_releasen");
//printk("""nin chardev_release twicen");
dump_stack();
return chardev_fasync(-1, filp, 0);
}
static int __init chardev_init(void)
{
int ret;
ret = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
if (ret
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!