210 - Concurrency Simulator

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

The program thatis currently being executed is said to be running, while all programs awaitingexecution are said to be ready. A program consists of a sequence of no morethan 25 statements, one per line, followed by an end statement. The statementsavailable are listed below.

 

Each statementrequires an integral number of time units to execute. The running program ispermitted to continue executing instructions for a period of time called itsquantum. When a program’s time quantum expires, another readyprogram will be selected to run. Any instruction currently being executed whenthe time quantum expires will be allowed to complete.

Programs arequeued first-in-first-out for execution in a ready queue. Theinitial order of the ready queue corresponds to the original order of theprograms in the input file. This order can change, however, as a result of theexecution of lock and unlock statements.

The lock and unlock statementsare used whenever a program wishes to claim mutually exclusive access to thevariables it is manipulating. These statements always occur in pairs,bracketing one or more other statements. A lock will always precedean unlock, and these statements will never be nested. Once aprogram successfully executes a lock statement, no otherprogram may successfully execute a lock statement until thelocking program runs and executes the corresponding unlock statement.Should a running program attempt to execute a lock while oneis already in effect, this program will be placed at the end of the blockedqueue. Programs blocked in this fashion lose any of their current timequantum remaining. When an unlock is executed, any program atthe head of the blocked queue is moved to the head of the ready queue. Thefirst statement this program will execute when it runs will be the lock statementthat previously failed. Note that it is up to the programs involved to enforcethe mutual exclusion protocol through correct usage of lock and unlock statements.(A renegade program with no lock/unlock pair couldalter any variables it wished, despite the proper use oflock/unlock bythe other programs.)

Input

The inputbegins with a single positive integer on a line by itself indicating the numberof the cases following, each of them as described below. This line is followedby a blank line, and there is also a blank line between two consecutive inputs.

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

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

Output

For each testcase, the output must follow the description below. The outputs of twoconsecutive cases will be separated by a blank line.

Your output willcontain of the output generated by the print statements as they occur duringthe simulation. When a print statement is executed, your program should displaythe 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 andcorrect output are 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值