在Ubuntu中使用Mono获取进程信息
本文关键字:获取 取进程 信息 Mono Ubuntu | 更新日期: 2023-09-27 18:02:05
我试图从/proc/stat在Ubuntu中使用MonoDevelop的Mono c#读取系统信息。但是当我使用FileInfo时,我得到0作为长度,我使用StreamReader我得到空字符串(编辑:哎呀,关于StreamReader不工作的那一点被证明是不真实的)。我如何打开文件并读取数据?
顺便说一句,当我使用终端时,运行"cat/proc/stat"我可以得到数据。我是否需要配置MonoDevelop才能正确访问?
我找了一整天也想不出来。
我使用strace获取以下诊断信息:
Process 2430 attached - interrupt to quit
read(0, "'n", 1024) = 1
gettimeofday({1362231405, 892525}, NULL) = 0
gettimeofday({1362231405, 893679}, NULL) = 0
gettimeofday({1362231405, 893917}, NULL) = 0
gettimeofday({1362231405, 894218}, NULL) = 0
gettimeofday({1362231405, 894372}, NULL) = 0
gettimeofday({1362231405, 894598}, NULL) = 0
gettimeofday({1362231405, 894722}, NULL) = 0
gettimeofday({1362231405, 895283}, NULL) = 0
gettimeofday({1362231405, 895466}, NULL) = 0
gettimeofday({1362231405, 895670}, NULL) = 0
gettimeofday({1362231405, 895801}, NULL) = 0
gettimeofday({1362231405, 896040}, NULL) = 0
gettimeofday({1362231405, 896242}, NULL) = 0
gettimeofday({1362231405, 897729}, NULL) = 0
gettimeofday({1362231405, 897898}, NULL) = 0
gettimeofday({1362231405, 898337}, NULL) = 0
gettimeofday({1362231405, 898481}, NULL) = 0
gettimeofday({1362231405, 898941}, NULL) = 0
gettimeofday({1362231405, 899116}, NULL) = 0
gettimeofday({1362231405, 900025}, NULL) = 0
gettimeofday({1362231405, 900163}, NULL) = 0
gettimeofday({1362231405, 900562}, NULL) = 0
gettimeofday({1362231405, 900721}, NULL) = 0
gettimeofday({1362231405, 900952}, NULL) = 0
gettimeofday({1362231405, 901109}, NULL) = 0
gettimeofday({1362231405, 901409}, NULL) = 0
gettimeofday({1362231405, 901534}, NULL) = 0
gettimeofday({1362231405, 902604}, NULL) = 0
write(1, "'n", 1) = 1
gettimeofday({1362231405, 902943}, NULL) = 0
gettimeofday({1362231405, 903281}, NULL) = 0
gettimeofday({1362231405, 903473}, NULL) = 0
gettimeofday({1362231405, 903724}, NULL) = 0
gettimeofday({1362231405, 903849}, NULL) = 0
gettimeofday({1362231405, 904343}, NULL) = 0
gettimeofday({1362231405, 904488}, NULL) = 0
gettimeofday({1362231405, 905391}, NULL) = 0
gettimeofday({1362231405, 905751}, NULL) = 0
gettimeofday({1362231405, 906501}, NULL) = 0
mprotect(0xb7216000, 4096, PROT_NONE) = 0
tgkill(2430, 2432, SIGRT_3) = 0
send(3, "'0'0'0'35'0'0'0e'0@d'2'0'0'0'1'f'0'0'0'10'0'0'0'1'0'0'0'216", 29, 0) = 29
futex(0x832fde4, FUTEX_WAIT_PRIVATE, 123, NULL) = 0
futex(0x832fdbc, FUTEX_WAKE_PRIVATE, 1) = 0
gettimeofday({1362231405, 909583}, NULL) = 0
gettimeofday({1362231405, 909900}, NULL) = 0
gettimeofday({1362231405, 910150}, NULL) = 0
gettimeofday({1362231405, 910444}, NULL) = 0
gettimeofday({1362231405, 910665}, NULL) = 0
gettimeofday({1362231405, 910957}, NULL) = 0
gettimeofday({1362231405, 911207}, NULL) = 0
gettimeofday({1362231405, 911632}, NULL) = 0
gettimeofday({1362231405, 911769}, NULL) = 0
gettimeofday({1362231405, 914613}, NULL) = 0
gettimeofday({1362231405, 914716}, NULL) = 0
gettimeofday({1362231405, 914840}, NULL) = 0
gettimeofday({1362231405, 915021}, NULL) = 0
gettimeofday({1362231405, 915133}, NULL) = 0
gettimeofday({1362231405, 915199}, NULL) = 0
gettimeofday({1362231405, 915397}, NULL) = 0
gettimeofday({1362231405, 915474}, NULL) = 0
gettimeofday({1362231405, 915606}, NULL) = 0
gettimeofday({1362231405, 915671}, NULL) = 0
gettimeofday({1362231405, 916001}, NULL) = 0
gettimeofday({1362231405, 916076}, NULL) = 0
gettimeofday({1362231405, 916232}, NULL) = 0
mprotect(0xb7216000, 4096, PROT_NONE) = 0
tgkill(2430, 2432, SIGRT_3) = 0
send(3, "'0'0'0'35'0'0'0f'0@d'2'0'0'0'1'f'0'0'0'10'0'0'0'1'0'0'0'217", 29, 0) = 29
futex(0x832fde4, FUTEX_WAIT_PRIVATE, 125, NULL) = 0
futex(0x832fdbc, FUTEX_WAKE_PRIVATE, 1) = 0
gettimeofday({1362231405, 918417}, NULL) = 0
gettimeofday({1362231405, 918589}, NULL) = 0
gettimeofday({1362231405, 918656}, NULL) = 0
gettimeofday({1362231405, 918780}, NULL) = 0
gettimeofday({1362231405, 918845}, NULL) = 0
gettimeofday({1362231405, 919729}, NULL) = 0
gettimeofday({1362231405, 919810}, NULL) = 0
gettimeofday({1362231405, 920057}, NULL) = 0
mprotect(0xb7216000, 4096, PROT_NONE) = 0
tgkill(2430, 2432, SIGRT_3) = 0
send(3, "'0'0'0'35'0'0'0g'0@d'2'0'0'0'1'f'0'0'0'10'0'0'0'1'0'0'0'221", 29, 0) = 29
futex(0x832fde4, FUTEX_WAIT_PRIVATE, 127, NULL) = 0
futex(0x832fdbc, FUTEX_WAKE_PRIVATE, 1) = 0
gettimeofday({1362231405, 921916}, NULL) = 0
gettimeofday({1362231405, 923160}, NULL) = 0
gettimeofday({1362231405, 923235}, NULL) = 0
gettimeofday({1362231405, 923442}, NULL) = 0
gettimeofday({1362231405, 923511}, NULL) = 0
gettimeofday({1362231405, 923631}, NULL) = 0
stat64("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
lstat64("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
gettimeofday({1362231405, 924059}, NULL) = 0
gettimeofday({1362231405, 925596}, NULL) = 0
gettimeofday({1362231405, 925692}, NULL) = 0
gettimeofday({1362231405, 926101}, NULL) = 0
gettimeofday({1362231405, 926169}, NULL) = 0
gettimeofday({1362231405, 926331}, NULL) = 0
gettimeofday({1362231405, 926397}, NULL) = 0
gettimeofday({1362231405, 926567}, NULL) = 0
gettimeofday({1362231405, 926634}, NULL) = 0
gettimeofday({1362231405, 926812}, NULL) = 0
gettimeofday({1362231405, 926881}, NULL) = 0
gettimeofday({1362231405, 927064}, NULL) = 0
gettimeofday({1362231405, 927133}, NULL) = 0
gettimeofday({1362231405, 929830}, NULL) = 0
gettimeofday({1362231405, 929928}, NULL) = 0
gettimeofday({1362231405, 930219}, NULL) = 0
gettimeofday({1362231405, 930288}, NULL) = 0
gettimeofday({1362231405, 930468}, NULL) = 0
gettimeofday({1362231405, 930535}, NULL) = 0
gettimeofday({1362231405, 930685}, NULL) = 0
gettimeofday({1362231405, 930758}, NULL) = 0
gettimeofday({1362231405, 931120}, NULL) = 0
gettimeofday({1362231405, 931189}, NULL) = 0
gettimeofday({1362231405, 931454}, NULL) = 0
gettimeofday({1362231405, 931522}, NULL) = 0
gettimeofday({1362231405, 931919}, NULL) = 0
gettimeofday({1362231405, 932012}, NULL) = 0
gettimeofday({1362231405, 935736}, NULL) = 0
gettimeofday({1362231405, 935883}, NULL) = 0
gettimeofday({1362231405, 936099}, NULL) = 0
gettimeofday({1362231405, 936164}, NULL) = 0
gettimeofday({1362231405, 936392}, NULL) = 0
gettimeofday({1362231405, 936459}, NULL) = 0
gettimeofday({1362231405, 936590}, NULL) = 0
gettimeofday({1362231405, 936672}, NULL) = 0
gettimeofday({1362231405, 936858}, NULL) = 0
gettimeofday({1362231405, 936976}, NULL) = 0
gettimeofday({1362231405, 944184}, NULL) = 0
stat64("/proc", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
lstat64("/proc", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
gettimeofday({1362231405, 944471}, NULL) = 0
gettimeofday({1362231405, 944635}, NULL) = 0
gettimeofday({1362231405, 944701}, NULL) = 0
gettimeofday({1362231405, 944850}, NULL) = 0
gettimeofday({1362231405, 944917}, NULL) = 0
gettimeofday({1362231405, 945178}, NULL) = 0
gettimeofday({1362231405, 945252}, NULL) = 0
gettimeofday({1362231405, 945670}, NULL) = 0
stat64("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
lstat64("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
gettimeofday({1362231405, 945984}, NULL) = 0
gettimeofday({1362231405, 946121}, NULL) = 0
stat64("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
lstat64("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
open("/proc/stat", O_RDONLY|O_LARGEFILE) = 4
fstat64(4, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
time(NULL) = 1362231405
gettimeofday({1362231405, 946703}, NULL) = 0
gettimeofday({1362231405, 947180}, NULL) = 0
gettimeofday({1362231405, 947347}, NULL) = 0
gettimeofday({1362231405, 947577}, NULL) = 0
gettimeofday({1362231405, 947739}, NULL) = 0
gettimeofday({1362231405, 948003}, NULL) = 0
fstat64(4, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
gettimeofday({1362231405, 948364}, NULL) = 0
gettimeofday({1362231405, 948855}, NULL) = 0
read(4, "cpu 11250 88 9225 32838 60 0 31"..., 4096) = 1729
gettimeofday({1362231405, 954504}, NULL) = 0
gettimeofday({1362231405, 954819}, NULL) = 0
gettimeofday({1362231405, 955084}, NULL) = 0
gettimeofday({1362231405, 955308}, NULL) = 0
gettimeofday({1362231405, 955494}, NULL) = 0
gettimeofday({1362231405, 955813}, NULL) = 0
gettimeofday({1362231405, 956022}, NULL) = 0
gettimeofday({1362231405, 956228}, NULL) = 0
gettimeofday({1362231405, 956389}, NULL) = 0
gettimeofday({1362231405, 956757}, NULL) = 0
gettimeofday({1362231405, 956985}, NULL) = 0
gettimeofday({1362231405, 957218}, NULL) = 0
close(4) = 0
gettimeofday({1362231405, 957587}, NULL) = 0
gettimeofday({1362231405, 957748}, NULL) = 0
gettimeofday({1362231405, 957964}, NULL) = 0
gettimeofday({1362231405, 958174}, NULL) = 0
select(1, [0], NULL, NULL, {0, 0}) = 0 (Timeout)
write(1, "'33[6n", 4) = 4
select(1, [0], NULL, NULL, {1, 0}) = 1 (in [0], left {0, 990314})
ioctl(0, FIONREAD, [6]) = 0
read(0, "'33[4;1R", 1024) = 6
select(1, [0], NULL, NULL, {0, 150}) = 0 (Timeout)
read(0, "'n", 1024) = 1
write(1, "'n", 1) = 1
gettimeofday({1362231418, 517172}, NULL) = 0
gettimeofday({1362231418, 517684}, NULL) = 0
gettimeofday({1362231418, 517833}, NULL) = 0
gettimeofday({1362231418, 518094}, NULL) = 0
gettimeofday({1362231418, 518231}, NULL) = 0
gettimeofday({1362231418, 518500}, NULL) = 0
gettimeofday({1362231418, 518675}, NULL) = 0
gettimeofday({1362231418, 518999}, NULL) = 0
select(1, [0], NULL, NULL, {0, 150}) = 0 (Timeout)
我终于想通了。因为在proc虚拟文件系统中,像/proc/stat
这样的文件并不是真正的文件。长度为零是正常的
我所需要做的就是使用StreamReader
来读取文件内容,无论是逐行(ReadLine
)还是整行(ReadToEnd
)