It takes a long time to understand the question.
Stick other people's translation ~
Your task is to simulate n programs (numbered 1 ~ N. Each program contains up to 25 Statements in five formats:
Var = constant (Value assignment );
Print VAR (print );
Lock;
Unlock;
End.
A variable is represented by a single lowercase letter. The initial value is 0, which is public to all programs. (therefore, assigning a value to a variable in one program may affect another program ). A constant is a non-negative integer smaller than 100. At each time, only one program is in the running state, and other programs are in the waiting state. The preceding five statements require T1, T2, T3, T4, and T5. A running program can run a maximum of Q units of time (as a quota) each time ). When the quota of a program is used up, after the current statement (if any) is executed, the program will be inserted into a waiting queue, and then the processor extracts a program from the first queue for further execution. At the beginning, the waiting queue contains various programs in the input order. However, due to the appearance of the lock and unlock statements, this sequence may change. Lock applies for exclusive access to all variables. Lock and unlock always appear in pairs and are not nested. The lock is always in front of the unlock. After a program successfully executes the lock command, once other programs attempt to execute the lock command, they will immediately be placed at the end of a so-called blocking Queue (a waste of resources will be wasted if no quota is used up ), after the UNLOCK Command is executed, the first program of the queue is blocked from entering the queue header. Input N, T1, T2, T3, T4, t5q, and n programs, and output program numbers and results of all print statements in chronological order. The Code is as follows:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <queue> 5 # include <cstring> 6 # include <deque> 7 using namespace std; 8 const int maxn = 1000; 9 int N, T1, T2, T3, T4, T5, Q; 10 char pro [maxn] [10]; 11 int ID [maxn]; 12 INT var [30]; 13 bool locked; 14 deque <int> readyq; 15 deque <int> blockq; 16 17 void run (int id) 18 {19 int q = Q; 20 while (q> 0) 21 {22 char * ins = pro [ID [ID]; 23 switch (INS [2]) 24 {25 case '=': // Var = constant; 26 {27/* record variable values through array Var */28 int Buf = 0; 29 for (INT I = 4; I <strlen (INS)-1; I ++) 30 Buf = Buf * 10 + (INS [I]-'0'); 31 var [INS [0]-'a'] = Buf; 32 Q-= T1; 33 break; 34} 35 case 'I': // print var; 36 {37 printf ("% d: % d \ n ", ID + 1, VAR [INS [6]-'a']); 38 Q-= t2; 39 break; 40} 41 case 'C ': // lock 42 {43/* once a program successfully executes a lock Statement, no other program may successfully execute a lock statement 44 until the locking program runs and executes the corresponding unlock statement. 45 shoshould a running program attempt to execute a lock while one is already in effect, 46 this program will be placed at the end of the blocked queue. */47 If (locked) 48 {49 blockq. push_back (ID); 50 return; 51} 52 locked = true; 53 Q-= T3; 54 B Reak; 55} 56 case 'l': // unlock; 57 {58 locked = false; 59/* When an unlock is executed, any program at the head of the blocked queue is moved to the head of the ready queue. */60 if (! Blockq. empty () 61 {62 readyq. push_front (blockq. front (); 63 blockq. pop_front (); 64} 65 Q-= T4; 66 break; 67} 68 case 'D': // end; 69 {70 Q-= T5; 71 return; 72 break; 73} 74} // switch; 75 ID [ID] ++; 76} // while; 77/* when a program time quantum expires, another ready program will be selected to run. 78 Any instruction currently being executed when the time quantum expires will be allowed to com Plete. */79 readyq. push_back (ID); 80} // run; 81 82 83 int main () 84 {85 int t; 86 scanf ("% d", & T ); 87 for (INT cases = 0; cases <t; cases ++) 88 {89 memset (VAR, 0, sizeof (VAR); 90 if (Cases) printf ("\ n"); 91 scanf ("% d", & N, & T1, & T2, & T3, & T4, & T5, & Q); 92 int line = 0; 93 for (INT I = 0; I <n; I ++) 94 {95 // note the multiline string record Method 96 /// ========================== ======================================= 97 fget S (PRO [Line ++], maxn, stdin); 98 ID [I] = line-1; // line can record the start-to-end operation of an ID; 99/* identification number based upon its location in the input data.100 (the first program has ID = 1, the second has ID = 2, etc .) */101 while (PRO [Line-1] [2]! = 'D) 102 fgets (PRO [Line ++], maxn, stdin ); 103/* programs are queued first-in-first-out for execution in a ready queue.104 the initial order of the ready queue corresponds to the original order of the programs in the input file. */105 // ============================================ =============== 106 readyq. push_back (I); 107} 108 locked = false; 109 while (! Readyq. empty () 110 {111 int pro_id = readyq. front (); 112 readyq. pop_front (); 113 run (pro_id); 114} 115} 116 return 0; 117}
View code
210-concurrency simulator (wf1991, deque, simulation)