themultiplexer 8 лет назад
Родитель
Сommit
5779b45d47
2 измененных файлов: 19 добавлений и 13 удалений
  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 Просмотреть файл

@@ -21,6 +21,14 @@ impl Process {
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 32
     /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
25 33
     pub fn has_parent(&self) -> bool {
26 34
         self.ppid != 0
@@ -59,9 +67,9 @@ impl Process {
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 73
 pub fn print(pid:pid_t) -> bool {
66 74
 
67 75
     if let Err(_) = pid::stat(pid) {
@@ -69,18 +77,17 @@ pub fn print(pid:pid_t) -> bool {
69 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 92
     true
86 93
 }

+ 0
- 1
hw4/task1/src/unit_test_readproc.rs Просмотреть файл

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

Загрузка…
Отмена
Сохранить