• <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)寒,忽然年以殘,念往昔,語(yǔ)默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢(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   管理


            久久久久99精品成人片| 久久久中文字幕日本| 97久久国产露脸精品国产| 亚洲国产精品高清久久久| 国产一级做a爰片久久毛片| 精品久久久久久无码免费| 国产香蕉久久精品综合网| 996久久国产精品线观看| 久久精品国产99久久久香蕉| 国产成人精品三上悠亚久久| 久久婷婷久久一区二区三区| 2021国内久久精品| 99久久国产主播综合精品| 色天使久久综合网天天| 亚洲国产精品久久| 99久久超碰中文字幕伊人| 国产免费久久精品99re丫y| 中文字幕久久欲求不满| 麻豆亚洲AV永久无码精品久久| 久久久国产精华液| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 奇米影视7777久久精品| 久久AⅤ人妻少妇嫩草影院| 久久精品国产亚洲AV无码偷窥| 久久精品无码av| 国产精品久久久久久久午夜片 | 无码任你躁久久久久久老妇| 国产精品久久久久影院嫩草| 无码人妻久久一区二区三区免费 | 免费一级做a爰片久久毛片潮| 久久美女人爽女人爽| 久久久久久综合一区中文字幕| 久久综合狠狠综合久久综合88| 日本欧美久久久久免费播放网| 2020国产成人久久精品| 久久综合亚洲鲁鲁五月天| 亚洲精品第一综合99久久| 中文字幕精品无码久久久久久3D日动漫 | 国产成人久久精品一区二区三区| 一本久久a久久精品亚洲| 久久久久久久久久久|