Talk C chestnuts together (101st back: C language instance -- use semaphores for inter-process synchronization and mutex 2)

Source: Internet
Author: User

Talk C chestnuts together (101st back: C language instance -- use semaphores for inter-process synchronization and mutex 2)

Hello, everyone. In the previous session, we were talking about the use of semaphores for inter-process synchronization and mutex. In this session, we will continue to talk about this example. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

In the previous article, we introduced how to use semaphores for synchronization and mutex between processes. However, we only introduced the theory, in this case, we will introduce how to use semaphores for inter-process synchronization and mutex. It's not our style, haha.

The Linux system we use provides a semaphore mechanism and corresponding APIs to operate semaphores. Semaphores are similar to the shared memory and Message Queue described in the previous chapter,It is also part of System v ipc. The method for operating the semaphore is similar to that for operating the shared memory and message queue.Next, we will introduce functions related to semaphores.

Semget Function
int semget(key_t key,int num_sems, int sem_flag)

This function is used to create a new semaphore or obtain an existing semaphore.

The first parameter is the key value. It is used to operate the structure of IPC in the kernel, that is, the structure of the semaphore in the kernel. (as described in the previous chapter) The second parameter is the number of semaphores, the value is usually 1. The third parameter indicates the permission to operate the semaphore. The permission is the same as the File Permission. If the function runs successfully, the semaphore identifier is returned; otherwise,-1 is returned; we can use this identifier to use semaphores; semop Function
int semop(int sem_id, struct sembuf *sem_ops,size_t sem_ops_num)

This function is used to modify the semaphore value, that is, the P/V Operation on the semaphore;

The first parameter is the semaphore identifier, which can be obtained through the semget function; the second parameter is a pointer that points to a variable of the struct type, the variable contains the operation content and operation method of the signal. The third parameter indicates the number of semaphore operations, that is, the number of the second parameter, usually 1; if the function runs successfully, 0 is returned; otherwise,-1 is returned;

The second parameter type of the function is a struct. The struct is defined as follows:

struct sembuf{    unsigned short sem_num;  /* semaphore number */    short          sem_op;   /* semaphore operation */    short          sem_flg;  /* operation flags */}

We will introduce in detail the members of this struct:

The first member indicates the semaphore number, usually 0, indicating a single semaphore. If not 0, it indicates a set of semaphores. The second Member indicates the number of semaphores to be operated, it is usually 1 or-1, that is, adding 1 to or subtracting 1 (corresponding to the V/P operation) to the semaphore. The third member indicates the operation method of the semaphore, which is usually SEM_UNDO, allows the system to track and process semaphores;

This function represents an atomic operation.When using it, you don't have to worry about being interrupted by other operations.

Semctl Function
int semctl(int sem_id, int sem_num, int cmd, ...)

This function is used to control semaphores. It is usually used to initialize or delete semaphores;

The first parameter is the semaphore identifier, which can be obtained through the semget function. The second parameter is the semaphore number, usually 0, indicating a single semaphore. If it is not 0, it indicates a set of semaphores; the third parameter is a command, which has many, commonly used SETVAL and IPC_RMID, indicating the initialization and deletion of semaphores. This function is a variable parameter function, other types of parameters may exist. If the function runs successfully, 0 is returned; otherwise,-1 is returned;

When using this function, we usually need the fourth parameter, which is a consortium type. The detailed definition is as follows:

union semun {    int              val;    /* Value for SETVAL */    struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */    unsigned short  *array;  /* Array for GETALL, SETALL */    struct seminfo  *__buf;  /* Buffer for IPC_INFO                                           (Linux-specific) */};

If this type is not defined in the system, we need to define this type.

If the value of the third parameter is SETVAL, the semaphore is initialized. We need the fourth parameter and use the val value in semun to initialize the semaphore. If the third parameter is assigned IPC_RMID, it indicates that the semaphore is deleted, so the fourth parameter is not required.

For more information, see the example of using semaphores to synchronize and mutually exclusive processes. I want to know what examples will be provided later, and I will try again.

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.