• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            不在浮沙筑高臺(tái)-demons
            C++/OS/disassembly/Anti-virus
            posts - 5,  comments - 5,  trackbacks - 0

            昨天接到操作系統(tǒng)課程設(shè)計(jì)題目,是用信號(hào)量同步進(jìn)程(線程)的,由于要用到Windows API ,好幾個(gè)同學(xué)問我API是干什么的,由于大家都沒接觸過,不知怎么用,所以我就以課設(shè)題目為例,給大家介紹一下API的一些基本知識(shí)。但請您注意,本文旨在介紹API使用,而不是給您提供現(xiàn)成的代碼讓您直接復(fù)制到課程設(shè)計(jì)報(bào)告中去,您應(yīng)該只把這做為一份參考,然后寫出自己的代碼。好了,我們從最基本的講起,首先看題目:

            Linux或Windows或Unix環(huán)境下,采用系統(tǒng)調(diào)用中的信號(hào)量、P、V操作,編程解決以問題。讀者與寫者問題。一個(gè)數(shù)據(jù)集為多個(gè)并發(fā)進(jìn)程所共享,其中一些進(jìn)程只要求讀該數(shù)據(jù)集的內(nèi)容,這些進(jìn)程稱為“讀者”,而另一些進(jìn)程則要求修改該數(shù)據(jù)集的內(nèi)容,這些進(jìn)程稱為“寫者”。具體要求是:允許多個(gè)讀者同時(shí)讀該數(shù)據(jù)集的內(nèi)容;若有一個(gè)寫者在寫,則其他讀者不能讀;若一個(gè)寫者在寫或有其他讀者在讀,則其他寫者均被拒絕;當(dāng)一個(gè)寫者正在寫,而有多個(gè)讀者與寫者在等待時(shí),寫者應(yīng)優(yōu)先喚醒。請用P、V操作寫出進(jìn)程的同步算法。要求打印:初始狀態(tài),中間變化的狀態(tài)信息,以及最終狀態(tài)信息。

            我以Windows做為示例,考慮到大家并非都懂C++,故代碼用C寫。分析:

            1.理想狀態(tài):我們程序中完全和作業(yè)中的方式一樣,比如定義信號(hào)量用 semaphore mutex; P操作就直接P(mutex),V操作就直接V(mutex),盡量避免直接使用API。

            好的,我們首先實(shí)現(xiàn)這兩種功能。由于筆者平時(shí)開發(fā)習(xí)慣,都會(huì)在特定的工程中的函數(shù)、數(shù)據(jù)類型加上工程標(biāo)志,這個(gè)項(xiàng)目是Operating system Course Design,我就取OSCD,做為函數(shù)和數(shù)據(jù)類型前綴。所以上面的semaphore mutex;定義信號(hào)量的方式就變?yōu)镺SCD_semaphore mutex(只是換了個(gè)衣服而已)。下面介紹第一個(gè)API函數(shù)(為了先避免字符編碼的問題,看我這個(gè)不要對(duì)函數(shù)名后的A太過在意,以后接觸Windows編程后,你自然就懂了):

            HANDLE CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,LONG lInitialCount,LONG lMaximumCount,LPCSTR lpName );首先看一下這個(gè)中的數(shù)據(jù)類型;其實(shí) LONG 就是typedef long LONG;  LPCSTR 就是char * (字符串的指針),LPSECURITY_ATTRIBUTES 和HANDLE目前不是一兩句話能將清楚的,LPSECURITY_ATTRIBUTES 可以先直接忽略掉,你可以把HANDLE 理解為操作信號(hào)的“手柄”,也可以先理解為指向信號(hào)量的類型,以后我們對(duì)信號(hào)量的操作就是通過它來完成的,在直接一點(diǎn),我們就可以把它就當(dāng)作是信號(hào)量,其中l(wèi)InitialCount就好比我們作業(yè)里mutex.value,中的value,我們需要在調(diào)用這個(gè)函數(shù)時(shí)給它賦初值,lMaximumCount是指定信號(hào)量的最大值,本例中我們?nèi)?0,最后一個(gè)參數(shù)lpName 課設(shè)用不到,直接傳NULL。回過頭來看看這個(gè)函數(shù)實(shí)現(xiàn)的功能:創(chuàng)建一個(gè)信號(hào)量并返回,在創(chuàng)建信號(hào)量時(shí)我們必須對(duì)信號(hào)量賦初值。也就是說我們真正用到的參數(shù)只有一個(gè)lInitialCount,下面我們對(duì)其封裝;

             

              typedef HANDLE OSCD_semaphore    //這樣以后就可以直接這樣 OSCD_semaphore mutex;定義信號(hào)量了;
               #define MAX_SEM_COUNT 10 //信號(hào)量最大值我們?nèi)?0
            OSCD_semaphore OSCD_CreateSemaphore(int nInitialCount)  //參數(shù)是初值,返回的是信號(hào)量。
            {
                OSCD_semaphore hSemaphore;
                if (nInitialCount>MAX_SEM_COUNT){ //
                    printf("semaphore count too big ,please specify a value range 0 to 10\n");
                    exit(0);
                } 
                hSemaphore=::CreateSemaphoreA(NULL,nInitialCount,MAX_SEM_COUNT,NULL );//真正創(chuàng)建信號(hào)量
                if (hSemaphore == NULL) {
                    printf("CreateSemaphore error: %d\n", GetLastError());
                    exit(0);
                }
                return hSemaphore;
            }

             

            拿本例中的 mutex 為例;我們只需要這么初始化它:OSCD_semaphore mutex;//定義mutex,    mutex= OSCD_CreateSemaphore(1);//初始化;下面是刪除信號(hào)量的函數(shù):

             

            void OSCD_DelSemaphore( OSCD_semaphore semaphore)
            {
                ::CloseHandle(semaphore);//你可以認(rèn)為這個(gè)API可以刪除掉用上面函數(shù)創(chuàng)建的信號(hào)量,要?jiǎng)h除那個(gè)信號(hào)量,參數(shù)就是哪個(gè)信號(hào)量,比如OSCD_DelSemaphore(mutex);
            }

             

            P操作:按照P操作的定義,先對(duì)信號(hào)量值減1,然后再和0比較,若大于等于0,就可以得到資源繼續(xù)運(yùn)行,小于0的話就等待。我先給出代碼再做解釋:

             

            void P( OSCD_semaphore semaphore)
            {
                ::WaitForSingleObject(semaphore,INFINITE);//這個(gè)API是等待信號(hào)量的函數(shù),若等待的信號(hào)量值大于等于0時(shí),便返回,程序繼續(xù)運(yùn)行,若無資源了,便會(huì)一直等待,函數(shù)不會(huì)返回,程序就不能再往下運(yùn)行
            }

             

            V操作:使信號(hào)量加1;很簡單:

             

            void V(OSCD_semaphore semaphore)
            {
                if (!::ReleaseSemaphore(semaphore,1,NULL))//該API使信號(hào)量加1(第二個(gè)參數(shù),如果你想使它一次加2,只需給第二個(gè)參數(shù)傳2 即可)
                {
                    printf("release semaphore error: %d\n",GetLastError());//檢查上面函數(shù)是否調(diào)用成功
                     exit(0);
                }
            }//該函數(shù)使用方法,還以mutex為例說明:V(mutex);

             

            至此我們的初步目標(biāo)已經(jīng)實(shí)現(xiàn),接下來就是創(chuàng)建線程的函數(shù);

             

            HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,DWORD dwStackSize,LPTHREAD_START_ROUTINE lpStartAddress,LPVOID lpParameter,DWORD dwCreationFlags, Lpdword lpThread); 
            去枝葉剪頁,其中只有第3個(gè)和第4個(gè)參數(shù)有用,LPTHREAD_START_ROUTINE 是一個(gè)線程函數(shù)指針(其實(shí)你也可以把線程理解為特殊的函數(shù),特殊主要體現(xiàn)在它能和其它線程函數(shù)并發(fā)執(zhí)行)函數(shù)的原型是這樣的DWORD WINAPI  threadfunction(LPVOID lpParameter),意思是,這個(gè)函數(shù)有一個(gè)空指針類型的參數(shù),該參數(shù)返回值是DWORD(unsigned long)型的,WINAPI 就是_stdcall 是函數(shù)的一種調(diào)用方式(如果現(xiàn)在不懂,沒關(guān)系,以后會(huì)懂的,不必糾結(jié)),第四個(gè)參數(shù)是一個(gè)空類型指針,仔細(xì)看,這個(gè)參數(shù)其實(shí)就是傳遞給線程函數(shù)的,趕緊看線程函數(shù)的參數(shù)是不是正是LPVOID lpParameter,我們可以把寫者要寫的內(nèi)容的指針放到這個(gè)參數(shù)里面?zhèn)鬟f給線程函數(shù),讓線程函數(shù)執(zhí)行時(shí)會(huì)從這個(gè)參數(shù)所指的內(nèi)容取出然后再寫進(jìn)緩沖區(qū)。總結(jié)一些,也就是我們要?jiǎng)?chuàng)建一個(gè)線程就必須提供一個(gè)固定的函數(shù)指針(也就是函數(shù)名),然后再把要傳給線程的數(shù)據(jù)的指針給lpParameter。好了我給出調(diào)用這個(gè)函數(shù)的示例:

             

             

            DWORD WINAPI  Writer(LPVOID lpParameter){
                  寫者的具體代碼…
            }
            char * p="DataToWrite";
            HANDLE  hThreads=::CreateThread(NULL,0,Reader,p,SW_NORMAL,NULL);//只需注意第3、4個(gè)參數(shù)就行,其它的參數(shù)不要管。

             

            這樣線程創(chuàng)建就完了;但看一下這種情況 //把創(chuàng)建線程作為最后一個(gè)語句時(shí)main函數(shù)會(huì)在創(chuàng)建完線程后退出,也就意味著程序結(jié)束,但這是線程只是創(chuàng)建成功,但并沒有執(zhí)行的機(jī)會(huì)。

             

            int main(){
            ………..
            HANDLE  hThreads=::CreateThread(NULL,0,Reader,p,SW_NORMAL,NULL);
            }

             

            要解決這個(gè)問題,就要用到另一個(gè)API,在這個(gè)實(shí)驗(yàn)中一共有多個(gè)線程,我們必須在創(chuàng)建完線程后等待它們?nèi)窟\(yùn)行結(jié)束后才能讓main函數(shù)退出。

            WaitForMultipleObjects( DWORD nCount,  HANDLE* lpHandles,  BOOL bWaitAll,  DWORD dwMilliseconds);這個(gè)函數(shù)中第一個(gè)參數(shù)指得是要等待線程的總數(shù)量,第二個(gè)參數(shù)是所有線程的“手柄”數(shù)組,因?yàn)榈却瘮?shù)是通過線程的“手柄”才能操作線程,等待函數(shù)也一樣,只有得到它的句柄才能在其內(nèi)核對(duì)象上等待;所以我們應(yīng)該這樣做:

            HANDLE hThreads[12];//本例中有六個(gè)讀者線程,六個(gè)寫者線程;

            然后再創(chuàng)建線程成功后把每個(gè)線程的句柄都保存在這個(gè)數(shù)組中WaitForMultipleObjects( DWORD nCount,  HANDLE* lpHandles,  BOOL bWaitAll,  DWORD dwMilliseconds);然后再這樣調(diào)用:

            WaitForMultipleObjects(12,hThreads,TRUE,INFINITE); //這個(gè)函數(shù)會(huì)在所有等待的線程都執(zhí)行完成后返回,接著main函數(shù)返回,程序結(jié)束。由于這個(gè)函數(shù)也可用于等待其中任一形成結(jié)束,后返回(第三個(gè)參數(shù)傳FALSE時(shí),意思是不必等所有線程都執(zhí)行完,只要有一個(gè)執(zhí)行完,就返回),所以我們在這傳TRUE,并且在最后一個(gè)參數(shù)(等待超時(shí),如果在這個(gè)時(shí)間內(nèi)線程還沒有結(jié)束,該函數(shù)也會(huì)返回,所以我們應(yīng)該把超時(shí)設(shè)置為無限。。)

            到了這里,基本都講完了,下面給出完整代碼

            /******************************************************************
                Student Number: 3100604012
                              Name: Duwen
                              Date:2012-6-9
                              Type:Demonstration code
                      Description:Operating System  Course Design (OSCD)---Thread synchronization 
                      with semaphore. you must know this code only as a demonstration of the Windows
                      API , it's unwise to copy this code to OSCD report directly, you should regard this
                      code as a reference to you. and then write it by yourself.
                
            ******************************************************************
            */

            #include "stdafx.h"
            #include <Windows.h>
            #define MAX_SEM_COUNT 10
            #pragma warning(disable:4996)        //Ignore 4996 warning 

            typedef HANDLE  OSCD_semaphore;

            ////////////////////////////////////////////////////////////////////////////

            //Define the semaphores needed as global variables
            OSCD_semaphore mutex,reader,writer;
            //Define the counters
            int readcount=0, writecount=0,readapp=0;
            //Allocate buffer
            char buffer[256]={0};

            ///////////////////////////////////////////////////////////////////

            //Function to create and initialize a new semaphore
            OSCD_semaphore OSCD_CreateSemaphore(int nInitialCount) 
            {
                OSCD_semaphore hSemaphore;
                if (nInitialCount>MAX_SEM_COUNT){ //sanity check
                    printf("semaphore count too big ,please specify a value range 0 to 10\n");
                    exit(0);
                }
                hSemaphore=::CreateSemaphoreA(NULL,nInitialCount,MAX_SEM_COUNT,NULL );
                if (hSemaphore == NULL) {
                    printf("CreateSemaphore error: %d\n", GetLastError());
                    exit(0);
                }
                return hSemaphore;
            }

            //Function to delete the semaphore
            void OSCD_DelSemaphore( OSCD_semaphore semaphore)
            {
                ::CloseHandle(semaphore);

            }

            //P operation
            void P(OSCD_semaphore semaphore)
            {
               ::WaitForSingleObject(semaphore,INFINITE);
            }

            //V operation
            void V(OSCD_semaphore semaphore)
            {
                if (!::ReleaseSemaphore(semaphore,1,NULL))
                {
                    printf("release semaphore error: %d\n",GetLastError());
                    exit(0);
                }
            }

            //Reader thread function
            DWORD WINAPI Reader(LPVOID lpParameter)
            {  
                char *pThreadName=(char *)lpParameter;
                Sleep((pThreadName[6]-'0')*5);
                P(mutex);
                if (writecount>0||buffer[0]==NULL){
                    if(writecount>0)
                        printf("%s find there exists writer(s) trying to write ,  wait\n",pThreadName);
                    else
                        printf("There is no data in buffer now,%s  wait\n" ,pThreadName);
                    readapp++;
                    V(mutex);
                    P(reader);
                    printf("%s gets semaphore successfully !",pThreadName);
                    P(mutex);
                    readapp--;
                    readcount++;
                    if (readapp>0) V(reader);
                    V(mutex);
                }
                else{
                    readcount++;
                    V(mutex);
                }
                printf("%s read the data that :%s\n",pThreadName,buffer);
                P(mutex);
                readcount -- ; 
                if ( readcount == 0 && writecount>0 )
                    V(writer) ;
                V(mutex) ;
                return 0;
            }

            //Writer thread function 
            DWORD WINAPI  Writer(LPVOID lpParameter)
            {  
                char *pDataToWrite=(char *)lpParameter;
                Sleep((pDataToWrite[7]-'0')*7);
                P(mutex);
                writecount ++ ;
                printf("A new writer is trying to write\n");
                //when there are  readers are  reading or writers are trying to write, waiting..
                if (readcount>0 || writecount>1 ){
                    V(mutex);
                    printf(" Waiting(writer) semaphore\t The contents to write is:%s\n ", pDataToWrite);
                    P(writer);
                }
                else V(mutex) ; //skip waiting
                int nLen=strlen(pDataToWrite);
                strncpy(buffer,pDataToWrite,nLen/2);//write
                Sleep(3);//wait 3 ms
                strcpy(buffer+nLen/2,pDataToWrite+nLen/2);
                P(mutex); 
                writecount -- ;
                if (writecount>0 ) V(writer) ; 
                else if (readapp>0 ) V(reader) ;
                printf("A writer waited gets semaphore ,Writing: \" %s \" to buffer \n ", pDataToWrite );
                V(mutex) ;

                return 0;
            }




            int _tmain(int argc, _TCHAR* argv[])
            {
                //Create semaphores
               mutex= OSCD_CreateSemaphore(1);
               reader=OSCD_CreateSemaphore(0);
               writer=OSCD_CreateSemaphore(0);

               int i,j; 
               char *DataToWrite[6]={
                   "(writer1) ","(writer2)","(writer3)",
                   "(writer4) ","(writer5)","(writer6)"
               };

               char *ReaderName[6]={
                   "reader1","reader2","reader3","reader4","reader5","reader6"
               };

               HANDLE  hThreads[12];
               for ( i=0,j=0;i<6;i++)
               {
                   hThreads[j]=::CreateThread(NULL,0,Reader,ReaderName[i],SW_NORMAL,NULL);
                   if(!hThreads[j++]){ printf("create thread failed\n") ;return 0;}

                   hThreads[j]=::CreateThread(NULL,0,Writer,DataToWrite[i],SW_NORMAL,NULL);
                   if(!hThreads[j++]){ printf("create thread failed\n"); return 0;}
                  
               }
               printf("CREATE THREADS COMPLETED\n");

               //Wait for all the threads ending up.and then exit.
               WaitForMultipleObjects(12,hThreads,TRUE,INFINITE);

               //Delete semaphores
               OSCD_DelSemaphore(mutex);
               OSCD_DelSemaphore(writer);
               OSCD_DelSemaphore(reader);

                return 0;
            }
            posted on 2012-06-09 20:44 demons 閱讀(1780) 評(píng)論(2)  編輯 收藏 引用 所屬分類: Operating System

            FeedBack:
            # re: Operating system Course Design(操作系統(tǒng)課程設(shè)計(jì))
            2012-06-10 08:39 | liji
            留個(gè)言,!  回復(fù)  更多評(píng)論
              
            # re: Operating system Course Design(操作系統(tǒng)課程設(shè)計(jì))
            2012-06-10 11:03 | Duwen
            李集?@liji
              回復(fù)  更多評(píng)論
              

            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲国产另类久久久精品小说| 久久91精品国产91| 99久久精品国产麻豆| 99久久久久| 久久久久亚洲AV成人网人人网站| 无码人妻精品一区二区三区久久久| 99久久国语露脸精品国产| 久久精品成人欧美大片| 国产成人精品综合久久久| 久久亚洲精品视频| 亚洲精品视频久久久| 97久久久久人妻精品专区| 久久最新免费视频| 久久免费精品视频| 久久久一本精品99久久精品88| 久久精品国产亚洲综合色| 久久www免费人成看片| 精品久久久久中文字| 国内精品久久久久影院优| 午夜精品久久久久久影视777| 久久久久亚洲Av无码专| 一级A毛片免费观看久久精品| 欧美久久综合性欧美| 久久人人妻人人爽人人爽| 亚洲婷婷国产精品电影人久久| 久久精品www人人爽人人| 狠狠色丁香婷婷久久综合五月 | 久久亚洲av无码精品浪潮| 综合久久给合久久狠狠狠97色| 精品一区二区久久久久久久网站| 久久久久亚洲AV综合波多野结衣| 99久久婷婷国产综合亚洲| 人人狠狠综合久久88成人| 久久人人爽人人人人片av| 日韩欧美亚洲综合久久影院Ds | 无码八A片人妻少妇久久| 精品久久久久久久久久中文字幕| 嫩草影院久久国产精品| 情人伊人久久综合亚洲| 好久久免费视频高清| 久久综合久久久|