Transplantation of mutex locks and synchronization in Linux (1)

Source: Internet
Author: User

Porting win32 program multi-thread content to Linux cannot be simply transplanted according to function correspondence. However, through the following ing, coupled with your in-depth understanding of these models, I believe it will be very successful.

Semaphores

Windows semaphores are counter variables that allow limited threads/processes to access shared resources. Linux POSIX semaphores are also counter variables that can be used to implement semaphores on Windows in Linux.

Semaphore type: Windows provides the famous named) semaphores and the unknown unnamed semaphores. A famous semaphore can be synchronized between processes. In Linux, only POSIX semaphores are used between different threads of the same process. System V semaphores can be used between processes.

Wait function timeout: when used in a wait function, you can specify a timeout value for the Windows semaphore object. In Linux, this function is not provided, and timeout issues can only be handled through application logic.

Create semaphores

In Windows, you can use CreateSemaphore () to create or open a famous or unknown semaphore.

HANDLE CreateSemaphore (

LPSECURITY_ATTRIBUTES l1_maphoreattributes,

LONG lInitialCount,

LONG lMaximumCount,

Lptstr lpName

);

In this Code:

Lw.maphoreattributes is a pointer to a security attribute. If the pointer is null, the semaphore cannot be inherited.

LInitialCount is the initial value of the semaphore.

LMaximumCount is the maximum value of the semaphore. The value must be greater than 0.

LpName is the name of the semaphore. If the value is NULL, the semaphore can only be shared among different threads of the same process. Otherwise, they can be shared between different processes.

This function creates a semaphore and returns the handle of the semaphore. It also sets the initial value to the value specified in the call. This allows a limited number of threads to access a shared resource.

In Linux, you can use sem_init () to create an unknown POSIX semaphore. This call can be used between threads of the same process.

It also initializes the semaphore counter: int sem_init (sem_t * sem, int pshared, unsigned int value ). In this Code:

Value semaphore counter) is the initial value of this semaphore.

Pshared can be ignored, because in the current implementation, POSIX semaphores cannot be shared between processes.

The maximum value is based on the SEM_VALUE_MAX defined in the demaphore. h header file.

In Linux, semget () is used to create a System V semaphore, which can be used between different integrated threads. It can be used to implement the same functions as the famous semaphores in Windows. This function returns a semaphore set identifier, which is associated with the key value of a parameter. When creating a new semaphore set, semget () initializes the semaphores associated with the semid_ds data structure as follows:

Sem_perm.cuid and sem_perm.uid are set as valid user IDs of the calling process.

Sem_perm.cgid and sem_perm.gid are set to the valid group ID of the calling process.

The lower 9 bits of sem_perm.mode are set to the Lower 9 bits of semflg.

Sem_nsems is set to the value of nsems.

Sem_otime is set to 0.

Sem_ctime is set to the current time.

The code used to create a System V semaphore is: int semget (key_t key, int nsems, int semflg ). The following are some explanations of this Code:

Key is a unique identifier. Different processes use it to identify this semaphore set. We can use ftok () to generate a unique key value.

IPC_PRIVATE is a special key_t value. When IPC_PRIVATE is used as the key, this system call will only use the Lower 9 bits of semflg, but ignore other content, to create a new semaphore set ).

Nsems is the number of semaphores in this semaphores set.

Semflg is the permission of this new semaphore set. To create a semaphore set, you can use IPC_CREAT to set bitwise operations or access permissions. If a semaphore set with this key value already exists, the IPC_CREAT/IPC_EXCL mark will fail.

Note: In System V semaphores, keys are used to uniquely identify semaphores. In Windows, semaphores are identified by a name.

To initialize the data structure of the semaphore set, you can use the IPC_SET command to call the semctl () System Call. Write the values of some members of the semid_ds Data Structure pointed to by arg. buf to the data structure of the semaphore set, and update the value of sem_ctime member of this structure. The user-provided arg. buf points to the semid_ds structure as follows:

Sem_perm.uid

Sem_perm.gid

Sem_perm.mode is only valid for a minimum of 9 characters)


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.