【发布时间】:2020-11-12 20:10:16
【问题描述】:
我有一个进程列表。
我想获取此进程列表并将其组织成树状结构,以便我可以轻松地可视化父子关系,并识别可疑或不需要的进程。
但是,我正在努力想出一种算法或伪代码来创建流程树。
我不能使用像 pstree 这样的工具,因为我正在处理文本进程日志。
你能指出任何可以应用于这个问题的数据结构或算法吗?
这里有一些示例代码来说明问题:
class Process():
def __init__(self, name, pid, parent_pid):
self.name = name
self.pid = pid
self.ppid = parent_pid
# assume you have a process list with these fields:
# process name, PID, Parent PID
proc1 = Process("one", 1, 0)
proc2 = Process("two", 2, 1)
proc3 = Process("three", 3, 2)
proc4 = Process("four", 4, 3)
proc5 = Process("five", 5, 3)
proc6 = Process("six", 6, 2)
# append each process to a list
process_list = [proc1, proc2, proc3, proc4, proc5, proc6]
for each_proc in process_list:
# how do I do this?
get_child_processes()
display_as_tree()
"""
Desired output:
one
|
|__two
|
|__three
| |
| |_four
| |
| |_five
|
|__six
"""
【问题讨论】:
标签: python-3.x process tree hierarchy