Debian Codegraph @

debian/rust-vendor/procfs/examples/ps.rs

raw
1 #![allow(clippy::print_literal)]
2
3 extern crate procfs;
4
5 /// A very basic clone of `ps` on Linux, in the simple no-argument mode.
6 /// It shows all the processes that share the same tty as our self
7
8 fn main() {
9 let mestat = procfs::process::Process::myself().unwrap().stat().unwrap();
10 let tps = procfs::ticks_per_second();
11
12 println!("{: >10} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD");
13
14 let tty = format!("pty/{}", mestat.tty_nr().1);
15 for p in procfs::process::all_processes().unwrap() {
16 let prc = p.unwrap();
17 if let Ok(stat) = prc.stat() {
18 if stat.tty_nr == mestat.tty_nr {
19 // total_time is in seconds
20 let total_time = (stat.utime + stat.stime) as f32 / (tps as f32);
21 println!("{: >10} {: <8} {: >8} {}", stat.pid, tty, total_time, stat.comm);
22 }
23 }
24 }
25 }