(1) 了解磁碟的物理組織。
(2) 熟悉windows 系統如何檢視磁碟相關係數。
(3) 掌握windows 系統提供的有關對磁碟操作 api。
1.相關係數資料結構說明
磁碟基本物理結構原型:
typedef struct _dtsk_geometry disk_geometry;
成員說明:
(1)cylinders:磁碟的柱面數。
(2)mediatype:介質型別,如3.5英吋,1.44mb軟盤。
(3)trackspercylinder :每個柱面的磁軌數。
(4)sectorspertrack:每個磁軌的扇區數。
(5)bytespersector:每個扇區的位元組數。
2.相關api 函式介紹
(1)檔案建立
函式createfile()用於開啟磁碟驅動器並返回乙個檔案控制代碼,這裡驅動器被當做檔案來處理。有關檔案操作函式的詳細說明參見4.1.2節。
原型:
handle createfile
( lpctste lpfilename,
//指向檔名的指標
dword dwdesiredaccess,
//讀/寫訪問模式
dword dwsharemode,
//共享模式
lpsecurity_attributes lpsecurityattributes,
//指向安全屬性的指標
dword dwcreateionadisposition,
//檔案存在標誌
dword dwflagsandattributes,
//檔案屬性
handle htemplatefile //指向訪問模板檔案的控制代碼
);
(2)獲取磁碟的基本資訊
函式deviceiocontrol() 用於獲取磁碟的基本資訊
原型:
bool deviceiocontrol
( handle hdevice,
//裝置控制代碼
dword dwiocontrolcode,
//操作控制**
lpvoid lpinbuffer,
//輸入資料緩衝區
dword ninbuffersize,
//輸入資料緩衝區大小
lpvoid lpoutbuffer,
//輸出資料緩衝區
dword noutbuffersize,
//輸出資料緩衝區大小
lpdword lpbytesreturned,
//可獲取的位元組計數
);
引數說明:
1 hdevice: 目標裝置的控制代碼,由createfile() 函式獲得。
2 dwiocontrolcode: 指定操作的控制資訊,用該值可以辨別將要執行的操作,以及對哪類裝置進行操作。該引數取值如表所示
dwiocontrolcode 的值值描述
ioctl_disk_get_drive_geometry
得到磁碟物理結構資訊
ioctl_disk_get_partition_info
得到磁碟分割槽資訊
fsctl_query_fat_bpb
返回fat16或fat12卷的前36位元組
fsctl_get_compression
獲取檔案或目錄的壓縮資訊
3 lpinbuffer:指向乙個緩衝區,該緩衝區存放指定操作所輸入資料。
4 ninbuffersize:由lpinbuffer所指緩衝區大小。
5 lpoutbuffer:指向乙個緩衝區,該緩衝區存放指定操作所輸出資料。
6 noutbuffersize:由lpoutbuffer所指緩衝區大小。
7 lpbytesreturned:實際輸出結果所佔位元組數。
返回值:
如果函式呼叫成功,則返回值為非0值。如果函式呼叫失敗,則返回值為0。若要得到更多的錯誤資訊,可呼叫函式getlasterror()。
(一)實驗內容
編寫乙個函式,根據給出的驅動器號讀取磁碟基本資訊,包括鍵盤的大小、該磁碟包括多少個扇區,該磁碟有多少個柱面,每個柱面的磁軌數、每個磁軌的扇區數、每個扇區包含的位元組數。
(二)主要**
//#include
"stdafx.h"
#include
"11.h"
#include
"winioctl.h"
#ifdef _debug
#define new debug_new
#undef this_file
static
char this_file=
__file__
;#endif
disk_geometry disk_info;
handle getdiskinformation
(char drivername)
;bool sectorread
(handle handle)
;bool sectorwrite
(handle handle)
;using namespace std;
int_tmain
(int argc,tchar *ar**[
],tchar *envp)
case
'r':
case
'q':
default:}
}return nretcode;
}handle getdiskinformation
(char drivername)
bool sectorread
(handle handle)
printf (
"please input the sector number to read from:\n");
scanf
("%d"
,§ornumber)
; printf (
"\n");
sector =disk_info.cylinders.quadpart*
disk_info.trackspercylinder*
disk_info.sectorspertrack;
if(sectornumber>sector)
printf
("there is not this sector !");
printf
("content:\n");
bytestoread=sectornumber*
(disk_info.bytespersector)
; rc=
setfilepointer
(handle,bytestoread,
null
,file_begin);if
(!readfile
(handle,readbuffer,bytestoread,
&bytestoread,
null))
printf
("\t text content:\n");
for(i=
0;i<
512;i++
)printf
("\n");
printf
("\t hex text content: \n");
for(i=
0;i<
512;i++
)printf
("\n");
return true;
}bool sectorwrite
(handle handle)
printf
("please input the sector number to write to\n");
scanf
("%s"
,§ornumber)
;printf
("\n");
sector=disk_info.cylinders.quadpart* disk_info.trackspercylinder* disk_info.sectorspertrack;
if(sectornumber>sector)
printf
("there is no sector! \n");
printf
("please input the content to write to disk a: \n");
scanf
("%s"
,&writebuffer)
; sectormove=sectornumber*
(disk_info.bytespersector)
; rc=
setfilepointer
(handle, sectormove,
null
,file_begin);if
(!writefile
(handle,writebuffer,
512,
&bytestowrite,
null))
printf
("write complete!\n");
return true;
}從實驗結果可以看出,對給定的磁碟驅動器中的軟體a,本實驗能正確識別出它每個扇區有512位元組,每個磁軌有18個扇區,每個柱面有兩個磁軌,共有80個柱面,該盤共有2280個磁軌,磁碟的大小是1.41mb。應當注意,磁軌上有一部分空間是儲存磁碟的物理資訊的,這部分空間系統是不能夠直接訪問的,因此沒有編入邏輯扇區,也就是說邏輯扇區比磁碟的實際扇區要小,因此計算出的磁碟大小是磁碟可用空間的大小,比磁碟的物理大小要小。
作業系統實驗十 獲取磁碟基本資訊
1 了解磁碟的物理組織。2 熟悉windows 系統如何檢視磁碟相關係數。3 掌握windows 系統提供的有關對磁碟操作 api。磁碟基本物理結構原型 typedef struct dtsk geometry disk geometry 1.檔案建立 函式createfile 用於開啟磁碟驅動器並...
作業系統實驗 獲取磁碟基本資訊
1 了解磁碟的物理組織。2 熟悉windows 系統如何檢視磁碟相關係數。3 掌握windows 系統提供的有關對磁碟操作 api。1.相關係數資料結構說明 磁碟基本物理結構原型 typedef struct disk geometry disk geometry 成員說明 1 cylinders ...
實驗十 獲取磁碟基本資訊
1 了解磁碟的物理組織。2 熟悉windows 系統如何檢視磁碟相關係數。3 掌握windows 系統提供的有關對磁碟操作 api。磁碟基本物理結構原型 typedef struct dtsk geometry disk geometry 1.檔案建立 函式createfile 用於開啟磁碟驅動器並...