Skip to content
This repository was archived by the owner on Feb 20, 2022. It is now read-only.

Commit ef75360

Browse files
committed
v1.0
0 parents  commit ef75360

File tree

10 files changed

+586
-0
lines changed

10 files changed

+586
-0
lines changed

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Custom
2+
.vs
3+
.idea
4+
.vscode
5+
log
6+
# Created by https://www.gitignore.io/api/osx,java,eclipse,intellij,maven
7+
# Edit at https://www.gitignore.io/?templates=osx,java,eclipse,intellij,maven
8+
9+
### Eclipse ###
10+
.metadata
11+
bin/
12+
tmp/
13+
*.tmp
14+
*.bak
15+
*.swp
16+
*~.nib
17+
local.properties
18+
.settings/
19+
.loadpath
20+
.recommenders
21+
22+
# External tool builders
23+
.externalToolBuilders/
24+
25+
# Locally stored "Eclipse launch configurations"
26+
*.launch
27+
28+
# PyDev specific (Python IDE for Eclipse)
29+
*.pydevproject
30+
31+
# CDT-specific (C/C++ Development Tooling)
32+
.cproject
33+
34+
# CDT- autotools
35+
.autotools
36+
37+
# Java annotation processor (APT)
38+
.factorypath
39+
40+
# PDT-specific (PHP Development Tools)
41+
.buildpath
42+
43+
# sbteclipse plugin
44+
.target
45+
46+
# Tern plugin
47+
.tern-project
48+
49+
# TeXlipse plugin
50+
.texlipse
51+
52+
# STS (Spring Tool Suite)
53+
.springBeans
54+
55+
# Code Recommenders
56+
.recommenders/
57+
58+
# Annotation Processing
59+
.apt_generated/
60+
61+
# Scala IDE specific (Scala & Java development for Eclipse)
62+
.cache-main
63+
.scala_dependencies
64+
.worksheet
65+
66+
### Eclipse Patch ###
67+
# Eclipse Core
68+
.project
69+
70+
# JDT-specific (Eclipse Java Development Tools)
71+
.classpath
72+
73+
# Annotation Processing
74+
.apt_generated
75+
76+
.sts4-cache/
77+
78+
*.iml
79+
*.ipr
80+
81+
# CMake
82+
cmake-build-*/
83+
84+
# File-based project format
85+
*.iws
86+
87+
# IntelliJ
88+
out/
89+
90+
91+
92+
# JIRA plugin
93+
atlassian-ide-plugin.xml
94+
95+
96+
### Java ###
97+
# Compiled class file
98+
*.class
99+
100+
# Log file
101+
*.log
102+
103+
# BlueJ files
104+
*.ctxt
105+
106+
# Mobile Tools for Java (J2ME)
107+
.mtj.tmp/
108+
109+
# Package Files #
110+
*.jar
111+
*.war
112+
*.nar
113+
*.ear
114+
*.zip
115+
*.tar.gz
116+
*.rar
117+
118+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
119+
hs_err_pid*
120+
121+
### Maven ###
122+
target/
123+
pom.xml.tag
124+
pom.xml.releaseBackup
125+
pom.xml.versionsBackup
126+
pom.xml.next
127+
release.properties
128+
dependency-reduced-pom.xml
129+
buildNumber.properties
130+
.mvn/timing.properties
131+
.mvn/wrapper/maven-wrapper.jar
132+
.flattened-pom.xml
133+
134+
### OSX ###
135+
# General
136+
.DS_Store
137+
.AppleDouble
138+
.LSOverride
139+
140+
# Icon must end with two \r
141+
Icon
142+
143+
# Thumbnails
144+
._*
145+
146+
# Files that might appear in the root of a volume
147+
.DocumentRevisions-V100
148+
.fseventsd
149+
.Spotlight-V100
150+
.TemporaryItems
151+
.Trashes
152+
.VolumeIcon.icns
153+
.com.apple.timemachine.donotpresent
154+
155+
# Directories potentially created on remote AFP share
156+
.AppleDB
157+
.AppleDesktop
158+
Network Trash Folder
159+
Temporary Items
160+
.apdisk
161+
162+
# End of https://www.gitignore.io/api/osx,java,eclipse,intellij,maven

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 CC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# java-pstree
2+
> 使用 java 实现的一个类似于 pstree 的进程打印工具
3+
4+
# Point
5+
* 文件读取
6+
* 建树
7+
* 树的遍历
8+
9+
# demo
10+
![alt](./docs/images/demo.png)

docs/images/demo.png

87.7 KB
Loading

pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>java-pstree</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<configuration>
15+
<source>1.8</source>
16+
<target>1.8</target>
17+
</configuration>
18+
</plugin>
19+
<plugin>
20+
<artifactId>maven-assembly-plugin</artifactId>
21+
<configuration>
22+
<archive>
23+
<manifest>
24+
<addClasspath>true</addClasspath>
25+
<mainClass>Main</mainClass>
26+
<classpathPrefix>libs/</classpathPrefix>
27+
</manifest>
28+
</archive>
29+
<descriptorRefs>
30+
<descriptorRef>jar-with-dependencies</descriptorRef>
31+
</descriptorRefs>
32+
</configuration>
33+
<executions>
34+
<execution>
35+
<id>make-assembly</id>
36+
<phase>package</phase>
37+
<goals>
38+
<goal>single</goal>
39+
</goals>
40+
</execution>
41+
</executions>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>

src/main/java/Main.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
public class Main {
6+
private final static Map<Integer, PcInfo> pidMap = new HashMap<>();
7+
private final static Map<Integer, ArrayList<PcInfo>> ppidMap = new HashMap<>();
8+
9+
public static void main(String[] args) {
10+
PcReader pcReader = new PcReader();
11+
ArrayList<PcInfo> arrayList = pcReader.read();
12+
for (PcInfo e : arrayList) {
13+
// 一个进程只会有一个 Pid
14+
pidMap.put(e.getPid(), e);
15+
// 一个 PPid 可以有多个进程
16+
if (ppidMap.get(e.getPpid()) == null) {
17+
ArrayList<PcInfo> tmp = new ArrayList<>();
18+
tmp.add(e);
19+
ppidMap.put(e.getPpid(), tmp);
20+
} else {
21+
ArrayList<PcInfo> tmp = ppidMap.get(e.getPpid());
22+
tmp.add(e);
23+
ppidMap.put(e.getPpid(), tmp);
24+
}
25+
}
26+
TreeNode<PcInfo> pcInfoTree = getTree(new TreeNode<>(pidMap.get(1)), ppidMap.get(1));
27+
for (TreeNode<PcInfo> node : pcInfoTree) {
28+
String indent = keepIndent(node.getLevel());
29+
System.out.println(indent + node.data.toString());
30+
}
31+
// try {
32+
// Process process = Runtime.getRuntime().exec(args[0]);
33+
// BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
34+
// String line = null;
35+
// StringBuilder cmdout = new StringBuilder();
36+
// while ((line = br.readLine()) != null) {
37+
// cmdout.append(line)
38+
// .append("\n");
39+
// }
40+
// System.out.println(cmdout.toString());
41+
// } catch (Exception e) {
42+
// e.printStackTrace();
43+
// }
44+
}
45+
46+
private static String keepIndent(int depth) {
47+
StringBuilder sb = new StringBuilder();
48+
for (int i = 0; i < depth; i++) {
49+
sb.append(
50+
i + 1 == depth ? "\t-" : "\t"
51+
);
52+
}
53+
return sb.toString();
54+
}
55+
56+
private static TreeNode<PcInfo> getTree(
57+
TreeNode<PcInfo> treeNode,
58+
ArrayList<PcInfo> pcInfoArrayList
59+
) {
60+
for (PcInfo pcInfo : pcInfoArrayList) {
61+
// 判断当前进程有无子进程
62+
if (ppidMap.get(pcInfo.getPid()) == null) {
63+
//无子进程
64+
treeNode.addChild(pcInfo);
65+
} else {
66+
TreeNode<PcInfo> tmp = treeNode.addChild(pcInfo);
67+
getTree(tmp, ppidMap.get(pcInfo.getPid()));
68+
}
69+
}
70+
return treeNode;
71+
}
72+
}

src/main/java/PcInfo.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class PcInfo {
2+
private int pid, ppid;
3+
private String name;
4+
5+
public PcInfo(int pid, int ppid, String name) {
6+
this.pid = pid;
7+
this.ppid = ppid;
8+
this.name = name;
9+
}
10+
11+
public int getPid() {
12+
return pid;
13+
}
14+
15+
public void setPid(int pid) {
16+
this.pid = pid;
17+
}
18+
19+
public int getPpid() {
20+
return ppid;
21+
}
22+
23+
public void setPpid(int ppid) {
24+
this.ppid = ppid;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return String.format("%s(%s)", name, pid);
38+
}
39+
}

0 commit comments

Comments
 (0)