#include#include void signalHandler(int signum) { std::cout << "Received signal: " << signum << std::endl; exit(signum); } int main() { signal(SIGINT, signalHandler); std::cout << "Running program..." << std::endl; while (true) {} // infinite loop return 0; }
#includeIn this example, the `fork()` function is used to create a child process. The child process will pause until it receives a signal. The parent process waits for the child process to start and then sends a custom signal (SIGUSR1) to it using the `kill()` function. The `csignal` package library contains the necessary functions and data types for working with SIGNAL data in C++.#include #include int main() { pid_t pid = fork(); if (pid == 0) { // child process std::cout << "Child process waiting for signal..." << std::endl; pause(); // pause until signal is received } else if (pid > 0) { // parent process sleep(1); // wait for child process to start std::cout << "Sending signal to child process..." << std::endl; kill(pid, SIGUSR1); // send a custom signal to child process } else { std::cerr << "Error creating child process." << std::endl; return 1; } return 0; }