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)

  • 相关阅读:
    leetcode 347. Top K Frequent Elements
    581. Shortest Unsorted Continuous Subarray
    leetcode 3. Longest Substring Without Repeating Characters
    leetcode 217. Contains Duplicate、219. Contains Duplicate II、220. Contains Duplicate、287. Find the Duplicate Number 、442. Find All Duplicates in an Array 、448. Find All Numbers Disappeared in an Array
    leetcode 461. Hamming Distance
    leetcode 19. Remove Nth Node From End of List
    leetcode 100. Same Tree、101. Symmetric Tree
    leetcode 171. Excel Sheet Column Number
    leetcode 242. Valid Anagram
    leetcode 326. Power of Three
  • 原文地址:https://www.cnblogs.com/johnpher/p/2570571.html
Copyright © 2011-2022 走看看