紫书第6章 数据结构基础 例题A~D

本文介绍了并发模拟器的工作原理,包括程序执行、锁与解锁语句的影响,以及如何模拟多程序在单处理器系统上的并发执行。此外,还涉及了矩阵链乘法的问题,探讨了在不同评估策略下所需基本乘法的数量,以优化计算效率。通过实例展示了如何判断有效的矩阵乘法规则。
摘要由CSDN通过智能技术生成

A . Concurrency 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 25 statements, one per line, followed by an end statement. The statements available are listed below.

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 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 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. 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 the mutual 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 input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed 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 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
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated 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 separate print statements should appear on separate lines.

Samples
Input 复制
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
Output
1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21
题目大意
你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行。每个程序包含不超过 25条语句,格式一共有5种:var = constant(赋值);print var(打印);lock;unlock;end。 变量用单个小写字母表示,初始为0,为所有程序公有(因此在一个程序里对某个变量 赋值可能会影响另一个程序)。常数是小于100的非负整数。 每个时刻只能有一个程序处于运行态,其他程序均处于等待态。上述5种语句分别需 要t1、t2、t3、t4、t5单位时间。运行态的程序每次最多运行Q个单位时间(称为配额)。当 一个程序的配额用完之后,把当前语句(如果存在)执行完之后该程序会被插入一个等待队 列中,然后处理器从队首取出一个程序继续执行。初始等待队列包含按输入顺序排列的各个 程序,但由于lock/unlock语句的出现,这个顺序可能会改变。 lock的作用是申请对所有变量的独占访问。lock和unlock总是成对出现,并且不会嵌套。 lock总是在unlock的前面。当一个程序成功执行完lock指令之后,其他程序一旦试图执行lock 指令,就会马上被放到一个所谓的阻止队列的尾部(没有用完的配额就浪费了)。当unlock 执行完毕后,阻止队列的第一个程序进入等待队列的首部。
输入n, t1, t2, t3, t4, t5, Q以及n个程序,按照时间顺序输出所有print语句的程序编号和结 果

分析:”
因为有“等待队列”和“阻止队列”的字眼,本题看上去是队列的一个简单应用,但请注意 “阻止队列的第一个程序进入等待队列的首部”。这违反了队列的规则:新元素插入 了队列首部而非尾部。
本题是对STL中的“双端队列”deque的应用;

#include<bits/stdc++.h>
using namespace std;
int main() {
   
	int n, a[10];
string s, ans;
int i,j;
    cin >>n;
    for (i = 0; i < n; i ++) {
   
        for (j = 0; j < 7; j ++) scanf("%d", &a[j]);
        getchar(); // 吸收多余字符
        queue<string> q1[a[0]]; deque<int> qr; queue<int> qb; // 每个程序对应指令;ready;阻塞
        for (j = 0; j < a[0]; j ++) {
    // n个程序
            while (getline(cin, s) && s != "end") q1[j].push(s);
            qr.push_back(j);
        }
        if (i != 0) puts(""); // 连续输出的空行
        map<string, string> vmp; // 变量对应的值
        bool isLock = false; // 标记是否有锁
        while (!qr.empty()) {
    // 等待队列非空
            int t = 0, k = qr.front(); qr.pop_front(); // 耗费时间,当前程序编号
            bool isBlock = false; // 标记是否发生阻塞
            while (t < a[6] && !q1[k
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值