Caozuojitong - Process Manager 2 (mutual exclusion and synchronization processes)

Operating Systems - Process Management 2 (process synchronization and mutual exclusion)

  1. Process synchronization and mutual exclusion
  • Resources of two or more processes can not be used simultaneously called critical resources . There is a critical resource poses a problem exclusive access between processes.

  • Mutual exclusion : the logic is completely independent, there is no relationship between the two processes as a resource to compete with each other constraints, called mutual exclusion.

  • Process Synchronization : There is collaboration process constantly adjust the relative speed or performance of procedures between them, in order to ensure the smooth implementation of the rational use of critical resources and processes. Typically implemented by means of intermediate media: as a semaphore operation , locking operation and the like. Synchronization mechanism should follow the rules:

    • Idle let into
    • Busy waiting
    • Limited waiting: waiting time to enter the critical process area must be limited to avoid entering the busy state, etc.
    • Let the right to wait: The process can not access their critical region should immediately release the processor.
  • Lock mechanism

    • Locking and unlocking

      Lock mechanism uses a lock variable windicating whether a critical section locked. w=1Representation is locked

      High efficiency lock and unlock primitives are:

      //加锁原语
      Lock w()
      {
          while (w == 1) { //表示当前进程进入不了临界区
              保护当前进程的CPU现场;
              将当前进程放入w的等待队列,将该进程置于"等待"状态;
              转进程调度;
          }
      }
      
      //开锁原语
      Unlock() {
          if (w等待队列不空) {
              移出等待队列首元素;
              将该进程置于就绪状态,并放入就绪队列;
          }
          w = 0;
      }

      To access all the critical area of ​​the process must perform locking primitives, locking primitives passed, the process may enter the critical section; after the completion of access to critical areas, perform unlock primitive, the release of this critical resource.

  • Semaphore mechanism

    Request signal mechanism and release critical resource primitive operation and wait for the operation of the operation signal, the operation is also called P and V operations. Semaphore: valid for synchronization and mutual exclusion of the processes implemented in the data structure semaphore synchronization mechanism. Common are integer semaphore, record semaphore, AND type semaphores and semaphore set .

    • Integer semaphore

      S represents an integer semaphore currently available number of critical resource class:

      The number of idle in the system such critical resources: s> 0

      s = 0: The number of critical system resources to 0, and no process is waiting

      S represents the absolute value of the number of system processes waiting: s <0

      wait(s):
          while (s <= 0) {
              该进程等待;
              s--;
          }
      
      signal(s): s++;
    • Recording semaphore

      Recording semaphore data structure:

      struct semaphore{
          int value;
          struct PCB *queue;
      };

      Value represents the value of the number of available critical resources of the system, the process list for the queue pointer to the PCB type of resource waiting queue.

      Recording semaphore primitive operations are:

      semaphore S;
      
      //wait操作
      wait(S) {
          S.value--;
          if (S.value >= 0) {
              本进程申请到资源,继续执行;
          }
          else{
              调用block原语,将本进程加入阻塞队列,保护CPU现场,释放处理机资源;
              转进程调度;
          }
      }
      
      //signal操作
      signal(S) {
          S.value++;
          if (S.value <= 0) {//说明PCB队列还有其它进程在等待
              在queue唤醒一个阻塞态进程;
          }
          释放本进程占用的该临界资源,继续执行;
      }
  1. Process synchronization example
  • Example 7.1 Using the printer synchronization

    semaphore s;
    s.value = 1;
    void main(){
        parbegin(p1,p2,p3,..,pn);
    }
    pi(){ //i = 1,2,3,...,n
        wait(s);
        打印;
        signal(s);
    }
  • Example 7.2 has a cache, shared by multiple processes, these processes have read process and the writing process. Write a plurality of processes using the same procedure cache to achieve synchronization process.

    semaphore empty,full;
    empty.value = 1; //empty用来表示缓存区是否是已被写入的状态
    full.value = 0; //full表示缓存区是否是已被读出的状态。
    
    reader() {
        while (true) {
            wait(full); //读之前先使full置于value为0的状态,避免其它进程也进入读取
            读缓存区;
            signal(empty); //读完后置empty的value为0,表示缓存区的数据已经读完了,可以继续写入了。
        }
    }
    
    writer() {
        while (true) {
            wait(empty);
            写缓存区;
            signal(full);
        }
    }
    
    //通过设置两个信号量实现了writer和reader的交替执行
    void main() {
        prebegin(writer,reader);
    }
  • Example 7.3 producer - consumer problem : the process of production of goods producer, into the buffer capacity of n for consumers to take. Consumers can not get things over the buffer zone, the producers can not fill the buffer zone to put things. The reading and writing files with the case is somewhat similar.

    /*
    用一个数组表示具有n个缓存区的缓存池。设有输入指针in指向一个可存放产品的缓存区,输出指针out指向可取得产品的缓存区。由于数组可以循环放置,所以当输入或输出加一时,可表示为: in = (in + 1) % n, out = (out + 1) % n。当(in + 1) % n = out时,表示缓存池已满,当in = out时,表示缓存池已空。整型counter表示缓存池中满缓存区的数量。
    还有以下变量:
    mutex:互斥使用缓存池信号量,初值mutex = 1
    empty:空缓存区的信号量
    full:满缓存区的信号量
    */
    semaphore mutex, empty, full;
    mutex.value = 1;
    empty.value = n;
    full.value = 0;
    
    product buffer[n];
    int in = 0,out = 0;
    
    void main() {
        prebegin(producer, consumer);
    }
    void producer() {
        while (true) {
            生产一件产品;
            wait(empty);
            wait(mutex);
            将产品放入缓存区;
            counter++;
              in = (in + 1) % n;
            signal(mutex);
            signal(full);
        }
    }
    void consumer() {
        while (true) {
            wait(full);
            wait(mutex);
            拿走一件产品;
            counter--;
            out = (out + 1) % n;
            signal(mutex);
            signal(empty);
        }
    }
    //注意:在这两个进程中两个wait()信号量的顺序不能改变,必须先要申请到full资源或empty资源才能继续申请mutex的资源,否则可能会造成申请到mutex的资源但却申请不到full或empty的资源的情况,然后进程就会进入等待状态,使得signal(mutex)不能执行,造成缓存池的锁死。

    This problem is more representative, generally used in the system processes the same type of resource is called the consumer of the resource, the process of release of such resources are called producers.

  • Example 7.4 readers - write the problem : F files can be shared by multiple processes to write F is called the writing process, read F is called the reading process. With the wait and signal resolving synchronization problems between process.

    //F可以同时被多个进程读,但不能被多个进程同时写,否则会造成数据的混乱.或者说一个写进程会对其他所有进程造成排斥。
    semaphore wmutex,rmutex;
    wmutex.value = 1;
    rmutex.value = 1; 
    int readcount = 0; //正在进行读操作的进程数量,因为readcount是一个会被多个读进程访问的资源,所以上文设置了rmutex来控制访问。
    void main() {
        prebegin(writer,reader);
    }
    void writer() {
        while (true) {
            wait(wmutex);
            写操作;
            signal(wmutex);
        }
    }
    void reader() {
        while (true) {
            wait(rmutex); //要想访问readcount必须先申请资源。
            if (readcount == 0) { //如果没有同类型进程在访问该资源,则需要进行申请
                wait(wmutex);
            }
            readcount++;
            signal(rmutex);
            读操作;
            wait(rmutex);
            readcount--;
            if (readcount == 0) signal(wmutex);
            signal(rmutex);
        }
    }

    This problem is typical of a class of problems, different from the producer - consumer issues, this model allows any number of specific actions into the process, but not other types enter. That there is no mutual exclusion between different types of processes. To solve this problem the key is that when there is at least one of the same type of process is accessing the resource, this process will be able to apply the resources do not have to directly access the resource.

  • Example 7.5 Dining Philosophers : five philosophers around a dinner table, the table has five chopsticks, put a between everyone. Philosopher only got about two chopsticks to eat, only to wait until someone else did not get the finish to get. Each philosopher eat something before does not put down the chopsticks in his hand. Describe the process of philosophers to eat.

    Approach assumes that every philosopher acquire chopsticks are left, and then let the right. The i-th bit philosophers chopstick process can be described as:

    //每一根筷子都是临界资源,为此设立一个筷子信号量数组
    semaphore chopstick[5];
    //每根筷子的初始值为1,筷子i相邻筷子的索引为(n+i-1)%n 和(i+1)%n。设哲学家i位于筷子i的右边
    void main() {
        prebegin(p1(),p2(),p3(),p4(),p5());
    }
    void pi() {   //p表示哲学家的吃饭过程
      while (true) {
            wait(chopstick[i]);
            wait(chopstick[(i + 1) % n]);
            吃饭;
            signal(chopstick[(i + 1) % n]);
            signal(chopstick[i]);
        }
    }

    But it also poses a problem, that is, all the philosophers at the same time picked up the chopsticks on the left, resulting in no philosopher can get the right hand chopsticks. Resulting in a deadlock .

    There are several ways to solve the deadlock, such as:

    1) Only when the right hand chopsticks philosophers are available only allowed to pick up the chopsticks

    2) a maximum of n - 1 number philosopher both reach the left chopstick to ensure there is always a philosopher can get a pair of chopsticks.

Guess you like

Origin www.cnblogs.com/lunar-ubuntu/p/12233515.html