我想使用模块打印进程 ID、进程名称和进程状态,作为测试,我尝试使用 getpid() 函数打印进程 ID,但出现以下错误:
函数'printf'的隐式声明
函数'getpid'的隐式声明
如何解决这些错误以及如何打印进程名称和状态。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
printf("The process id is %\n", (int) getpid());
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h> //task_pid_nr
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");
printk("The process id is %d\n", (int) task_pid_nr(current));
printk("The process vid is %d\n", (int) task_pid_vnr(current));
printk("The process name is %s\n", current->comm);
printk("The process tty is %d\n", current->signal->tty);
printk("The process group is %d\n", (int) task_tgid_nr(current));
printk("\n\n");
//return -1; //debug mode of working
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
}
/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");