Skip to content
Snippets Groups Projects
Commit cf90bb94 authored by Mertens De Andrade Guilherme's avatar Mertens De Andrade Guilherme
Browse files

second question

parent 8c28709b
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <unistd.h>
volatile bool running = true;
void print_in_exit() {
std::cout << "Message during exit" << std::endl;
}
void handle_signal(int signal_number) {
std::cout << "Received signal: " << signal_number << std::endl;
std::cout << "Stopping the program" << std::endl;
running = false;
}
int main(){
std::cout << "started" << std::endl;
if (atexit(print_in_exit) != 0) {
perror("Fail in exit");
exit(EXIT_FAILURE);
}
struct sigaction sa;
sa.sa_handler = handle_signal;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, nullptr) == -1) {
perror("Fail in exit");
exit(EXIT_FAILURE);
}
if (sigaction(SIGTERM, &sa, nullptr) == -1) {
perror("Fail in exit");
exit(EXIT_FAILURE);
}
while(running){
std::cout << "id: " << getpid() << std::endl;
std::cout << "father id: " << getppid() << std::endl;
std::cout << "groupd id: " << getpgrp() << std::endl;
std::cout << rand() % 100 << std::endl;
sleep(1);
}
std::cout << "finished" << std::endl;
return EXIT_SUCCESS;
}
/*
father id: 15834
groupd id: 16168
62
^CReceived signal: 2
Stopping the program
finished
using kill -s INT 17116 here is the result:
46
Received signal: 2
Stopping the program
finished
now with only kill:
id: 17828
father id: 15834
groupd id: 17828
62
[1] 17828 terminated ./a.out
after the change in the code:
id: 18796
father id: 15834
groupd id: 18796
90
Received signal: 15
Stopping the program
finished
~/workspace:
after -s KILL:
id: 19282
father id: 15834
groupd id: 19282
11
[1] 19282 killed ./a.out
SIGKILL cannot be shown: https://stackoverflow.com/questions/35569659/the-signals-sigkill-and-sigstop-cannot-be-caught-blocked-or-ignored-why
when I used kill without -s INT, nothing happened, but when I used it with -s INT, the first terminal got deleted
It's not stopping with CTR+C, neither with kill, but I'm getting the signals
With -s KILL it stopped
groupd id: 22843
86
id: 22843
father id: 16628
groupd id: 22843
77
^CReceived signal: 2
Stopping the program
finished
Message during exit
I just saw that I forgot to add the perror() part
With kill:
21
Received signal: 15
Stopping the program
finished
Message during exit
With kill -s KILL:
id: 24595
father id: 16628
groupd id: 24595
62
[1] 24595 killed ./a.out
*/
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment