UNIX network programming-lock (2)

Source: Internet
Author: User

Table of Contents

  • 1 UNIX network programming-lock (2)
    • 1.1 semaphores API
    • 1.2 Implementation of semaphores mutex lock
      • 1.2.1 The member variables are as follows:
      • 1.2.2 implement the following member functions:
    • 1.3 Implementation of the semaphore read/write lock
      • 1.3.1 member variables of RWSem:
      • 1.3.2 member functions of RWSem:
    • 1.4 download the semaphore read/write implementation source code

This blog is about semaphores. semaphores provide mutual exclusion between processes, which is convenient. To use mutex to implement the semaphore function, you must create mutex on the shared memory. So when mutex is required between threads, it is best to use mutex; When mutex is used between processes, sem is used. In summary, mutex directly uses the process, and it seems useless; sem uses the thread, and it seems superfluous.

1.1 semaphores API

Sem_t * sem_open (const char * name, int oflag, mode_t mode, unsigned int value );

Create a semaphore. The value is very important. The initial semaphore is the value.

Int sem_unlink (const char * name );

Destroy semaphores by name

Sem_t * sem_open (const char * name, int oflag );

Open an existing semaphore

Int sem_close (sem_t * sem );

Disabling a semaphore does not destroy it. It still exists in the kernel, but is detached from the current process.

Int sem_wait (sem_t * sem );

If the sem value is 0, the system waits. Otherwise, the sem value is-1.

Int sem_trywait (sem_t * sem );

If the sem value is 0, the system gives up waiting and returns the EAGAIN value; otherwise, the sem value is-1.

Int sem_post (sem_t * sem );

Sem value + 1

Int sem_getvalue (sem_t * sem, int *

If the returned result is successful, sval is the current sem value; otherwise, an error occurs when obtaining the value.

1.2 Implementation of semaphores mutex lock

The mutex class of semaphores is SemMutex.

1.2.1 The member variables are as follows:

Sem mutex _; Sem actually encapsulates an sem * pointer, initialized to SEM_FAILED

Std: string name _; semaphore name

1.2.2 implement the following member functions:

SemMutex (const char * name );

Name of the initialization Mutex of the constructor

Bool Create ();

Call sem_open to create a semaphore. The important thing is that the semaphore count is initialized to 1.

Bool Destroy ();

Destroy semaphores

Bool Open ();

Open the created semaphore

Bool IsOpen ();

Indicates whether the semaphore has been opened. In fact, the sem pointer is SEM_FAILED.

Bool Lock ();

When sem_wait is called, if the semaphore value is 0, the system waits until it occupies the lock resource. Otherwise, the sem value is-1.

Bool UnLock ();

Call sem_post to release the lock resource. The sem value is + 1.

Bool TryLock ();

Call sem_trywait. If the lock resource is occupied and the sem value is 0, you do not have to wait and return EAGAIN directly.

Int LockStatus ();

Get the lock status,-1, error, 0-lock status, 1-no lock status; in fact, it calls sem_getvalue to get the sem count to view the status. In fact, to implement mutex semaphores is to implement a 2-value semaphores. The semaphores are always changing between 0 and 1. When 1 is used, it is unlocked and will not be blocked; when the value is 0, the lock status will block.

1.3 Implementation of the semaphore read/write lock

The implementation of the semaphore read/write lock requires an Sem to record the number of concurrent reads and a SemMutex to achieve mutual exclusion. Its Class is RWSem.

The key is to determine the lock status.

P_mutex _-> LockStatus = UNLOCK; indicates no lock

P_mutex _-> LockStatus = LOCK & p_read _-> GetValue () = 0; the write LOCK has been applied, that is, the LOCK has been applied, but the number of read locks is 0.

P_mutex _-> LockStatus = LOCK & p_read _-> GetValue ()> 0; the read LOCK has been applied, that is, the LOCK has been applied, but the number of read locks> 0

1.3.1 member variables of RWSem:

SemMutex * p_mutex _; implements read/write mutex

Sem * p_read _; record the number of currently reading locks

1.3.2 member functions of RWSem:

Bool Create ();

Create p_mutex _ and p_read _

Bool Destroy ();

Destroy p_mutex _ and p_read _

Bool Open ();

Open p_mutex _ and p_read _

Bool Close ();

Disable p_mutex _ and p_read _

Bool WLock ();

Try to read the lock. When the read lock has occupied the resource or the write lock stops the resource, it will fail. No matter which lock occupies the resource, the LockStatus of p_mutex _ is locked. The implementation mechanism is to call the Wait of p_mutex _.

Bool TryWLock ();

Try to read the lock. When the read lock has occupied the resource or the write lock stops the resource, it will fail. No matter which lock occupies the resource, the LockStatus of p_mutex _ is locked. The implementation mechanism is to call the TryWait of p_mutex _.

Bool TryRLock ();

If the write lock occupies the resource, false is returned.

When the write lock does not occupy the resource and the read lock does not occupy the resource, p_read _ calls Post, the number of read locks is + 1, and p_mutex _-> Wait () is called (),

When the write lock does not occupy the resource and the read lock has occupied the resource, p_read _ calls Post. The number of read locks is + 1, and p_mutex _-> Wait () is not required. Otherwise, the lock will be blocked.

Bool RLock ();

When the write lock occupies the resource, call p_mutex _-> Wait () to block it.

When the write lock does not occupy the resource and the read lock does not occupy the resource, p_read _ calls Post, the number of read locks is + 1, and p_mutex _-> Wait () is called (), indicates that the lock resource has been occupied.

When the write lock does not occupy the resource and the read lock has occupied the resource, p_read _ calls Post. The number of read locks is + 1, and p_mutex _-> Wait () is not required. Otherwise, the lock will be blocked.

Bool UnLock ();

When the read lock is applied, p_read _ is called _. post (): The number of read locks-1. If the number of read locks is reduced to 0, p_mutex _-> Post () is called to unlock resource usage.

When a write lock is added, p_mutex _. Post () is called (),

Int LockStatus ();

0-No lock; 1-write lock; 2-read lock

1.4 download the semaphore read/write implementation source code

Rw_mutex is the implementation of the semaphore mutex lock and rw_sem is the implementation of the semaphore read/write lock

Example of semaphores mutex lock

Example of a semaphore read/write lock

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.