1. 程式人生 > >poj 2905 雙向隊列(待補充)

poj 2905 雙向隊列(待補充)

lec ron rom file describe roc ostream () separate

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

Statement typeSyntax
Assignment ?variable? = ?constant?
Output print ?variable?
Begin mutual exclusion look
End mutual exclusion unlock
Stop execution end

A ?variable? is any single lowercase alphabetic character and a ?constant? is an unsigned decimal number less than 1000. 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 3 statements always occur in pairs, bracketing one ormore 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 themutual 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 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

Your output will contain 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. A sample input and correct output is 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
本題目的題意是:一共有n個程序,每個程序的運行時間是t,程序的操作共有5種,時間分別是t1,t2,t3,t4,t5,執行完的程序會插入到等待隊列中,初始隊列按照程序的順序出現,但是Lock和unlock的出現會打亂這個順序
lock的作用是對所有變量的獨占訪問,當一個程序執行完lock後,若其他程序訪問,則會被放到阻止隊列的隊尾,當unlock執行時,阻止隊列的一個程序會放在執行隊列的一個首部
分析:建立兩個雙向隊列(具體操作參照STL文檔),ready和block,如果遇到lock執行block.push_back(i)遇到unlock執行ready.push_front(block.front()),block.pop_front()
雙向隊列:#include<deque>
     deque<type> name;
代碼是參照某位大神的代碼打的,開始實在沒有看懂題目/笑哭
 1 #include<iostream>//裏面包含string及其函數
 2 #include<deque>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 deque<int> ready,block;
 7 int var[26],id[1024],st[5],program,q,t,i;//關於這一段請看下面
 8 string code[1024];//存儲指令內容
 9 bool locked;
10 
11 void run(int i){
12     int time=q;
13     while (time>0){
14         string now=code[id[i]];
15         if(now[2] == =){
16             var[now[0] - a] = isdigit(now[5]) ? (now[4]-0)*10+(now[5]-0) : now[4]-0;  //isdigit檢查是否為數字,var依次存儲a,b,c等變量當前值
17             time -= st[0];
18         }
19         else if(now[2] == i){
20             cout<<i+1<<": "<<var[now[6]-a]<<endl;
21             time -= st[1];
22         }
23         else if(now[2] == c){
24             if(locked){block.push_back(i);return;}
25             locked = true;
26             time -= st[2];
27         }
28         else if(now[2] == l){
29             if(!block.empty()) ready.push_front(block.front()),block.pop_front();
30             time -= st[3];
31             locked = false;
32         }
33         else if(now[2] == d){return ;}
34         id[i]++;
35     }
36     ready.push_back(i);
37 }
38 
39 int main(){
40     cin>>t;
41     while (t--){
42         cin>>program>>st[0]>>st[1]>>st[2]>>st[3]>>st[4]>>q;
43         memset(var,0,sizeof(var));
44         ready.clear();block.clear();
45         int line_num = 0;
46         for (i=0;i<program;i++){
47             ready.push_back(i);
48             getline(cin,code[line_num++]);
49             id[i]=line_num-1;
50             while (code[line_num-1]!="end"){
51                 getline(cin,code[line_num++]);
52             }
53         }
54         locked=false;
55         while (!ready.empty()){
56             int now=ready.front();ready.pop_front();
57             run(now);
58         }
59         if(t) cout<<endl;
60     }
61     return 0;
62 }

poj 2905 雙向隊列(待補充)