c - 如何在linux中查找内核级进程分配的内存?
问题描述
我编写了一个简单的内核模块来删除给定字符串中的所有元音。我想了解进程分配了多少内存、页面交换次数和上下文切换次数。但由于某种原因,sudo pmap pid
不返回任何东西。
内核级进程是否有任何其他命令,如 pmap ?
这是c代码(t2.c):
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("K Jivitesh Narayan");
MODULE_DESCRIPTION("A program to remove all the vowels from a string");
int init_module(void)
{
char str[20];
int len, i, j;
len=strlen(str);
strcpy(str,"Hello World");
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='o' || str[i]=='i' || str[i]=='u')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
printk(KERN_INFO "After deleting the vowels, the string will be : %s",str);
printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
current->comm, current->pid);
return 0;
}
void cleanup_module(void){
printk(KERN_INFO "The program is removed \n");
}
我的 Makefile 是:
obj-m += t2.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
在和之后make
,我得到输出(最后 2 行):sudo insmod t2.ko
dmesg
[ 1108.915141] After deleting the vowels, the string will be : Hll Wrld
[ 1108.915144] The process is "insmod" (pid 3299)
的输出sudo pmap 3299
什么都没有。没有错误。
**编辑:这是添加 jiffies 模块后的代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/jiffies.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("K Jivitesh Narayan");
MODULE_DESCRIPTION("A program to remove all the vowels from a string");
int init_module(void)
{
unsigned long j1 = jiffies + 3 * HZ;
char str[20];
int len, i, j;
len=strlen(str);
strcpy(str,"Hello World");
printk(KERN_INFO "The process is \"%s\" (pid %i)\n", current->comm, current->pid);
while(jiffies < j1);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='o' || str[i]=='i' || str[i]=='u')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
printk(KERN_INFO "After deleting the vowels, the string will be : %s",str);
return 0;
}
void cleanup_module(void){
printk(KERN_INFO "The program is removed \n");
}
解决方案
推荐阅读
- postgresql - 如何将 postgres 中 ksuid 的默认值赋予列?postgres中是否有ksuid的替代品?
- r - 使用 ggplotly() 时删除构面之间的空白
- java - Spring JPA 实体映射 LocalDate 和 LocalDateTime 字段到 Sql Server
- dataweave - Mulesoft 条件语句
- terraform - 如何在 terraform 代码中访问 terragrunt 变量
- ubuntu - 即使我正确输入密码,Oracle VirtualBox Ubuntu OS 也无法打开我的虚拟机
- amazon-web-services - AWS 连接超时 + EC2 实例连接不起作用
- c# - 源更改时如何更新 Xamarin 数据绑定
- html - 多个绝对定位元素重叠
- r - 如何在棒棒糖图形上添加图例?