OS版本:openEuler 22.03 LTS
架构:x86_64
描述:调用内核随机数生成函数,并打印。
// random_num_test.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/random.h>
static int __init random_num_init(void)
{
int i;
u32 randnum32 = 0;
u64 randnum64 = 0;
for (i = 0; i < 10; i++) {
randnum32 = get_random_u32();
randnum64 = get_random_u64();
printk(KERN_INFO "randnum_u32[%d] = %u, randnum_u64[%d] = %llu\n",
i, randnum32, i, randnum64);
}
return 0;
}
static void __exit random_num_exit(void)
{
return;
}
module_init(random_num_init);
module_exit(random_num_exit);
MODULE_LICENSE("GPL");
Makefile如下:
obj-m+=random_num_test.o
CONFIG_MODULE_SIG=n
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
输出结果:

本文介绍了一个在openEuler 22.03 LTS x86_64架构下生成随机数的内核模块实现。该模块通过调用内核API get_random_u32() 和 get_random_u64() 分别生成32位和64位的随机数,并打印输出。
921

被折叠的 条评论
为什么被折叠?



