Linux用户空间访问设备

Linux下设备也被当作文件进行访问,Linux用户空间访问设备主要有3种方式。

1.查询方式

2.poll方式

3.select方式

1.查询方式

查询方式为最简单的方式,也是效率最低的方式。

函数原型:

int open(const char *pathname, int flags);

flats:O_RDONLY, O_WRONLY, O_RDWR

ssize_t read(int fd, void *buf, size_t count);

读成功返回读的字节数。

ssize_t write(int fd, const void *buf, size_t count);

写成功返回写的字节数。

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
	char capacity[16] = {0};
	int fd, ret;

	fd = open("/sys/class/power_supply/max1720x_battery/capacity", O_RDONLY);

	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	while (1) {
		read(fd, &capacity, sizeof(capacity));
		printf("capacity = %s\n", capacity);
		sleep(1);
	}

	close(fd);
	return EXIT_SUCCESS;
}

2.poll方式

poll方式查询是否可对设备进行无阻塞的访问,如果不可访问,则会睡眠在等待队列上。

数据结构:

struct pollfd {
               int   fd;         /* file descriptor */
               short events;     /* requested events */
               short revents;    /* returned events */
           };


常用events:POLLIN,POLLOUT,POLLRDNORM,POLLWRNORM

注意:POLLIN和POLLRDNORM配合使用,POLLOUT和POLLWRNORM配合使用

函数原型:

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

#include <assert.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)
{
	char capacity[16] = {0};
	int fd, ret;
	struct pollfd pfd;

	fd = open("/sys/class/power_supply/max1720x_battery/capacity", O_RDONLY);

	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	pfd.fd = fd;
	pfd.events = (POLLIN | POLLRDNORM);

	while (1) {
		puts("Starting poll...");

		ret = poll(&pfd, (unsigned long)1, 5000);   //wait for 5 secs

		if (ret < 0) {
			perror("poll");
		}

		if ((pfd.revents & POLLIN) == POLLIN) {
			read(pfd.fd, &capacity, sizeof(capacity));
			printf("POLLIN : capacity = %s\n", capacity);
		}
	}

	close(fd);
	return EXIT_SUCCESS;
}

3.select方式

select方式查询是否可对设备进行无阻塞的访问,如果不可访问,则会睡眠在等待队列上。

函数原型:

int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);

void FD_CLR(int fd, fd_set *set);
int  FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);

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

int main(int argc, char **argv)
{
	char capacity[16] = {0};
	int fd, ret;
	fd_set fds;

	fd = open("/sys/class/power_supply/max1720x_battery/capacity", O_RDONLY);

	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}


	while (1) {
		puts("Starting select...");

		FD_ZERO(&fds);
		FD_SET(fd, &fds);

		ret = select(1, &fds, NULL, NULL, NULL);

		if (ret < 0) {
			perror("select");
		}

		if (FD_ISSET(fd, &fds)) {
			read(fd, &capacity, sizeof(capacity));
			printf("SELECT : capacity = %s\n", capacity);
		}
	}
	close(fd);
	return EXIT_SUCCESS;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux操作系统中有两个主要的运行空间用户空间和内核空间。 1. 用户空间(User Space):用户空间是操作系统中用于运行用户程序的区域。在用户空间中,用户可以执行各种应用程序,如文本编辑器、浏览器、音乐播放器等。用户空间提供了一系列的系统调用(system call)接口,允许应用程序与底层的操作系统内核进行交互。用户空间通常拥有较低的权限,不能直接访问和操作硬件资源。 2. 内核空间(Kernel Space):内核空间是操作系统内核运行的区域,它是操作系统的核心部分。在内核空间中,操作系统直接控制着硬件资源,如CPU、内存、设备驱动等。内核提供了各种系统服务,如进程管理、文件系统、网络协议栈等。与用户空间相比,内核空间拥有更高的权限,能够执行特权指令并直接访问硬件资源。 用户空间和内核空间之间通过系统调用进行通信。当应用程序需要执行一些特权操作时(例如读写硬件设备、创建新进程),它会通过系统调用请求内核帮助执行这些操作。内核会在接收到系统调用请求后,检查请求的合法性,并在必要时执行相应的操作,然后将结果返回给用户空间用户空间和内核空间的划分是为了提高系统的安全性和稳定性。通过将用户程序与操作系统内核隔离开来,可以防止恶意程序对系统的破坏,并确保操作系统的正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值