Просмотр исходного кода

Made main safer. Wrote pstree.rs. Created test.

themultiplexer 8 лет назад
Родитель
Сommit
2d7e56805e
3 измененных файлов: 117 добавлений и 22 удалений
  1. 77
    22
      hw4/task1/src/main.rs
  2. 34
    0
      hw4/task1/src/pstree.rs
  3. 6
    0
      hw4/task1/src/unit_test_pstree.rs

+ 77
- 22
hw4/task1/src/main.rs Просмотреть файл

@@ -1,36 +1,91 @@
1 1
 extern crate procinfo;
2 2
 
3
+use std::env;
4
+use std::process;
5
+
3 6
 mod readproc;
4 7
 mod pstree;
5 8
 
6 9
 fn main() {
7 10
 
8
-    // PID and PPID
9
-    let pid_tuple = readproc::self_pids().unwrap();
10
-    let pid = pid_tuple.0;
11
-    let ppid = pid_tuple.1;
11
+    let args:Vec<String> = env::args().collect();
12
+
13
+    match args.len() {
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);
31
+                }
32
+                Err(_) => {}
33
+            }
34
+
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;
42
+
43
+                    println!("My mem : {} (vspace), {} (code), {} (data)", vspace, code, data);
44
+                }
45
+                Err(_) => {}
46
+            }
47
+
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(_) => {}
54
+            }
55
+
56
+
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(_) => {}
63
+            }
64
+        }
65
+
66
+        2 => {
67
+            match args[1].parse::<i32>() {
68
+                Ok(pid) => {
69
+                   if !pstree::print(pid) {
70
+                       println!("Invalid PID");
71
+                       process::exit(1);
72
+                   }
73
+                }
74
+                Err(_) => {
75
+                    println!("Error while parsing PID");
76
+                    process::exit(1);
77
+                }
78
+            }
79
+        }
12 80
 
13
-    // Commands
14
-    let pid_command = readproc::get_pid_command(pid).unwrap();
15
-    let ppid_command = readproc::get_pid_command(ppid).unwrap();
81
+        _ => {
82
+            println!("Correct usage: no param or param PID");
83
+            process::exit(1);
84
+        }
85
+    }
16 86
 
17
-    // Threads
18
-    let pid_threads = readproc::get_thread_count(pid).unwrap();
19
-    let ppid_threads = readproc::get_thread_count(ppid).unwrap();
20 87
 
21
-    // Output for PID and PPID Information
22
-    println!("My PID : {} - {} running {} threads", pid, pid_command, pid_threads);
23
-    println!("My PPID: {} - {} running {} threads", ppid, ppid_command, ppid_threads);
24 88
 
25
-    // Memory
26
-    let vspace = readproc::get_ownprocess_mem().unwrap().0;
27
-    let code = readproc::get_ownprocess_mem().unwrap().1;
28
-    let data = readproc::get_ownprocess_mem().unwrap().2;
29
-    println!("My mem : {} (vspace), {} (code), {} (data)", vspace, code, data);
30 89
 
31
-    // Last Process
32
-    println!("Last process created in system was: {}", readproc::get_last_created_command().unwrap());
33 90
 
34
-    // Number of tasks
35
-    println!("Total number of tasks: {}", readproc::get_task_total().unwrap());
36 91
 }

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

@@ -0,0 +1,34 @@
1
+use procinfo::pid;
2
+
3
+
4
+pub fn print(pid:i32) -> bool{
5
+    if let Err(_) = pid::stat(pid) {
6
+        return false
7
+    }
8
+
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);
13
+    }
14
+
15
+    true
16
+}
17
+
18
+fn print_recursive(from_pid:i32, to_pid:i32, output: &mut String) {
19
+    if let Ok(stat) = pid::stat(from_pid) {
20
+
21
+        if output.len() == 0 {
22
+            *output = format!("{}({}){}", stat.command, stat.pid, output);
23
+        } else {
24
+            *output = format!("{}({})---{}", stat.command, stat.pid, output);
25
+        }
26
+
27
+
28
+        if stat.ppid != 0 && stat.pid != to_pid {
29
+            print_recursive(stat.ppid, to_pid, output);
30
+        }
31
+    }
32
+}
33
+
34
+

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

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

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