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)

  • 相关阅读:
    [NOIP模拟赛][贪心]奶牛晒衣服.
    BZOJ3750: [POI2015]Piecz
    BZOJ2348 [Baltic 2011]Plagiarism
    高精度乘法【高乘高
    codevs 1215 迷宫
    变量交换
    a+b问题与圆柱体表面积的计算
    算数表达式的练习
    [bzoj1070][SCOI2007]修车[ 网络流]
    [bzoj2502]清理雪道[上下界网络流]
  • 原文地址:https://www.cnblogs.com/johnpher/p/2570571.html
Copyright © 2011-2022 走看看