瀏覽代碼

Enabled tests. Commented pstree.

themultiplexer 8 年之前
父節點
當前提交
371a3acf72
共有 4 個檔案被更改,包括 16 行新增4 行删除
  1. 2
    2
      hw4/task1/Cargo.toml
  2. 3
    0
      hw4/task1/src/main.rs
  3. 10
    1
      hw4/task1/src/pstree.rs
  4. 1
    1
      hw4/task1/src/unit_test_readproc.rs

+ 2
- 2
hw4/task1/Cargo.toml 查看文件

@@ -1,8 +1,8 @@
1 1
 [package]
2 2
 name = "task1"
3 3
 version = "0.1.0"
4
-authors = ["Joshua Rutschmann <joshua.rutschmann@gmx.de>"]
4
+authors = ["Lorenz Bung & Joshua Rutschmann"]
5 5
 
6 6
 [dependencies]
7 7
 procinfo = "^0.4.2"
8
-libc = "0.2"
8
+libc = "^0.2"

+ 3
- 0
hw4/task1/src/main.rs 查看文件

@@ -6,6 +6,9 @@ use std::process;
6 6
 mod readproc;
7 7
 mod pstree;
8 8
 
9
+mod unit_test_pstree;
10
+mod unit_test_readproc;
11
+
9 12
 fn main() {
10 13
 
11 14
     let args:Vec<String> = env::args().collect();

+ 10
- 1
hw4/task1/src/pstree.rs 查看文件

@@ -3,6 +3,7 @@ extern crate libc;
3 3
 use procinfo::pid;
4 4
 use self::libc::pid_t;
5 5
 
6
+/// Datenstruktur für einen Prozess.
6 7
 struct Process {
7 8
     name : String,
8 9
     pid : pid_t,
@@ -11,6 +12,7 @@ struct Process {
11 12
 
12 13
 impl Process {
13 14
 
15
+    /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
14 16
     fn new(with_pid:pid_t) -> Self {
15 17
         if let Ok(stat) = pid::stat(with_pid) {
16 18
             Process{name: stat.command, pid:stat.pid, ppid:stat.ppid}
@@ -19,14 +21,17 @@ impl Process {
19 21
         }
20 22
     }
21 23
 
24
+    /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
22 25
     fn has_parent(&self) -> bool {
23 26
         self.ppid != 0
24 27
     }
25 28
 
29
+    /// Gibt den Elternprozess zurück.
26 30
     fn parent(&self) -> Self {
27 31
         Process::new(self.ppid)
28 32
     }
29 33
 
34
+    /// Prüft ob das Prozess-Struct einen (entfernten) Elternprozess mit dem übergebenen pid hat.
30 35
     fn has_parent_with_pid(&self, pid: pid_t) -> bool {
31 36
         if self.pid == pid {
32 37
             return true
@@ -39,6 +44,7 @@ impl Process {
39 44
         false
40 45
     }
41 46
 
47
+    /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
42 48
     fn print_recursive(&self, to_pid:pid_t, output: &mut String) {
43 49
 
44 50
         if output.len() == 0 {
@@ -53,6 +59,9 @@ impl Process {
53 59
     }
54 60
 }
55 61
 
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
56 65
 pub fn print(pid:pid_t) -> bool {
57 66
 
58 67
     if let Err(_) = pid::stat(pid) {
@@ -61,7 +70,6 @@ pub fn print(pid:pid_t) -> bool {
61 70
     }
62 71
 
63 72
     if let Ok(my_pid) = pid::stat_self() {
64
-        let mut output = "".to_string();
65 73
         let my_proc = Process::new(my_pid.pid);
66 74
 
67 75
         if !my_proc.has_parent_with_pid(pid) {
@@ -69,6 +77,7 @@ pub fn print(pid:pid_t) -> bool {
69 77
             return false
70 78
         }
71 79
 
80
+        let mut output = String::new();
72 81
         my_proc.print_recursive(pid, &mut output);
73 82
         println!("{}", output);
74 83
     }

+ 1
- 1
hw4/task1/src/unit_test_readproc.rs 查看文件

@@ -1,7 +1,7 @@
1 1
 #[cfg(test)]
2 2
 mod tests {
3 3
     use procinfo::pid::{status, status_self};
4
-    use {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 6
 
7 7
     fn sol_self_pids() -> (i32, i32) {

Loading…
取消
儲存