Parallel Computer Simulator
 

Description

Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, executing some number of instructions from each program before switching to the next. You are to simulate the concurrent execution of up to ten programs on such a system and determine the output that they will produce.

The program that is currently being executed is said to be running, while all programs awaiting execution are said to be ready. A program consists of a sequence of no more than 200 statements, one per line, followed by an end statement. The statements available are listed below.

Statement type Syntax
Assignment variable› = ‹constant
Output print ‹variable
Begin mutual exclusion look
End mutual exclusion unlock
Stop execution end

A ‹variable› is any single lowercase alphabetic character and a ‹constant› is an unsigned decimal number less than 1000. There are only 26 variables in the computer system, and they are shared among the programs. Thus assignments to a variable in one program affect the value that might be printed by a different program. All variables are initially set to zero.

Each statement requires an integral number of time units to execute. The running program is permitted to continue executing instructions for a period of time called its quantum. When a program’s time quantum expires, another ready program will be selected to run. Any instruction currently being executed when the time quantum expires will be allowed to complete.

Programs are queued first-in-first-out for execution in a ready queue. The initial order of the ready queue corresponds to the original order of the programs in the input file. This order can change, however, as a result of the execution of lock and unlock statements.

The lock and unlock statements are used whenever a program wishes to claim mutually exclusive access to the variables it is manipulating. These 3 statements always occur in pairs, bracketing one ormore other statements. A lock will always precede an unlock, and these statements will never be nested. Once a program successfully executes a lock statement, no other program may successfully execute a lock statement until the locking program runs and executes the corresponding unlock statement. Should a running program attempt to execute a lock while one is already in effect, this program will be placed at the end of the blocked queue. Programs blocked in this fashion lose any of their current time quantum remaining. When an unlock is executed, any program at the head of the blocked queue is moved to the head of the ready queue. The first statement this program will execute when it runs will be the lock statement that previously failed. Note that it is up to the programs involved to enforce themutual exclusion protocol through correct usage of lock and unlock statements. (A renegade program with no lock/unlock pair could alter any variables it wished, despite the proper use of lock/unlock by the other programs.)

Input

The first line of the input file consists of seven integers separated by spaces. These integers specify (in order): the number of programs which follow, the unit execution times for each of the five statements (in the order given above), and the number of time units comprising the time quantum. The remainder of the input consists of the programs, which are correctly formed from statements according to the rules described above.

All program statements begin in the first column of a line. Blanks appearing in a statement should be ignored. Associated with each program is an identification number based upon its location in the input data (the first program has ID=1, the second has ID=2, etc.).

Output

Your output will contain the output generated by the print statements as they occur during the simulation. When a print statement is executed, your program should display the program ID, a colon, a space, and the value of the selected variable. Output from separate print statements should appear on separate lines. A sample input and correct output is shown below.

Sample Input

1 3 1 1 1 1 1 1
a = 4
print a
lock
b = 9
print b
unlock
print b
end
a = 3
print a
lock
b = 8
print b
unlock
print b
end
b = 5
a=17
print a
print b
lock
b = 21
print b
unlock
print b
end

Sample Output

1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21
本题目的题意是:一共有n个程序,每个程序的运行时间是t,程序的操作共有5种,时间分别是t1,t2,t3,t4,t5,执行完的程序会插入到等待队列中,初始队列按照程序的顺序出现,但是Lock和unlock的出现会打乱这个顺序
lock的作用是对所有变量的独占访问,当一个程序执行完lock后,若其他程序访问,则会被放到阻止队列的队尾,当unlock执行时,阻止队列的一个程序会放在执行队列的一个首部
分析:建立两个双向队列(具体操作参照STL文档),ready和block,如果遇到lock执行block.push_back(i)遇到unlock执行ready.push_front(block.front()),block.pop_front()
双向队列:#include<deque>
     deque<type> name;
代码是参照某位大神的代码打的,开始实在没有看懂题目/笑哭
 #include<iostream>//里面包含string及其函数
#include<deque>
#include<cstring>
using namespace std; deque<int> ready,block;
int var[],id[],st[],program,q,t,i;//关于这一段请看下面
string code[];//存储指令内容
bool locked; void run(int i){
int time=q;
while (time>){
string now=code[id[i]];
if(now[] == '='){
var[now[] - 'a'] = isdigit(now[]) ? (now[]-'')*+(now[]-'') : now[]-''; //isdigit检查是否为数字,var依次存储a,b,c等变量当前值
time -= st[];
}
else if(now[] == 'i'){
cout<<i+<<": "<<var[now[]-'a']<<endl;
time -= st[];
}
else if(now[] == 'c'){
if(locked){block.push_back(i);return;}
locked = true;
time -= st[];
}
else if(now[] == 'l'){
if(!block.empty()) ready.push_front(block.front()),block.pop_front();
time -= st[];
locked = false;
}
else if(now[] == 'd'){return ;}
id[i]++;
}
ready.push_back(i);
} int main(){
cin>>t;
while (t--){
cin>>program>>st[]>>st[]>>st[]>>st[]>>st[]>>q;
memset(var,,sizeof(var));
ready.clear();block.clear();
int line_num = ;
for (i=;i<program;i++){
ready.push_back(i);
getline(cin,code[line_num++]);
id[i]=line_num-;
while (code[line_num-]!="end"){
getline(cin,code[line_num++]);
}
}
locked=false;
while (!ready.empty()){
int now=ready.front();ready.pop_front();
run(now);
}
if(t) cout<<endl;
}
return ;
}

poj 2905 双向队列(待补充)的更多相关文章

  1. Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...

  2. 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuq ...

  3. python基础知识4——collection类——计数器,有序字典,默认字典,可命名元组,双向队列

    1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能  Counter 我们从中挑选一些相对常用的方法来举例: 在上面的例子 ...

  4. javascript中的双向队列

    1.概念 我们知道队列是一种先进先出的结构,只能在队伍的开头添加元素,队伍的结尾删除元素.双向队列的概念就是同时允许在队伍的开头和结尾添加和删除元素.在javascript中有一个处理数组的方法Arr ...

  5. stl中双向队列用法

    双向队列的操作如下: d[i]:返回d中下标为I的元素的引用. d.front():返回的一个元素的引用. d.back():返回最后一个元素的引用. d.pop_back():删除尾部的元素.不返回 ...

  6. 队列(Queue)--环形队列、优先队列和双向队列

    1. 队列概述 队列和堆栈都是有序列表,属于抽象型数据类型(ADT),所有加入和删除的动作都发生在不同的两端,并符合First In, First Out(先进先出)的特性. 特性: ·FIFO ·拥 ...

  7. C++ Double Ended Queues(双向队列)

    双向队列和向量很相似,但是它允许在容器头部快速插入和删除(就像在尾部一样). Constructors 创建一个新双向队列 Operators 比较和赋值双向队列 assign() 设置双向队列的值 ...

  8. SDUT1466双向队列

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1466&cid=1182 题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列 ...

  9. PHP — 用PHP实现一个双向队列

    1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...

随机推荐

  1. css给div添加阴影效果

    直接上代码: <style type="text/css">.mydiv{   width:250px; height:auto; border:#909090 1px ...

  2. js实现前端下载文件

    在前端下载文本格式的文件时,可采用下面的方式: (1)创建基于文件内容的Blob对象: (2)通过URL上的createObjectURL方法,将blob对象转换成一个能被浏览器解析的文件地址. (3 ...

  3. 从一个实例谈谈postgresql索引锁

    最近客户在使用我司开发的数据库时,报告了如下问题(也不能算是问题,就是疑惑吧),环境如下: OS : Red Hat Enterprise Linux Server release 6.7 (Sant ...

  4. WebP 的前世今生

    除了视频,图片占据了 PC 和 App 的大部分流量,为运营方带来高额的成本支出,同时过多的图片加载会影响到网站与 App 的加载速度.因此在保证图片质量的前提下缩小图片的体积就成了迫在眉睫的事情. ...

  5. Struts2知识整理

    准备找工作了.好忐忑!!! 整理整理知识,好好准备. 其实现在Struts2好像不是特别流行,不过还是有用武之地的. struts2简介 struts2是基于mvc开发模型的框架,属于表现层框架 核心 ...

  6. 使用原生JavaScript的Canvas实现拖拽式图形绘制,支持画笔、线条、箭头、三角形、矩形、平行四边形、梯形以及多边形和圆形,不依赖任何库和插件,有演示demo

    前言 需要用到图形绘制,没有找到完整的图形绘制实现,所以自己实现了一个 - - 一.实现的功能 1.基于oop思想构建,支持坐标点.线条(由坐标点组成,包含方向).多边形(由多个坐标点组成).圆形(包 ...

  7. Nodejs学习笔记(十五)--- Node.js + Koa2 构建网站简单示例

    目录 前言 搭建项目及其它准备工作 创建数据库 创建Koa2项目 安装项目其它需要包 清除冗余文件并重新规划项目目录 配置文件 规划示例路由,并新建相关文件 实现数据访问和业务逻辑相关方法 编写mys ...

  8. salesforce零基础学习(八十三)analytics:reportChart实现Dashboard(仪表盘)功能效果

    项目中经常会用到Report以及Dashboard来分析汇总数据,Dashboard可以指定view as user,如果针对不同的用户需要显示其允许查看的数据,比如  根据role hierarch ...

  9. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  10. Mybatis查询时报 Bad format for Time '454:54:54' in column 6 异常

    报     Bad format for Time '454:54:54' in column 6 解决方案:1. 查询实体类和.xml数据是否相对应 2. 查询sql是否正确 3. 查看表的设计,是 ...