Browse Source

Enabled tests. Commented pstree.

themultiplexer 8 years ago
parent
commit
371a3acf72
4 changed files with 16 additions and 4 deletions
  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 View File

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

+ 3
- 0
hw4/task1/src/main.rs View File

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

+ 10
- 1
hw4/task1/src/pstree.rs View File

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

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

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

Loading…
Cancel
Save