zoukankan      html  css  js  c++  java
  • Semaphore and Mutex

    In my project of sina spider as well as the course of Operating System, I come up with the use of Semaphore and Mutex, I make a note here for my study and my work.

    Definition

    In computer science, a semaphore is a variable or abstract data type provides a simple but useful abstraction controlling access by multiple processes to a common resource in a parallel programming environment.

    Semaphores are a useful tool in the prevention of race conditions; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores (same functionality that mutexes have).

    P & V:

    operation P:

    //apply for a resource

    Semaphore--;

    //if allocation fails, the process sleeps

    if(Semaphore < 0)

            sleep();

    operation V:

    //release a resource

    Semaphore++;

    //if resource is avaliable, wake up a process

    if(Semaphore > 0)

              wakeupaprocess() 

    Semaphore vs. mutex

    A mutex is essentially the same thing as a binary semaphore, and sometimes uses the same basic implementation. However, the term "mutex" is used to describe a construct which prevents two processes from accessing a shared resource concurrently. The term "binary semaphore" is used to describe a construct which limits access to a single resource.

    In many cases a mutex has a concept of an "owner": the process which locked the mutex is the only process allowed to unlock it. In contrast, semaphores generally do not have this restriction, something the producer-consumer example above depends upon.

    Example 1: A Mutex Example

    Description:

          There is a classroom , if a student enters it, variable 'conunt' increases by 1, and if a student exits it, variable 'conunt' decreases by 1.

    Solution:

          Initialize mutex = 1

          Enter:

          P(mutex)

      count++

      V(mutex)

      Exit:

          P(mutex)

      count--

      V(mutex)

    Example 2: Use in my Crawler 

    Description:

    In one of my Crawler for Sina microblog, I use parallel programming and there is a UID stack that need to be locked while one thread is visiting it, and when the top element of UID stack is poped out, the lock can be released. So I used the Semaphore to deal with it.

    Solution:

          Initialize mutex = 1

          With the control of UID Stack:

          P(mutex)

      UIDStack.top()

      UIDStack.pop()

      V(mutex)

  • 相关阅读:
    ubuntu
    long long 的输入输出问题
    hdu 4135 a到b的范围中多少数与n互质(容斥)
    hdu4757 可持续化01字典树+LCA
    E
    bzoj4260 求两个不相交的区间各自异或后相加的最大值。
    hdu4638 问一段区间能组成多少段连续的数
    hdu4637 计算俩运动对象的时间交
    hdu4632 回文子序列
    hdu4635 有向点双
  • 原文地址:https://www.cnblogs.com/johnpher/p/2570571.html
Copyright © 2011-2022 走看看