I wish to understand, if my implementation of signal handling in my code is correct
Some context
Some external application creates two pipes, which I will be connecting to, this external application has been correctly implemented (Guaranteed)
My two pipes are pe_exchange_0 and pe_trader_0 the {0} is passed in via command line args
Now, the external application is writing to the pe_exchange_0 pipe, and my code is suppose to be reading the contents it is writing into. The max size of the content it writes is 50 (so our char buffer needs to be of size 50).
Now after this is done, I need to send a SIGUSR1 from my application (the trader) to the exchange and write a message saying "Hello"
The process id of the exchange is the parent id so i can simply do kill(exchange_pid, SIGUSR1);
When sending a message to my exchange
Now here is a sample example
EXCHANGE (external application) -> Trader: Hi There!
my code does not detect the sigusr1 call and does not print out the message from the exchange
it should send back a subsequent echo
Trader(my app) -> Exchange(External) = Hello World
This is what I have so far.
void signal_handler(int SIG);
volatile sig_atomic_t g_sigint_flag = 0;
volatile sig_atomic_t g_sigusr1_flag = 0;
int fd1;
int fd2;
void exchange_signal_handler(int signo);
int main(int argc, char ** argv) {
char exchange[100];
char trader[100];
sprintf(exchange, "/tmp/pe_exchange_%s", argv[1]);
sprintf(trader, "/tmp/pe_trader_%s", argv[1]);
fd1 = open(exchange,O_RDONLY);
fd2 = open(trader,O_WRONLY);
if (fd1== -1) {
perror("open pipe failed");
return 1;
}
if (fd2== -1) {
perror("open pipe failed");
return 1;
}
if (signal(SIGINT, signal_handler) == SIG_ERR) {
printf("Unable to register signal handler for SIGINT\n");
return 1;
}
if (signal(SIGUSR1, exchange_signal_handler) == SIG_ERR) {
printf("Unable to register exchange signal handler for SIGUSR1\n");
return 1;
}
pid_t exchange_pid = getppid();
while (1) {
if (g_sigusr1_flag) {
g_sigusr1_flag = 0;
printf("This is working partially");// Reset the flag
}
sleep(1);
}
close(fd2);
close(fd1);
return 0;
}
void signal_handler(int SIG) {
if (SIG == SIGINT) {
printf("Terminated\n");
g_sigint_flag = 1;
}
}
void exchange_signal_handler(int signo) {
if (signo == SIGUSR1) {
printf("Received SIGUSR1 signal from exchange\n");
g_sigusr1_flag = 1;
}
}