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为此设备号
insmod helloDev.ko
gcc test.c
./a.out