Kaynağa Gözat

load extern crates in main.rs

themultiplexer 8 yıl önce
ebeveyn
işleme
6df4aba92e

+ 2
- 0
hw5/task1/src/child/mod.rs Dosyayı Görüntüle

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

+ 6
- 6
hw5/task1/src/child/pstree.rs Dosyayı Görüntüle

@@ -3,13 +3,13 @@ use procinfo::pid;
3 3
 /// Datenstruktur für einen Prozess.
4 4
 pub struct Process {
5 5
     name: String,
6
-    pid: u32,
7
-    ppid: u32,
6
+    pid: i32,
7
+    ppid: i32,
8 8
 }
9 9
 
10 10
 impl Process {
11 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 13
         if let Ok(stat) = pid::stat(with_pid) {
14 14
             Process {
15 15
                 name: stat.command,
@@ -41,7 +41,7 @@ impl Process {
41 41
     }
42 42
 
43 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 45
         if self.pid == pid {
46 46
             return true;
47 47
         }
@@ -54,7 +54,7 @@ impl Process {
54 54
     }
55 55
 
56 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 59
         if output.len() == 0 {
60 60
             *output = format!("{}({}){}", self.name, self.pid, output);
@@ -70,7 +70,7 @@ impl Process {
70 70
 
71 71
 /// Geht von eigenem Prozess aus und gibt die Prozesskette bis zum übergebenem PID aus
72 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 75
     if let Err(_) = pid::stat(pid) {
76 76
         println!("Invalid PID");

+ 5
- 1
hw5/task1/src/main.rs Dosyayı Görüntüle

@@ -15,7 +15,11 @@ fn main() {
15 15
 
16 16
         match procinfo::pid::stat_self(){
17 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 24
             Err(_) => {},
21 25
         }

Loading…
İptal
Kaydet