首页 > 其他 > 详细

应用层write调用驱动层write

时间:2021-09-03 23:50:01      阅读:28      评论:0      收藏:0      [点我收藏+]

test.c

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>

#define DATA_NUM (64)

int main(int argc, char *argv[])
{
    int fd, i;
    int r_len, w_len;
    fd_set fdset;
    char buf[DATA_NUM] = {0};
    memset(buf, 0, sizeof(buf));
    fd = open("/dev/hello", O_RDWR);                /* 调用字符设备open函数 */
    if ( -1 == fd )
    {
        perror("open file error\r\n");
        return -1;
    }
    else
    {
        printf("open successe\r\n");
    }

    w_len = write(fd, buf, DATA_NUM);               /* 调用字符设备write函数 */
    r_len = read(fd, buf, DATA_NUM);                /* 调用字符设备read函数 */
    printf("%d %d \r\n", w_len, r_len);
    printf("%s \r\n", buf);

    return 0;
}

一、创建字符驱动

#mknode /dev/hello c 232 0
c表示字符类型,232为主设备号,0为此设备号

二、装载上一章编写的helloDev.ko的驱动

insmod helloDev.ko

三、编译上面的test.c代码并执行

gcc test.c

./a.out

技术分享图片

四、查看dmesg信息,可以看到内核打印函数 printk() 的输出信息

技术分享图片

应用层write调用驱动层write

原文:https://www.cnblogs.com/yuersan/p/15221288.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!