From: Joe Van Dyk Date: 2005-11-17T14:18:15+09:00 Subject: Re: Improving code... On 11/16/05, Gregory Brown wrote: > On 11/16/05, Joe Van Dyk wrote: > > > # See 'man proc' for details. > > def get_process_stats > > proc_stats = File.read("/proc/#{ @pid }/stat") > > > > # EEWWWW -- gets the stuff from /proc//stat and puts it into vars > > pid, comm, state, ppid, pgrp, session, tty_nr, tpgid, flags, > > minflt, cminflt, majflt, cmajflt, utime, stime, cutime, > > cstime, priority, nice, who_cares, itrealvade, > > starttime, vsize, rss, rlim, startcode, endcode, startstack, > > kstkesp, kstkeip, signal, blocked, sigignore, sigcatch, > > wchan, nswap, cnswap, exit_signal, processor = > > proc_stats.scanf("%d %s %c %d %d %d %d %d %d %d \ > > %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d \ > > %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d") > > proc_stats = File.read("/proc/#{ @pid }/stat").split(" ") > > you might need to change your split to match the appropriate pattern, > matching a space above. This makes proc_stats an array > > > # Just put the ones I need into the hash > > stats = {} > > stats[:system_cpu_time] = stime > > stats[:user_cpu_time] = utime > > stats[:processor] = processor > > stats > > end > > end > > stats = { :system_cpu_time => proc_stats[14], > :user_cpu_time => proc_stats[13], > :processor => proc_stats.last } > > the indicies might not be correct above, but hopefully the idea works ;) That is *exactly* what I had before I decided to use scanf. scanf has the advantage of automatically converting everything to the correct type. But I'm not sure what's best in this case. Later on, I might need to get more of the process information, so I thought getting all the values, and then just putting the ones I need into the hash would be better. Which version would people rather work with?