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.