210-concurrency Simulator

Source: Internet
Author: User

Concurrency Simulator

PS: This topic, read test instructions on the great Kung Fu. Follow the Rujia program went through, debugging once again understand a probably, can only say that the foundation is not very good, but also need a lot of time to learn.

PS: Because the layout of the problem is more troublesome, here OJ Address: uva210-concurrency Simulator

Your task is to simulate parallel execution of n programs (numbered 1~n in input order). Each program contains no more than 25 statements, with a total of 5 formats:

  • var = constant (assigned value);
  • Print var (printing);
  • Lock
  • Unlock
  • End

The variable is represented by a single lowercase letter, initially 0, and is public for all programs (so assigning a variable in one program may affect another program). A constant is a non-negative integer that is less than 100.

Only one program is running at a time, and the other programs are in a waiting state. The above 5 kinds of statements need T1, T2, T3, T4, T5 unit time respectively. The run-state program runs at most Q units of time (called quotas) at a time. When a program's quota is exhausted, the program is inserted into a wait queue after the current statement (if it exists) is executed, and the processor takes a program from the first team to continue. The initial wait queue contains the individual programs arranged in the input order, but this order may change due to the presence of the Lock/unlock statement.

The role of lock is to request exclusive access to all variables. Lock and unlock always appear in pairs and are not nested. Lock is always in front of the unlock. When a program succeeds in executing the lock instruction, the other program will immediately be placed at the end of a so-called block queue (waste of unused quotas) once it attempts to execute the lock instruction. When unlock executes, the first program that blocks the queue enters the header of the wait queue.

/** PID: Process number Id[pid]: command location to which the process executes Prog[id[pid]]: The instruction content of the instruction location **/#include <cstdio>#include <queue>#include <cstring>#include <cctype>using namespace STD;Const intMaxnum =1005;//N processesintN//Indicates the time taken for 5 instructionsinttimes[5];//Cycle timeintQuantum//26 Variablesintval[ -];//InstructionCharprog[maxnum][Ten];//value is where the process is running in the instructionintId[maxnum];//Ready queue deque<int>Readyq;//block queue Queue<int>BLOCKQ;//LockBOOLLocked//Execution InstructionsvoidRunintPID) {//Cycle time    intQ = Quantum; while(Q >0) {//Get instructions        Char*p = Prog[id[pid]];Switch(p[2]) {//Example A =             Case ' = ': {//constant is a positive integer and less thanval[p[0] -' A '] =IsDigit(p[5]) ? (p[4] -' 0 ') *Ten+ (p[5] -' 0 '): p[4] -' 0 '; Q-= times[0]; Break; }//Example print a             Case ' I ': {printf("%d:%d\n", PID +1, val[p[6] -' A ']); Q-= times[1]; Break; }//Example Lock             Case ' C ': {//Lock, the process joins the blocking queue                if(locked) {Blockq.push (PID);return; }//LockLocked =true; Q-= times[2]; Break; }//Example Unlock             Case ' l ': {//UnlockLocked =false;//When the blocking queue is not empty, the first of the blocked queues is added to the first of the ready queue                if(!blockq.empty ()) {intPid2 = Blockq.front ();                    Blockq.pop ();                Readyq.push_front (PID2); } Q-= times[3]; Break; }//End             Case ' d ': {return; }        }//Find the next instruction in the processid[pid]++; }//Add the process to the execution queue at the endReadyq.push_back (PID);}intMain () {//T Group use cases    intTscanf("%d", &t); while(t--) {scanf("%d%d%d%d%d%d%d\n", &n, &times[0], &times[1], &times[2], &times[3], &times[4], &quantum);memset(Val,0,sizeof(Val));//Indicates the first few lines of instruction        intline =0;//provide some instructions for each process         for(inti =0; I < n; i++) {//Read a line into the instruction arrayFgets (prog[line++], maxnum, stdin);//Initialize each process at the beginning of the instruction arrayId[i] = line-1;//instruction is not end, then one line of instruction is read             while(Prog[line-1][2] !=' d ') {fgets (prog[line++], maxnum, stdin); }//Put process numberReadyq.push_back (i); }//Pre-lock is unlockedLocked =false;//Ready queue is not empty         while(!readyq.empty ()) {//Get the front process number of the ready queue            intPID = Readyq.front (); Readyq.pop_front ();//Execution InstructionsRun (PID); }if(T) {printf("\ n"); }    }return 0;}

210-concurrency Simulator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.