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
12bc11e7
Commit
12bc11e7
authored
5 months ago
by
Mertens De Andrade Guilherme
Browse files
Options
Downloads
Patches
Plain Diff
third question part 1
parent
cf90bb94
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server_3.c
+124
-0
124 additions, 0 deletions
server_3.c
with
124 additions
and
0 deletions
server_3.c
0 → 100644
+
124
−
0
View file @
12bc11e7
#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
;
}
void
print_process_info
(
const
char
*
process_type
)
{
std
::
cout
<<
process_type
<<
" | id: "
<<
getpid
()
<<
std
::
endl
;
std
::
cout
<<
process_type
<<
" | father id: "
<<
getppid
()
<<
std
::
endl
;
std
::
cout
<<
process_type
<<
" | groupd id: "
<<
getpgrp
()
<<
std
::
endl
;
std
::
cout
<<
rand
()
%
100
<<
std
::
endl
;
}
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
);
}
pid_t
pid
=
fork
();
if
(
pid
==
0
)
{
while
(
running
)
{
print_process_info
(
"Child"
);
sleep
(
1
);
}
}
else
{
while
(
running
)
{
print_process_info
(
"Parent"
);
sleep
(
1
);
}
}
std
::
cout
<<
"finished"
<<
std
::
endl
;
return
EXIT_SUCCESS
;
}
/*
Ouput:
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
23
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
23
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
67
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
67
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
35
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
35
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
29
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
29
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
2
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
2
^CReceived signal: 2
Received signal: 2
Stopping the program
Stopping the program
finished
finished
Message during exit
Message during exit
They have the same group id, and they both stop cin CTR+C because they belong to the same group,
and this singal is sent to all the process in the group
*/
\ 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