Browse Source

Optimized pstree.rs

themultiplexer 8 years ago
parent
commit
52ecb7c209
2 changed files with 8 additions and 7 deletions
  1. 1
    0
      hw4/task1/Cargo.toml
  2. 7
    7
      hw4/task1/src/pstree.rs

+ 1
- 0
hw4/task1/Cargo.toml View File

5
 
5
 
6
 [dependencies]
6
 [dependencies]
7
 procinfo = "^0.4.2"
7
 procinfo = "^0.4.2"
8
+libc = "0.2"

+ 7
- 7
hw4/task1/src/pstree.rs View File

1
+extern crate libc;
2
+
1
 use procinfo::pid;
3
 use procinfo::pid;
2
-use std::libc::
4
+use self::libc::pid_t;
3
 
5
 
4
 struct Process {
6
 struct Process {
5
     name : String,
7
     name : String,
13
         if let Ok(stat) = pid::stat(with_pid) {
15
         if let Ok(stat) = pid::stat(with_pid) {
14
             Process{name: stat.command, pid:stat.pid, ppid:stat.ppid}
16
             Process{name: stat.command, pid:stat.pid, ppid:stat.ppid}
15
         } else {
17
         } else {
16
-            Process{name: "".to_string(), pid:0, ppid:0}
18
+            panic!("Internal Error: Process not found")
17
         }
19
         }
18
     }
20
     }
19
 
21
 
26
     }
28
     }
27
 
29
 
28
     fn has_parent_with_pid(&self, pid: pid_t) -> bool {
30
     fn has_parent_with_pid(&self, pid: pid_t) -> bool {
29
-        let mut p = self;
30
-
31
-        if p.pid == pid {
31
+        if self.pid == pid {
32
             return true
32
             return true
33
         }
33
         }
34
 
34
 
35
-        if p.has_parent() {
36
-            return p.parent().has_parent_with_pid(pid)
35
+        if self.has_parent() {
36
+            return self.parent().has_parent_with_pid(pid)
37
         }
37
         }
38
 
38
 
39
         false
39
         false

Loading…
Cancel
Save