当前位置:网站首页>PV operation daily question - exam questions

PV operation daily question - exam questions

2022-06-10 16:10:00 liangsena


I just did a simulation problem of electrical engineering , The difficulty is only moderate , But the title is a little loose , I modified it a little , If there is something wrong, please send a private letter .

One 、 Problem description

A school is going to have a final exam , Each examination room has N A student ,1 A teacher . In and out , Only one person can pass through the entrance of the examination room at a time , First come first served , Finish first and leave first . Regulations : When N After all the students entered the examination room , Only teachers can give out papers ; Students need to wait for the teacher's test signal before they can answer the questions ; Students can leave after handing in their papers , The teacher must wait for all the students to hand in their papers before leaving the examination room . Suppose that both teachers and students are regarded as processes , Excuse me, PV The operation solves the synchronization and mutual exclusion in the above problems , And explain the meaning of semaphore and initial value .


Two 、 problem solving

int count=0;            // The current number of students in the examination room 
semaphore empty=0;      // Whether all the students in the examination room have finished 
semaphore full=0;       //N All the candidates arrived 
semaphore start=0;      // Test start signal 
semaphore end=0;        // Whether all exams are handed in 
semaphore door=0;       // Mutual exclusion in and out of the examination room 
smeaphore mutexCount=0; // Exclusive access count

Student-i(int i=1,2,...,N)
{
    
    while(1)
    {
    
        // Enter the examination room 

        P(door);
         Entrance ;
        V(door);
        P(mutexCount);
        count++;
        if(count==N)
        {
    
            V(full);
        }
        V(mutexCount);

        // Answer questions are handed in 

        if(start==1)
        {
    
             Answer the questions ;
             Hand in the paper after writing ;
        }
        P(mutexCount);
        count--;
        if(count==0)
        {
    
            V(empty);
        }
        V(mutexCount);

        // Out of the examination room 
        P(door);
         Out of the door ;
        V(door);
    }
}

Teacher()
{
    
    while(1)
    {
    
        P(door);
         Entrance ;
        V(door);

        P(full);
         Hair curls ;
        start=1;

        P(empty);
         Seal the test paper ;

        P(door);
         Out of the examination room ;
        V(door);
    }
}

For semaphores :

  1. Whether a group of students have arrived 、 Finish all the exams
  2. A set of answer signals
  3. Current number of students , And mutually exclusive semaphores that access it
  4. Finally, the semaphore of the mutually exclusive access gate

For student progress :

  1. Entrance
  2. Waiting for the answer , Answer the questions , Hand in papers
  3. Out of the door

On the teacher's progress :

  1. Entrance
  2. When the students arrive , Send an answer signal
  3. When the students have finished , Seal the paper
  4. Out of the door

3、 ... and 、 twitter

This topic is a bit like the enhanced reader writer question , The process is complicated .

原网站

版权声明
本文为[liangsena]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101014232931.html