Print process number (PID) _linux shell with interface provided by kernel

Source: Internet
Author: User

C File Printpid.c

Copy Code code as follows:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/list.h>

static __init int printpid (void)//install module functions
{
struct Task_struct *task,*p;
struct List_head *ps;
int count=0;
PRINTK ("begin.\n");
task=&init_task;
List_for_each (Ps,&task->tasks)
{
P=list_entry (ps,struct task_struct,tasks);
count++;
PRINTK ("%d\t%s\n", P->pid,p->comm);
}
PRINTK ("Process counts:%d\n", count);
return 0;
}
static __exit void Exitpid (void)//unload function
{
PRINTK ("exit!\n");
}
Module_init (PRINTPID); The implemented function must be placed in the
Module_exit (EXITPID);

The Pid.ko file is generated after make.

And then use the following commands:

Copy Code code as follows:

#安装模块
sudo insmod pid

#显示日志信息
Dmesg

#显示挂载的挂载的所有模块
Lsmod

#卸载
sudo rmmod pid

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.