标签:style blog http io color ar os for sp
题目有点长,理解题花了不少时间
粘下别人的翻译~
你的任务是模拟n个程序(按输入顺序编号为1~n)的并行执行。每个程序包含不超过25条语句,格式一共有5种:
var=constant(赋值);
print var(打印);
lock;
unlock;
end。
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 /*通过数组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 Should 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 break; 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 complete. */ 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%d%d%d%d%d%d", &n, &t1, &t2, &t3, &t4, &t5, &Q); 92 int line = 0; 93 for(int i = 0; i < n; i++) 94 { 95 ///注意记录多行字符串方法 96 ///================================================ 97 fgets(pro[line++], maxn, stdin); 98 ID[i] = line-1; ///line可记录某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 }
210 - Concurrency Simulator(WF1991, deque, 模拟)
标签:style blog http io color ar os for sp
原文地址:http://www.cnblogs.com/LLGemini/p/4072276.html