How to implement the POSIX semaphore API using the system V semaphores API

Source: Internet
Author: User
Tags posix semaphore terminates

Overview

One of the major challenges you will face when porting code to the z/OS platform is the availability of the POSIX semaphore API on z/OS. Typically, many of the programs running on the Linux/unix/windows platform are developed with POSIX semaphore.

One solution to this problem is to use the System V API instead of the POSIX API to implement z/OS. This involves a lot of code changes throughout the code, as well as a lot of writing and testing work. Another better solution is to use the System V API available to implement the POSIX API in Z/OS. If you take this approach, the code changes required for porting are minimal. These code changes are not included in the application code, but are included as separate semaphore headers and C files. This header and C files are generic and can be used on any platform.

This article illustrates how to use the System V Semaphore API to implement the POSIX Semaphore API.

This article is useful for developers who want to use the POSIX API but the development platform only supports the System V API, such as z/OS. This article also makes a significant distinction between POSIX and System V semaphore from a development perspective.

Compare POSIX Semaphore and System V semaphore

There are two types of semaphore: POSIX semaphore or System V semaphore.

You can operate on the semaphores either as a separate unit or as an element in a collection. Because System V IPC semaphores can be located in a large array, they are extremely heavyweight. The semaphore collection consists of a control structure and a semaphore array. A set of semaphore can include up to 25 elements. System V IPC semaphore functions include Semget (), Semop (), and Semctl ().

Semget ()-Creates a new semaphore set or accesses an existing collection using the Semget () system call.

SEMOP ()-Performs the semaphore operation.

Semctl ()-If you are the creator of the semaphore, you may be able to change its ownership or permissions.

POSIX semaphore are much lighter than System V semaphore. The POSIX semaphore structure defines a single semaphore instead of a semaphore array. The POSIX semaphore functions include:

Sem_open ()-connected to (can also be created by case) a named semaphore

Sem_init ()-Initializes a semaphore structure (belonging to the internal structure of the caller, so it is not a named semaphore)

Sem_close ()-Terminates the connection of an open semaphore

Sem_unlink ()-Terminates an open semaphore connection and deletes the connection when the last process closes it semaphore

Sem_destroy ()-Initializes a semaphore structure (belonging to the internal structure of the caller, so it is not a named semaphore)

Sem_getvalue ()-Copies the value of the semaphore to the specified integer

Sem_wait () and sem_trywait ()-when Semaphore is held by other processes and sometimes blocks, or if semaphore is held by another process, an error is returned

Sem_post ()-Increase number of semaphore

POSIX has its own simple semantics for creating, initializing, and performing operations on semaphore. These semantics provide an efficient way to handle interprocess communication. System V semaphore is useful if you need to perform multiple increments-decrement in a single step to achieve atomic operations. In addition, please use POSIX semaphore as much as possible.

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.