Signal for IPC

This commit is contained in:
2024-05-02 01:24:30 +05:30
parent 00b38699c3
commit 2011b70ded
5 changed files with 134 additions and 0 deletions

23
Signals/CMakeLists.txt Normal file
View File

@@ -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 "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>: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.

51
Signals/ctrl_c.c Normal file
View File

@@ -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 <signal.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

17
Signals/kill_receiver.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <signal.h>
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;
}

11
Signals/kill_sender.c Normal file
View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <signal.h>
int
main(int argc, char** argv) {
kill(5827, SIGUSR1);
scanf("\n");
return 0;
}

32
Signals/raise.c Normal file
View File

@@ -0,0 +1,32 @@
#include <signal.h>
#include <stdio.h>
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");
}