UNIX Environment Advanced Programming: System V Semaphore

Source: Internet
Author: User
Tags count current time mutex semaphore valid

1. Semaphore (semaphore) is mainly used to protect critical resources.

A process can determine whether some shared resources can be accessed based on it.

In addition to being used for access control, semaphores can also be used for process synchronization, i.e. interprocess communication.

2. Signal Quantity Classification:

A. Two value semaphore: The semaphore value can only be 0 or 1, similar to a mutex mutex, but the two are different:

The difference between a mutex and a two-value semaphore:

The semaphore emphasizes sharing resources, as long as the shared resources are available, other processes can also modify the semaphore value;

Mutexes emphasize processes, and when resources are consumed by the process, the process itself must be locked up.

B. Count semaphore: The value of the semaphore can be arbitrarily non-negative.

The System V Semaphore adds another level of complexity to the semaphore by defining the following concepts.

Count Semaphore set: one or more semaphores (constituting a set), each of which is a count semaphore. There is a limit to the number of semaphores per set, generally in 25 order of magnitude.

3.semget function (semaphore creation)

The Semget function creates a semaphore set or accesses an existing semaphore set.

#include <sys/sem.h>  
int senget (key_t key,int nsems,int oflag);

The Nsems parameter specifies the number of semaphores in the collection. If we do not create a new semaphore set, but simply access an existing set, we can specify that parameter as 0. Once we have created a semaphore set, we cannot change the semaphore number.

Oflag values are Sem_r and sem_a are often worth combining. They can also be associated with IPC _creat or Ipc_creat | IPC_EXCL by bitwise OR.

When the actual operation is to create a new semaphore set, the following members of the corresponding SEMID_DS structure are initialized.

(1) The UID and cuid members of the sem_perm structure are placed as valid user Id,gid and Cgid members of the calling process as valid group IDs of the calling process.

(2) The Read and write permission bits in the Oflag parameter are stored in the Sem_perm.mode.

(3) The sem_otime is placed as the 0,sem_ctime and is placed as the current time.

(4) Sem_nsems is placed as the value of the Nsems parameter.

(5) The various SEM structures associated with each semaphore in the set are not initialized. These structures are initialized when the Semctl is invoked with the Set_val or SetAll commands.

4.semop function (Operation Semaphore)

When a semaphore set is opened using Semget, the operation of one or more semaphores is performed using the SEMOP function.

#include <sys/sem.h>  
int semop (int semid,struct sembuf *opsptr,size_t nops);

Where opsptr points to an array of the following structures:

struct sembuf{  
           unsigned short sem_num;  /* Semaphore number
           *          /short sem_op;   /* Semaphore Operation * *          SEM_FLG;  * Operation flags *
/};

Assuming that there is a semaphore variable SV,

P (SV): For waiting, if the SV is greater than 0, subtract 1, and suspend execution of the process if its value equals 0

V (SV): Used to send a signal, and if there are other processes pending for the SV, let it resume and, if no process is suspended for the SV, add 1

Semaphore Sv=1;

Loop forever{

P (SV);

Critical code section;

V (SV);

Noncritical Code section;

}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.