瀏覽代碼

Fixed main completely. Struct for pstree.

themultiplexer 8 年之前
父節點
當前提交
6d5ff5fb9d
共有 3 個文件被更改,包括 88 次插入56 次删除
  1. 29
    39
      hw4/task1/src/main.rs
  2. 57
    16
      hw4/task1/src/pstree.rs
  3. 2
    1
      hw4/task1/src/unit_test_pstree.rs

+ 29
- 39
hw4/task1/src/main.rs 查看文件

@@ -12,54 +12,45 @@ fn main() {
12 12
 
13 13
     match args.len() {
14 14
         1 => {
15
-            match readproc::self_pids() {
16
-                Ok(pid_tuple) => {
17
-                    let pid = pid_tuple.0;
18
-                    let ppid = pid_tuple.1;
19
-
20
-                    // Commands
21
-                    let pid_command = readproc::get_pid_command(pid).unwrap();
22
-                    let ppid_command = readproc::get_pid_command(ppid).unwrap();
23
-
24
-                    // Threads
25
-                    let pid_threads = readproc::get_thread_count(pid).unwrap();
26
-                    let ppid_threads = readproc::get_thread_count(ppid).unwrap();
27
-
28
-                    // Output for PID and PPID Information
29
-                    println!("My PID : {} - {} running {} threads", pid, pid_command, pid_threads);
30
-                    println!("My PPID: {} - {} running {} threads", ppid, ppid_command, ppid_threads);
15
+            if let Ok(pid_tuple) = readproc::self_pids() {
16
+                let pid = pid_tuple.0;
17
+                let ppid = pid_tuple.1;
18
+
19
+                // Commands & Threads for PID
20
+                if let Ok(pid_command) = readproc::get_pid_command(pid){
21
+                    if let Ok(pid_threads) = readproc::get_thread_count(pid){
22
+                        println!("My PID : {} - {} running {} threads", pid, pid_command, pid_threads);
23
+                    }
31 24
                 }
32
-                Err(_) => {}
25
+
26
+                // Commands & Threads for Parent-PID
27
+                if let Ok(ppid_command) = readproc::get_pid_command(ppid){
28
+                    if let Ok(ppid_threads) = readproc::get_thread_count(ppid){
29
+                        println!("My PPID: {} - {} running {} threads", ppid, ppid_command, ppid_threads);
30
+                    }
31
+                }
32
+
33 33
             }
34 34
 
35 35
 
36
-            match readproc::get_ownprocess_mem() {
37
-                Ok(size_tuple) => {
38
-                    // Memory
39
-                    let vspace = size_tuple.0;
40
-                    let code = size_tuple.1;
41
-                    let data = size_tuple.2;
36
+            if let Ok(size_tuple) = readproc::get_ownprocess_mem() {
37
+                // Memory
38
+                let vspace = size_tuple.0;
39
+                let code = size_tuple.1;
40
+                let data = size_tuple.2;
42 41
 
43
-                    println!("My mem : {} (vspace), {} (code), {} (data)", vspace, code, data);
44
-                }
45
-                Err(_) => {}
42
+                println!("My mem : {} (vspace), {} (code), {} (data)", vspace, code, data);
46 43
             }
47 44
 
48
-            match readproc::get_last_created_command() {
49
-                Ok(last_command) => {
50
-                    // Last Process
51
-                    println!("Last process created in system was: {}", last_command);
52
-                }
53
-                Err(_) => {}
45
+            if let Ok(last_command) = readproc::get_last_created_command() {
46
+                // Last Process
47
+                println!("Last process created in system was: {}", last_command);
54 48
             }
55 49
 
56 50
 
57
-            match readproc::get_task_total() {
58
-                Ok(task_total) => {
59
-                    // Number of tasks
60
-                    println!("Total number of tasks: {}", task_total);
61
-                }
62
-                Err(_) => {}
51
+            if let Ok(task_total) = readproc::get_task_total() {
52
+                // Number of tasks
53
+                println!("Total number of tasks: {}", task_total);
63 54
             }
64 55
         }
65 56
 
@@ -67,7 +58,6 @@ fn main() {
67 58
             match args[1].parse::<i32>() {
68 59
                 Ok(pid) => {
69 60
                    if !pstree::print(pid) {
70
-                       println!("Invalid PID");
71 61
                        process::exit(1);
72 62
                    }
73 63
                 }

+ 57
- 16
hw4/task1/src/pstree.rs 查看文件

@@ -1,34 +1,75 @@
1 1
 use procinfo::pid;
2
+use std::os::unix::raw::pid_t;
2 3
 
4
+struct Process {
5
+    name : String,
6
+    pid : pid_t,
7
+    ppid : pid_t,
8
+}
3 9
 
4
-pub fn print(pid:i32) -> bool{
5
-    if let Err(_) = pid::stat(pid) {
6
-        return false
10
+impl Process {
11
+
12
+    fn new(with_pid:pid_t) -> Self {
13
+        if let Ok(stat) = pid::stat(with_pid) {
14
+            Process{name: stat.command, pid:stat.pid, ppid:stat.ppid}
15
+        } else {
16
+            Process{name: "".to_string(), pid:0, ppid:0}
17
+        }
7 18
     }
8 19
 
9
-    if let Ok(my_pid) = pid::stat_self() {
10
-        let mut output = "".to_string();
11
-        print_recursive(my_pid.pid, pid, &mut output);
12
-        println!("{}", output);
20
+    fn has_parent(&self) -> bool {
21
+        self.ppid != 0
13 22
     }
14 23
 
15
-    true
16
-}
24
+    fn parent(&self) -> Self {
25
+        Process::new(self.ppid)
26
+    }
17 27
 
18
-fn print_recursive(from_pid:i32, to_pid:i32, output: &mut String) {
19
-    if let Ok(stat) = pid::stat(from_pid) {
28
+    fn has_parent_with_pid(&self, pid: pid_t) -> bool {
29
+        let mut p = self;
30
+
31
+        if p.pid == pid {
32
+            return true
33
+        }
34
+
35
+        if p.has_parent() {
36
+            return p.parent().has_parent_with_pid(pid)
37
+        }
38
+        false
39
+    }
40
+
41
+    fn print_recursive(d, to_pid:pid_t, output: &mut String) {
20 42
 
21 43
         if output.len() == 0 {
22
-            *output = format!("{}({}){}", stat.command, stat.pid, output);
44
+            *output = format!("{}({}){}", from.name, from.pid, output);
23 45
         } else {
24
-            *output = format!("{}({})---{}", stat.command, stat.pid, output);
46
+            *output = format!("{}({})---{}", from.name, from.pid, output);
25 47
         }
26 48
 
27
-
28
-        if stat.ppid != 0 && stat.pid != to_pid {
29
-            print_recursive(stat.ppid, to_pid, output);
49
+        if from.has_parent() && from.pid != to_pid {
50
+            Process::print_recursive(&from.parent(), to_pid, output);
30 51
         }
31 52
     }
32 53
 }
33 54
 
55
+pub fn print(pid:pid_t) -> bool {
56
+    if let Err(_) = pid::stat(pid) {
57
+        println!("Invalid PID");
58
+        return false
59
+    }
34 60
 
61
+    if let Ok(my_pid) = pid::stat_self() {
62
+        let mut output = "".to_string();
63
+        let my_proc = Process::new(my_pid.pid);
64
+
65
+        if !my_proc.has_parent_with_pid(pid) {
66
+            println!("This Process has no parent {}", pid);
67
+            return false
68
+        }
69
+
70
+        Process::print_recursive(&my_proc,pid, &mut output);
71
+        println!("{}", output);
72
+    }
73
+
74
+    true
75
+}

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

@@ -1,6 +1,7 @@
1 1
 #[cfg(test)]
2 2
 mod tests {
3 3
     #[test]
4
-    fn it_works() {
4
+    fn invalid_pid() {
5
+        assert_eq!()
5 6
     }
6 7
 }

Loading…
取消
儲存