代码:客户端与字符设备交互代码

来源:互联网 发布:小型机房网络拓扑 编辑:程序博客网 时间:2024/03/29 14:42


#include <stdio.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <unistd.h>#define BUF_SIZE  60#define MEM_CLEAR    0x1#define MEM_PRINTER    0x2int main(void){int fd;int len;int ret;char buf1[BUF_SIZE] = "hello";char buf2[BUF_SIZE] = " world";fd = open("/dev/virtdev", O_RDWR);if (fd < 0){perror("open error");return -1;}len = write(fd, buf1, strlen(buf1));len = write(fd, buf2, strlen(buf2));lseek(fd, 0, SEEK_SET);len = read(fd, buf1, BUF_SIZE);printf("the string is : %s\n", buf1);ret = ioctl(fd, MEM_CLEAR, NULL);if (ret == -1){perror("ioctl error");return -1;}return 0;}


运行结果:

allen@allen-lql ~/workspace/mytest/virtdev_test/client $ ./usrthe string is : hello worldallen@allen-lql ~/workspace/mytest/virtdev_test/client $ dmesg[43726.330077] write 5 byte(s) from 0[43726.330080] write 6 byte(s) from 5[43726.330083] read 60 byte(s) from 0[43726.330114] virtdev is set to zero.


0 0