新聞中心
概述

創(chuàng)新互聯建站制作網站網頁找三站合一網站制作公司,專注于網頁設計,網站設計、做網站,網站設計,企業(yè)網站搭建,網站開發(fā),建網站業(yè)務,680元做網站,已為1000多家服務,創(chuàng)新互聯建站網站建設將一如既往的為我們的客戶提供最優(yōu)質的網站建設、網絡營銷推廣服務!
隨著信息技術的高速發(fā)展,操作系統(tǒng)的功能越來越強大,越來越多的應用程序需要操作系統(tǒng)向它們發(fā)送事件通知。Linux操作系統(tǒng)為了快速響應外部設備的插拔事件,引入了Uevent事件機制。本文將從以下幾個方面深入:Uevent定義、Uevent事件源、Uevent與udev、Uevent處理機制等。
Uevent定義
Uevent全稱為Userspace Event,即用戶空間事件。用戶空間事件是Linux內核為用戶空間提供給的一個機制,用于通知操作系統(tǒng)和相關程序發(fā)生了某些特定事件。這些事件通常管轄于管理UEVENT_HOTPLUG和UEVENT_COLDPLUG兩個宏過程中來調度。
Uevent事件源
Uevent事件源指的是Uevent事件的來源。Linux中Uevent事件源主要來自兩個方面:驅動程序和內核自身。
1. 驅動程序:當一個設備插入或者被拔出時,系統(tǒng)會自動將事件通知發(fā)給內核,內核通過驅動程序將事件通知送到用戶空間(udev)。實際上,這個程序可以處理任何事件,不僅是設備插拔事件,同時也包括內核之間的消息事件等。
2. 內核自身:對于內核內部事件,如內存的初始分配、網絡狀態(tài)變化等Uevent可以及時通知用戶空間的相關進程。
Uevent與udev
Uevent與udev是兩個概念密不可分的東西,udev作為一個用戶空間的設備管理程序,由驅動程序向內核捕獲Uevent。當udev收到內核的Uevent事件時,它首先會偵聽內核事件,然后在匹配設備節(jié)點和正則表達式方面執(zhí)行是否需要更改udev規(guī)則,從而在設備節(jié)點中添加或刪除屬性。同時,udev會隨時保持與內核同步,以確保設備變化的信息是實時的。
Uevent處理機制
Uevent的處理機制可以分為內核部分和用戶空間部分兩個環(huán)節(jié)。
1. 內核部分:內核創(chuàng)建uevent結構體,內部賦值、獲取子系統(tǒng)、查詢屬性并賦值等操作。在完成所有的處理后,內核會調用uevent_netlink_send()函數將uevent結構體發(fā)送到用戶空間。
2. 用戶空間部分:當內核將uevent發(fā)送到用戶空間之后,用戶空間的udev進程會處理uevent事件通知,從而更新/dev目錄下與該設備相關的信息等。同時,udev進程還發(fā)射事件通知給可以調用事件監(jiān)聽的用戶空間應用程序。
本文介紹了Linux下的Uevent事件機制,包括Uevent定義、Uevent事件源、Uevent與udev、Uevent處理機制等相關內容。通過深入探究Uevent事件機制的原理和工作原理,我們可以更好地理解Linux操作系統(tǒng)的核心功能,進一步提升系統(tǒng)的應用性能。
成都網站建設公司-創(chuàng)新互聯,建站經驗豐富以策略為先導10多年以來專注數字化網站建設,提供企業(yè)網站建設,高端網站設計,響應式網站制作,設計師量身打造品牌風格,熱線:028-86922220在linux下用什么IO模型好些
接觸和學習Linux已模配一年有余了. 對 Linux 的設備模型(bus+device+driver+sysfs+uevent)理解尚困運未深刻.所以,寫份簡單的代碼旦尺指強化理解. 我自己寫的 bus, 和在 bus 上注冊 device 和 driver. 并讓 device 和 driver 成功的match. 還未實現 uevent 時間…
如何手動創(chuàng)建一個設備節(jié)點,寫出主要命令及參數
Linux下生成驅動設備節(jié)點文件的方法有3個:1、手動mknod;2、利用devfs;3、利用udev
在剛開始寫Linux
設備驅動程序
的時候晌明帆,很多時候都是利用mknod命令手動創(chuàng)建設備宴雹節(jié)點,實際上
Linux內核
為我們提供了一組函數,可以用來在模塊加載的時候自動在/dev目錄下創(chuàng)建相應設備節(jié)點,并在卸載模塊時刪除該節(jié)點。
在2.6.17以前,在/dev目錄下生成設備文件很容易,
devfs_mk_bdev
devfs_mk_cdev
devfs_mk_symlink
devfs_mk_dir
devfs_remove
這幾個是純devfs的api,2.6.17以前可用,但后來devfs被sysfs+udev的形式取代,同時期sysfs
文件系統(tǒng)
可以用的api:
class_device_create_file,在2.6.26以后也不行了,現在,使用的是device_create ,從2.6.18開始可用
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, const char *fmt, …)
從2.6.26起又多了一個參數drvdata: the data to be added to the device for callbacks
不會用可以給此參數賦NULL
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, …)
下面著重講解第三種方法udev
在驅動用加入對udev的支持主要做的就是:在驅動初始化的代碼里調用class_create(…)為該設備創(chuàng)建一個class,再為每個設備調用device_create(…)( 在2.6較早的內核中用class_device_create)創(chuàng)建對應的設備。
內核中定義的struct class
結構體
,顧名思義,一個struct class結構體
類
型變量對應一個類,內核同時提供了class_create(…)函數,可以用它來創(chuàng)建一個類,這個類存放于sysfs下面,一旦創(chuàng)建好了這個類,再調用 device_create(…)函數來在/dev目錄下創(chuàng)建相應的設備節(jié)點。這樣,加載模塊的時候,用戶空間中的udev會自動響應 device_create(…)函數,去/sysfs下尋找對應的類從而創(chuàng)建設備節(jié)點。
struct class和class_create(…) 以及device_create(…)都包含在在/include/linux/device.h中,使用的時候一定要包含這個
頭槐唯文件
,否則
編譯器
會報錯。
struct class定義在頭文件include/linux/device.h中
class_create(…)在/drivers/base/class.c中實現
device_create(…)函數在/drivers/base/core.c中實現
class_destroy(…),device_destroy(…)也在/drivers/base/core.c中實現調用過程類似如下:
static struct class *spidev_class;
/*—*/
static int __devinit spidev_probe(struct spi_device *spi)
{
….
dev =device_create(spidev_class, &spi->dev, spidev->devt,
spidev, “spidev%d.%d”,
spi->master->bus_num, spi->chip_select);
…
}
static int __devexit spidev_remove(struct spi_device *spi)
{
……
device_destroy(spidev_class, spidev->devt);
…..
return 0;
}
static struct spi_driver spidev_spi = {
.driver = {
.name =”spidev”,
.owner = THIS_MODULE,
},
.probe = spidev_probe,
.remove = __devexit_p(spidev_remove),
};
/*—*/
static int __init spidev_init(void)
{
….
spidev_class =class_create(THIS_MODULE, “spidev”);
if (IS_ERR(spidev_class)) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
return PTR_ERR(spidev_class);
}
….
}
module_init(spidev_init);
static void __exit spidev_exit(void)
{
……
class_destroy(spidev_class);
……
}
module_exit(spidev_exit);
MODULE_DESCRIPTION(“User mode SPI device interface”);
MODULE_LICENSE(“GPL”);
下面以一個簡單字符設備驅動來展示如何使用這幾個函數
#include
#include
#include
#include
#include
#include
int HELLO_MAJOR = 0;
int HELLO_MINOR = 0;
int NUMBER_OF_DEVICES = 2;
struct class *my_class;
//struct cdev cdev;
//dev_t devno;
struct hello_dev {
struct device *dev;
dev_t chrdev;
struct cdev cdev;
};
static struct hello_dev *my_hello_dev = NULL;
struct file_operations hello_fops = {
.owner = THIS_MODULE
};
static int __init hello_init (void)
{
int err = 0;
struct device *dev;
my_hello_dev = kzalloc(sizeof(struct hello_dev), GFP_KERNEL);
if (NULL == my_hello_dev) {
printk(“%s kzalloc failed!\n”,__func__);
return -ENOMEM;
}
devno = MKDEV(HELLO_MAJOR, HELLO_MINOR);
if (HELLO_MAJOR)
err= register_chrdev_region(my_hello_dev->chrdev, 2, “memdev”);
else
{
err = alloc_chrdev_region(&my_hello_dev->chrdev, 0, 2, “memdev”);
HELLO_MAJOR = MAJOR(devno);
}
if (err) {
printk(“%s alloc_chrdev_region failed!\n”,__func__);
goto alloc_chrdev_err;
}
printk(“MAJOR IS %d\n”,HELLO_MAJOR);
cdev_init(&(my_hello_dev->cdev), &hello_fops);
my_hello_dev->cdev.owner = THIS_MODULE;
err = cdev_add(&(my_hello_dev->cdev), my_hello_dev->chrdev, 1);
if (err) {
printk(“%s cdev_add failed!\n”,__func__);
goto cdev_add_err;
}
printk (KERN_INFO “Character driver Registered\n”);
my_class =class_create(THIS_MODULE,”hello_char_class”); //類名為hello_char_class
if(IS_ERR(my_class))
{
err = PTR_ERR(my_class);
printk(“%s class_create failed!\n”,__func__);
goto class_err;
}
dev = device_create(my_class,NULL,my_hello_dev->chrdev,NULL,”memdev%d”,0); //設備名為memdev
if (IS_ERR(dev)) {
err = PTR_ERR(dev);
gyro_err(“%s device_create failed!\n”,__func__);
goto device_err;
}
printk(“hello module initialization\n”);
return 0;
device_err:
device_destroy(my_class, my_hello_dev->chrdev);
class_err:
cdev_del(my_hello_dev->chrdev);
cdev_add_err:
unregister_chrdev_region(my_hello_dev->chrdev, 1);
alloc_chrdev_err:
kfree(my_hello_dev);
return err;
}
static void __exit hello_exit (void)
{
cdev_del (&(my_hello_dev->cdev));
unregister_chrdev_region (my_hello_dev->chrdev,1);
device_destroy(my_class, devno);//delete device node under /dev//必須先刪除設備,再刪除class類
class_destroy(my_class); //delete class created by us
printk (KERN_INFO “char driver cleaned up\n”);
}
module_init (hello_init);
module_exit (hello_exit);
MODULE_LICENSE (“GPL”);
這樣,模塊加載后,就能在/dev目錄下找到memdev這個設備節(jié)點了。
例2:內核中的drivers/i2c/i2c-dev.c
在i2cdev_attach_adapter中調用device_create(i2c_dev_class, &adap->dev,
MKDEV(I2C_MAJOR, adap->nr), NULL,
“i2c-%d”, adap->nr);
這樣在dev目錄就產生i2c-0 或i2c-1節(jié)點
接下來就是udev應用,udev是
應用層
的東西,udev需要內核sysfs和tmpfs的支持,sysfs為udev提供設備入口和uevent通道,tmpfs為udev設備文件提供存放空間
udev的源碼可以在去相關網站下載,然后就是對其在運行環(huán)境下的移植,指定交叉編譯環(huán)境,修改Makefile下的CROSS_COMPILE,如為mipsel-linux-,DESTDIR=xxx,或直接make CROSS_COMPILE=mipsel-linux-,DESTDIR=xxx 并install
把主要生成的udevd、udevstart拷貝rootfs下的/in/目錄內,udev的
配置文件
udev.conf和rules.d下的rules文件拷貝到rootfs下的/etc/目錄內
并在rootfs/etc/init.d/rcS中添加以下幾行:
echo “Starting udevd…”
/in/udevd –daemon
/in/udevstart
(原rcS內容如下:
# mount filesystems
/bin/mount -t proc /proc /proc
/bin/mount -t sysfs sysfs /sys
/bin/mount -t tmpfs tmpfs /dev
# create necessary devices
/bin/mknod /dev/null c 1 3
/bin/mkdir /dev/pts
/bin/mount -t devpts devpts /dev/pts
/bin/mknod /dev/audio c 14 4
/bin/mknod /dev/ts c 10 16
)
這樣當系統(tǒng)啟動后,udevd和udevstart就會解析配置文件,并自動在/dev下創(chuàng)建設備節(jié)點文件
如何在linux ubuntu中使用android手機程序
這個恩一般我記得連接Android的操作系統(tǒng)你得給u添加一個驅動到/etc/udev/下面絕孝毀哦。ubuntu現在是基于uevent驅動的熱插拔機制。
基于uevent驅動加載機制,需并備要再/etc/udev/rules.d/51-android.rules文件,內容如下SUBSYSTEM==”u”, SYSFS{idVendor}==”18d1″, MODE=”0666″同時慎畢chmod修改777權限。供u驅動使用
linux uevent事件的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于linux uevent事件,探究Linux下的Uevent事件機制,在linux下用什么IO模型好些,如何手動創(chuàng)建一個設備節(jié)點,寫出主要命令及參數,如何在linux ubuntu中使用android手機程序的信息別忘了在本站進行查找喔。
創(chuàng)新互聯【028-86922220】值得信賴的成都網站建設公司。多年持續(xù)為眾多企業(yè)提供成都網站建設,成都品牌建站設計,成都高端網站制作開發(fā),SEO優(yōu)化排名推廣服務,全網營銷讓企業(yè)網站產生價值。
文章題目:探究Linux下的Uevent事件機制(linuxuevent事件)
標題URL:http://www.dlmjj.cn/article/dpidoep.html


咨詢
建站咨詢
