The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

软件课程设计2 生产者消费者问题

#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);//检查是否为素数



//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++;
    }

    
//创建线程
    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(
"所有线程结束\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(
"生产者已结束,数组中为空,消费者线程被迫结束\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(
"此时队列中的数字为:");
        
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 结束\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(
"消费者已经结束,存储空间已满\n");
                ReleaseMutex(h_Mutex);
                
break;
            }

            ReleaseMutex(h_Mutex);
            
//
            WaitForSingleObject(s_Empty,INFINITE);
            WaitForSingleObject(h_Mutex,INFINITE);
            printf(
"生产者 %d[%d] 在队尾插入了第 %d 个元素 %d \n",id,primeNum,++produceNum,i);
            prime.push_back(i);
            printf(
"此时队列中的数字为:");
            
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(
"生产者 %d 结束.\n\n\n",id);
    CloseHandle(h_Mutex);
    CloseHandle(s_Full);
    CloseHandle(s_Empty);
    nProduce
--;
}

posted on 2011-01-05 02:27 abilitytao 阅读(821) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理