Ver código fonte

load extern crates in main.rs

themultiplexer 8 anos atrás
pai
commit
3e14614664
3 arquivos alterados com 123 adições e 3 exclusões
  1. 30
    3
      hw5/task1/src/child/mod.rs
  2. 92
    0
      hw5/task1/src/child/pstree.rs
  3. 1
    0
      hw5/task1/src/main.rs

+ 30
- 3
hw5/task1/src/child/mod.rs Ver arquivo

@@ -1,7 +1,34 @@
1
-
1
+use nix::unistd::{fork, getpid};
2
+use nix::sys::wait::wait;
3
+use nix::unistd::ForkResult::{Child, Parent};
2 4
 
3 5
 
4 6
 pub fn run_childs(start_pid: i32, arg: &str) -> Result<(), String> {
5
-    let count = arg.parse::<u32>();
6
-    match count { }
7
+    let count = arg.parse::<u8>();
8
+    match count {
9
+        Ok(value) => {
10
+            for i in 0..value {
11
+                let pid = fork();
12
+                match pid {
13
+                    Ok(Child) => {
14
+                        println!("hello, I am child (pid:{})", getpid());
15
+                    }
16
+                    Ok(Parent { child }) => {
17
+                        println!("hello, I am parent of {} (pid:{})", child, getpid());
18
+                        match wait(){
19
+                            Ok(ws) => { println!("{:?}", ws); }
20
+                            Err(_) => {}
21
+                        }
22
+                    }
23
+                    // panic, fork should never fail unless there is a
24
+                    // serious problem with the OS
25
+                    Err(_) => panic!("fork failed"),
26
+                }
27
+            }
28
+            Ok(())
29
+        },
30
+        Err(_) => {
31
+            Err("Parse Failed".to_string())
32
+        },
33
+    }
7 34
 }

+ 92
- 0
hw5/task1/src/child/pstree.rs Ver arquivo

@@ -0,0 +1,92 @@
1
+use procinfo::pid;
2
+
3
+/// Datenstruktur für einen Prozess.
4
+pub struct Process {
5
+    name: String,
6
+    pid: u32,
7
+    ppid: u32,
8
+}
9
+
10
+impl Process {
11
+    /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
12
+    pub fn new(with_pid: u32) -> Self {
13
+        if let Ok(stat) = pid::stat(with_pid) {
14
+            Process {
15
+                name: stat.command,
16
+                pid: stat.pid,
17
+                ppid: stat.ppid,
18
+            }
19
+        } else {
20
+            panic!("Internal Error: Process not found")
21
+        }
22
+    }
23
+
24
+    /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
25
+    pub fn me() -> Self {
26
+        if let Ok(my_pid) = pid::stat_self() {
27
+            Process::new(my_pid.pid)
28
+        } else {
29
+            panic!("Internal Error: I don't have a PID but I am running.")
30
+        }
31
+    }
32
+
33
+    /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
34
+    pub fn has_parent(&self) -> bool {
35
+        self.ppid != 0
36
+    }
37
+
38
+    /// Gibt den Elternprozess zurück.
39
+    pub fn parent(&self) -> Self {
40
+        Process::new(self.ppid)
41
+    }
42
+
43
+    /// Prüft ob das Prozess-Struct einen (entfernten) Elternprozess mit dem übergebenen pid hat.
44
+    pub fn has_parent_with_pid(&self, pid: u32) -> bool {
45
+        if self.pid == pid {
46
+            return true;
47
+        }
48
+
49
+        if self.has_parent() {
50
+            return self.parent().has_parent_with_pid(pid);
51
+        }
52
+
53
+        false
54
+    }
55
+
56
+    /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
57
+    pub fn print_recursive(&self, to_pid: u32, output: &mut String) {
58
+
59
+        if output.len() == 0 {
60
+            *output = format!("{}({}){}", self.name, self.pid, output);
61
+        } else {
62
+            *output = format!("{}({})---{}", self.name, self.pid, output);
63
+        }
64
+
65
+        if self.has_parent() && self.pid != to_pid {
66
+            self.parent().print_recursive(to_pid, output);
67
+        }
68
+    }
69
+}
70
+
71
+/// Geht von eigenem Prozess aus und gibt die Prozesskette bis zum übergebenem PID aus
72
+/// und fängt mögliche Fehler ab.
73
+pub fn print(pid: u32) -> bool {
74
+
75
+    if let Err(_) = pid::stat(pid) {
76
+        println!("Invalid PID");
77
+        return false;
78
+    }
79
+
80
+    let my_proc = Process::me();
81
+
82
+    if !my_proc.has_parent_with_pid(pid) {
83
+        println!("This Process has no parent {}", pid);
84
+        return false;
85
+    }
86
+
87
+    let mut output = String::new();
88
+    my_proc.print_recursive(pid, &mut output);
89
+    println!("{}", output);
90
+
91
+    true
92
+}

+ 1
- 0
hw5/task1/src/main.rs Ver arquivo

@@ -1,4 +1,5 @@
1 1
 extern crate procinfo;
2
+extern crate nix;
2 3
 
3 4
 use std::env::args;
4 5
 

Carregando…
Cancelar
Salvar