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

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

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

            #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;//進程號
                char type;//類型
                int s;//start
                int t;//end
            }
            ;

            void PrimePriority(char *file);
            void ReadThread(void *p);
            void FindThread(void *p);
            bool check(int n);//檢查是否為素數(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ù)組中為空,消費者線程被迫結(jié)束\n");
                        ReleaseMutex(h_Mutex);
            //不要忘了釋放
                        break;
                    }

                    ReleaseMutex(h_Mutex);

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

                    printf(
            "消費者線程 %d[%d] 取出了第 %d 個元素: %d \n",id,n,i+1,prime.front());
                    prime.pop_front();
                    printf(
            "此時隊列中的數(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(
            "消費者 %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(
            "消費者已經(jīng)結(jié)束,存儲空間已滿\n");
                            ReleaseMutex(h_Mutex);
                            
            break;
                        }

                        ReleaseMutex(h_Mutex);
                        
            //
                        WaitForSingleObject(s_Empty,INFINITE);
                        WaitForSingleObject(h_Mutex,INFINITE);
                        printf(
            "生產(chǎn)者 %d[%d] 在隊尾插入了第 %d 個元素 %d \n",id,primeNum,++produceNum,i);
                        prime.push_back(i);
                        printf(
            "此時隊列中的數(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 閱讀(852) 評論(0)  編輯 收藏 引用


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


            婷婷久久综合| 久久成人国产精品二三区| 国产精品成人久久久久三级午夜电影 | 精品久久久久久无码中文字幕一区 | 天天综合久久一二三区| 久久五月精品中文字幕| 无码任你躁久久久久久久| 婷婷综合久久中文字幕蜜桃三电影 | 欧美日韩久久中文字幕| 久久精品国产亚洲AV蜜臀色欲| 中文字幕无码久久久| 久久精品国产亚洲AV嫖农村妇女| 久久中文娱乐网| 久久噜噜久久久精品66| 久久伊人五月丁香狠狠色| 久久人人爽人人爽人人AV | 久久这里只精品国产99热| 久久强奷乱码老熟女网站| 91超碰碰碰碰久久久久久综合| 国产精品久久久久a影院| 久久黄色视频| 久久精品人人做人人妻人人玩| 久久久久久青草大香综合精品| 国产成人久久精品一区二区三区| 精品国产热久久久福利| 精品无码久久久久国产| 久久无码中文字幕东京热| 精品国产综合区久久久久久| 香蕉久久永久视频| 99久久精品免费看国产| 国内精品久久久久影院优 | 久久久久免费精品国产| 久久夜色精品国产网站| 奇米影视7777久久精品人人爽| 久久久久香蕉视频| 94久久国产乱子伦精品免费| 日韩av无码久久精品免费| 久久精品a亚洲国产v高清不卡 | 人妻久久久一区二区三区| 久久精品久久久久观看99水蜜桃| 久久中文字幕无码专区 |