Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. extern crate rand;
  2. extern crate nix;
  3. use rand::Rng;
  4. use std::io;
  5. use std::process::exit;
  6. use std::thread::sleep;
  7. use std::time::{Duration, Instant};
  8. use nix::sys::wait::WaitStatus;
  9.  
  10. use nix::sys::wait::waitpid;
  11. use nix::unistd::{fork, getpid, getppid, ForkResult};
  12.  
  13. static mut process_count: i32 = 0;
  14. static mut thread_count: i32 = 0;
  15.  
  16.  
  17. fn generate_array(len: usize)->Vec<f64>
  18. {
  19. let mut my_vector: Vec<f64> = Vec::new();
  20. for _ in 0..len {
  21. my_vector.push(rand::thread_rng().gen_range(0.0, 100.0)); // 0 - 99.99999
  22. }
  23. return my_vector;
  24. }
  25.  
  26. fn get_array_size_from_user()->usize
  27. {
  28. let mut n = String::new();
  29. io::stdin()
  30. .read_line(&mut n)
  31. .expect("failed to read input.");
  32. let n: usize = n.trim().parse().expect("invalid input");
  33.  
  34. return n;
  35. }
  36.  
  37. fn display_array(array: &mut Vec<f64>)
  38. {
  39. println!("{:?}", array);
  40. println!();
  41. }
  42.  
  43. fn clear_screen()
  44. {
  45. print!("{}[2J", 27 as char);
  46. //print!("\x1B[2J"); // 2nd option
  47. }
  48.  
  49. pub fn mergeSort(a: &mut Vec<f64>, low: usize, high: usize)
  50. {
  51. let middle=(low+high)/2;
  52. let mut len=(high-low+1);
  53.  
  54. if (len<2)
  55. {
  56. return;
  57. }
  58.  
  59. let lpid = fork();
  60.  
  61.  
  62. match lpid{
  63.  
  64. Ok(ForkResult::Child)=>
  65. {
  66. println!("Left Process Running ");
  67. mergeSort(a,low,middle);
  68. exit(0);
  69. }
  70. Ok(ForkResult::Parent {child})=>
  71. {
  72.  
  73. let rpid = fork();
  74. match rpid{
  75.  
  76.  
  77. Ok(ForkResult::Child)=>
  78. {
  79. println!("Right Process Running ");
  80. mergeSort(a,middle+1,high);
  81. exit(0);
  82. }
  83.  
  84. Ok(ForkResult::Parent { child, .. })=>
  85. {
  86. let wait_status_r= waitpid(child, None);
  87. match wait_status_r {
  88. // assert that waitpid returned correct status and the pid is the one of the child
  89. Ok(WaitStatus::Exited(pid, status)) => {
  90. println!("right process with pid {} exited with status: {}", pid, status);
  91. },
  92.  
  93. // panic, must never happen
  94. Ok(_) => panic!("Child still alive, should never happen"),
  95.  
  96. // panic, waitpid should never fail
  97. Err(_) => panic!("Error: right waitpid Failed")
  98. }
  99. }
  100. Err(err) => {
  101. panic!("Right process not created: {}",err);
  102. }
  103.  
  104. };
  105. let wait_status_l= waitpid(child, None);
  106. match wait_status_l {
  107. // assert that waitpid returned correct status and the pid is the one of the child
  108. Ok(WaitStatus::Exited(pid, status)) => {
  109. println!("left process with pid {} exited with status: {}", pid, status);
  110. },
  111.  
  112. // panic, must never happen
  113. Ok(_) => panic!("Child still alive, should never happen"),
  114.  
  115. // panic, waitpid should never fail
  116. Err(_) => panic!("Error: left waitpid Failed")
  117. }
  118.  
  119. }
  120.  
  121. Err(err) =>{
  122. panic!("Left process not created {}",err);
  123. }
  124.  
  125. };
  126.  
  127. // Wait for child processes to finish
  128. // let wait_status_l= waitpid(child, None);
  129. // match wait_status_l {
  130. // // assert that waitpid returned correct status and the pid is the one of the child
  131. // Ok(WaitStatus::Exited(pid, status)) => {
  132. // println!("child process with pid {} exited with status: {}", pid, status);
  133. // },
  134.  
  135. // // panic, must never happen
  136. // Ok(_) => panic!("Child still alive, should never happen"),
  137.  
  138. // // panic, waitpid should never fail
  139. // Err(_) => panic!("Error: waitpid Failed")
  140. // }
  141. // let wait_status_r= waitpid(rpid, None);
  142. // match wait_status_r {
  143. // // assert that waitpid returned correct status and the pid is the one of the child
  144. // Ok(WaitStatus::Exited(pid, status)) => {
  145. // println!("child process with pid {} exited with status: {}", pid, status);
  146. // },
  147.  
  148. // // panic, must never happen
  149. // Ok(_) => panic!("Child still alive, should never happen"),
  150.  
  151. // // panic, waitpid should never fail
  152. // Err(_) => panic!("Error: waitpid Failed")
  153. // }
  154. // Merge the sorted subarrays
  155. merge(a, low, middle, high);
  156.  
  157. }
  158.  
  159.  
  160. fn merge(a: &mut Vec<f64>, low: usize, m:usize, high:usize)
  161. {
  162. println!("x");
  163. let mut left = a[low..m+1].to_vec();
  164. let mut right = a[m+1..high+1].to_vec();
  165.  
  166. println!("left: {:?}",left);
  167. println!("right: {:?}",right);
  168.  
  169.  
  170. left.reverse();
  171. right.reverse();
  172.  
  173.  
  174.  
  175. for k in low..high + 1 {
  176. if left.is_empty() {
  177. a[k] = right.pop().unwrap();
  178. continue;
  179. }
  180. if right.is_empty() {
  181. a[k] = left.pop().unwrap();
  182. continue;
  183. }
  184. if right.last() < left.last() {
  185. a[k] = right.pop().unwrap();
  186. }
  187. else {
  188. a[k] = left.pop().unwrap();
  189. }
  190. }
  191.  
  192. println!("array: {:?}",a);
  193. }
  194.  
  195. unsafe fn display_process_thread_counts()
  196. {
  197. unsafe{
  198. println!("process count:");
  199. println!("{}", process_count);
  200. println!("thread count:");
  201. println!("{}", thread_count);
  202. }
  203. }
  204.  
  205. unsafe fn process_count_plus_plus()
  206. {
  207. process_count += 1;
  208. }
  209.  
  210. unsafe fn thread_count_plus_plus()
  211. {
  212. thread_count += 1;
  213. }
  214.  
  215. fn print_time(start: Instant, end: Instant)
  216. {
  217. println!("TIME:");
  218. println!("{:?}", end.checked_duration_since(start));
  219. }
  220.  
  221. fn main() {
  222.  
  223. println!("ENTER SIZE OF ARRAY \n");
  224. let array_size = get_array_size_from_user();
  225.  
  226. let mut generated_array = generate_array(array_size);
  227.  
  228. clear_screen();
  229.  
  230. println!("GENERATED ARRAY: \n");
  231. display_array(&mut generated_array);
  232.  
  233. // SORTING
  234. let start = Instant::now();
  235. mergeSort(&mut generated_array, 0, array_size - 1);
  236. let end = Instant::now();
  237.  
  238. // RESULT
  239.  
  240. //unsafe{
  241. // process_count_plus_plus();
  242. // thread_count_plus_plus();
  243. //}
  244.  
  245.  
  246. println!("SORTED ARRAY: \n");
  247. display_array(&mut generated_array);
  248.  
  249. print_time(start, end);
  250.  
  251. unsafe{
  252. display_process_thread_counts();
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement