Concurrency Simulator UVa210

Times: 2 hrs.

很好的一道STL题,本题核心练习了queue和deque的运用。

首先,本题的输入就比较麻烦,带空格,选用cin.getline()读取整行,使用string流读取了操作数。储存方式最终选择用了两个向量,一个储存指令类型,一个储存指令对应的操作数(无操作数用-1填充),这两个向量都是从1开始储存,第0位保存当前并行程序运行位置(类似于IP寄存器)。

过程模拟上没有太多问题,主要注意对quantum、lock和unlock含义。quantum决定一个程序最多执行的操作数量,lock会阻止其他并行程序的lock,并挂起到等待queue,直到unlock,恢复等待queue挂起的第一个程序。

#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int var[26] = { 0 };
deque<int> exc, wait;
vector<vector<int>> opc, opd;
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int T, kase = 0;
    cin >> T;
    while (T--)
    {
        opc.clear(); opd.clear(); wait.clear();  memset(var, 0, sizeof var);
        int n, time[5], q, cnt = 0;
        cin >> n >> time[0] >> time[1] >> time[2] >> time[3] >> time[4] >> q;
        opc.resize(n); opd.resize(n);
        opc[0].push_back(1);
        opd[0].push_back(-1);
        cin.ignore();
        while (cnt < n)
        {
            char cmd[20];
            char x;
            cin.getline(cmd, 20);
            if (cmd[0] == 'p' && cmd[1] == 'r')
            {
                opc[cnt].push_back(1);
                opd[cnt].push_back(cmd[6] - 'a');
            }
            else if (cmd[0] == 'l' && cmd[1] == 'o')
            {
                opc[cnt].push_back(2);
                opd[cnt].push_back(-1);
            }
            else if (cmd[0] == 'u' && cmd[1] == 'n')
            {
                opc[cnt].push_back(3);
                opd[cnt].push_back(-1);
            }
            else if (cmd[0] == 'e' && cmd[1] == 'n')
            {
                opc[cnt].push_back(4);
                opd[cnt].push_back(-1);
                exc.push_back(cnt);
                cnt++;
                if (cnt < n)
                {
                    opc[cnt].push_back(1);
                    opd[cnt].push_back(-1);
                }
            }
            else
            {
                stringstream ss(cmd + 3);
                int xx;
                opc[cnt].push_back(cmd[0] - 'a' + 5);
                ss >> xx;
                opd[cnt].push_back(xx);
            }
        }
        int lock = 0;
        if (kase++) cout << endl;
        while (!exc.empty())
        {
            int prom = exc.front(), tot = opc[prom][0], sum = 0, flag = 1;
            exc.pop_front();
            while (sum < q)
            {
                //cout << opc[prom][tot] << endl;
                if (opc[prom][tot] >= 5)
                    var[opc[prom][tot] - 5] = opd[prom][tot], sum += time[0];
                else if (opc[prom][tot] == 1)
                    cout << prom + 1 << ": " << var[opd[prom][tot]] << endl, sum += time[1];
                else if (opc[prom][tot] == 2)
                {
                    if (!lock)
                        lock = 1;
                    else
                        wait.push_back(prom), flag = 0, sum = q + 1;
                    sum += time[2];
                }
                else if (opc[prom][tot] == 3)
                {
                    lock = 0, sum += time[3];
                    if (!wait.empty())
                    {
                        exc.push_front(wait.front());
                        wait.pop_front();
                    }
                }
                else
                    sum = q + 1, flag = 0;
                if (flag)
                    tot++;
            }
            opc[prom][0] = tot;
            if (flag)
                exc.push_back(prom);
        }

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值