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.)
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.).
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.
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
1: 3
2: 3
3: 17
3: 9
1: 9
1: 9
2: 8
2: 8
3: 21
3: 21