宣布:

static char status[128] = "off\0";

并实现了一个read函数:
static ssize_t read_proc(struct file *filep, char __user *buf,
                    size_t len, loff_t *offp)
{
    ssize_t cnt = strlen(status), ret;

    ret = copy_to_user(buf, status, cnt);
    *offp += cnt;
    return cnt;
}

如何考虑offp
目前它无休止地将status打印到屏幕上

最佳答案

感谢各位的评论,我提出了以下实现,我相信这是使用offp的正确方法:

static ssize_t read_proc(struct file *filep, char __user *buf,
                    size_t len, loff_t *offp)
{

    ssize_t cnt = strlen(status), ret;

    /* ret contains the amount of chare wasn't successfully written to `buf` */
    ret = copy_to_user(buf, status, cnt);
    *offp += cnt - ret;

    /* Making sure there are no left bytes of data to send user */
    if (*offp > cnt)
         return 0;
    else
         return cnt;
}

关于c - 如何在Linux内核读取功能中将字符串copy_to_user和使用offp,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21032073/

10-15 23:08