diff --git a/Signals/CMakeLists.txt b/Signals/CMakeLists.txt new file mode 100644 index 0000000..8f69200 --- /dev/null +++ b/Signals/CMakeLists.txt @@ -0,0 +1,23 @@ +# CMakeList.txt : CMake project for Signals, include source and define +# project specific logic here. +# +cmake_minimum_required (VERSION 3.8) + +# Enable Hot Reload for MSVC compilers if supported. +if (POLICY CMP0141) + cmake_policy(SET CMP0141 NEW) + set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$,$>,$<$:EditAndContinue>,$<$:ProgramDatabase>>") +endif() + +project(Signals VERSION 1.0.0 LANGUAGES C CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_executable(SIGINT "ctrl_c.c") +add_executable(RaiseSysCall "raise.c") +add_executable(KillSender "kill_sender.c") +add_executable(KillReceiver "kill_receiver.c") + +# TODO: Add tests and install targets if needed. diff --git a/Signals/ctrl_c.c b/Signals/ctrl_c.c new file mode 100644 index 0000000..ba411f9 --- /dev/null +++ b/Signals/ctrl_c.c @@ -0,0 +1,51 @@ +/* + Consider a program below, + There is some special thing attached to SIGABRT signal. + + You can see, in below code, line no 23 registers a handler routine with signal SIGABRT. + when abort() is called, it delivers the signal SIGABRT to this process, and abort_signal_handler() will be invoked. + Note that, it is mandatory to terminate the process by calling exit(0) from abort_signal_handler(). Handler routine + for SIGABRT signal must not return to caller. It it returns, Kernel will kill the process instead. You can perform experiment + by removing the line 16 in below code, and you will notice that process is terminated after execution of abort_signal_handler(). + Hence, Process either commit suicide or it will be killed by the OS. SIGABRT signal cannot be blocked(= ignore) by the process. + +*/ + + +#include +#include +#include + +static void +ctrlC_signal_handler(int sig) { + + printf("Ctrl-C pressed\n"); + printf("Bye Bye\n"); + exit(0); + +} + +static void +abort_signal_handler(int sig) { + + printf("process is aborted\n"); + printf("Bye Bye\n"); + exit(0); + +} + +int +main(int argc, char* argv[]) { + + signal(SIGINT, ctrlC_signal_handler); + signal(SIGABRT, abort_signal_handler); + + char ch; + printf("About process (y/n) ?\n"); + scanf("%c", &ch); + if (ch == 'y') + abort(); + + + return 0; +} \ No newline at end of file diff --git a/Signals/kill_receiver.c b/Signals/kill_receiver.c new file mode 100644 index 0000000..76045b7 --- /dev/null +++ b/Signals/kill_receiver.c @@ -0,0 +1,17 @@ +#include +#include + +static void +signal_handler(int sig) { + + printf("Signal %d received\n", sig); + +} + +int +main(int argc, char** argv) { + + signal(SIGUSR1, signal_handler); + scanf("\n"); + return 0; +} \ No newline at end of file diff --git a/Signals/kill_sender.c b/Signals/kill_sender.c new file mode 100644 index 0000000..3327406 --- /dev/null +++ b/Signals/kill_sender.c @@ -0,0 +1,11 @@ +#include +#include + + +int +main(int argc, char** argv) { + + kill(5827, SIGUSR1); + scanf("\n"); + return 0; +} \ No newline at end of file diff --git a/Signals/raise.c b/Signals/raise.c new file mode 100644 index 0000000..52a1e81 --- /dev/null +++ b/Signals/raise.c @@ -0,0 +1,32 @@ +#include +#include + +void signal_catchfunc(int); + +int main() { + + int ret; + + ret = signal(SIGINT, signal_catchfunc); + + if (ret == SIG_ERR) { + printf("Error: unable to set signal handler.\n"); + exit(0); + } + + printf("Going to raise a signal\n"); + ret = raise(SIGINT); + if (ret != 0) { + printf("Error: unable to raise SIGINT signal.\n"); + exit(0); + } + + printf("Exiting...\n"); + return 0; +} + +void signal_catchfunc(int signal) { + + printf("!! signal caught !!\n"); + +} \ No newline at end of file