Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp1-ProgDev-mertens-guilherme
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mertens De Andrade Guilherme
tp1-ProgDev-mertens-guilherme
Commits
cf90bb94
Commit
cf90bb94
authored
5 months ago
by
Mertens De Andrade Guilherme
Browse files
Options
Downloads
Patches
Plain Diff
second question
parent
8c28709b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server_2.c
+139
-0
139 additions, 0 deletions
server_2.c
with
139 additions
and
0 deletions
server_2.c
0 → 100644
+
139
−
0
View file @
cf90bb94
#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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment