• 东北大学操作系统实验:进程同步和通信(生产者消费者问题模拟)


    //producer_consumer.cc
    
    #include<iostream>
    #include<vector>
    #include<queue>
    using namespace std;
    
    const int bufferSize = 8;
    vector<int> buffer(bufferSize);
    queue<int> waitQueue;
    int id;
    int writeptr, readptr;
    int consumer, producer;
    
    void init()
    {
    	for (int i = 0; i < bufferSize; ++i)
    	{
    		buffer[i] = 0;
    	}
    	while(!waitQueue.empty())
    	{
    		waitQueue.pop();
    	}
    	id = 0;
    	writeptr = readptr = 0;
    	consumer = producer = 0;//the num of waiting 
    }
    
    int findEmpty(vector<int>& v, int len)
    {
    	for(int i = 0; i < len; ++i)
    	{
    		if(v[i] == 0)
    			return i;
    	}
    	return -1; 
    }
    
    int findDirty(vector<int>& v, int len)
    {
    	for(int i = 0; i < len; ++i)
    	{
    		if(v[i] > 0)
    			return i;
    	}
    	return -1; 
    }
    
    void show()
    {
        for(int i = 0; i < bufferSize; ++i)
        {
            if(buffer[i] > 0)
                cout << "  " << buffer[i] << "  |";
            else
                cout << "      |";
        }
        cout << "
    writeptr = 	" << writeptr << "	readptr = 	" << readptr << "	";
        if(producer)
            cout << "PRODUCER wait : " << producer << "	";
        else
            cout << "PRODUCER ready		";
        if(consumer)
            cout << "CONSUMER wait : " << consumer;
        else
            cout << "CONSUMER ready";
        cout << "
    ";
    }
    
    void produce()
    {	
    	if(findEmpty(buffer, bufferSize) == -1) // buffer full
    	{	
    		++id;
    		producer++;
    		waitQueue.push(id);
    	}
    	else
    	{
    		if(waitQueue.empty())
    		{
    			id++;
    			buffer[writeptr] = id;
    		}
    		else
    		{
    			producer--;
    			buffer[writeptr] = waitQueue.front();
    			waitQueue.pop();
    		}
    		writeptr = (writeptr + 1) % bufferSize;
    	}
    }
    
    void consume()
    {
    	if(findDirty(buffer, bufferSize) == -1) // buffer empty
    	{
    		if(waitQueue.empty())
    		{
    			consumer++;
    		}
    		else
    		{
    			buffer[writeptr] = waitQueue.front();
    			waitQueue.pop();
    			writeptr = (writeptr + 1) % bufferSize;
    		}
    	}
    	else
    	{
    		buffer[readptr] = 0;
    		readptr = (readptr + 1) % bufferSize;
    	}
    }
    
    void work()
    {
    	char c;
    	cin >> c;
    	while(c != 'q' && (c == 'p' || c == 'c'))
    	 // q = quit   p = produce one   c = consume one
    	{
    		if(c == 'p')
    		{
    			produce();
    		}
    		else if(c == 'c')
    		{
    			consume();
    		}
    		show();
    		cin >> c;
    	}
    }
    
    int main(int argc, char const *argv[])
    {
    	init();
    	work();
    	return 0;
    }

  • 相关阅读:
    搞IT的不如去养鸡养猪了
    matlabemacs运行matlab程序出错.不能调用matlab命令行模式
    微软利用全球天文望远镜数据建立宇宙地图
    程序人生2005年(30)
    IT领袖峰会精彩观点:4G时代不存在信令问题
    初识MySql数据库
    Android屏幕尺寸适配注意事项
    Java多线程详解(一)
    ios键盘高度
    ASP.NET MVC——Action的执行
  • 原文地址:https://www.cnblogs.com/deepspace/p/10260723.html
Copyright © 2020-2023  润新知