当前位置:网站首页>15.10 the POSIX semaphore Unix environment programming chapter 15

15.10 the POSIX semaphore Unix environment programming chapter 15

2022-08-09 10:28:00 Mary Soda Meatloaf

POSIX semaphores come in two forms: named and unnamed.They differ in the form of creation and destruction, but otherwise work the same.

Unnamed semaphores exist only in memory and require that processes that can use the semaphore must have access to memory.This means they can only be applied to threads in the same process, or threads in different processes that have mapped the same memory contents into their address spaces.

Named semaphores can be accessed by name and thus can be used by threads in any process whose name is known.

We can call the sem_open function to create a new named semaphore or use an existing semaphore.

#include sem_t *sem_open(const char *name,int oflag,...)//Successfully returns the pointer of the semaphore, if error returns SEM_FAILED

When using an existing named semaphore, we only specify two parameters: the name of the semaphore and the 0 value of the oflag parameter.

When we specify the O_CREAT flag, we need to provide two additional parameters.The mode parameter specifies who can access the semaphore.

When creating a semaphore, the value parameter is used to specify the initial value of the semaphore.Its value is 0-SEM_VALUE_MAX.

If we want to ensure that we are creating a semaphore, we can set the oflag parameter to O_CREAT|O_EXCL.If the semaphore already exists, it will cause sem_open to fail.

In order to increase portability, certain rules must be followed when choosing semaphore names:

  • The first character of the name should be a slash.
  • Names should not contain card slashes to avoid implementation-defined behavior
  • The maximum length of a semaphore name is implementation-defined.

If you want to operate on the semaphore, the sem_open function will return a semaphore pointer for us to pass to other semaphore functions, but when the semaphore operation is completed, you can call the sem_close function to release any semaphorerelated resources.

#include int sem_close(sem_t *sem);//successful return 0, error return -1

If the process exits without first calling sem_close, the kernel will automatically close any open semaphores.

The sem_unlink function deletes the semaphore name. If there is no open semaphore reference, the semaphore will be destroyed.Otherwise, destruction will be delayed until the last open reference is closed.

#include int sem_unlink(const char *name);//successful return 0, error return -1

You can use the sem_wait or sem_trywait functions to reduce the semaphore by 1

#include int sem_trywait(sem_t *sem);//When the semaphore is 0, it will not blockint sem_wait(sem_t *sem);//If the semaphore is 0, it will block, and it will return when the semaphore is successfully decremented by 1 or interrupted by the signal//Two functions, return 0 for success, -1 for error

Call the sem_timewait function to block for a certain period of time

#include #include int sem_timedwait(sem_t *restrict sem,const struct timespec *rstrict tsptr);//successful return 0, error return -1

When you want to give up waiting for a semaphore, you can specify an absolute time with the tsptr parameter.

Call the sem_post function to increase the semaphore value by 1.

#include semaphore.h>int sem_post(sem_t *sem);//Success returns 0, failure returns -1

When calling sem_post, if the process is blocked during the call to sem_wait, the process will be woken up and the semaphore count that will be incremented by 1 by sem_post will be decremented by 1 again by sem_wait.

Use the sem_init function to create an unnamed semaphore

#include int sem_init(sem_t *sem,int pshared,unsigned int value);//Success returns 0, failure returns -1

The pshared parameter indicates whether to use the semaphore in multiple processes.If so, set it to a non-zero value.The value parameter specifies the initial value of the semaphore.

When the use of the unnamed semaphore has been completed, the sem_destroy function can be called to discard it

#include int sem_destroy(sem_t *sem);//Success returns 0, failure returns -1

After calling sem_destroy, you cannot use any semaphore function with sem unless it is reinitialized by calling sem_init.

The sem_getvalue function can be used to retrieve the semaphore value

#include int sem_getvalue(sem_t restrict sem,int *restrict valp);//Success returns 0, failure returns -1

原网站

版权声明
本文为[Mary Soda Meatloaf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091026200417.html