Implemented most functionality

This commit is contained in:
uzy lol 2024-12-06 20:52:25 -08:00
parent 0319f66d39
commit b1087b461d
2 changed files with 8 additions and 13 deletions

View File

@ -49,7 +49,7 @@ static int __init temp_monitor_init(void) {
printk(KERN_ALERT "temp_monitor: Failed to register a major number\n");
return major_number;
}
temp_monitor_class = class_create(THIS_MODULE, DEVICE_NAME);
temp_monitor_class = class_create(DEVICE_NAME);
if (IS_ERR(temp_monitor_class)) {
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "temp_monitor: Failed to register device class\n");
@ -62,7 +62,7 @@ static int __init temp_monitor_init(void) {
printk(KERN_ALERT "temp_monitor: Failed to create the device\n");
return PTR_ERR(temp_monitor_device);
}
printk(KERN_INFO "temp_monitor: Device initialized\n");
printk(KERN_INFO "temp_monitor: Device initialized with major number %d\n", major_number); // Added this line
return 0;
}
@ -79,4 +79,4 @@ module_exit(temp_monitor_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A CPU Temperature Monitoring Kernel Module");
MODULE_DESCRIPTION("A CPU Temperature Monitoring Kernel Module");

View File

@ -3,27 +3,22 @@
#include <unistd.h>
#include <sys/ioctl.h>
#define DEVICE_NAME "/dev/temp_monitor"
#define IOCTL_GET_TEMP _IOR('t', 1, int)
int main() {
int fd;
int temp;
fd = open(DEVICE_NAME, O_RDONLY);
int fd, temp;
fd = open("/dev/temp_monitor", O_RDWR);
if (fd < 0) {
perror("Failed to open the device");
return -1;
}
if (ioctl(fd, IOCTL_GET_TEMP, &temp) < 0) {
if (ioctl(fd, IOCTL_GET_TEMP, &temp) == -1) {
perror("Failed to get temperature");
close(fd);
return -1;
}
printf("Current CPU Temperature: %d°C\n", temp);
printf("Current temperature: %d\n", temp);
close(fd);
return 0;
}
}