我正在研究Linux内核模块。它需要有一个方法,应该将所有进程显示为分层树。我编写了一个递归方法来显示进程及其子进程。我还尝试通过检查每个进程与init_task
的距离来将其格式化为树。但是,我不认为这棵树是正确的。有人能否提示使用我编写的方法显示分层流程树的更好方法?非常感谢!
void print_tree(struct task_struct *root){
struct list_head *theList;
struct task_struct *new;
struct task_struct *new_copy;
char separator[100] = " ";
list_for_each(theList, &root->children){
new = list_entry(theList, struct task_struct, sibling);
new_copy = new;
while(new_copy->parent != &init_task){
new_copy = new_copy->parent;
strcat(separator, " ");
}
printk("%s ->[%d] %s\n",separator, new->pid, new->comm);
if((&new->children)->next != &new->children)
print_tree(new);
}
}
我得到的树:
[20291.173735] ->[1] systemd
[20291.173741] ->[121] systemd-journal
[20291.173745] ->[137] systemd-udevd
[20291.173749] ->[14456] systemd-udevd
[20291.173753] ->[155] dbus-daemon
[20291.173756] ->[160] systemd-logind
[20291.173760] ->[165] login
[20291.173764] ->[1872] bash
[20291.173768] ->[1947] xinit
[20291.173771] ->[1948] Xorg.bin
[20291.173776] ->[1951] sh
[20291.173780] ->[1956] xfce4-session
[20291.173784] ->[1974] xfwm4
[20291.173789] ->[1976] Thunar
[20291.173793] ->[1978] xfce4-panel
[20291.173798] ->[1992] panel-6-systray
[20291.173803] ->[1994] panel-2-actions
[20291.173807] ->[1980] xfdesktop
[20291.173812] ->[1983] xfce4-terminal
[20291.173816] ->[2006] gnome-pty-helpe
[20291.173821] ->[2010] bash
[20291.173825] ->[14454] sudo
[20291.173829] ->[14455] insmod
[20291.173833] ->[1852] systemd
[20291.173837] ->[1857] (sd-pam)
[20291.173840] ->[1898] dhcpcd
[20291.173844] ->[1960] dbus-daemon
[20291.173847] ->[1959] dbus-launch
[20291.173851] ->[1962] polkitd
[20291.173855] ->[1970] xfconfd
[20291.173858] ->[1973] gpg-agent
[20291.173862] ->[1981] xfsettingsd
[20291.173899] ->[1987] xfce4-power-man
[20291.173902] ->[1997] firefox
[20291.173906] ->[2004] upowerd
[20291.173910] ->[2036] at-spi-bus-laun
[20291.173914] ->[2075] gvim
[20291.173917] ->[6212] gvim
[20291.173921] ->[2] kthreadd
[20543.924720] ->[3] ksoftirqd/0
[20543.924720] ->[5] kworker/0:0H
[20543.924720] ->[6] kworker/u2:0
[20543.924720] ->[7] rcu_preempt
[20543.924720] ->[8] rcu_sched
[20543.924720] ->[9] rcu_bh
[20543.924720] ->[10] migration/0
[20543.924720] ->[11] watchdog/0
[20543.924720] ->[12] khelper
[20543.924720] ->[13] kdevtmpfs
[20543.928133] ->[14] netns
[20543.928142] ->[15] perf
[20543.928146] ->[16] khungtaskd
[20543.928150] ->[17] writeback
[20543.928154] ->[18] ksmd
[20543.928157] ->[19] khugepaged
[20543.928161] ->[20] crypto
[20543.928165] ->[21] kintegrityd
[20543.928169] ->[22] bioset
[20543.928172] ->[23] kblockd
[20543.928176] ->[24] devfreq_wq
[20543.928180] ->[26] kswapd0
[20543.928183] ->[27] fsnotify_mark
[20543.928188] ->[32] kthrotld
[20543.928191] ->[33] ipv6_addrconf
[20543.928195] ->[34] deferwq
[20543.928198] ->[58] ata_sff
[20543.928202] ->[59] scsi_eh_0
[20543.928206] ->[60] scsi_tmf_0
[20543.928210] ->[61] kworker/u2:2
[20543.928213] ->[62] scsi_eh_1
[20543.928217] ->[63] scsi_tmf_1
[20543.928221] ->[64] scsi_eh_2
[20543.928225] ->[65] scsi_tmf_2
[20543.928229] ->[73] kworker/0:1H
[20543.928232] ->[82] jbd2/sda1-8
[20543.928236] ->[83] ext4-rsv-conver
[20543.928240] ->[161] kpsmoused
[20543.928244] ->[1892] cfg80211
[20543.928248] ->[12406]kworker/0:0
[20543.928252] ->[13669] kworker/0:1
[20543.928256] ->[14017] kworker/0:2
[20543.928260] ->[14345] kworker/0:
当我打印所有当前流程及其父母时,这就是我得到的:
[15558.128445] Loading Module
[15558.128457] [1] systemd whose parent is swapper/0
[15558.128461] [2] kthreadd whose parent is swapper/0
[15558.128465] [3] ksoftirqd/0 whose parent is kthreadd
[15558.128482] [5] kworker/0:0H whose parent is kthreadd
[15558.129625] [6] kworker/u2:0 whose parent is kthreadd
[15558.129629] [7] rcu_preempt whose parent is kthreadd
[15558.129633] [8] rcu_sched whose parent is kthreadd
[15558.129637] [9] rcu_bh whose parent is kthreadd
[15558.129641] [10] migration/0 whose parent is kthreadd
[15558.129645] [11] watchdog/0 whose parent is kthreadd
[15558.129649] [12] khelper whose parent is kthreadd
[15558.129652] [13] kdevtmpfs whose parent is kthreadd
[15558.129656] [14] netns whose parent is kthreadd
[15558.129660] [15] perf whose parent is kthreadd
[15558.129663] [16] khungtaskd whose parent is kthreadd
[15558.129668] [17] writeback whose parent is kthreadd
[15558.129671] [18] ksmd whose parent is kthreadd
[15558.129675] [19] khugepaged whose parent is kthreadd
[15558.129678] [20] crypto whose parent is kthreadd
[15558.129682] [21] kintegrityd whose parent is kthreadd
[15558.129686] [22] bioset whose parent is kthreadd
[15558.129689] [23] kblockd whose parent is kthreadd
[15558.129693] [24] devfreq_wq whose parent is kthreadd
[15558.129696] [26] kswapd0 whose parent is kthreadd
[15558.129700] [27] fsnotify_mark whose parent is kthreadd
[15558.129704] [32] kthrotld whose parent is kthreadd
[15558.129708] [33] ipv6_addrconf whose parent is kthreadd
[15558.129711] [34] deferwq whose parent is kthreadd
[15558.129715] [58] ata_sff whose parent is kthreadd
[15558.129718] [59] scsi_eh_0 whose parent is kthreadd
[15558.129722] [60] scsi_tmf_0 whose parent is kthreadd
[15558.129726] [61] kworker/u2:2 whose parent is kthreadd
[15558.129729] [62] scsi_eh_1 whose parent is kthreadd
[15558.129733] [63] scsi_tmf_1 whose parent is kthreadd
[15558.129736] [64] scsi_eh_2 whose parent is kthreadd
[15558.129740] [65] scsi_tmf_2 whose parent is kthreadd
[15558.129744] [73] kworker/0:1H whose parent is kthreadd
[15558.129747] [82] jbd2/sda1-8 whose parent is kthreadd
[15558.129751] [83] ext4-rsv-conver whose parent is kthreadd
[15558.129755] [121] systemd-journal whose parent is systemd
[15558.129759] [137] systemd-udevd whose parent is systemd
[15558.129763] [155] dbus-daemon whose parent is systemd
[15558.129767] [160] systemd-logind whose parent is systemd
[15558.129771] [161] kpsmoused whose parent is kthreadd
[15558.129774] [165] login whose parent is systemd
[15558.129778] [1852] systemd whose parent is systemd
[15558.129782] [1857] (sd-pam) whose parent is systemd
[15558.129785] [1872] bash whose parent is login
[15558.129789] [1892] cfg80211 whose parent is kthreadd
[15558.129793] [1898] dhcpcd whose parent is systemd
[15558.129796] [1947] xinit whose parent is bash
[15558.129800] [1948] Xorg.bin whose parent is xinit
[15558.129803] [1951] sh whose parent is xinit
[15558.129807] [1956] xfce4-session whose parent is sh
[15558.129811] [1959] dbus-launch whose parent is systemd
[15558.129814] [1960] dbus-daemon whose parent is systemd
[15558.129818] [1962] polkitd whose parent is systemd
[15558.129822] [1970] xfconfd whose parent is systemd
[15558.129825] [1973] gpg-agent whose parent is systemd
[15558.129829] [1974] xfwm4 whose parent is xfce4-session
[15558.129832] [1976] Thunar whose parent is xfce4-session
[15558.129836] [1978] xfce4-panel whose parent is xfce4-session
[15558.129840] [1980] xfdesktop whose parent is xfce4-session
[15558.129844] [1981] xfsettingsd whose parent is systemd
[15558.129848] [1983] xfce4-terminal whose parent is xfce4-session
[15558.129851] [1987] xfce4-power-man whose parent is systemd
[15558.129855] [1992] panel-6-systray whose parent is xfce4-panel
[15558.129859] [1994] panel-2-actions whose parent is xfce4-panel
[15558.129862] [1997] firefox whose parent is systemd
[15558.129866] [2004] upowerd whose parent is systemd
[15558.129870] [2006] gnome-pty-helpe whose parent is xfce4-terminal
[15558.129874] [2010] bash whose parent is xfce4-terminal
[15558.129877] [2036] at-spi-bus-laun whose parent is systemd
[15558.129881] [2075] gvim whose parent is systemd
[15558.129885] [6212] gvim whose parent is systemd
[15558.129888] [6703] kworker/0:2 whose parent is kthreadd
[15558.129892] [9072] kworker/0:0 whose parent is kthreadd
[15558.129896] [9245] kworker/0:1 whose parent is kthreadd
[15558.129899] [9252] kworker/0:3 whose parent is kthreadd
[15558.129903] [10267] sudo whose parent is bash
[15558.129906] [10268] insmod whose parent is sudo
[15558.129910] [10269] systemd-udevd whose parent is systemd-udevd
答案 0 :(得分:0)
当然,我没有冲洗separator
缓冲区这是一个愚蠢的事实。通过简单地在list_for_each
循环内移动它的声明来修复它。树现在很漂亮。