驱动开发day3 7.25

ioctl控制LED、蜂鸣器、风扇、马达 (为每一个硬件注册一个驱动)

头文件

#ifndef __HEAD_H__
#define __HEAD_H__typedef struct{volatile unsigned int MODER;volatile unsigned int OTYPER;volatile unsigned int OSPEEDR;volatile unsigned int PUPDR;volatile unsigned int IDR;volatile unsigned int ODR;
}gpio_t;#define PHY_LED1_ADDR 0x50006000
#define PHY_LED2_ADDR 0x50007000
#define PHY_LED3_ADDR 0x50006000
#define PHY_RCC_ADDR 0x50000a28//功能码
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
#endif

功能函数

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"int main(int argc, char const *argv[])
{int a;char buf[128] = {0};printf("invoking open\n");int fd = open("/dev/myled0", O_RDWR);if (fd < 0){printf("open file failed");exit(-1);}while (1){printf("请输入控制指令:0(关灯) 1(开灯)>");scanf("%d",&a);switch (a){case 1:ioctl(fd,LED_ON);break;case 0:ioctl(fd,LED_OFF);default:break;}}printf("invoking close\n");close(fd);return 0;
}

LED驱动函数

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
unsigned int *vir_moder;
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;int mycdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);unsigned long ret;if (size > sizeof(kbuf)){size = sizeof(kbuf);}ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy_to_user failed\n");return ret;}return 0;
}ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);if (size > sizeof(kbuf)){size = sizeof(kbuf);}long ret;ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy_from_user failed\n");return -EIO;}return 0;
}int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{switch (cmd){case LED_ON:switch (arg){case 1:vir_led1->ODR |= (0x1 << 10);break;case 2:vir_led2->ODR |= (0x1 << 10);break;case 3:vir_led3->ODR |= (0x1 << 8);break;default:break;}break;case LED_OFF:switch (arg){case 1:vir_led1->ODR &= (~(0x1 << 10));break;case 2:vir_led2->ODR &= (~(0x1 << 10));break;case 3:vir_led1->ODR &= (~(0x1 << 8));break;default:break;}break;default:break;}return 0;
}
struct file_operations fops = {.open = mycdev_open,.read = mycdev_read,.write = mycdev_write,.release = mycdev_close,.unlocked_ioctl = mycdev_ioctl,
};int all_led_init(void)
{// 寄存器地址的映射vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));if (vir_led1 == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));if (vir_led2 == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));if (vir_led3 == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}vir_rcc = ioremap(PHY_RCC_ADDR, 4);if (vir_rcc == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}printk("物理地址映射成功\n");// 寄存器初始化// rcc(*vir_rcc) |= (0x3 << 4);// led1vir_led1->MODER &= (~(0x3 << 20));vir_led1->MODER |= (0x1 << 20);// led2vir_led2->MODER &= (~(0x3 << 20));vir_led2->MODER |= (0x1 << 20);// led3vir_led3->MODER &= (~(0x3 << 16));vir_led3->MODER |= (0x1 << 16);printk("寄存器初始化成功\n");return 0;
}
static int __init mycdev_init(void)
{int i;// 注册字符设备驱动major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("register error\n");return major;}printk("register success\n");cls = class_create(THIS_MODULE, "myled");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功]\n");for (i = 0; i < 3; i++){dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);if (IS_ERR(dev)){printk("向上提交结点设备信息失败\n");return -PTR_ERR(dev);}}printk("向上提交设备节点成功\n");// 寄存器映射以及初始化all_led_init();return 0;
}static void __exit mycdev_exit(void)
{// 取消地址映射iounmap(vir_led1);iounmap(vir_led2);iounmap(vir_led3);// 销毁节点信息int i;for (i = 0; i < 3; i++){device_destroy(cls, MKDEV(major, i));}// 销毁目录信息class_destroy(cls);// 注销字符设备驱动unregister_chrdev(major, "mycdev");
}module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

蜂鸣器驱动函数

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
gpio_t *vir_buzzer;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;int mycdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);unsigned long ret;if (size > sizeof(kbuf)){size = sizeof(kbuf);}ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy_to_user failed\n");return ret;}return 0;
}ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);if (size > sizeof(kbuf)){size = sizeof(kbuf);}long ret;ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy_from_user failed\n");return -EIO;}return 0;
}int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{switch (cmd){case BUZZ_ON:vir_buzzer->ODR |= (0x1 << 6);break;case BUZZ_OFF:vir_buzzer->ODR &= (~(0x1 << 6));break;default:break;}return 0;
}
struct file_operations fops = {.open = mycdev_open,.read = mycdev_read,.write = mycdev_write,.release = mycdev_close,.unlocked_ioctl = mycdev_ioctl,
};int all_buzzer_init(void)
{// 寄存器地址的映射vir_buzzer = ioremap(PHY_BUZZ_ADDR, 4);if (vir_buzzer == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}vir_rcc = ioremap(PHY_RCC_ADDR, 4);if (vir_rcc == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}printk("物理地址映射成功\n");// 寄存器初始化// rcc(*vir_rcc) |= (0x1 << 1);// fanvir_buzzer->MODER &= (~(0x3 << 12));vir_buzzer->MODER |= (0x1 << 12);// vir_fan->AFRH &= (~(0xf << 4));//vir_fan->AFRH |= (0x1 << 4);printk("寄存器初始化成功\n");return 0;
}
static int __init mycdev_init(void)
{// 注册字符设备驱动major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("register error\n");return major;}printk("register success\n");cls = class_create(THIS_MODULE, "mybuzzer");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功]\n");dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mybuzzer");if (IS_ERR(dev)){printk("向上提交结点设备信息失败\n");return -PTR_ERR(dev);}
printk("向上提交设备节点成功\n");// 寄存器映射以及初始化
all_buzzer_init();return 0;
}static void __exit mycdev_exit(void)
{// 取消地址映射iounmap(vir_buzzer);// 销毁节点信息device_destroy(cls, MKDEV(major, 0));// 销毁目录信息class_destroy(cls);// 注销字符设备驱动unregister_chrdev(major, "mycdev");
}module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

风扇驱动程序

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
gpio_t *vir_fan;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;int mycdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);unsigned long ret;if (size > sizeof(kbuf)){size = sizeof(kbuf);}ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy_to_user failed\n");return ret;}return 0;
}ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);if (size > sizeof(kbuf)){size = sizeof(kbuf);}long ret;ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy_from_user failed\n");return -EIO;}return 0;
}int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{switch (cmd){case fan_ON:vir_fan->ODR |= (0x1 << 9);break;case fan_OFF:vir_fan->ODR &= (~(0x1 << 9));break;default:break;}return 0;
}
struct file_operations fops = {.open = mycdev_open,.read = mycdev_read,.write = mycdev_write,.release = mycdev_close,.unlocked_ioctl = mycdev_ioctl,
};int all_fan_init(void)
{// 寄存器地址的映射vir_fan = ioremap(PHY_FAN_ADDR, 4);if (vir_fan == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}vir_rcc = ioremap(PHY_RCC_ADDR, 4);if (vir_rcc == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}printk("物理地址映射成功\n");// 寄存器初始化// rcc(*vir_rcc) |= (0x1 << 4);// fanvir_fan->MODER &= (~(0x3 << 18));vir_fan->MODER |= (0x1 << 18);// vir_fan->AFRH &= (~(0xf << 4));//vir_fan->AFRH |= (0x1 << 4);printk("寄存器初始化成功\n");return 0;
}
static int __init mycdev_init(void)
{// 注册字符设备驱动major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("register error\n");return major;}printk("register success\n");cls = class_create(THIS_MODULE, "myfan");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功]\n");dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myfan");if (IS_ERR(dev)){printk("向上提交结点设备信息失败\n");return -PTR_ERR(dev);}
printk("向上提交设备节点成功\n");// 寄存器映射以及初始化
all_fan_init();return 0;
}static void __exit mycdev_exit(void)
{// 取消地址映射iounmap(vir_fan);// 销毁节点信息device_destroy(cls, MKDEV(major, 0));// 销毁目录信息class_destroy(cls);// 注销字符设备驱动unregister_chrdev(major, "mycdev");
}module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

马达驱动函数

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"// 定义一个变量保存主设备号
unsigned int major;
char kbuf[128] = {0};
gpio_t *vir_motor;
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;int mycdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);unsigned long ret;if (size > sizeof(kbuf)){size = sizeof(kbuf);}ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy_to_user failed\n");return ret;}return 0;
}ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *loff)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);if (size > sizeof(kbuf)){size = sizeof(kbuf);}long ret;ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy_from_user failed\n");return -EIO;}return 0;
}int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{switch (cmd){case MOTOR_ON:vir_motor->ODR |= (0x1 << 6);break;case MOTOR_OFF:vir_motor->ODR &= (~(0x1 << 6));break;default:break;}return 0;
}
struct file_operations fops = {.open = mycdev_open,.read = mycdev_read,.write = mycdev_write,.release = mycdev_close,.unlocked_ioctl = mycdev_ioctl,
};int all_motor_init(void)
{// 寄存器地址的映射vir_motor = ioremap(PHY_MOTOR_ADDR, 4);if (vir_motor == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}vir_rcc = ioremap(PHY_RCC_ADDR, 4);if (vir_rcc == NULL){printk("ioremap failed:%d\n", __LINE__);return -ENOMEM;}printk("物理地址映射成功\n");// 寄存器初始化// rcc(*vir_rcc) |= (0x1 << 5);// fanvir_motor->MODER &= (~(0x3 << 12));vir_motor->MODER |= (0x1 << 12);// vir_fan->AFRH &= (~(0xf << 4));//vir_fan->AFRH |= (0x1 << 4);printk("寄存器初始化成功\n");return 0;
}
static int __init mycdev_init(void)
{// 注册字符设备驱动major = register_chrdev(0, "mychrdev", &fops);if (major < 0){printk("register error\n");return major;}printk("register success\n");cls = class_create(THIS_MODULE, "mymotor");if (IS_ERR(cls)){printk("向上提交目录失败\n");return -PTR_ERR(cls);}printk("向上提交目录信息成功]\n");dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mymotor");if (IS_ERR(dev)){printk("向上提交结点设备信息失败\n");return -PTR_ERR(dev);}
printk("向上提交设备节点成功\n");// 寄存器映射以及初始化
all_motor_init();return 0;
}static void __exit mycdev_exit(void)
{// 取消地址映射iounmap(vir_motor);// 销毁节点信息device_destroy(cls, MKDEV(major, 0));// 销毁目录信息class_destroy(cls);// 注销字符设备驱动unregister_chrdev(major, "mycdev");
}module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/12780.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

list与erase()

运行代码&#xff1a; //list与erase() #include"std_lib_facilities.h" //声明Item类 struct Item {string name;int iid;double value;Item():name(" "),iid(0),value(0.0){}Item(string ss,int ii,double vv):name(ss),iid(ii),value(vv){}friend istr…

JMM是如何保证原子性呢?

3.3.1加锁 锁是一种通用技术&#xff0c;比如Java提供的Synchronized关键字就是锁的一种实现&#xff0c;Synchronized是排他锁/独占锁&#xff0c;就是有你没我的意思&#xff0c;只要其他线程到来访问&#xff0c;发现锁还未释放&#xff0c;就要在外面等待&#xff0c;因为S…

Redis—相关背景

Redis—相关背景 &#x1f50e;Redis—特性In-memory data structures—在内存中存储数据Programmability—可编程性Extensibility—可扩展性Persistence—持久化Clustering—集群High availability—高可用 &#x1f50e;Redis 为什么快&#x1f50e;Redis 的使用场景Real-tim…

【TiDB理论知识06】PD架构与作用

目录 一 PD的架构与功能 PD架构 PD作用 名词解释 路由功能 二 TSO的分配 概念 分配过程 性能问题 高可用问题 三 PD的调度原理 总流程 1 信息收集 2 生成调度 3 执行调度 四 Label的作用 Label的配置 给TiKV打标签 PD配置 一 PD的架构与功能 PD架构 PD集群…

学习Maven Web 应用

Maven Web 应用 本章节我们将学习如何使用版本控制系统 Maven 来管理一个基于 web 的项目&#xff0c;如何创建、构建、部署已经运行一个 web 应用。 创建 Web 应用 我们可以使用 maven-archetype-webapp 插件来创建一个简单的 Java web 应用。 打开命令控制台&#xff0c;…

数据库应用:Redis安装部署

目录 一、理论 1.缓存 2.关系型数据库与非关系型数据库 3.Redis 4.Redis安装部署 5.Redis命令工具 6.Redis数据库常用命令 7.Redis多数据库操作 二、实验 1.Redis安装部署 2.Redis命令工具 3.Redis数据库命令 4.Redis多数据库操作 三、问题 1.RESP连接CentOS 7 R…

【单片机】温控系统参数辨识及单片机PID控制

温控系统参数辨识及单片机PID控制 1. 温控系统组成2. matlab辨识系统参数2.1 采集阶跃响应信号导入matlab系统辨识模块 PID控制 1. 温控系统组成 半导体制冷片正向通电制冷&#xff0c;反向通电制热。系统采用半导体制冷片&#xff08;帕尔贴&#xff09;作为执行单元&#xf…

微信小程序 样式和全局配置

WXSS wxss 把屏幕分为750个物理像素&#xff0c;大屏大&#xff0c;小屏小&#xff0c;随着设备不一致自动适配 推荐使用iPhone6作为标准&#xff0c;1个rpx 0.5个px&#xff0c;把px乘以2就是rpx的参数 import 导入外部样式表 import /common/common.wxss 样式 权重一…

Vue2 第四节 计算属性,监视属性

1.计算属性 2.监视属性 3.计算属性与监视属性之间的关系 一.计算属性 定义&#xff1a;要用的属性不存在&#xff0c;要通过已有属性计算得来原理&#xff1a;底层借助了Object.defineproperty方法提供的getter和setterget函数什么时候会执行&#xff1a;初次读取的时候会执…

Redis Sentinel 及 Redis Cluster

Redis Sentinel Redis-Sentinel(哨兵模式)是Redis官方推荐的高可用性(HA)解决方案&#xff0c;当用Redis做Master-slave的高可用方案时&#xff0c;假如master宕机了&#xff0c;Redis本身(包括它的很多客户端)都没有实现自动进行主备切换&#xff0c;而Redis-sentinel本身也是…

day47-SSM分页

SSM分页&#xff08;增删改查登录注册&#xff09; applicationContext.xml中加入mybatis-config.xml路径 mybatis-config.xml Mapper接口 Service接口及其实现类 Mapper.xml page.jsp personDetail.jsp addPerson.jsp updatePerson.jsp login.jsp regist…

正则表达式简略记录

1、元字符&#xff1a; . ------- 匹配除换行符外的任意字符 \w ------- 匹配字母或数字或下划线或汉字 \s ------- 匹配任意的空白符 \d ------- 匹配数字 \b ------- 匹配单词的开始或结束 ^ ------- 匹配字符串的开始 $ ------- 匹配字符串的结束 2、字符转义 \ 3、重复次…

Java、Java EE、Spring和Spring Boot核心注解

简介 文章迁移至个人博客 此页面包含常用 Java、Java EE、Spring、 Spring Boot、JPA 常用注解的一站式说明。 Spring核心注解 Component注解 Component 注解表明被注解的类是一个“spring bean/组件”。Component注解告诉Spring容器自动创建Spring bean。 Autowired注解 …

搭建自己的Git服务器

环境 服务端&#xff1a;Ubuntu 22.04 客户端&#xff1a;Win11_x64 前提条件&#xff1a;需要确保在Windows机器上能够ping通Ubuntu服务器, 并且服务端与客户端均已安装了Git软件 服务端上的配置操作 以Ubuntu服务器作为Git服务端的运行环境&#xff0c;并方便后期免密推…

kafka消费报错卡死:内存溢出OutOfMemoryError: Java heap space

文章目录 现象排查解决 现象 我们信控平台使用Java语言开发&#xff0c;Spring Cloud微服务架构&#xff0c;采用容器化部署&#xff0c;所有服务都部署在docker里面&#xff0c;使用docker-compose进行管理&#xff0c;使用portainer进行监控平台部署客户现场后&#xff0c;一…

如何做好IT类的技术面试

目录 一、IT行业的招聘渠道 二、如何做好技术面试官 三、谈谈IT行业如何做好招聘工作 四、面试IT公司的小技巧 五、面试有哪些常见的问题 六、关于面试的一些建议 面试可能是我们每个人都必须会遇到的事情&#xff0c;而技术面试更具有专业性&#xff0c;以下会从几个方面…

openGauss学习笔记-23 openGauss 简单数据管理-时间/日期函数和操作符

文章目录 openGauss学习笔记-23 openGauss 简单数据管理-时间/日期函数和操作符23.1 时间日期操作符23.2 时间/日期函数23.3 TIMESTAMPDIFF23.4 EXTRACT23.5 date_part openGauss学习笔记-23 openGauss 简单数据管理-时间/日期函数和操作符 23.1 时间日期操作符 用户在使用时…

【Java 高频面试闯关秘籍】大梦谁先觉,平生我自知

一、mysql使用innodb引擎&#xff0c;请简述mysql索引的最左前缀如何优化orderby语句&#xff1f; 关键点&#xff1a; &#xff08;1&#xff09;如果排序字段不在索引列上&#xff0c;filesort有两种算法&#xff1a;mysql就要启动双路排序和单路排序 &#xff08;2&#x…

【系统监控程序】

用python编写一个系统监控程序&#xff0c;需要每隔一秒钟记录系统每个进程的资源占用和整体的资源占用情况&#xff0c;并输出成json&#xff0c;保存到文本文件。 import psutil import json import timedef get_process_usage():process_list []for proc in psutil.proces…

系统资源监控程序的改进

当需要监控硬盘读写和网络传输时&#xff0c;我们可以进一步使用psutil库来获取这些信息。以下是修改后的示例程序&#xff0c;增加了对硬盘读写和网络传输的监控&#xff1a; import psutil import json import timedef get_process_usage():process_list []for proc in psu…