Explorar el Código

load extern crates in main.rs

themultiplexer hace 8 años
padre
commit
6df4aba92e
Se han modificado 3 ficheros con 13 adiciones y 7 borrados
  1. 2
    0
      hw5/task1/src/child/mod.rs
  2. 6
    6
      hw5/task1/src/child/pstree.rs
  3. 5
    1
      hw5/task1/src/main.rs

+ 2
- 0
hw5/task1/src/child/mod.rs Ver fichero

2
 use nix::sys::wait::wait;
2
 use nix::sys::wait::wait;
3
 use nix::unistd::ForkResult::{Child, Parent};
3
 use nix::unistd::ForkResult::{Child, Parent};
4
 
4
 
5
+mod pstree;
5
 
6
 
6
 pub fn run_childs(start_pid: i32, arg: &str) -> Result<(), String> {
7
 pub fn run_childs(start_pid: i32, arg: &str) -> Result<(), String> {
7
     let count = arg.parse::<u8>();
8
     let count = arg.parse::<u8>();
25
                     Err(_) => panic!("fork failed"),
26
                     Err(_) => panic!("fork failed"),
26
                 }
27
                 }
27
             }
28
             }
29
+            pstree::print(start_pid);
28
             Ok(())
30
             Ok(())
29
         },
31
         },
30
         Err(_) => {
32
         Err(_) => {

+ 6
- 6
hw5/task1/src/child/pstree.rs Ver fichero

3
 /// Datenstruktur für einen Prozess.
3
 /// Datenstruktur für einen Prozess.
4
 pub struct Process {
4
 pub struct Process {
5
     name: String,
5
     name: String,
6
-    pid: u32,
7
-    ppid: u32,
6
+    pid: i32,
7
+    ppid: i32,
8
 }
8
 }
9
 
9
 
10
 impl Process {
10
 impl Process {
11
     /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
11
     /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
12
-    pub fn new(with_pid: u32) -> Self {
12
+    pub fn new(with_pid: i32) -> Self {
13
         if let Ok(stat) = pid::stat(with_pid) {
13
         if let Ok(stat) = pid::stat(with_pid) {
14
             Process {
14
             Process {
15
                 name: stat.command,
15
                 name: stat.command,
41
     }
41
     }
42
 
42
 
43
     /// Prüft ob das Prozess-Struct einen (entfernten) Elternprozess mit dem übergebenen pid hat.
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 {
44
+    pub fn has_parent_with_pid(&self, pid: i32) -> bool {
45
         if self.pid == pid {
45
         if self.pid == pid {
46
             return true;
46
             return true;
47
         }
47
         }
54
     }
54
     }
55
 
55
 
56
     /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
56
     /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
57
-    pub fn print_recursive(&self, to_pid: u32, output: &mut String) {
57
+    pub fn print_recursive(&self, to_pid: i32, output: &mut String) {
58
 
58
 
59
         if output.len() == 0 {
59
         if output.len() == 0 {
60
             *output = format!("{}({}){}", self.name, self.pid, output);
60
             *output = format!("{}({}){}", self.name, self.pid, output);
70
 
70
 
71
 /// Geht von eigenem Prozess aus und gibt die Prozesskette bis zum übergebenem PID aus
71
 /// Geht von eigenem Prozess aus und gibt die Prozesskette bis zum übergebenem PID aus
72
 /// und fängt mögliche Fehler ab.
72
 /// und fängt mögliche Fehler ab.
73
-pub fn print(pid: u32) -> bool {
73
+pub fn print(pid: i32) -> bool {
74
 
74
 
75
     if let Err(_) = pid::stat(pid) {
75
     if let Err(_) = pid::stat(pid) {
76
         println!("Invalid PID");
76
         println!("Invalid PID");

+ 5
- 1
hw5/task1/src/main.rs Ver fichero

15
 
15
 
16
         match procinfo::pid::stat_self(){
16
         match procinfo::pid::stat_self(){
17
             Ok(stat) => {
17
             Ok(stat) => {
18
-                child::run_childs(stat.pid, &arguments[2]);
18
+                let result = child::run_childs(stat.pid, &arguments[2]);
19
+                match result {
20
+                    Ok(_) => {},
21
+                    Err(_) => {},
22
+                }
19
             },
23
             },
20
             Err(_) => {},
24
             Err(_) => {},
21
         }
25
         }

Loading…
Cancelar
Guardar