Linux IPC Synchronization (quad): Semaphore

Source: Internet
Author: User
Tags mutex posix semaphore

Posix Semaphore

Well-known signal volume

/*Sem_open-initialize and open a named semaphore*/#include<fcntl.h>/*For O_* Constants*/#include<sys/stat.h>/*For Mode Constants*/#include<semaphore.h>sem_t*sem_open (Const Char*name,intoflag); sem_t*sem_open (Const Char*name,intOflag, mode_t mode, unsignedintvalue);/*sem_wait, Sem_timedwait, sem_trywait-lock a semaphore*/#include<semaphore.h>intSem_wait (sem_t *sem);intSem_trywait (sem_t *sem);intSem_timedwait (sem_t *sem,Const structTimespec *abs_timeout);/*Sem_post-unlock a semaphore*/#include<semaphore.h>intSem_post (sem_t *sem);/*Sem_close-close a named semaphore*/#include<semaphore.h>intSem_close (sem_t *sem);/*Sem_unlink-remove a named semaphore*/#include<semaphore.h>intSem_unlink (Const Char*name); Link with-pthread.

Amount of memory-based semaphores

/**/<semaphore.h>int int int value); /*  */<semaphore.h>int Sem_destroy (sem_t *sem);

Sem_init () initializes the unnamed semaphore at the address pointed-by SEM. The value argument specifies the initial value for the semaphore.

The pshared argument Indicates whether this semaphore are to be shared between the threads of a process, or betwee N Processes.

If pshared have the value 0, then the semaphore are shared between the threads of a process, and should be located at some a Ddress is visible

to any threads (e.g, a global variable, or a variable allocated dynamically on the heap).

If a memory-based semaphore is shared between different processes (Sem_init's pshared parameter is 1), the semaphore must be stored in shared memory.

Both POSIX shared memory and System V shared memory are persisted with the kernel, so as long as the shared memory exists, the amount of semaphores on the shared memory also exists.

The difference between semaphores, mutexes, and condition variables:

(1) The semaphore also has an attribute that is not provided by a mutex: the mutex must always be threads unlocked by the line that locks it, and the post action of the semaphore does not have to be performed by the same thread executing its wait operation.

(2) The mutex is either locked or unlocked (similar to a binary semaphore)

(3) The semaphore has a state associated with it, and the post action of the semaphore is always remembered. When sending a signal to a conditional variable, if no thread waits on the condition variable, the signal is lost.

Limitations of POSIX semaphores

Sem_nsems_max the maximum number of semaphores a process can open simultaneously

Sem_value_max the maximum value of a semaphore

System v Signal Volume

System V creation and operation are semaphore sets

/*Semget-get a System V semaphore set Identifier*/#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intSemget (key_t Key,intNsems,intSEMFLG);/*Semop, Semtimedop-system V semaphore operations*/#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intSemopintSemid,structSEMBUF *SOPs, unsigned nsops);intSemtimedop (intSemid,structSEMBUF *SOPs, unsigned nsops,structTimespec *timeout);/*Semctl-system V semaphore control Operations*/#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intSemctl (intSemid,intSemnum,intcmd, ...); Union Semun {intVal/*Value for Setval*/     structSemid_ds *buf;/*Buffer for Ipc_stat, Ipc_set*/unsigned Short*array;/*Array for GETALL, SETALL*/     structSeminfo *__buf;/*Buffer for Ipc_info (linux-specific)*/};/*The SEMID_DS data structure is defined in <sys/sem.h> as follows:*/structSemid_ds {structIpc_perm Sem_perm;/*Ownership and Permissions*/time_t sem_otime;/*Last Semop time*/time_t sem_ctime;/* Last Change time*/unsignedLongSem_nsems;/*No. of semaphores in Set*/};/*The ipc_perm structure is defined as follows (the highlighted fields are settable using Ipc_set):*/structipc_perm {key_t __key;/*Key supplied to Semget (2)*/uid_t uid; /*effective UID of owner*/gid_t GID; /*effective GID of owner*/uid_t cuid; /*effective UID of creator*/gid_t Cgid; /*effective GID of creator*/unsigned ShortMode/*Permissions*/unsigned Short__seq;/*Sequence Number*/};

Note: 1. Semget If you only access a semaphore collection, the Nsems parameter is specified as 0. Once a semaphore set has been created, the number of semaphores in it cannot be changed.

2. The flag parameter of the Semget can be Sem_r (R for Read), Sem_a (A for Alter), bitwise OR on ipc_creat, IPC_EXCL

3. struct SEMBUF in Semop

Each semaphore in a System V semaphore set has the following associated values:

               unsigned short semval;    /*  semaphore value */
                unsigned short semzcnt; /* # Waiting for Zero */
                unsigned short semncnt; /* # Waiting for increase */
    & nbsp                pid_t sempid;   /* ID of process that does last OP */

            &NBSP;SEMOP () performs Operations on selected semaphores in the set indicated by Semid. Each of the NSOPS elements in the array pointed to by SOPs
             specifies an operation to being performed on a semaphore. The elements of this structure is of type struct SEMBUF, containing the following
             members:

unsigned short sem_num; /* Semaphore number */
Short Sem_op; /* Semaphore operation */
Short SEM_FLG; /* Operation flags */

Limitations of System V semaphores

Linux IPC Synchronization (quad): Semaphore

Related Article

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.