Loading

应用层write调用驱动层write

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

image

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

image

posted @ 2021-09-02 23:18  鱼儿叁  阅读(109)  评论(0编辑  收藏  举报