?? 設(shè)計(jì)報(bào)告.txt
字號(hào):
一、基本信息
實(shí)踐題目:同步對(duì)象實(shí)現(xiàn)P、V操作
二、實(shí)踐內(nèi)容簡(jiǎn)要描述
實(shí)踐目標(biāo):
用Win32提供的同步對(duì)象實(shí)現(xiàn)P、V操作,并解決任務(wù)4中有限緩沖區(qū)問(wèn)題
實(shí)踐內(nèi)容:
用同步對(duì)象實(shí)現(xiàn)P、V操作解決一下問(wèn)題:
寫一個(gè)線程實(shí)現(xiàn)C/C++語(yǔ)言程序:一些線程負(fù)責(zé)找出某個(gè)數(shù)據(jù)范圍的素?cái)?shù),并放到一個(gè)數(shù)組中,另一些線程負(fù)責(zé)將數(shù)組中的素?cái)?shù)按次序取出,并顯示出來(lái)。顯示過(guò)的數(shù)據(jù)要從數(shù)組中刪除。要求定義一個(gè)全局變量的數(shù)組:int prime[9]用于存放找到的待顯示的素?cái)?shù)。
三、實(shí)踐報(bào)告主要內(nèi)容
設(shè)計(jì)思路:
實(shí)現(xiàn)P、V操作用了binary semaphore來(lái)實(shí)現(xiàn),
wait操作的實(shí)現(xiàn)
Wait(s1);
c--;
if(c<0){
signal(s1);
wait(s2);
}
signal(s1);
signal操作的實(shí)現(xiàn):
wait(s1);
c++;
if(c<=0)
signal(s2);
else
signal(s1);
線程同步仍采用了生產(chǎn)者-消費(fèi)者算法。
P、V操作用了一個(gè)返回值,為0時(shí)表示超時(shí),為1表示等待成功。
主要數(shù)據(jù)結(jié)構(gòu):
線程信息結(jié)構(gòu)體:
int searchCount = 0; //已開線程數(shù)
int displayCount = 0; //已開線程數(shù)
CRITICAL_SECTION my_section;
HANDLE h_semaphore_s1_e = CreateSemaphore(NULL, 1, 1, "Bin_S1_Empty"); //Empty的S1
HANDLE h_semaphore_s2_e = CreateSemaphore(NULL, 0, 1, "Bin_S2_Empty"); //Empty的S2
HANDLE h_semaphore_s1_f = CreateSemaphore(NULL, 1, 1, "Bin_S1_Full"); //Full的S1
HANDLE h_semaphore_s2_f = CreateSemaphore(NULL, 0, 1, "Bin_S2_Full"); //Full的S2
int Count_Empty; //Empty計(jì)數(shù)器
int Count_Full; //Full計(jì)數(shù)器
struct ThreadInfo
{
int serial;
char command;
int num_from;
int num_to;
};
int prime[9];
void GetCommand( char* file );
void thd_display(void* p);
void thd_search(void* p);
BOOL IsPrime(int num); //用于判斷是否為素?cái)?shù)
int GetUsed(int num[9]); //用于獲得剩余空間
void P(LPCTSTR lpcstr,int &count); //P 操作
void V(LPCTSTR lpcstr,int &count); //V 操作
主要代碼分析:
int wait(int *,HANDLE Event[]); // binary semaphore實(shí)現(xiàn)的P 操作
int signal(int *,HANDLE Event[]); // binary semaphore實(shí)現(xiàn)的V 操作
四、實(shí)踐結(jié)果
基本數(shù)據(jù):
源程序代碼行數(shù) 完成實(shí)踐投入的總時(shí)間 資料查閱時(shí)間 編程調(diào)試時(shí)間 源程序文件
413 1小時(shí) 0.5小時(shí) 0.5小時(shí) ex5.cpp
測(cè)試數(shù)據(jù)設(shè)計(jì):
1.“ex4.dat”文件內(nèi)容測(cè)試結(jié)果同ex4:
2.自設(shè)測(cè)試數(shù)據(jù)
1 w 10 100
2 w 1 10
3 W 2 20
4 D 20 0
5 D 3 0
測(cè)試結(jié)果分析:
對(duì)應(yīng)于數(shù)據(jù)的運(yùn)行結(jié)果:
Reader Priority:
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 2 begins to write the date .
Search thread 2 finish writing the date .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Search thread 2 begins to write the date .
Search thread 2 finish writing the date .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "11".
Display thread 4 finishes outputting prime num .
Display thread 5 begins to output prime num .
----------Display thread 5 output the prime num "13".
Display thread 5 finishes outputting prime num .
Search thread 2 begins to write the date .
Search thread 2 finish writing the date .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "7".
Display thread 4 finishes outputting prime num .
Display thread 5 begins to output prime num .
----------Display thread 5 output the prime num "7".
Display thread 5 finishes outputting prime num .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "11".
Display thread 4 finishes outputting prime num .
Display thread 5 begins to output prime num .
----------Display thread 5 output the prime num "17".
Display thread 5 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "23".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "31".
Display thread 4 finishes outputting prime num .
Search thread 3 begins to write the date .
Search thread 3 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "19".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "37".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "41".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "43".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "47".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "53".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "59".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "61".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "67".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "71".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "73".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "79".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "83".
Display thread 4 finishes outputting prime num .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "13".
Display thread 4 finishes outputting prime num .
Display thread 4 begins to output prime num .
----------Display thread 4 output the prime num "29".
Display thread 4 finishes outputting prime num .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
Search thread 1 begins to write the date .
Search thread 1 finish writing the date .
All search and display thread have finished Operating.
The array at last.
89 97 0 19 3 3 5 5 17
Press any key to finish this Program.
Thank you test this Program!
程序運(yùn)行過(guò)程中,線程1、線程2、線程3為找素?cái)?shù)線程,線程4、線程5負(fù)責(zé)顯示,它們均交叉訪問(wèn)臨界區(qū),程序運(yùn)行結(jié)果完全正確。
五、實(shí)踐體會(huì)
該實(shí)驗(yàn)與實(shí)驗(yàn)四相比,只是用軟件的方法來(lái)實(shí)現(xiàn)內(nèi)核對(duì)象的功能,只須細(xì)心就能順利完成任務(wù)。
通過(guò)實(shí)驗(yàn)我基本了解了P、V操作的具體實(shí)現(xiàn),以及有限緩沖區(qū)問(wèn)題的解決方法。
七、參考文獻(xiàn)
1、MSDN
2、《操作系統(tǒng)概念(第六版 影印版)》,Abraham Silberschatz、Peter Baer Galvin、Greg Gagne,
高等教育出版社
附:源程序部分關(guān)鍵代碼
void P(LPCTSTR lpcstr,int &count)
{
HANDLE h_s1,h_s2;
if(strcmp(lpcstr,"Empty") == 0)
{
h_s1=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S1_Empty");
h_s2=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S2_Empty");
}
else if(strcmp(lpcstr,"Full") == 0)
{
h_s1=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S1_Full");
h_s2=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S2_Full");
}
WaitForSingleObject(h_s1, INFINITE);
count--;
if (count < 0)
{
ReleaseSemaphore(h_s1, 1, NULL);
WaitForSingleObject(h_s2, INFINITE);
}
ReleaseSemaphore(h_s1, 1, NULL);
CloseHandle(h_s1);
CloseHandle(h_s2);
}
void V(LPCTSTR lpcstr,int &count)
{
HANDLE h_s1,h_s2;
if(strcmp(lpcstr,"Empty") == 0)
{
h_s1=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S1_Empty");
h_s2=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S2_Empty");
}
else if(strcmp(lpcstr,"Full") == 0)
{
h_s1=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S1_Full");
h_s2=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Bin_S2_Full");
}
WaitForSingleObject(h_s1, INFINITE);
count++;
if (count <= 0)
{
ReleaseSemaphore(h_s2, 1, NULL);
}
else
{
ReleaseSemaphore(h_s1, 1, NULL);
}
CloseHandle(h_s1);
CloseHandle(h_s2);
}
任務(wù)六 LINUX下以原生方式查看軟盤信息
一、基本信息
實(shí)踐題目:在LINUX下以原生(raw)方式查看軟盤的信息
二、實(shí)踐內(nèi)容簡(jiǎn)要描述
實(shí)踐目標(biāo):
熟悉Linux操作系統(tǒng)下的基本操作及其程序設(shè)計(jì)的基本方法。
學(xué)會(huì)在LINUX下使用原生方式查看軟盤上的數(shù)據(jù)信息。
實(shí)踐內(nèi)容:
在LINUX下使用原生方式查詢并顯示目前磁盤的以下信息:
-每扇區(qū)字節(jié)數(shù)
-每磁道扇區(qū)數(shù)
-每柱面的磁道數(shù)(磁頭數(shù))
三、實(shí)踐報(bào)告主要內(nèi)容
設(shè)計(jì)思路:
只要正確定位文件的讀取位置并確定所需讀取的字節(jié)數(shù)便可以正確讀取軟盤上的數(shù)據(jù),先用open函數(shù)以只讀方式打開軟盤,再用lseek定位讀取位置,然后用read讀取相應(yīng)字節(jié)的數(shù)據(jù)即可。
主要數(shù)據(jù)結(jié)構(gòu):
int fd; //文件描述符
size_t num_read;
int bytes_per_sector = 0; //每扇區(qū)字節(jié)數(shù)
int sectors_per_track = 0; //每磁道扇區(qū)數(shù)
int number_of_surface = 0; //每柱面的磁道數(shù)(磁頭數(shù))
int * pi; //指向文件緩沖區(qū)的指針
pi = &bytes_per_sector;
pi = §ors_per_track;
pi = &number_of_surface;
主要代碼分析: 詳見(jiàn)代碼注釋。
四、實(shí)踐結(jié)果
基本數(shù)據(jù):
源程序代碼行數(shù) 完成實(shí)踐投入的總時(shí)間 資料查閱時(shí)間 編程調(diào)試時(shí)間 源程序文件
43 0.5小時(shí) 0.4小時(shí) 0.1小時(shí) ex6.cpp
測(cè)試數(shù)據(jù)設(shè)計(jì):
3.5寸軟盤一張。
測(cè)試結(jié)果分析:
程序運(yùn)行結(jié)果為:
Bytes per sector: 512
Sectors per track: 18
Number of surfaces:: 2
程序運(yùn)行結(jié)果完全正確。
五、實(shí)踐體會(huì)
以原生方式讀取磁盤信息時(shí),將磁盤當(dāng)作一個(gè)文件打開,要正確定位文件的讀取位置,并確定讀取字節(jié)數(shù),這樣便能從中讀取相應(yīng)數(shù)據(jù)。通過(guò)實(shí)驗(yàn),了解了軟盤上BOOT的布局,并熟悉了Linux操作系統(tǒng)下程序設(shè)計(jì)的基本方法和基本操作。
六、參考文獻(xiàn)
1、《Linux基礎(chǔ)教程》,黃慶生 吳振華 蘇哲 編著,人民郵電出版社
附:源程序代碼
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
//每扇區(qū)字節(jié)數(shù) 0x0b-0x0c 11 - 12
//每磁道扇區(qū)數(shù) 0x18-0x19 24 - 25
//每柱面的磁道數(shù) 0x1a-0x1b 26 – 27
int fd;
size_t num_read;
int bytes_per_sector = 0;
int sectors_per_track = 0;
int number_of_surface = 0;
int * pi;
fd = open("/dev/fd0", O_RDONLY);
pi = &bytes_per_sector;
lseek(fd,(off_t)11,SEEK_SET);
num_read = read(fd,(void *)pi,(size_t)2);
printf("Bytes per sector: %d\n",bytes_per_sector);
pi = §ors_per_track;
lseek(fd,(off_t)24,SEEK_SET);
num_read = read(fd,(void *)pi,(size_t)2);
printf("Sectors per track: %d\n",sectors_per_track);
pi = &number_of_surface;
lseek(fd,(off_t)26,SEEK_SET);
num_read = read(fd,(void *)pi,(size_t)2);
printf("Number of surfaces: %d\n",number_of_surface);
return 0;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -