Browse Source

Finished child part of task1

themultiplexer 8 years ago
parent
commit
3720b0a9d2
3 changed files with 48 additions and 22 deletions
  1. 29
    19
      hw5/task1/src/child/mod.rs
  2. 11
    3
      hw5/task1/src/main.rs
  3. 8
    0
      hw5/task1/src/unit_tests.rs

+ 29
- 19
hw5/task1/src/child/mod.rs View File

1
 use nix::unistd::{fork, getpid};
1
 use nix::unistd::{fork, getpid};
2
 use nix::sys::wait::wait;
2
 use nix::sys::wait::wait;
3
+use nix::sys::wait::WaitStatus;
3
 use nix::unistd::ForkResult::{Child, Parent};
4
 use nix::unistd::ForkResult::{Child, Parent};
4
 
5
 
5
 mod pstree;
6
 mod pstree;
8
     let count = arg.parse::<u8>();
9
     let count = arg.parse::<u8>();
9
     match count {
10
     match count {
10
         Ok(value) => {
11
         Ok(value) => {
11
-            for i in 0..value {
12
-                let pid = fork();
13
-                match pid {
14
-                    Ok(Child) => {
15
-                        println!("hello, I am child (pid:{})", getpid());
16
-                    }
17
-                    Ok(Parent { child }) => {
18
-                        println!("hello, I am parent of {} (pid:{})", child, getpid());
19
-                        match wait(){
20
-                            Ok(ws) => { println!("{:?}", ws); }
21
-                            Err(_) => {}
22
-                        }
23
-                    }
24
-                    // panic, fork should never fail unless there is a
25
-                    // serious problem with the OS
26
-                    Err(_) => panic!("fork failed"),
27
-                }
28
-            }
29
-            pstree::print(start_pid);
12
+
13
+            if value > 0 { }
14
+            fork_children(0, value - 1, start_pid);
30
             Ok(())
15
             Ok(())
31
         },
16
         },
32
         Err(_) => {
17
         Err(_) => {
33
             Err("Parse Failed".to_string())
18
             Err("Parse Failed".to_string())
34
         },
19
         },
35
     }
20
     }
21
+}
22
+
23
+pub fn fork_children(count: u8, to:u8, start_pid: i32) {
24
+    let pid = fork();
25
+    match pid {
26
+        Ok(Child) => {
27
+            println!("hello, I am child (pid:{})", getpid());
28
+            if count < to {
29
+                fork_children(count + 1, to, start_pid);
30
+            } else {
31
+                println!();
32
+                pstree::print(start_pid);
33
+            }
34
+        }
35
+
36
+        Ok(Parent { child }) => {
37
+            if let Ok(ws) = wait() {
38
+                if let WaitStatus::Exited(child_pid, exit_code) = ws {
39
+                    println!("I am {} and my child is {}. After I waited for {}, it sent me status {:?}", getpid(), child, child_pid, exit_code);
40
+                }
41
+            }
42
+        }
43
+
44
+        Err(_) => {}
45
+    }
36
 }
46
 }

+ 11
- 3
hw5/task1/src/main.rs View File

2
 extern crate nix;
2
 extern crate nix;
3
 
3
 
4
 use std::env::args;
4
 use std::env::args;
5
+use std::process;
5
 
6
 
7
+mod unit_tests;
6
 mod zombie;
8
 mod zombie;
7
 mod child;
9
 mod child;
8
 
10
 
18
                 let result = child::run_childs(stat.pid, &arguments[1]);
20
                 let result = child::run_childs(stat.pid, &arguments[1]);
19
                 match result {
21
                 match result {
20
                     Ok(_) => {},
22
                     Ok(_) => {},
21
-                    Err(_) => {},
23
+                    Err(e) => {
24
+                        println!("{}", e);
25
+                        process::exit(1)
26
+                    },
22
                 }
27
                 }
23
             },
28
             },
24
-            Err(_) => {},
29
+            Err(_) => {
30
+                println!("Couldn't retrieve my own PID.");
31
+                process::exit(1)
32
+            },
25
         }
33
         }
26
 
34
 
27
 
35
 
28
     } else {
36
     } else {
29
-        //zombie::run_zombie();
37
+        zombie::run_zombie();
30
     }
38
     }
31
 
39
 
32
 }
40
 }

+ 8
- 0
hw5/task1/src/unit_tests.rs View File

1
+#[cfg(test)]
2
+mod tests {
3
+    use child::*;
4
+
5
+    fn test_zero_forks(){
6
+        assert_eq!(run_childs(123,"2"))
7
+    }
8
+}

Loading…
Cancel
Save