C library functions -sigwait()
C Standard Library - <signal.h>
Description
sigwaitThe function is a function in the C standard library, used to synchronously wait for the arrival of signals. It removes one of the signals in the specified signal set from the queue and returns its number. This function is defined in<signal.h>In the header file.
Syntax
int sigwait(const sigset_t *set, int *sig);
Parameter
const sigset_t *set: points to asigset_tA signal set variable of type, which specifies the signals to wait for.int *sig: A pointer to an integer variable, used to store the captured signal number.
Return Value
- Returns 0 on success.
- Returns an error code on failure.
Example
The following is an example usingsigwaitfunction synchronously waits forSIGINTAn example program for signals. The program prints a message while waiting for signals, and when it captures theSIGINTsignal, usesigwaitThe function synchronously waits for and processes signals.
Example
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
// Thread function, waiting for the SIGINT signal
void* wait_for_signal(void* arg) {
sigset_t *set = (sigset_t *)arg;
int sig;
printf("Thread waiting for signal...\n");
if (sigwait(set, &sig) == 0) {
printf("Caught signal %d: %s\n", sig, strsignal(sig));
} else {
perror("sigwait");
}
return NULL;
}
int main() {
pthread_t thread;
sigset_t set;
// Initialize the signal set and add SIGINT to it
sigemptyset(&set);
sigaddset(&set, SIGINT);
// Block the SIGINT signal in the main thread
pthread_sigmask(SIG_BLOCK, &set, NULL);
// Create a thread to wait for the SIGINT signal
pthread_create(&thread, NULL, wait_for_signal, (void*)&set);
// The main thread continues to run until Ctrl+C is pressed to send the SIGINT signal
while (1) {
printf("Main thread running... Press Ctrl+C to send SIGINT\n");
sleep(1);
}
// Wait for the child thread to finish
pthread_join(thread, NULL);
return 0;
}
After compiling and running the program, press Ctrl+C to sendSIGINTthe signal, and the output will be:
Main thread running... Press Ctrl+C to send SIGINT Main thread running... Press Ctrl+C to send SIGINT Main thread running... Press Ctrl+C to send SIGINT Thread waiting for signal... Caught signal 2: Interrupt
The program blocks in the main threadSIGINTsignal, it creates a child thread to wait for and handle theSIGINTsignal. When catchingSIGINTwhen the signal occurs, the child thread usessigwaitthe function to synchronously wait for and process the signal.
Explanation
sigemptyset(&set);: Initialize the signal set to be empty.sigaddset(&set, SIGINT);: mapsSIGINTAdd the signal to the signal set.pthread_sigmask(SIG_BLOCK, &set, NULL);: Block in the main threadSIGINTthe signal, so that the child thread can handle it.pthread_create(&thread, NULL, wait_for_signal, (void*)&set);: Create a child thread to waitSIGINTsignal.sigwait(set, &sig);: the child thread usessigwaitThe function synchronously waits for and processes.SIGINTsignal.
Notes
sigwaitThe function returns only when one of the signals in the signal set is captured.sigwaitIt can be used together with a signal mask to ensure that signals are handled in the intended thread.- Usage
sigwaitIt can avoid race conditions and complexity in signal handlers.