Linux使用open函数无法打开驱动

测试程序如下:

#include 
#include 
#define MEM_CLEAR 1
int main()
{
	int dev_fd;
	char read_buf[30];
	char write_buf[]="hello how are you";
	dev_fd = open("/dev/globalmem", O_RDWR | O_NONBLOCK);
	if(dev_fd==-1)
	{
		printf("cannot open file /dev/globalmem\n");
		exit(1);
	}
	ioctl(dev_fd, MEM_CLEAR, 0);
	lseek(dev_fd, 0, 0);
	write(dev_fd, write_buf, 20);
	lseek(dev_fd, 0, 0);
	read(dev_fd, read_buf, 20);
	printf("read_buf=%s\n", read_buf);
	close(dev_fd);
	return 0;
}

每次运行之后都输出 cannot open file /dev/globalmem

修改驱动的文件访问权限

sudo chmod 777 /dev/globalmem

再运行测试文件,成功

参考文章:https://blog.csdn.net/a29562268/article/details/78443087

你可能感兴趣的:(Linux)