所以這是我試圖運行代碼:看不慣讀取系統調用的行爲
#include<fcntl.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
int main(){
int ret;
ret = read(STDIN_FILENO,(int*)2000,3);
printf("%d--%s\n",ret,strerror(errno));
return 0;
}
,這是輸出我在終端
[email protected]:~/Desktop/testing$ gcc test.c
[email protected]:~/Desktop/testing$ ./a.out
lls
-1--Bad address
[email protected]:~/Desktop/testing$ ls
a.out htmlget_ori.c mysocket.cpp Packet Sniffer.c resolutionfinder.c test.c
[email protected]:~/Desktop/testing$
問題1獲得:當我在讀取電話read(STDIN_FILENO,(int*)2000,3);
中輸入地址2000時,地址在哪裏。我認爲這是我嘗試訪問的RAM的絕對地址。我是對的還是被抵消,並被添加到堆棧段基地址。我不知道。該方案是不是給我的記憶違反SEGFAULT
而給我Bad address
問題2:好了,所以代碼崩潰時,我給了輸入作爲lls
和bash執行的是「LLS」中的「LS」的一部分。原因是代碼在讀取第一個「l」後崩潰,其餘的「ls」部分由bash執行。但爲什麼bash正在執行左邊的「ls」部分。爲什麼bash這樣做是因爲我的代碼崩潰了,即使bash是它的父進程,它也不應該從我寫的代碼打開的文件描述符(STDIN_FILNO)中讀取。 (我這麼認爲)...
感謝您的時間。
「我認爲這是我嘗試訪問的RAM的絕對地址。」你認爲是這樣,根據什麼信息? – 2011-01-30 09:31:20
* @ Anirudh *:確實沒有比指定固定內存位置更好的方法嗎?你能否提供一些關於你想要實現的背景信息? – stakx 2011-01-30 09:41:33
爲什麼不正常使用`read()`,即`unsigned char buffer [3];讀(STDIN_FILENO,&buffer,sizeof(buffer));`??? – 2011-01-30 09:44:22