UVa210:Concurrency Simulator

                Concurrency Simulator

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 25 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 lock
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 100. There are only 26 variables in the computer system, and thay 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 integeral 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 expired 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 wished to claim mutually exclusive access to the variables it is manipulating. These statements always occur in pairs, bracketing one or more other statements. A lock will always precede an unlock, and these statements will never be nested. One a program successfully execute 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 the mutual exclusion protocol through correct usage of lock and unlock statements. (A renegade program with no lock/unlock pair could alter any variable it wished, despite the proper use of lock/unlock by the other programs)

Input

The input begins with a single positive integer on a line by itself indicating the number of the case following by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input file consists of seven integers separated by spaces. These integers specify (in order): the number of program which follow, the unit execution time 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 colum of a line.
Blanks appearing in a statement should be ignored.Associated which 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

For each test case, the output must follow the description below. The outputs of two consecutive cases will be seperated by a blank line.

Your output will contain of 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 seperate print statement should appear on seperate lines.

A sample input and correct output are showed 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
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

时间: 2024-10-11 12:48:27

UVa210:Concurrency Simulator的相关文章

210 - Concurrency Simulator

Concurrency Simulator PS:这道题目,看懂题意就费了好大功夫.跟着RuJia的程序走了一遍,调试了一遍才明白个大概,只能说基础不是很好,还需要大量的时间学习. PS:因为该题排版较麻烦,这里给出OJ网址:UVa210 - Concurrency Simulator 你的任务是模拟n个程序(按输入顺序编号为1-n)的并行执行.每个程序包含不超过25条语句,格式一共有5种: var = constant(赋值): print var(打印): lock: unlock: end

UVa 210 Concurrency Simulator(双端队列)

题意  模拟程序并行运行 STL队列 双端队列 的应用  用双端队列维护即将执行的程序  再用个队列维护等待变量释放的程序   用lock表示变量锁定状态 先将所有程序依次放到执行队列中  每次取出队首程序运行不超过lim时间  未运行完又放到执行队列队尾 遇到lock时  若当前锁定状态为false就将锁定状态变为true  否则将当前程序放到等待队列队尾并结束运行 遇到unlock时  若等待队列有程序  就将等待队列队首程序放到执行队列队首 遇到end时 退出当前执行(不再进队尾) #in

UVA 210 Concurrency Simulator

题目链接:https://vjudge.net/problem/UVA-210 题目大意 一共有 T 组案例. 对于每组案例,你的任务是模拟n个程序(按输入顺序编号1~n)的并行执行.每个程序包含不超过25条语句. 格式一共是5种:赋值(var=constant),打印(print var),lock,unlock,end,耗时分别为$t_1,t_2,t_3,t_4,t_5$?. 变量用一个小写字母表示,初始时为0,为所有并行程序共有,且它的值始终保持在[0,100]内,所以一个程序对某一个变量

UVa 210 Concurrency Simulator (双端队列+模拟)

题意:给定n个程序,每种程序有五种操作,分别为 var = constant(赋值),print var (打印), lock, unlock,end. 变量用小写字母表示,初始化为0,为程序所公有(一个程序里对某个变量修改可以会影响其他程序里的这个变量), 常数小于100(也就是说最多两位数). 每个时刻都只能有一个程序处于运行状态,其他的都在等待,上述五种操作用时分别是t1, t2, t3, t4, t5.运行中的程序, 每次最多能运行q个时间,当q个时间被用完后,它会被放在等待队列的尾部,

210 - Concurrency Simulator(WF1991, deque, 模拟)

题目有点长,理解题花了不少时间 粘下别人的翻译~ 你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行.每个程序包含不超过25条语句,格式一共有5种: var=constant(赋值): print  var(打印): lock: unlock: end. 变量用单个小写字母表示,初始值为0,为所有程序公有(因此在一个程序里对某个变量赋值可能会影响到另一个程序).常数是小于100的非负整数. 每个时刻只能有一个程序处于运行态,其他程序均处于等待.上述五种语句分别需要t1.t2.t3.t4.

210 - Concurrency Simulator【模拟、双端队列】

没有什么特别的,就是按照题意进行模拟,代码有点长... #include<cstdio> #include<queue> #include<map> #include<cstring> #include<algorithm> using namespace std; const int maxn = 222; int n,t[10],q; int Value[maxn]; /* 1 a = 1 2 print 3 lock 4 unlock 5

Java 7 Concurrency Cookbook 翻译 序言

在日常的Java代码开发过程中,很难免地有对多线程的需求,掌握java多线程和并发的机制也是Java程序员写出更健壮和高效代码的基础.笔者找寻国内已出版的关于Java多线程和并发的的中文书籍和翻译书籍,大家一致推荐的是<Java Concurrency in Practice>,笔者暂时还没有看英文原版,笔者看的是它的翻译版<Java并发编程实战>,笔者读起来感觉并不通畅,不知道是翻译的问题还是原版本来就写得不够流畅,同时感觉知识的深度也超过了入门的需求. 笔者在机缘巧合之下,发现

深入浅出 Java Concurrency (35): 线程池 part 8 线程池的实现及原理 (3)[转]

线程池任务执行结果 这一节来探讨下线程池中任务执行的结果以及如何阻塞线程.取消任务等等. 1 package info.imxylz.study.concurrency.future;2 3 public class SleepForResultDemo implements Runnable {4 5     static boolean result = false;6 7     static void sleepWhile(long ms) {8         try {9      

深入浅出 Java Concurrency (13): 锁机制 part 8 读写锁 (ReentrantReadWriteLock) (1)[转]

从这一节开始介绍锁里面的最后一个工具:读写锁(ReadWriteLock). ReentrantLock 实现了标准的互斥操作,也就是一次只能有一个线程持有锁,也即所谓独占锁的概念.前面的章节中一直在强调这个特点.显然这个特点在一定程度上面减低了吞吐量,实际上独占锁是一种保守的锁策略,在这种情况下任何“读/读”,“写/读”,“写/写”操作都不能同时发生.但是同样需要强调的一个概念是,锁是有一定的开销的,当并发比较大的时候,锁的开销就比较客观了.所以如果可能的话就尽量少用锁,非要用锁的话就尝试看能