利用kernel提供的接口打印進(jìn)程號(hào)(pid)
c文件printPid.c
#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) //安裝模塊函數(shù)
{
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) //卸載函數(shù)
{
printk("exit!\n");
}
module_init(printPid); //實(shí)現(xiàn)的函數(shù)必須放入其中
module_exit(exitPid);
make后會(huì)生成pid.ko文件。
然后用這幾個(gè)命令:
#安裝模塊
sudo insmod pid
#顯示日志信息
dmesg
#顯示掛載的掛載的所有模塊
lsmod
#卸載
sudo rmmod pid
相關(guān)文章
shell for循環(huán)與數(shù)組應(yīng)用介紹
先舉一個(gè)例子,shell for循環(huán)讀取一個(gè)文件,把每一行賦值給一個(gè)數(shù)組的變量,然后介紹shell數(shù)組的相關(guān)知識(shí),有需要的朋友可以參考下2013-02-02shell腳本中常見(jiàn)的一些特殊符號(hào)和作用詳解
這篇文章主要介紹了shell腳本中常見(jiàn)的一些特殊符號(hào)和它的作用詳解,總結(jié)的很簡(jiǎn)潔,容易看懂,需要的朋友可以參考下2014-06-06crontab每10秒執(zhí)行一次的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇crontab每10秒執(zhí)行一次的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Shell使用Epoch進(jìn)行日期時(shí)間轉(zhuǎn)換和計(jì)算的幾個(gè)小函數(shù)
這篇文章主要介紹了當(dāng)你遇到一個(gè)date命令不給力的系統(tǒng)時(shí),可以試試這幾個(gè)小函數(shù),需要的朋友可以參考下2016-12-12Linux中使用Shell腳本查看Java線程的CPU使用情況
這篇文章主要介紹了Linux中使用Shell腳本查看Java線程的CPU使用情況,需要的朋友可以參考下2014-06-06詳解shell腳本中的case條件語(yǔ)句介紹和使用案例
這篇文章主要介紹了詳解shell腳本中的case條件語(yǔ)句介紹和使用案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04