2013-03-02 58 views
0

我想在MonoDevelop中使用Mono C#從/ proc/stat讀取/ proc/stat中的系統信息。但是當我使用FileInfo時,我得到0作爲長度,並且我使用StreamReader我得到了空字符串(編輯:oops,關於StreamReader不工作的那一點竟然是不真實的)。我如何打開文件並讀取數據?使用Mono獲取在Ubuntu中的proc信息

順便說一句,當我使用終端,運行「貓/ 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\[email protected]\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\[email protected]\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\[email protected]\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) 
+1

嘗試下'strace'運行您的程序,並告訴我們哪些系統調用它正在發出讀取文件。 – 2013-03-02 12:51:03

+0

@John Zwinck,我已經包含strace信息 – unruledboy 2013-03-02 13:44:31

+0

@John Zwinck,對不起,我想到了,實際上沒有這些「特殊」文件的長度,因爲它們不是真正的文件。不過,我仍然可以通過StreamReader讀取這些文件的內容。我的錯。我使用worng參數。 – unruledboy 2013-03-02 13:50:54

回答

0

我終於想通了。因爲proc虛擬文件系統中的文件如/proc/stat並不是真正的文件。長度爲零是正常的。

所有我需要做的就是用StreamReader讀取文件的內容,無論是逐行(ReadLine)或整(ReadToEnd

+1

你確定欺騙了我 - 你在你的問題中說StreamReader給你一個空字符串。我想那不是這種情況? – 2013-03-02 14:48:37

+0

非常抱歉,我確實犯了一個錯誤。我沒有輸出StreamReader結果,因爲FileInfo.Length = 0混淆了我的想法。 – unruledboy 2013-03-04 00:18:03