2017-01-17 91 views
0

我試圖mmap()文件描述符到內存,但我得到錯誤。mmap返回'沒有這樣的文件或目錄'

LOG_BUFFER_SIZE被定義爲500

printf("fcntl fd check: %d\n", fcntl(ss->fd_log, F_GETFD)); 
ss->log_mmap = mmap(NULL, LOG_BUFFER_SIZE, PROT_WRITE | PROT_WRITE, MAP_PRIVATE, ss->fd_log, 0); 

if (ss->data_mmap == NULL){ 
    printf("mmap() error: %s (errno=%d)\n", strerror(errno), errno); 
    exit(EXIT_FAILURE); 
} 

的文件描述符是有效的,因爲我已經檢查:

fcntl fd check: 1 
mmap() error for fd #6: No such file or directory (errno=2) 

這是使用strace:

access("../ss_data/SunLoadRight", F_OK) = -1 ENOENT (No such file or directory) 
open("../ss_data/log/SunLoadRight.log", O_RDWR|O_CREAT|O_APPEND|O_CLOEXEC, 0662) = 6 
fcntl(6, F_GETFD)      = 0x1 (flags FD_CLOEXEC) 
write(1, "fcntl fd check: 1\n", 18fcntl fd check: 1 
)  = 18 
mmap(NULL, 500, PROT_WRITE, MAP_PRIVATE, 6, 0) = 0x7f624c426000 
write(1, "mmap() error for fd #6: No such "..., 60mmap() error for fd #6: No such file or directory (errno=2) 
) = 60 
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0 
rt_sigaction(SIGPROF, {SIG_DFL, [], SA_RESTORER, 0x7f624be704b0}, NULL, 8) = 0 
open("gmon.out", O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0666) = 7 
write(7, "gmon\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20) = 20 
writev(7, [{"\0", 1}, {"\320\[email protected]\0\0\0\0\0\354\[email protected]\0\0\0\0\0H7\0\0d\0\0\0seconds\0"..., 40}, {"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 28304}], 3) = 28345 
writev(7, [{"\1", 1}, {"\320*@\0\0\0\0\0$\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\300,@\0\0\0\0\0A\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\320,@\0\0\0\0\0\222\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\340,@\0\0\0\0\0n\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\360,@\0\0\0\0\0\341\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"[email protected]\0\0\0\0\0\367\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\[email protected]\0\0\0\0\0>\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"0/@\0\0\0\0\0\22\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"[email protected]\0\0\0\0\0\37\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\[email protected]\0\0\0\0\0\232\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\320\[email protected]\0\0\0\0\0\246\[email protected]\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"0\[email protected]\0\0\0\0\0-\[email protected]\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"\220\[email protected]\0\0\0\0\0\22\[email protected]\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"\300\[email protected]\0\0\0\0\0-\[email protected]\0\0\0\0\0P\0\0\0", 20}, {"\1", 1}, {"\200\[email protected]\0\0\0\0\0\"\[email protected]\0\0\0\0\0\1\0\0\0", 20}, {"\1", 1}, {"\240\[email protected]\0\0\0\0\0\"\[email protected]\0\0\0\0\0\1\0\0\0", 20}, ...], 62) = 651 
close(7)        = 0 
exit_group(1)       = ? 
+++ exited with 1 +++ 

這是所有對我來說都很新的東西,所以我可能在某個地方犯了一些基本的錯誤。我猜想有什麼國旗?

+1

分配給'ss-> log_mmap',然後檢查'ss-> data_mmap'是可疑的。另外'PROT_WRITE | PROT_WRITE'?還要看看man page在失敗的情況下對mmap的返回值所說的內容。 – Mat

+0

第二個輸出行與第二個'printf'不匹配。 fd#6的''來自哪裏? – mch

回答

0

您正在設置ss->log_mmap,但檢查的是ss->data_mmap

從你的strace中,mmap()已經返回了一個有效的指針。它看起來不像有任何不正確的事情發生或者有任何失敗。我懷疑你的mmap()工作正常。另外,ENOENT不是mmap()要返回的有效錯誤代碼,因此您可能在mmap()沒有錯誤時檢查errno

失敗時,mmap()返回MAP_FAILED而不是NULL。他們沒有相同的價值。

A 私有只寫一個文件的映射似乎不是很有用。您不會寫入該文件,只能訪問您無法讀取的私有內存區域,並且其他進程無法看到。您可能需要MAP_SHARED以便寫入文件。

相關問題