Sfoglia il codice sorgente

Added methods in readproc.rs. Some need review.

Lorenz Bung 8 anni fa
parent
commit
5347553d91
2 ha cambiato i file con 61 aggiunte e 12 eliminazioni
  1. 29
    2
      hw4/task1/src/main.rs
  2. 32
    10
      hw4/task1/src/readproc.rs

+ 29
- 2
hw4/task1/src/main.rs Vedi File

@@ -4,6 +4,33 @@ mod readproc;
4 4
 mod pstree;
5 5
 
6 6
 fn main() {
7
-    println!("Hello, world!");
8
-    println!("{:?}", readproc::self_pids());
7
+
8
+    // PID and PPID
9
+    let pid_tuple = readproc::self_pids().unwrap();
10
+    let pid = pid_tuple.0;
11
+    let ppid = pid_tuple.1;
12
+
13
+    // Commands
14
+    let pid_command = readproc::get_pid_command(pid).unwrap();
15
+    let ppid_command = readproc::get_pid_command(ppid).unwrap();
16
+
17
+    // Threads
18
+    let pid_threads = readproc::get_thread_count(pid).unwrap();
19
+    let ppid_threads = readproc::get_thread_count(ppid).unwrap();
20
+
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
+
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
+
31
+    // Last Process
32
+    println!("Last process created in system was: {}", readproc::get_last_created_command().unwrap());
33
+
34
+    // Number of tasks
35
+    println!("Total number of tasks: {}", readproc::get_task_total().unwrap());
9 36
 }

+ 32
- 10
hw4/task1/src/readproc.rs Vedi File

@@ -1,31 +1,53 @@
1
-use procinfo::pid::Stat;
2 1
 use procinfo::pid;
2
+use procinfo::loadavg;
3 3
 
4 4
 pub fn self_pids() -> Result<(i32, i32), &'static str>{
5 5
     match pid::stat_self() {
6 6
         Ok(stat) => { Ok((stat.pid, stat.ppid)) }
7
-        Err(_) => { Err("Fehler") }
7
+        Err(_) => { Err("PID not alive: PID and PPID not found") }
8 8
     }
9 9
 }
10 10
 
11 11
 pub fn get_pid_command(pid: i32) -> Result<String, &'static str> {
12 12
     match pid::stat(pid) {
13 13
         Ok(stat) => { Ok(stat.command) }
14
-        Err(_) => { Err("Fehler") }
14
+        Err(_) => { Err("PID not alive: no command name found") }
15 15
     }
16 16
 }
17 17
 
18
-pub fn get_last_created_command() -> Result<String, &'static str> {
19
-    match pid::stat_self() {
20
-        Ok(stat) => { Ok(stat.command) }
21
-        Err(_) => { Err("Fehler") }
18
+/*pub fn get_last_created_command() -> Result<String, &'static str> {
19
+    match loadavg() {
20
+        Ok(stat) => {
21
+            let last_pid = stat.last_created_pid;
22
+            match pid::stat(last_pid) {
23
+                Ok(st) => { Ok(st.command) }
24
+                Err(_) => { Err("No last command via PID found") }
25
+            }
26
+        }
27
+        Err(_) => { Err("No last command via PID found") }
22 28
     }
23
-
24
-}
29
+}*/
25 30
 
26 31
 pub fn get_thread_count(pid: i32) -> Result<u32, &'static str> {
27 32
     match pid::stat(pid) {
28 33
         Ok(stat) => { Ok(stat.num_threads as u32) }
29
-        Err(_) => { Err("Fehler") }
34
+        Err(_) => { Err("PID not alive: no threads counted") }
35
+    }
36
+}
37
+
38
+pub fn get_task_total() -> Result<u32, &'static str> {
39
+    match loadavg() {
40
+        Ok(stat) => { Ok(stat.tasks_total) }
41
+        Err(_) => { Err("No total count of tasks in system found") }
42
+    }
43
+}
44
+
45
+pub fn get_ownprocess_mem() -> Result<(usize, usize, usize), &'static str> {
46
+    match pid::stat_self() {
47
+        Ok(stat) => {
48
+            let csize = stat.end_code - stat.start_code;
49
+            let dsize = stat.end_data - stat.start_data;
50
+            Ok((stat.vsize, csize, dsize)) }
51
+        Err(_) => { Err("PID not alive: no memory found") }
30 52
     }
31 53
 }

Loading…
Annulla
Salva