Linux Application Development (11): semaphore

Source: Internet
Author: User

I. chicken ribs

1. semaphores, which are different from other processes in communication modes, are mainly used for (1) process mutex (protecting street Resources) (2) process synchronization (Process Cooperation)

2. 2-value semaphore: the semaphore value can only be 0 and 1, similar to mutex lock. It is similar here because the difference between the signal and the mutex lock is:

(1) The signal light emphasizes sharing resources. As long as the shared resources are available, other processes can also modify the semaphore value.

(2) The mutex lock emphasizes the process. After resources are used by the process, the process must be unlocked.

3. Counting semaphores: any non-negative values can be used for semaphores.

Ii. Use of semaphores (the functions here are based on semaphores, that is, all semaphores in a process are often considered as a set ):

1. Create/Open semaphores: int semget (key_t key, int nsems, int semflg );

(1) parameters:

= Key: key value, obtained by ftok

= Nsems: specifies the number of semaphores that are enabled or newly created.

= Semflg: ID, same as Message Queue

2. semaphore: int semop (INT Semid, struct sembuf * SOPs, unsigned nsops)

(1) parameters:

= Semid: the ID of the semaphore set

= SOPs: determines the operation to be performed on the semaphore. It is an array of the sembuf struct type. (Here we use a struct type to indicate an operation)

Struct sembuf {

Unsigned short sem_num; // specifies the number of semaphores to be used that belong to the semaphores set.

Short sem_op; // specifies the name of the operation to be performed on the semaphore.

Short sem_flg; // operation flag

}

Values available for the semflg operation flag:

= Ipc_nowait: when the signal operation is not met, semop () will not be blocked, return immediately, and set the error message

= Ipc_undo: when the program ends (whether normal or abnormal), the semaphore is released. This aims to avoid the end Of the locked resource when the program ends in an exception, this causes the resource to be locked forever.

= Nsops, number of elements in the Operation Array SOPs, that is, it is used to specify how many operations are required

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.