【正文】
nfo[i].entity ==39。r39。){//創(chuàng)建讀者進(jìn)程h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),amp。thread_info[i],0,amp。thread_ID)。}else{//創(chuàng)建寫線程h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),amp。thread_info[i],0,amp。thread_ID)。}}//等待所有的線程結(jié)束wait_for_all=WaitForMultipleObjects(n_thread,h_Thread,TRUE,1)。printf(All reader and writer have finished operating.\n)。}/////////////////////////////////////////////////////主函數(shù)int main(int argc,char *argv[]){ char ch。 while(true) { //打印提示信息 printf(*********************************************\n)。 printf( 1:Reader Priority\n)。 printf( 2:Writer Priority\n)。 printf( 3:Exit to Windows\n)。 printf(*********************************************\n)。 printf(Enter your choice(1,2or3):)。 //如果輸入信息不正確,繼續(xù)輸入 do{ ch=(char)_getch()。 }while(ch!=39。139。amp。amp。ch!=39。239。amp。amp。ch!=39。339。)。 system(cls)。 //選擇3,返回 if(ch==39。339。) return 0。 //選擇1,讀者優(yōu)先 else if(ch==39。139。) ReaderPriority()。 //選擇2,寫者優(yōu)先 else WriterPriority()。 //結(jié)束 printf(\nPress Any Key To Continue:)。 _getch()。 system(cls)。 } return 0。}說明:在Win32 API中,互斥對象Mutex與P、V中的互斥信號量有類似的地方,但也有不同:在P、V操作中的互斥信號量可以有一個任意大小的初值,但互斥對象Mutex沒有,它可以被看成是初值為1的互斥信號量。而且一個線程在取得Mutex的所有權(quán)之后,即使不調(diào)用ReleaseMutex函數(shù),在線程結(jié)束時,線程也會自動釋放Mutex的所有權(quán)。臨界區(qū)對象CriticalSection則與P、V操作中初值為1的互斥信號量語意相同。它在線程結(jié)束時,會將CriticalSection的所有權(quán)傳遞結(jié)它的向類型線程。這樣就可以滿足在一個線程中申請所有權(quán),在另—個線程釋放所有權(quán)的要求。在讀者優(yōu)先中的write互斥信號號以及寫者優(yōu)先中的read和write互斥信號量就應(yīng)該用CriticalSection實(shí)現(xiàn)而不應(yīng)該用Mutex。用WairForSingleSigna1函數(shù)可以獲得一個Mutex的所有權(quán),類似于P操作,而ReleaseMutex函數(shù)可以釋放一個Mutex的所有權(quán),類似于V操作。用EnterCriticalSection函數(shù)可以進(jìn)入一個CriticalSectin,類似于P操作,而LeaveCriticalSection函數(shù)離開一個CriticalSection,類似于V操作。備注:ReaderPriority函數(shù)最后有Wait_for_all=WaitForMultipleObject (n_thread, h_Thread, TRUE, 1)。是因?yàn)橹骱瘮?shù)要等待所有的線程都結(jié)束之后才退出。因?yàn)椴恢烙卸嗌倬€程,所以源文件最初有:define MAX_THREAD_NUM 64 //最大線程數(shù)目即線程最多不能超過MAX_THREAD_NUM個。線程對象的數(shù)組大小為MAX_THREAD_NUM。如果創(chuàng)建的線程沒有這么多,空間會有浪費(fèi),但是可以達(dá)到犧牲空間來節(jié)省時間的目的。有的書上還有其他的處理方法。一種是在主函數(shù)的最后加上Sleep(1000),即通過主函數(shù)睡眠的方法等待其他進(jìn)程結(jié)束,這當(dāng)然不是一種很好的方法,因?yàn)樗叩却臅r間沒法控制。另一種方法是增加循環(huán)變量threadCount(線程的個數(shù)),每個線程結(jié)束的時候就會執(zhí)行語句threadCount。主函數(shù)的最后測試:while (threadCount0)。但是這種方式會讓主函數(shù)循環(huán)等待,浪費(fèi)了CPU資源。相比之下,考慮到運(yùn)行效率,還是實(shí)例中給出的方法比較好些。六、示例程序的結(jié)果分析:1 R 3 52 W 4 53 R 5 24 R 6 55 W 3讀者優(yōu)先結(jié)果:Reader Priority:Reader thread 1 sents the reading require.Reader thread 1 begins to read file.Writer thread 2 sent the wrinting require.Reader thread 3 sents the reading require.Reader thread 3 begins to read file.Writer thread 5 sent the wrinting require.Reader thread 4 sents the reading require.Reader thread 4 begins to read file.Reader thread 3 finished reading file.Reader thread 1 finished reading file.Reader thread 4 finished reading file.Writer thread 2 begins to write to the file.Writer thread 2 finishing writing to the file.Writer thread 5 begins to write to the file.Writer thread 5 finishing writing to the file.All reader and writer have finished operating.Press Any Key To Continue:分析如下:Writer thread 2 sent the wrinting require.當(dāng)寫者2發(fā)出寫申請時,有讀者1在讀文件,所以寫者2阻塞。Reader thread 3 sents the reading require.Reader thread 3 begins to read file.讀者3發(fā)出讀申請后,立刻得到滿足。Writer thread 5 sent the wrinting require.當(dāng)寫者5發(fā)出寫申請時,有讀者1和3在讀文件,所以寫者5阻塞。Reader thread 4 sents the reading require.Reader thread 4 begins to read file.讀者4發(fā)出讀申請后、立刻得到滿足。Writer thread 2 begins to write to the file.當(dāng)讀者1,3和4讀完文件后,寫者2開始與文件。Writer thread 5 begins to write to the file.當(dāng)寫者2寫完文件后,寫者5開始寫文件。請寫出寫者優(yōu)先結(jié)果,并進(jìn)行分析七、課程設(shè)計(jì)任務(wù)學(xué)習(xí)Windows2000環(huán)境下多線程編程的相關(guān)知識。復(fù)習(xí)用信號量實(shí)現(xiàn)讀者——寫者問題的相關(guān)知識,并在Windows2000/XP環(huán)境下完成寫者優(yōu)先的編程(可以參照給出的讀者優(yōu)先的相關(guān)程序)。根據(jù)自己編寫的寫者優(yōu)先程序進(jìn)行分析,主要介紹一下程序中用到的變量,互斥對象及臨界區(qū)對象的作用。寫出程序的運(yùn)行結(jié)果,并分析結(jié)果。八、評分標(biāo)準(zhǔn)本課程設(shè)計(jì)采用五級評分制:優(yōu):編寫的寫者優(yōu)先程序運(yùn)行結(jié)果正確,程序有完整的注釋,對編寫的程序及結(jié)果的分析正確,課程設(shè)計(jì)報(bào)告完整,并能正確地回答老師提出的問題。良:編寫的寫者優(yōu)先程序運(yùn)行結(jié)果正確,有注釋,對編寫的程序及結(jié)果的分析基本正確,課程設(shè)計(jì)報(bào)告完整,并能正確地回答老師提出的問題。中:編寫的程序基本正確,對程序及結(jié)果的分析也基本正確,課程設(shè)計(jì)報(bào)告基本完整,基本能回答老師提出的問題。及格:不能正確運(yùn)行程序,但是能夠正確寫出寫者優(yōu)先程序的偽代碼(以前布置過作業(yè)),并能夠?qū)未a進(jìn)行正確的分析,能夠根據(jù)寫者優(yōu)先原理大致寫出程序運(yùn)行結(jié)果并分析,課程設(shè)計(jì)報(bào)告基本完整,基本能回答老師提出的問題。不及格:根本不理解讀者寫者問題,不能回答老師提出的相關(guān)問題。注意:由于在以往的課程設(shè)計(jì)中部分同學(xué)存在抄襲現(xiàn)象,本次課程設(shè)計(jì)提交任務(wù)時除了提交程序和課程設(shè)計(jì)報(bào)告之外,還要接受教師的提問。若在檢查過程中發(fā)現(xiàn)程序和前面檢查過的同學(xué)基本相同,但是不能正確回答老師提出的問題,對程序不太了解,則認(rèn)為存在抄襲現(xiàn)象,記為不及