Linux process communication (SystemV) Section 4 ------> semaphores

Source: Internet
Author: User
1. it is not an IPC mechanism. It provides synchronization between different processes or between different threads of the same process! 2. type: & lt; 1 & gt ;. binary signal: If the value is 0 or, there is a resource available. if the value is 0, it indicates that it is used again. Other processes wait for the same result... information & 1. it is not an IPC mechanism. It provides synchronization between different processes or between different threads of the same process! 2. type: <1>. binary signal: If the value is 0 or, there is a resource available. if the value is 0, it indicates that it is used again. other processes are waiting for a problem equivalent to P and V... <2>. count semaphores: between 0 and a value greater than 0, generally the number of available resources <3>. count semaphores: a collection composed of one or more semaphores, each of which is a count semaphores. Generally more than 25. >>>: The P/V operation we know is the same as the P/V operation. if P arrives, it is the VALUE --, and then it is returned after OK is used, that is, VALUE ++. if you cannot apply for resources, you have to block yourself! 3. semaphore data structure: # include Structsemid_ds //!> Semaphore iddescription {structipc_perm sem_perm; //!> Operation permission structure struct sem * sem_base; //!> Set the Semaphore pointer ushort sem_nsems; //!> Set the number of semaphores time_t sem_otime; //!> Time_t sem_ctime of the last (last) operation; //!> Last modification time}; # include Structipc_perm //!> Ipc permission (Structure of IPC permission settings) {uid_t uid; //!> Owner User ID gid_t gid; //!> Owner Group ID uid_t cuid; //!> Creator ID gid_t cgid; //!> Creator group ID mode_t mode; //!> Read and write permissions ulong_t seq; //!> Key_t key; //!> Ipc key}; mode nine-bit low: 0400 readable. 0200 users can write. 0040 members are readable. 0020 members are writable. 0004 other users are readable. 0002 other users can write. struct sem //!> Semapore {ushort_t semval; //!> Signal value, non-negative short sempid; //!> The last successful process PID ushort_t semncnt; //!> Number of processes waiting for semval to increase (long) ushort_t semzcnt; //!> Wait for the number of processes in which semval = 0 (0)} 4. create and operate the semaphore: <1>. the semget function is used to create a semaphore set or access an existing one! # Include # Include # Include Int semget (key_t key, int nsems, int oflag); parameter: nsems: Number of Semaphores in The semaphores set (ATTENTION: if is only a set of semaphores that access an EXIST, then this value is 0, because after a set of semaphores is created, the number of semaphores cannot be changed!) (But if it is created, it must be a number greater than 0!) Oflag: The value is a combination of SEM_R and SEM_A (read and write), or a bitwise OR return value of IPC_CREAT and IPC_EXCL: After the semaphore sign (semid) is created, for semid_ds initialization, the uid and cuid of sem_perm are the IDs of valid users in the process, and gid and cgid are the group IDs oflag of valid users in the process: sem_perm.mode parameter sem_otime = 0; sem_ctime = current time sem_nsems = nsems; <2>. after creating the OK button, you can use semop to operate the semaphore. Int semop (int semid, struct sembuf * opsptr, size_t nops); parameter: struct sembuf {short sem_num; //!> Signal No. short sem_op; //!> Signal operation short sem_flg; //!> Operation flag}; nops: number of elements in the array pointed by the pointer opsptr (array response content: specify an operation for a specific semaphore! Use sem_num to know the specific semaphore) <3>. perform various operations on a semaphore using the semctl function: int semctl (int semid, int semnum, int cmd ,...); parameter: semid: Set of semaphores to be operated semnum: a semaphores in the signal set (0 ---> value of MAX-1), which can be GATVAL, SETVAL, GETNCNT, GETZCNT, the last GETPID parameter is optional, depending on the third parameter: cmd union semun {int val; //!> Use structsemid_ds buf for SETVAL; //!> Ushort * array for IPC_STAT (used to retrieve values); //!> Use for GETALL and SETALL}; cmd value: IPC_STAT: specify the Union as semid_ds and put it in the buf IPC_SET: set three fields in the set according to the data in the buf, that is, the uid in the sem_perm struct, gid, mode IPC_RMID: delete the semaphore set from the system GETVAL: returns the semval value of semnum SETVAL: sets the semval value of semnum, which is specified by the val in semun GETPID: return sempid value of semnum GETNCNT: return semnum semncnt value GETZCNT: return semnum semzcnt value GETALL: take all the semaphores on the set and put them in the array SEIALL: set the value of each semaphore in the set according to the value in the array: the usage of the semaphore is connected with the "Shared Memory Zone, therefore, there is a specific design in the shared memory area for process communication. this is taken from the column in shanshanpt.

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.