## **Overview**
Provides functions and structures related to inter-process communication \(IPC\), including signals, semaphores, and message queues.
**Since:**
1.0
**Version:**
1.0
## **Summary**
## Files
## Data Structures
## Macros
Defines the input parameter used to obtain information from the kernel. | |
SEM_FAILED ((sem_t *)0) | Defines the semaphore failure flag. |
Stops a child process without sending the SIGCHLD signal. | |
SA_NOCLDSTOP 0x00000001 | Stops a child process without sending the SIGCHLD signal. |
Sets the SIGCHLD flag to prevent zombie processes. | |
Sets the SIGCHLD flag to prevent zombie processes. | |
Transfers the siginfo_t structure with the signal. | |
SA_SIGINFO 0x00000004 | Transfers the siginfo_t structure with the signal. |
SA_ONSTACK 0x08000000 | Uses the signal stack specified by sigaltstack for signal processing. |
SA_ONSTACK 0x08000000 | Uses the signal stack specified by sigaltstack for signal processing. |
SA_NODEFER 0x40000000 | The signals being processed are not masked during signal processing. |
SA_NODEFER 0x40000000 | The signals being processed are not masked during signal processing. |
The default processing mode is restored after the signal processing is complete. | |
The default processing mode is restored after the signal processing is complete. | |
Not intended for application use. This flag is used by C libraries to indicate that the sa_restorer field contains the address of a trampoline. | |
Not intended for application use. This flag is used by C libraries to indicate that the sa_restorer field contains the address of a trampoline. | |
MINSIGSTKSZ 2048 | Defines the minimum size of the replaceable signal stack. |
MINSIGSTKSZ 2048 | Defines the minimum size of the replaceable signal stack. |
SIGSTKSZ 8192 | Defines the size of a signal stack. |
SIGSTKSZ 8192 | Defines the size of a signal stack. |
_NSIG_BPW __BITS_PER_LONG | Number of bits occupied by a word. |
_NSIG_WORDS (_NSIG / _NSIG_BPW) | Number of words required by all signals. |
Interrupts a process from keyboard. | |
Exits a process from keyboard. A core file is generated. | |
Breakpoint, used for debugging. | |
Equivalent to SIGABRT. | |
Child process terminated or stopped. | |
Stops a process by entering the command on the terminal. | |
Input required by the background process. | |
Output required by the background process. | |
Virtual timer, used to calculate the CPU occupation time of a process. | |
Calculates the CPU time occupied by a process and the system calling time. | |
SIGPOLL SIGIO | Equivalent to SIGIO. |
Equivalent to SIGSYS. | |
SIGRTMIN 32 | |
SIGRTMIN (__libc_current_sigrtmin()) | |
SIGRTMAX (__libc_current_sigrtmax()) | |
Transfers signals in a 32-bit mode even if the task runs in a 26-bit mode. | |
SA_NOMASK SA_NODEFER | Signals being processed are not masked during signal processing. |
SA_ONESHOT SA_RESETHAND | The default processing mode is restored after the signal processing is complete. |
Defines the function behaviors such as sigprocmask() and pthread_sigmask(), which are used to add signals to the mask set. | |
Defines the function behaviors such as sigprocmask() and pthread_sigmask(), which are used to remove signals from the mask set. | |
Defines the function behaviors such as sigprocmask() and pthread_sigmask(), which are used to set the mask set. | |
sa_handler _u._sa_handler | Defines the simplified writing used to indicate the __sa_handler.sa_handler field in sigaction. |
sa_handler __sa_handler.sa_handler | Defines the simplified writing used to indicate the __sa_handler.sa_handler field in sigaction. |
sa_sigaction _u._sa_sigaction | Defines the simplified writing used to indicate the __sa_handler.sa_sigaction field in sigaction. |
sa_sigaction __sa_handler.sa_sigaction | Defines the simplified writing used to indicate the __sa_handler.sa_sigaction field in sigaction. |
Defines the function behaviors such as sigset() and adds a specified signal to the process signal mask without changing the current processing mode of the signal. | |
Sets the notification method in sigevent: signal notification. | |
Sets the notification method in sigevent: no notification. | |
Sets the notification method in sigevent: thread notification. | |
Defines the value returned when a function such as signal() fails. | |
Defines the default signal processing mode. | |
Defines the signal processing mode in which the signal is ignored. | |
Defines the input parameter used to create an IPC object. | |
Defines the input parameter used together with IPC_CREAT to prevent duplicate key values during IPC creation. | |
Defines the input parameter that specifies whether the communication is in non-blocking mode. | |
Defines the input parameter used to delete an IPC object. | |
Defines the input parameter used to set information to the kernel. | |
Defines the input parameter used to obtain the system-level restriction information of a specified communication type, for example, shminfo. | |
Defines the IPC object as a private object. |
## Typedefs
typedef struct __ucontext Describes the user execution context. | |
typedef struct sigaltstack | |
Function pointer of signal handler. |
## Functions
mq_close (mqd_t mqdes) | Closes a message queue that is no longer used. |
mq_getattr (mqd_t mqdes, struct mq_attr *attr) | Obtains the attributes of the message queue specified by the descriptor. The values of mq_maxmsg, mq_msgsize, and mq_curmsgs are fixed. |
mq_open (const char *name, int oflag,...) | Creates a message queue or opens an existing message queue. |
mq_receive (mqd_t mqdes, char *buffer, size_t size, unsigned *prioptr) | Receives a message from a specified message queue. |
mq_send (mqd_t mqdes, const char *buffer, size_t size, unsigned prio) | Sends a message to a specified message queue. |
mq_setattr (mqd_t mqdes, const struct mq_attr *__restrict newattr, struct mq_attr *__restrict oldattr) | Sets the attributes of the message queue specified by the descriptor. The mq_maxmsg, mq_msgsize, and mq_curmsgs attributes cannot be modified. mq_flags supports the O_NONBLOCK attribute only. |
mq_timedreceive (mqd_t mqdes, char *__restrict buffer, size_t size, unsigned *__restrict prioptr, const struct timespec *__restrict timeout) | Receives a message from the message queue, with a timeout period specified. |
mq_timedsend (mqd_t mqdes, const char *buffer, size_t size, unsigned prio, const struct timespec *timeout) | Sends a message to a specified message queue, with a timeout period specified. |
mq_unlink (const char *name) | Decreases the reference count of the message queue by 1, or deletes the message queue if the reference count is 0. |
sem_destroy (sem_t *sem) | Destroys a specified anonymous semaphore that is no longer used. |
sem_getvalue (sem_t *__restrict sem, int *__restrict sval) | Obtains the count value of a specified semaphore. |
sem_init (sem_t *sem, int pshared, unsigned int value) | Creates and initializes an anonymous semaphore. |
Increments the semaphore count by 1. | |
sem_timedwait (sem_t *__restrict sem, const struct timespec *__restrict timeout) | Obtains the semaphore, with a timeout period specified. |
sem_trywait (sem_t *sem) | Attempts to obtain the semaphore. |
sem_unlink (const char *name) | Deletes a specified semaphore. |
kill (pid_t pid, int sig) | Sends a signal to a specified process. |
sigemptyset (sigset_t *set) | Clears all signals in a specified signal set. |
sigfillset (sigset_t *set) | Adds all signals to a specified signal set. |
sigaddset (sigset_t *set, int signum) | Adds a signal to a specified signal set. |
sigdelset (sigset_t *set, int signum) | Delete a signal from a specified signal set. |
sigismember (const sigset_t *set, int signum) | Checks whether a signal is in the signal set. |
sigprocmask (int how, const sigset_t *__restrict set, sigset_t *__restrict oldset) | Changes the signal mask of the calling thread. |
sigsuspend (const sigset_t *mask) | Sets mask as the signal mask set of the current process and suspends the process until a signal processing action is triggered. |
sigaction (int signum, const struct sigaction *__restrict act, struct sigaction *__restrict oldact) | Checks or changes the processing action associated with a specified signal. Do not use the process creation function or thread creation function in the registered callback processing function. Otherwise, the process is abnormal. For example, do not use the fork or pthread_create function. |
sigpending (sigset_t *set) | Obtains the signal suspended by the current thread. |
sigwait (const sigset_t *__restrict set, int *__restrict sig) | Suspends the calling thread and waits for the signal. |
sigwaitinfo (const sigset_t *__restrict set, siginfo_t *__restrict info) | Suspends the calling thread and waits for the signal, with a timeout period specified. |
sigtimedwait (const sigset_t *__restrict set, siginfo_t *__restrict info, const struct timespec *__restrict timeout) | Suspends the calling thread and waits for the signal, with a timeout period specified. |
pthread_sigmask (int how, const sigset_t *__restrict set, sigset_t *__restrict oldset) | Checks or changes the signal mask of the calling thread. |
psignal (int signum, const char *s) | |
sigpause (int signum) | Deletes the signal specified by signum from the signal mask of the calling process and suspends the process until the signal is received. |
sighold (int signum) | Adds the signal specified by signum to the signal mask of the calling process. |
sigrelse (int signum) | Deletes the signal specified by signum from the signal mask of the calling process. |
sigisemptyset (const sigset_t *set) | Checks whether the signal set is empty. |
sigorset (sigset_t *dest, const sigset_t *left, const sigset_t *right) | Adds the union set of left and right to dest. |
sigandset (sigset_t *dest, const sigset_t *left, const sigset_t *right) | Adds the intersection of left and right to dest. |
signal (int signum, sighandler_t handler) | Changes the processing action of a specified signal. Do not use the process creation function or thread creation function in the registered callback processing function. Otherwise, the process is abnormal. For example, do not use the fork or pthread_create function. |
raise (int signum) | Sends a signal to the calling process. |
## **Details**
## **Macro Definition Documentation**
## MINSIGSTKSZ \[1/2\]
```
#define MINSIGSTKSZ 2048
```
**Description:**
Defines the minimum size of the replaceable signal stack.
Minimum size of the replaceable signal stack.
## MINSIGSTKSZ \[2/2\]
```
#define MINSIGSTKSZ 2048
```
**Description:**
Defines the minimum size of the replaceable signal stack.
Minimum size of the replaceable signal stack.
## sa\_handler \[1/2\]
```
#define sa_handler _u._sa_handler
```
**Description:**
Defines the simplified writing used to indicate the **\_\_sa\_handler.sa\_handler** field in [sigaction](sigaction.md).
Indicates the **\_u.\_sa\_handler** field in [sigaction](sigaction.md).
## sa\_handler \[2/2\]
```
#define sa_handler __sa_handler.sa_handler
```
**Description:**
Defines the simplified writing used to indicate the **\_\_sa\_handler.sa\_handler** field in [sigaction](sigaction.md).
Indicates the **\_u.\_sa\_handler** field in [sigaction](sigaction.md).
## SA\_NOCLDSTOP \[1/2\]
```
#define SA_NOCLDSTOP 1
```
**Description:**
Stops a child process without sending the [SIGCHLD](IPC.md#ga0e63521a64cc8bc2b91d36a87d32c9f8) signal.
[SIGCHLD](IPC.md#ga0e63521a64cc8bc2b91d36a87d32c9f8) is not generated when the child process is stopped.
## SA\_NOCLDSTOP \[2/2\]
```
#define SA_NOCLDSTOP 0x00000001
```
**Description:**
Stops a child process without sending the [SIGCHLD](IPC.md#ga0e63521a64cc8bc2b91d36a87d32c9f8) signal.
[SIGCHLD](IPC.md#ga0e63521a64cc8bc2b91d36a87d32c9f8) is not generated when the child process is stopped.
## SA\_NODEFER \[1/2\]
```
#define SA_NODEFER 0x40000000
```
**Description:**
The signals being processed are not masked during signal processing.
Signals being processed are not masked during signal processing.
## SA\_NODEFER \[2/2\]
```
#define SA_NODEFER 0x40000000
```
**Description:**
The signals being processed are not masked during signal processing.
Signals being processed are not masked during signal processing.
## SA\_ONSTACK \[1/2\]
```
#define SA_ONSTACK 0x08000000
```
**Description:**
Uses the signal stack specified by [sigaltstack](sigaltstack.md) for signal processing.
The signal stack specified by [sigaltstack](sigaltstack.md) is used for signal processing.
## SA\_ONSTACK \[2/2\]
```
#define SA_ONSTACK 0x08000000
```
**Description:**
Uses the signal stack specified by [sigaltstack](sigaltstack.md) for signal processing.
The signal stack specified by [sigaltstack](sigaltstack.md) is used for signal processing.
## sa\_sigaction \[1/2\]
```
#define sa_sigaction _u._sa_sigaction
```
**Description:**
Defines the simplified writing used to indicate the **\_\_sa\_handler.sa\_sigaction** field in [sigaction](sigaction.md).
Indicates the **\_\_sa\_handler.sa\_sigaction** field in [sigaction](sigaction.md).
## sa\_sigaction \[2/2\]
```
#define sa_sigaction __sa_handler.sa_sigaction
```
**Description:**
Defines the simplified writing used to indicate the **\_\_sa\_handler.sa\_sigaction** field in [sigaction](sigaction.md).
Indicates the **\_\_sa\_handler.sa\_sigaction** field in [sigaction](sigaction.md).
## SA\_SIGINFO \[1/2\]
```
#define SA_SIGINFO 4
```
**Description:**
Transfers the **siginfo\_t** structure with the signal.
Transfers the **siginfo\_t** with the signal.
## SA\_SIGINFO \[2/2\]
```
#define SA_SIGINFO 0x00000004
```
**Description:**
Transfers the **siginfo\_t** structure with the signal.
Transfers the **siginfo\_t** with the signal.
## SIGRTMAX \[1/2\]
```
#define SIGRTMAX [_NSIG](IPC.md#ga275020780cb70a8b0cba8db5accc5d19)
```
**Description:**
End of a reliable signal.
Defines the highest available signal number.
## SIGRTMAX \[2/2\]
```
#define SIGRTMAX (__libc_current_sigrtmax())
```
**Description:**
End of a reliable signal.
Defines the highest available signal number.
## SIGRTMIN \[1/2\]
```
#define SIGRTMIN 32
```
**Description:**
Start of a reliable signal.
Defines the lowest available signal number.
## SIGRTMIN \[2/2\]
```
#define SIGRTMIN (__libc_current_sigrtmin())
```
**Description:**
Start of a reliable signal.
Defines the lowest available signal number.
## SIGSTKSZ \[1/2\]
```
#define SIGSTKSZ 8192
```
**Description:**
Defines the size of a signal stack.
Size of a signal stack.
## SIGSTKSZ \[2/2\]
```
#define SIGSTKSZ 8192
```
**Description:**
Defines the size of a signal stack.
Size of a signal stack.
## **Typedef Documentation**
## stack\_t
```
typedef struct [sigaltstack](sigaltstack.md) [stack_t](IPC.md#ga395f9ff4025fe05bb535322593abde72)
```
**Description:**
Describes a signal stack.
Describes stack information for signal processing.
## **Function Documentation**
## kill\(\)
```
int kill (pid_t pid, int sig )
```
**Description:**
Sends a signal to a specified process.
**Parameters:**
pid | Indicates the target process ID. Parameters 0 and -1 are not supported. |
sig | Indicates the signal to be sent. |
**Attention:**
The default signal behavior does not support **STOP** and **CONTINUE**, and **COREDUMP** is not involved. [SIGSTOP](IPC.md#ga069e358bc9a864b7dc4fed2440eda14c), [SIGKILL](IPC.md#gaddd8dcd406ce514ab3b4f576a5343d42), and [SIGCONT](IPC.md#ga024f43063003e753afc6cca1acd6e104) cannot be masked. If a process is killed but its parent process is not recycled, a zombie process will be generated. After an asynchronous signal is sent to a process, the signal callback is executed only after the process is scheduled. The kernel cannot forcibly kill the process by using the signal. After the process is killed, [SIGCHLD](IPC.md#ga0e63521a64cc8bc2b91d36a87d32c9f8) is sent to its parent process. The sending action cannot be canceled. A sleeping process cannot be woken up by a signal. For example, a process invokes the **sleep** function to enter the sleeping state. Currently, suspended and zombie processes cannot be forcibly killed.
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
The current process does not have permission to send the signal to the target process. |
## mq\_close\(\)
```
int mq_close (mqd_t mqdes)
```
**Description:**
Closes a message queue that is no longer used.
**Parameters:**
mqdes | Indicates the descriptor of the message queue to be closed. |
**Returns:**
Returns **0** if the message queue is closed; returns **-1** and sets **errno** to a value in the following table if the message queue fails to be closed.
Failed to delete the message queue in the function. | |
Failed to release the space allocated with the message queue. | |
mqdes is invalid when the message queue is deleted in the function. |
## mq\_getattr\(\)
```
int mq_getattr (mqd_t mqdes, struct [mq_attr](mq_attr.md) * attr )
```
**Description:**
Obtains the attributes of the message queue specified by the descriptor. The values of **mq\_maxmsg**, **mq\_msgsize**, and **mq\_curmsgs** are fixed.
**Parameters:**
mqdes | Indicates the message queue descriptor. |
attr | Indicates the pointer to the obtained message queue attributes. |
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
## mq\_open\(\)
```
mqd_t mq_open (const char * name, int oflag, ... )
```
**Description:**
Creates a message queue or opens an existing message queue.
**Parameters:**
name | Indicates the name of the message queue to be created or opened. The maximum value is 259 bytes. |
oflag | Indicates the open flag, including O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, and O_NONBLOCK. |
mode | Used for permission control (not supported currently). Its type is mode_t. This is parameter is required only during the creation of a message queue. |
attr | Indicates the pointer to the queue attributes to be set. Its type is struct mq_attr. This parameter is optional and is used only during the creation of a message queue. |
**Attention:**
The message consists of 64 bytes by default, and the maximum length of the message is 64 KB. A maximum of 1024 message queues can be created. The mq\_curmsgs field in [mq\_attr](mq_attr.md) is not supported.
**Returns:**
Returns a message queue descriptor if the creation or opening is successful; returns **\(mqd\_t\)-1** and sets **errno** to a value in the following table if the operation fails.
Both O_CREAT and O_EXCL are specified in oflag, and a queue with this name already exists. | |
O_CREAT is specified in oflag, and attr is invalid. To be specific, the attribute value of the mq_maxmsg field is less than 0 or greater than USHRT_MAX, or the attribute value of mq_msgsize is less than 0 or greater than USHRT_MAX-4. name is NULL, or the length of the string specified by name is 0. | |
O_CREAT is not specified in oflag, and no queue with this name exists. | |
The number of opened message queues exceeds the maximum. |
## mq\_receive\(\)
```
ssize_t mq_receive (mqd_t mqdes, char * buffer, size_t size, unsigned * prioptr )
```
**Description:**
Receives a message from a specified message queue.
The message queue must have been open by [mq\_open\(\)](IPC.md#gaf5d8bf423701bd1783849119511381a5).
**Parameters:**
**Returns:**
Returns the number of bytes in the received message if the message is received; returns **\(mqd\_t\)-1** and sets **errno** to a value in the following table if the message fails to be received.
## mq\_send\(\)
```
int mq_send (mqd_t mqdes, const char * buffer, size_t size, unsigned prio )
```
**Description:**
Sends a message to a specified message queue.
The message queue must have been opened using [mq\_open\(\)](IPC.md#gaf5d8bf423701bd1783849119511381a5).
**Parameters:**
**Returns:**
Returns **0** if the message is sent; returns **-1** and sets **errno** to a value in the following table if the message fails to be sent.
## mq\_setattr\(\)
```
int mq_setattr (mqd_t mqdes, const struct [mq_attr](mq_attr.md) *__restrict newattr, struct [mq_attr](mq_attr.md) *__restrict oldattr )
```
**Description:**
Sets the attributes of the message queue specified by the descriptor. The **mq\_maxmsg**, **mq\_msgsize**, and **mq\_curmsgs** attributes cannot be modified. **mq\_flags** supports the **O\_NONBLOCK** attribute only.
The message queue must have been opened using [mq\_open\(\)](IPC.md#gaf5d8bf423701bd1783849119511381a5).
**Parameters:**
mqdes | Indicates the message queue descriptor. |
newattr | Indicates the pointer to the message queue attribute to be set. |
oldattr | Indicates the pointer to the original attribute. |
**Returns:**
Returns **0** if the setting is successful; returns **-1** and sets **errno** to a value in the following table if the setting fails.
## mq\_timedreceive\(\)
```
ssize_t mq_timedreceive (mqd_t mqdes, char *__restrict buffer, size_t size, unsigned *__restrict prioptr, const struct [timespec](timespec.md) *__restrict timeout )
```
**Description:**
Receives a message from the message queue, with a timeout period specified.
The message queue must have been open by [mq\_open\(\)](IPC.md#gaf5d8bf423701bd1783849119511381a5).
**Parameters:**
**Returns:**
Returns the number of bytes in the message if the message is received; returns **\(mqd\_t\)-1** and sets **errno** to a value in the following table if the message fails to be received.
## mq\_timedsend\(\)
```
int mq_timedsend (mqd_t mqdes, const char * buffer, size_t size, unsigned prio, const struct [timespec](timespec.md) * timeout )
```
**Description:**
Sends a message to a specified message queue, with a timeout period specified.
The message queue must have been open by [mq\_open\(\)](IPC.md#gaf5d8bf423701bd1783849119511381a5).
**Parameters:**
**Returns:**
Returns **0** if the message is sent; returns **-1** and sets **errno** to a value in the following table if the message fails to be sent.
## mq\_unlink\(\)
```
int mq_unlink (const char * name)
```
**Description:**
Decreases the reference count of the message queue by 1, or deletes the message queue if the reference count is **0**.
**Parameters:**
name | Indicates the message queue name. |
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
There is no message queue with the given name. | |
Failed to delete the message queue. | |
The message queue to be deleted is being used. | |
name is NULL, or the length of the string specified by name is 0. |
## psignal\(\)
```
void psignal (int signum, const char * s )
```
**Description:**
Prints signal information.
This function is used to print a message on the standard error output. The message consists of the character string **s**, a colon, a space, a string describing the signal information **signum**, and a newline character. If the string **s** is **NULL** or empty, the colon and space are omitted. If **signum** is invalid, the message displayed will indicate an unknown signal.
**Parameters:**
signum | Indicates the signal to be operated. |
s | Indicates the customized string s to be printed. |
## pthread\_sigmask\(\)
```
int pthread_sigmask (int how, const sigset_t *__restrict set, sigset_t *__restrict oldset )
```
**Description:**
Checks or changes the signal mask of the calling thread.
**Parameters:**
how | Indicates the function behavior. |
set | Indicates the signal set to be changed. |
oldset | Used for returning the original mask set of the calling thread. |
Adds the signals in set to the mask set of the thread. | |
Removes the signals in set from the mask set of the thread. | |
Sets set of blocked signals as the mask set of the thread. |
**Returns:**
Returns **0** if the change is successful; returns **errno** as described below if the change fails.
The set or oldset points outside the process's allocated address space. |
## raise\(\)
```
int raise (int signum)
```
**Description:**
Sends a signal to the calling process.
**Parameters:**
signum | Indicates the signal to be sent. |
**Returns:**
Returns 0 if the operation is successful; returns nonzero if the operation fails.
## sem\_close\(\)
```
int sem_close ([sem_t](sem_t.md) * sem)
```
**Description:**
Closes a specified semaphore.
**Parameters:**
sem | Indicates the pointer to the semaphore to close. |
**Returns:**
Returns **0** if the semaphore is closed. errno | Description
## sem\_destroy\(\)
```
int sem_destroy ([sem_t](sem_t.md) * sem)
```
**Description:**
Destroys a specified anonymous semaphore that is no longer used.
**Parameters:**
sem | Indicates the pointer to the semaphore to destroy. |
**Returns:**
Returns **0** if the destruction is successful.
## sem\_getvalue\(\)
```
int sem_getvalue ([sem_t](sem_t.md) *__restrict sem, int *__restrict sval )
```
**Description:**
Obtains the count value of a specified semaphore.
**Parameters:**
sem | Indicates the pointer to the semaphore. |
sval | Indicates the pointer to the obtained count value. |
**Returns:**
Returns **0** if the operation is successful.
## sem\_init\(\)
```
int sem_init ([sem_t](sem_t.md) * sem, int pshared, unsigned int value )
```
**Description:**
Creates and initializes an anonymous semaphore.
**Parameters:**
**Returns:**
Returns **0** if the initialization is successful; returns **-1** and sets **errno** to a value in the following table if the initialization fails.
The input parameters are invalid and the initial count value exceeds the limit. |
## sem\_post\(\)
```
int sem_post ([sem_t](sem_t.md) * sem)
```
**Description:**
Increments the semaphore count by 1.
Generally, this function is used to increment the count of available shared resources when the process is to leave its critical section. This function is used together with [sem\_wait\(\)](IPC.md#gaad70020dca2241a2b78e272ca033271b) to protect shared resources.
**Parameters:**
sem | Indicates the pointer to the semaphore to be operated. |
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
The number of semaphores exceeds the maximum number. |
## sem\_timedwait\(\)
```
int sem_timedwait ([sem_t](sem_t.md) *__restrict sem, const struct [timespec](timespec.md) *__restrict timeout )
```
**Description:**
Obtains the semaphore, with a timeout period specified.
If the semaphore count is greater than **0**, the shared resources can be obtained and the semaphore count decreases by 1. After the function returns **0**, the code execution enters the resource critical area. Otherwise, the shared resource is unavailable, the function keeps waiting, and code execution cannot enter the resource critical area. If no semaphore is obtained within the specified timeout period, **-1** is returned. This function is used together with [sem\_post\(\)](IPC.md#ga015dce85cab8477c679cc47968958247) to protect shared resources.
**Parameters:**
sem | Indicates the pointer to the semaphore to be operated. |
timeout | Indicates the timeout period to be set. |
**Returns:**
Returns **0** if the semaphore is obtained; returns **-1** and sets **errno** to a value in the following table if the semaphore fails to be obtained.
The waiting for the semaphore is interrupted. | |
The semaphore is set to non-blocking mode, and the semaphore count is 0. | |
Waiting for the semaphore timed out. |
## sem\_trywait\(\)
```
int sem_trywait ([sem_t](sem_t.md) * sem)
```
**Description:**
Attempts to obtain the semaphore.
If the semaphore count is greater than **0**, the shared resources can be obtained and the semaphore count decreases by 1. After the function returns **0**, the code execution enters the resource critical area. Otherwise, the shared resource is unavailable, and the function returns **-1**. This function is used together with [sem\_post\(\)](IPC.md#ga015dce85cab8477c679cc47968958247) to protect shared resources.
**Parameters:**
sem | Indicates the pointer to the semaphore to be operated. |
**Returns:**
Returns **0** if the semaphore is obtained; returns **-1** and sets **errno** to a value in the following table if the semaphore fails to be obtained.
The semaphore is set to non-blocking mode, and the semaphore count is 0. |
## sem\_unlink\(\)
```
int sem_unlink (const char * name)
```
**Description:**
Deletes a specified semaphore.
This function is used to subtract 1 from the open count of the semaphore. If the open count is **0**, this function deletes the semaphore.
**Parameters:**
name | Indicates the name of the semaphore to be deleted. |
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
There is no semaphore with the given name. |
## sem\_wait\(\)
```
int sem_wait ([sem_t](sem_t.md) * sem)
```
**Description:**
Obtains the semaphore.
If the semaphore count is greater than **0**, the shared resources can be obtained and the semaphore count decreases by 1. After the function returns **0**, the code execution enters the resource critical area. Otherwise, the shared resource is unavailable, the function keeps waiting, and code execution cannot enter the resource critical area. This function is used together with [sem\_post\(\)](IPC.md#ga015dce85cab8477c679cc47968958247) to protect shared resources.
**Parameters:**
sem | Indicates the pointer to the semaphore to be obtained. |
**Returns:**
Returns **0** if the semaphore is obtained; returns **-1** and sets **errno** to a value in the following table if the semaphore fails to be obtained.
The waiting for the semaphore is interrupted. | |
The semaphore is set to non-blocking mode, and the semaphore count is 0. |
## sigaction\(\)
```
int [sigaction](sigaction.md) (int signum, const struct [sigaction](sigaction.md) *__restrict act, struct [sigaction](sigaction.md) *__restrict oldact )
```
**Description:**
Checks or changes the processing action associated with a specified signal. Do not use the process creation function or thread creation function in the registered callback processing function. Otherwise, the process is abnormal. For example, do not use the **fork** or **pthread\_create** function.
**Parameters:**
signum | Indicates the signal to be operated. |
act | Indicates the new signal processing action. |
oldact | Indicates the original signal processing action if the value is not NULL. |
**Attention:**
The **sa\_flag** field in **act** supports the [SA\_SIGINFO](IPC.md#ga59b4c0774aace526b10b6d737075a790) flag only.
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
signum is invalid or cannot be captured. | |
## sigaddset\(\)
```
int sigaddset (sigset_t * set, int signum )
```
**Description:**
Adds a signal to a specified signal set.
**Parameters:**
set | Indicates the pointer to the signal set to hold signals. |
signum | Indicates the signal to be added. |
**Returns:**
Returns **0** if the operation is successful. If **set** or **signum** is invalid, the process is abnormal.
## sigandset\(\)
```
int sigandset (sigset_t * dest, const sigset_t * left, const sigset_t * right )
```
**Description:**
Adds the intersection of **left** and **right** to **dest**.
**Parameters:**
dest | Indicates the pointer to the target signal set. |
left | Indicates the left set to be added. |
right | Indicates the right set to be added. |
**Returns:**
Returns **0** if the operation is successful.
## sigdelset\(\)
```
int sigdelset (sigset_t * set, int signum )
```
**Description:**
Delete a signal from a specified signal set.
**Parameters:**
set | Indicates the pointer to the signal set to hold signals. |
signum | Indicates the signal to be deleted. |
**Returns:**
Returns **0** if the operation is successful. If **set** or **signum** is invalid, the process is abnormal.
## sigemptyset\(\)
```
int sigemptyset (sigset_t * set)
```
**Description:**
Clears all signals in a specified signal set.
**Parameters:**
set | Indicates the pointer to the signal set to be cleared. |
**Returns:**
Returns **0** if the operation is successful. If **set** is invalid, the process is abnormal.
## sigfillset\(\)
```
int sigfillset (sigset_t * set)
```
**Description:**
Adds all signals to a specified signal set.
**Parameters:**
set | Indicates the pointer to the signal set to hold signals. |
**Returns:**
Returns **0** if the operation is successful. If **set** is invalid, the process is abnormal.
## sighold\(\)
```
int sighold (int signum)
```
**Description:**
Adds the signal specified by **signum** to the signal mask of the calling process.
**Parameters:**
signum | Indicates the signal to be added. |
**Returns:**
Returns **0** if the operation is successful; returns **-1** if the operation fails.
## sigisemptyset\(\)
```
int sigisemptyset (const sigset_t * set)
```
**Description:**
Checks whether the signal set is empty.
**Parameters:**
set | Indicates the pointer to the signal set to be checked. |
**Returns:**
Returns **0** if the set is not empty; returns **1** if the set is empty; returns **-1** if the operation fails.
## sigismember\(\)
```
int sigismember (const sigset_t * set, int signum )
```
**Description:**
Checks whether a signal is in the signal set.
**Parameters:**
set | Indicates the pointer to the signal set to be checked. |
signum | Indicates the signal to be checked. |
**Returns:**
Returns **0** if the signal is not in the set; returns **1** if the signal is in the set. If **set** or **signum** is invalid, the process is abnormal.
## signal\(\)
```
[sighandler_t](IPC.md#ga754cdc0bcfffe07baa426dc252c9101a) signal (int signum, [sighandler_t](IPC.md#ga754cdc0bcfffe07baa426dc252c9101a) handler )
```
**Description:**
Changes the processing action of a specified signal. Do not use the process creation function or thread creation function in the registered callback processing function. Otherwise, the process is abnormal. For example, do not use the **fork** or **pthread\_create** function.
**Parameters:**
signum | Indicates the signal to be operated. |
handler | Indicates the pointer to the specified processing action (SIG_IGN or SIG_DFL, or to a custom processing action. |
**Returns:**
Returns the pointer to the original processing action if the operation is successful; returns [SIG\_ERR](IPC.md#ga3c330fbddd84bf34e65af370b11998d6) and sets **errno** to a value in the following table if the operation fails.
## sigorset\(\)
```
int sigorset (sigset_t * dest, const sigset_t * left, const sigset_t * right )
```
**Description:**
Adds the union set of **left** and **right** to **dest**.
**Parameters:**
dest | Indicates the pointer to the target signal set. |
left | Indicates the left set to be added. |
right | Indicates the right set to be added. |
**Returns:**
Returns **0** if the operation is successful.
## sigpause\(\)
```
int sigpause (int signum)
```
**Description:**
Deletes the signal specified by **signum** from the signal mask of the calling process and suspends the process until the signal is received.
**Parameters:**
signum | Indicates the signal to be operated. |
**Attention:**
After the signal is received, the original signal mask of the process is restored.
**Returns:**
Returns **-1** and sets **errno** to a value in the following table.
The suspension wait is interrupted by the signal. |
## sigpending\(\)
```
int sigpending (sigset_t * set)
```
**Description:**
Obtains the signal suspended by the current thread.
**Parameters:**
set | Indicates the pointer to the signal set used to store the signal suspended by the current thread. |
**Returns:**
Returns **0** if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
set points to an invalid space. | |
The suspension wait is interrupted by the signal. |
## sigprocmask\(\)
```
int sigprocmask (int how, const sigset_t *__restrict set, sigset_t *__restrict oldset )
```
**Description:**
Changes the signal mask of the calling thread.
**Parameters:**
how | Indicates the function behavior. |
set | Indicates the pointer to the signal set to be changed. |
oldset | Indicates the pointer to the original mask set of the calling thread. |
Adds the signals in set to the mask set of the thread. | |
Removes the signals in set from the mask set of the thread. | |
Sets set of blocked signals as the mask set of the thread. |
**Returns:**
Returns **0** if the change is successful; returns **-1** and sets **errno** to a value in the following table if the change fails.
The set or oldset points outside the process's allocated address space. |
## sigrelse\(\)
```
int sigrelse (int signum)
```
**Description:**
Deletes the signal specified by **signum** from the signal mask of the calling process.
**Parameters:**
signum | Indicates the signal to be deleted. |
**Returns:**
Returns **0** if the deletion is successful; returns **-1** if the deletion fails.
## sigsuspend\(\)
```
int sigsuspend (const sigset_t * mask)
```
**Description:**
Sets **mask** as the signal mask set of the current process and suspends the process until a signal processing action is triggered.
**Parameters:**
mask | Indicates the pointer to the signal set to be set. |
**Returns:**
Returns **-1** and sets **errno** to a value in the following table.
mask points to an invalid space. | |
The suspension wait is interrupted by the signal. | |
## sigtimedwait\(\)
```
int sigtimedwait (const sigset_t *__restrict set, siginfo_t *__restrict info, const struct [timespec](timespec.md) *__restrict timeout )
```
**Description:**
Suspends the calling thread and waits for the signal, with a timeout period specified.
This function is used to suspend execution of the calling thread until one of the signals specified in the signal set becomes pending. This function specifies the timeout behavior.
**Parameters:**
set | Indicates the pointer to the signal set to be waited. |
info | Used to save the information about the waited signal if the value is not NULL. |
timeout | Indicates the timeout duration. |
**Attention:**
The signal to be waited must be in the mask set of the current thread. Once the signal is received, it is deleted from the pending list. The waiting for the signal cannot be interrupted by other signals, that is, **EINTR** is not supported.
**Returns:**
Returns the value of the waited signal if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails.
No signal specified by set is generated within the specified timeout period. | |
## sigwait\(\)
```
int sigwait (const sigset_t *__restrict set, int *__restrict sig )
```
**Description:**
Suspends the calling thread and waits for the signal.
This function is used to suspend execution of the calling thread until one of the signals specified in the signal set becomes pending.
**Parameters:**
set | Indicates the pointer to the signal set to be waited. |
sig | Used for saving the waited signal. |
**Attention:**
The signal to be waited must be in the mask set of the current thread. Once the signal is received, it is deleted from the pending list.
**Returns:**
Returns **0** if the signal is received; returns **-1** and sets **errno** to a value in the following table if the signal fails to be received.
The suspension wait is interrupted by the signal. |
## sigwaitinfo\(\)
```
int sigwaitinfo (const sigset_t *__restrict set, siginfo_t *__restrict info )
```
**Description:**
Suspends the calling thread and waits for the signal, with a timeout period specified.
This function is used to suspend execution of the calling thread until one of the signals specified in the signal set becomes pending. This function does not specify the timeout behavior.
**Parameters:**
set | Indicates the pointer to the signal set to be waited. |
info | Used to save the information about the waited signal if the value is not NULL. |
**Attention:**
The signal to be waited must be in the mask set of the current thread. Once the signal is received, it is deleted from the pending list. The waiting for the signal cannot be interrupted by other signals, that is, **EINTR** is not supported.
**Returns:**
Returns the value of the waited signal if the operation is successful; returns **-1** and sets **errno** to a value in the following table if the operation fails. errno | Description ----—|-----------------------------—