Browse Source

Enhanced pstree.

themultiplexer 8 years ago
parent
commit
5779b45d47
2 changed files with 19 additions and 13 deletions
  1. 19
    12
      hw4/task1/src/pstree.rs
  2. 0
    1
      hw4/task1/src/unit_test_readproc.rs

+ 19
- 12
hw4/task1/src/pstree.rs View File

21
         }
21
         }
22
     }
22
     }
23
 
23
 
24
+    fn me() -> Self {
25
+        if let Ok(my_pid) = pid::stat_self() {
26
+            Process::new(my_pid.pid)
27
+        } else {
28
+            panic!("Internal Error: I don't have a PID but I am running.")
29
+        }
30
+    }
31
+
24
     /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
32
     /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
25
     pub fn has_parent(&self) -> bool {
33
     pub fn has_parent(&self) -> bool {
26
         self.ppid != 0
34
         self.ppid != 0
59
     }
67
     }
60
 }
68
 }
61
 
69
 
62
-/// Erstellt einen 'Process' aus der übergebenen PID und
63
-/// Gibt über Rekursion über die Eltern eine Prozesskette aus.
64
-/// Fängt mögliche Fehler ab und
70
+/// Erstellt einen 'Process' aus der übergebenen PID und gibt die Prozesskette
71
+/// und fängt mögliche Fehler ab.
72
+///
65
 pub fn print(pid:pid_t) -> bool {
73
 pub fn print(pid:pid_t) -> bool {
66
 
74
 
67
     if let Err(_) = pid::stat(pid) {
75
     if let Err(_) = pid::stat(pid) {
69
         return false
77
         return false
70
     }
78
     }
71
 
79
 
72
-    if let Ok(my_pid) = pid::stat_self() {
73
-        let my_proc = Process::new(my_pid.pid);
74
 
80
 
75
-        if !my_proc.has_parent_with_pid(pid) {
76
-            println!("This Process has no parent {}", pid);
77
-            return false
78
-        }
81
+    let my_proc = Process::me();
79
 
82
 
80
-        let mut output = String::new();
81
-        my_proc.print_recursive(pid, &mut output);
82
-        println!("{}", output);
83
+    if !my_proc.has_parent_with_pid(pid) {
84
+        println!("This Process has no parent {}", pid);
85
+        return false
83
     }
86
     }
84
 
87
 
88
+    let mut output = String::new();
89
+    my_proc.print_recursive(pid, &mut output);
90
+    println!("{}", output);
91
+
85
     true
92
     true
86
 }
93
 }

+ 0
- 1
hw4/task1/src/unit_test_readproc.rs View File

3
     use procinfo::pid::{status, status_self};
3
     use procinfo::pid::{status, status_self};
4
     use readproc::{self_pids, get_pid_command, get_thread_count, get_ownprocess_mem, get_task_total};
4
     use readproc::{self_pids, get_pid_command, get_thread_count, get_ownprocess_mem, get_task_total};
5
 
5
 
6
-
7
     fn sol_self_pids() -> (i32, i32) {
6
     fn sol_self_pids() -> (i32, i32) {
8
         match status_self() {
7
         match status_self() {
9
             Ok(status) => (status.pid, status.ppid),
8
             Ok(status) => (status.pid, status.ppid),

Loading…
Cancel
Save