Linux application Development-semaphore mutex programming

Source: Internet
Author: User
Tags function prototype mutex semaphore

Linux Application Development-semaphore mutex programming A corresponding function


1 Create/Open Semaphore collection


Function name Semget


function prototype int semget (key_t key, int nsems, int semflg)


function function
Gets the identifier of the semaphore collection
When the semaphore indicated by key does not exist, and the SEMFLG contains Ipc_creat, a set of semaphores is created


Owning header file
<sys/types.h> <sys/ipc.h> <sys/sem.h>


return value
The identifier of the successful return semaphore
Failed return-1


Parameter description
Key value, that is, the kernel number is combined with the project ID
The number of Nsems to generate the semaphore
SEMFLG logo, can go to ipc_creat







2 Operating Signal Volume


Function name Semop


function prototype int semop (int semid, struct sembuf *sops, unsigned nsops)


function function
The amount of semaphore in the set of Operation Semaphores


Owning header file
<sys/types.h><sys/ipc.h><sys/sem.h>


return value
Successful return 0
Failed return-1


Parameter description
Semid: identifier of the semaphore to be manipulated
SOPs: How many semaphores to operate
Nsops: What to do with the semaphore


3 Generating Key values
The key value is the number of values the file generates in the kernel combined with the project ID.
Key = key_t Ftok (char *fname, int id)


4 Control Signal Volume
Semctl (int semid, int semnum, int cmd ...)


Semid Signal Volume identifier


Semnum Semaphore number


CMD corresponding instructions into the getval,setval and so on


for a-process, B-process, both using semaphores to communicate

Linux application Development-semaphore mutex programming

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.