• <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>

            The Fourth Dimension Space

            枯葉北風(fēng)寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢(mèng)令

            軟件課程設(shè)計(jì)2 生產(chǎn)者消費(fèi)者問題

            #include <windows.h>
            #include 
            <conio.h>
            #include 
            <fstream>
            #include 
            <cstdio>
            #include 
            <cmath>
            #include 
            <deque>
            using namespace std;
            #define FULL 9
            #define EMPTY 0
            int const maxn = 64;

            deque
            <int>prime;
            int nProduce=0;
            int nConsumer=0;

            HANDLE hMutex;
            HANDLE sFull;
            HANDLE sEmpty;

            struct ThreadInfo
            {
                
            int id;//進(jìn)程號(hào)
                char type;//類型
                int s;//start
                int t;//end
            }
            ;

            void PrimePriority(char *file);
            void ReadThread(void *p);
            void FindThread(void *p);
            bool check(int n);//檢查是否為素?cái)?shù)



            //main function
            int main( int agrc, char* argv[] )
            {
                
            char ch;
                prime.clear();
                
            while (true)
                
            {
                    
            // Cleare screen
                    system( "cls" );
                    
            // display prompt info
                    printf("*********************************************\n");
                    printf(
            "       1.Start test\n");
                    printf(
            "       2.Exit to Windows\n");
                    printf(
            "*********************************************\n");
                    printf(
            "Input your choice(1or2): ");
                    
            // if the number inputed is error, retry!
                    do{
                        ch 
            = (char)_getch();
                    }
            while( ch != '1' && ch != '2');

                    system ( 
            "cls" );
                    
            if ( ch == '1')
                        PrimePriority(
            "ex4.dat");
                    
            else if ( ch == '2')
                        
            return 0;
                    printf(
            "\nPress any key to finish this Program. \nThank you test this Proggram!\n");
                    _getch();
                }
             //endvalue while
            }
             //endvalue main

            bool Cheak(int n)
            {
                
            for(int i=2;i<n;i++)if(n%i==0)return false;
                
            return true;
            }



            void PrimePriority( char* file )
            {
                
            //定義
                int nThread=0;
                
            int tem;
                HANDLE hThread[maxn];
                DWORD threadID[maxn];
                ThreadInfo threadInfo[maxn];
                
            //初始化
                nProduce=nConsumer=0;
                sFull
            =CreateSemaphore(NULL,0,9,"full");
                sEmpty
            =CreateSemaphore(NULL,9,9,"empty");
                hMutex
            =CreateMutex(NULL,false,"mutex");
                
            //文件讀入
                ifstream  inFile;
                inFile.open(file);        
            //open file
                while ( inFile )
                
            {
                    
            // read every reader/writer info
                    inFile>>threadInfo[nThread].id;
                    inFile
            >>threadInfo[nThread].type;
                    inFile
            >>threadInfo[nThread].s;
                    inFile
            >>threadInfo[nThread++].t;
                    inFile.
            get();
                }

                
            //收集信息
                for(int i=0;i<nThread;i++)
                
            {
                    
            char ch=threadInfo[i].type;
                    
            if(ch=='D'||ch=='d')nConsumer++;
                    
            if(ch=='W'||ch=='w')nProduce++;
                }

                
            //創(chuàng)建線程
                for(int i=0;i<nThread;i++)
                
            {
                    
            char ch=threadInfo[i].type;
                    
            if(ch=='D'||ch=='d')hThread[i] = CreateThread(
                        NULL,
            0,(LPTHREAD_START_ROUTINE)ReadThread,&threadInfo[i],0,&threadID[i]);
                    
            if(ch=='W'||ch=='w')hThread[i] = CreateThread(
                        NULL,
            0,(LPTHREAD_START_ROUTINE)FindThread,&threadInfo[i],0,&threadID[i]);
                }

                WaitForMultipleObjects(nThread,hThread,
            true,INFINITE);
                printf(
            "所有線程結(jié)束\n");
                CloseHandle(hMutex);
                CloseHandle(sEmpty);
                CloseHandle(sFull);

            }
            // endvalue readerPriority



            void ReadThread(void *p)
            {
                
            //定義子Handle
                HANDLE h_Mutex;
                HANDLE s_Full;
                HANDLE s_Empty;
                
            //初始化
                s_Full = OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"full");
                s_Empty 
            = OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"empty");
                h_Mutex 
            = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex");

                
            int id,n;
                id 
            = ((ThreadInfo*)p)->id;
                n 
            = ((ThreadInfo*)p)->s;

                
            for(int i=0;i<n;i++)
                
            {

                    
            //block one
                    WaitForSingleObject(h_Mutex,INFINITE);
                    
            if(nProduce==0 && prime.empty()){
                        printf(
            "生產(chǎn)者已結(jié)束,數(shù)組中為空,消費(fèi)者線程被迫結(jié)束\n");
                        ReleaseMutex(h_Mutex);
            //不要忘了釋放
                        break;
                    }

                    ReleaseMutex(h_Mutex);

                    
            //block two
                    WaitForSingleObject(s_Full,INFINITE);
                    WaitForSingleObject(h_Mutex,INFINITE);

                    printf(
            "消費(fèi)者線程 %d[%d] 取出了第 %d 個(gè)元素: %d \n",id,n,i+1,prime.front());
                    prime.pop_front();
                    printf(
            "此時(shí)隊(duì)列中的數(shù)字為:");
                    
            for(deque<int>::iterator it=prime.begin();it!=prime.end();it++){
                        printf(
            "%d ",*it);
                    }

                    printf(
            "\n\n\n");
                    ReleaseMutex(h_Mutex);
                    ReleaseSemaphore(s_Empty,
            1,NULL);
                }

                printf(
            "消費(fèi)者 %d 結(jié)束\n\n\n",id);
                nConsumer
            --;
            }



            void FindThread(void *p)
            {
                
            //定義子Handle
                HANDLE h_Mutex;
                HANDLE s_Full;
                HANDLE s_Empty;
                
            //初始化
                s_Full = OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"full");
                s_Empty 
            = OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"empty");
                h_Mutex 
            = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex");

                
            int id,s,t;
                id 
            = ((ThreadInfo*)p) -> id;
                s 
            = ((ThreadInfo*)p) -> s;
                t
            = ((ThreadInfo*)p) -> t;

                
            int primeNum=0;
                
            for(int i=s;i<=t;i++)
                    
            if(Cheak(i))primeNum++;
                
            int produceNum=0;

                
            for(int i=s;i<=t;i++)
                
            {
                    
            if(Cheak(i))
                    
            {
                        WaitForSingleObject(h_Mutex,INFINITE);
                        
            if(nConsumer==EMPTY && prime.size()==FULL)
                        
            {
                            printf(
            "消費(fèi)者已經(jīng)結(jié)束,存儲(chǔ)空間已滿\n");
                            ReleaseMutex(h_Mutex);
                            
            break;
                        }

                        ReleaseMutex(h_Mutex);
                        
            //
                        WaitForSingleObject(s_Empty,INFINITE);
                        WaitForSingleObject(h_Mutex,INFINITE);
                        printf(
            "生產(chǎn)者 %d[%d] 在隊(duì)尾插入了第 %d 個(gè)元素 %d \n",id,primeNum,++produceNum,i);
                        prime.push_back(i);
                        printf(
            "此時(shí)隊(duì)列中的數(shù)字為:");
                        
            for(deque<int>::iterator it=prime.begin();it!=prime.end();it++){
                            printf(
            "%d ",*it);
                        }

                        printf(
            "\n\n\n");
                        ReleaseMutex(h_Mutex);
                        ReleaseSemaphore(s_Full,
            1,NULL);

                    }

                }

                printf(
            "生產(chǎn)者 %d 結(jié)束.\n\n\n",id);
                CloseHandle(h_Mutex);
                CloseHandle(s_Full);
                CloseHandle(s_Empty);
                nProduce
            --;
            }

            posted on 2011-01-05 02:27 abilitytao 閱讀(847) 評(píng)論(0)  編輯 收藏 引用


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


            久久久久亚洲国产| 久久久久亚洲精品无码网址 | 97精品伊人久久久大香线蕉| 久久精品中文字幕第23页| 亚洲成色WWW久久网站| 久久精品国产72国产精福利| 看全色黄大色大片免费久久久| 亚洲?V乱码久久精品蜜桃| 青青青青久久精品国产h| 亚洲av成人无码久久精品| 久久99精品久久久久子伦| 亚洲熟妇无码另类久久久| 久久久精品国产Sm最大网站| 精品久久久一二三区| 亚洲婷婷国产精品电影人久久| 青青青国产精品国产精品久久久久 | 精品午夜久久福利大片| 午夜不卡久久精品无码免费| 四虎国产精品免费久久久| 一级女性全黄久久生活片免费| 波多野结衣中文字幕久久| 亚洲国产香蕉人人爽成AV片久久| 国产三级久久久精品麻豆三级| 亚洲精品国产自在久久| 国产高清美女一级a毛片久久w| 99久久精品国产高清一区二区| 久久久噜噜噜久久中文福利| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 曰曰摸天天摸人人看久久久| 久久强奷乱码老熟女网站| 久久精品人妻中文系列| 久久国产美女免费观看精品| 狠狠色丁香婷婷综合久久来| 亚洲精品乱码久久久久久自慰| 午夜精品久久久久久影视riav| 国产伊人久久| 狠狠色婷婷久久一区二区| 久久强奷乱码老熟女| 国产精品日韩深夜福利久久 | 国产激情久久久久影院老熟女免费| 日韩人妻无码一区二区三区久久 |