The ProcTable class encapsulates process table information.
The version of the sys-proctable library
Returns an array of fields that each ProcTableStruct will contain. This may be useful if you want to know in advance what fields are available without having to perform at least one read of the /proc table.
Example:
Sys::ProcTable.fields.each{ |field| puts "Field: #{field}" }
# File lib/linux/sys/proctable.rb, line 256 def self.fields @fields end
In block form, yields a ProcTableStruct for each process entry that you have rights to. This method returns an array of ProcTableStruct's in non-block form.
If a pid is provided, then only a single ProcTableStruct is yielded or returned, or nil if no process information is found for that pid.
Example:
# Iterate over all processes ProcTable.ps do |proc_info| p proc_info end # Print process table information for only pid 1001 p ProcTable.ps(1001)
# File lib/linux/sys/proctable.rb, line 104 def self.ps(pid=nil) array = block_given? ? nil : [] struct = nil raise TypeError unless pid.is_a?(Fixnum) if pid Dir.foreach("/proc"){ |file| next if file =~ /\D/ # Skip non-numeric directories next unless file.to_i == pid if pid struct = ProcTableStruct.new # Get /proc/<pid>/cmdline information. Strip out embedded nulls. begin data = IO.read("/proc/#{file}/cmdline").tr("\0000", ' ').strip struct.cmdline = data rescue next # Process terminated, on to the next process end # Get /proc/<pid>/cwd information struct.cwd = File.readlink("/proc/#{file}/cwd") rescue nil # Get /proc/<pid>/environ information. Environment information # is represented as a Hash, with the environment variable as the # key and its value as the hash value. struct.environ = {} begin IO.read("/proc/#{file}/environ").split("\00"").each{ |str| key, value = str.split('=') struct.environ[key] = value } rescue Errno::EACCES, Errno::ESRCH, Errno::ENOENT # Ignore and move on. end # Get /proc/<pid>/exe information struct.exe = File.readlink("/proc/#{file}/exe") rescue nil # Get /proc/<pid>/fd information. File descriptor information # is represented as a Hash, with the fd as the key, and its # symlink as the value. struct.fd = {} begin Dir["/proc/#{file}/fd/*"].each do |fd| struct.fd[File.basename(fd)] = File.readlink(fd) rescue nil end rescue # Ignore and move on end # Get /proc/<pid>/root information struct.root = File.readlink("/proc/#{file}/root") rescue nil # Get /proc/<pid>/stat information stat = IO.read("/proc/#{file}/stat") rescue next # Deal with spaces in comm name. Courtesy of Ara Howard. re = /\([^\)]+\)/ comm = stat[re] comm.tr!(' ', '-') stat[re] = comm stat = stat.split struct.pid = stat[0].to_i struct.comm = stat[1].tr('()','') # Remove parens struct.state = stat[2] struct.ppid = stat[3].to_i struct.pgrp = stat[4].to_i struct.session = stat[5].to_i struct.tty_nr = stat[6].to_i struct.tpgid = stat[7].to_i struct.flags = stat[8].to_i struct.minflt = stat[9].to_i struct.cminflt = stat[10].to_i struct.majflt = stat[11].to_i struct.cmajflt = stat[12].to_i struct.utime = stat[13].to_i struct.stime = stat[14].to_i struct.cutime = stat[15].to_i struct.cstime = stat[16].to_i struct.priority = stat[17].to_i struct.nice = stat[18].to_i # Skip 19 struct.itrealvalue = stat[20].to_i struct.starttime = stat[21].to_i struct.vsize = stat[22].to_i struct.rss = stat[23].to_i struct.rlim = stat[24].to_i struct.startcode = stat[25].to_i struct.endcode = stat[26].to_i struct.startstack = stat[27].to_i struct.kstkesp = stat[28].to_i struct.kstkeip = stat[29].to_i struct.signal = stat[30].to_i struct.blocked = stat[31].to_i struct.sigignore = stat[32].to_i struct.sigcatch = stat[33].to_i struct.wchan = stat[34].to_i struct.nswap = stat[35].to_i struct.cnswap = stat[36].to_i struct.exit_signal = stat[37].to_i struct.processor = stat[38].to_i struct.rt_priority = stat[39].to_i struct.policy = stat[40].to_i # Get /proc/<pid>/status information (name, uid, euid, gid, egid) IO.foreach("/proc/#{file}/status") do |line| case line when /Name:\s*?(\w+)/ struct.name = $1 when /Uid:\s*?(\d+)\s*?(\d+)/ struct.uid = $1.to_i struct.euid = $2.to_i when /Gid:\s*?(\d+)\s*?(\d+)/ struct.gid = $1.to_i struct.egid = $2.to_i end end # If cmdline is empty use comm instead struct.cmdline = struct.comm if struct.cmdline.empty? # Manually calculate CPU and memory usage struct.pctcpu = get_pctcpu(struct.utime, struct.starttime) struct.pctmem = get_pctmem(struct.rss) struct.freeze # This is read-only data if block_given? yield struct else array << struct end } pid ? struct : array end
Generated with the Darkfish Rdoc Generator 2.