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
cc1f87fc
Commit
cc1f87fc
authored
5 months ago
by
Mertens De Andrade Guilherme
Browse files
Options
Downloads
Patches
Plain Diff
Question 2.2
parent
7bd597f5
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_4.c
+141
-0
141 additions, 0 deletions
server_4.c
with
141 additions
and
0 deletions
server_4.c
0 → 100644
+
141
−
0
View file @
cc1f87fc
#include
<iostream>
#include
<csignal>
#include
<cstdlib>
#include
<unistd.h>
#include
<cstring>
#include
<sys/wait.h>
volatile
bool
running
=
true
;
int
pipefd
[
2
];
// File descriptors for pipe
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
;
}
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
);
}
// Create the pipe
if
(
pipe
(
pipefd
)
==
-
1
)
{
perror
(
"Pipe failed"
);
exit
(
EXIT_FAILURE
);
}
pid_t
pid
=
fork
();
if
(
pid
==
0
)
{
close
(
pipefd
[
1
]);
int
number
;
while
(
running
)
{
ssize_t
bytes_read
=
read
(
pipefd
[
0
],
&
number
,
sizeof
(
number
));
if
(
bytes_read
>
0
)
{
std
::
cout
<<
"Child received: "
<<
number
<<
std
::
endl
;
}
sleep
(
1
);
}
close
(
pipefd
[
0
]);
}
else
{
close
(
pipefd
[
0
]);
int
number
=
0
;
while
(
running
)
{
print_process_info
(
"Parent"
);
write
(
pipefd
[
1
],
&
number
,
sizeof
(
number
));
number
++
;
sleep
(
1
);
int
status
;
pid_t
result
=
waitpid
(
pid
,
&
status
,
WNOHANG
);
if
(
result
==
pid
)
{
std
::
cout
<<
"Child stopped, father stopping"
<<
std
::
endl
;
running
=
false
;
}
}
close
(
pipefd
[
1
]);
}
std
::
cout
<<
"finished"
<<
std
::
endl
;
return
EXIT_SUCCESS
;
}
/*
with ctr+c
Child received: 45
Parent | id: 2934
Parent | father id: 2610
Parent | groupd id: 2934
Child received: 46
^CReceived signal: 2
Stopping the program
Received signal: 2
Stopping the program
finished
Message during exit
finished
Message during exit
whil kill parent
Parent | id: 3396
Parent | father id: 2610
Parent | groupd id: 3396
Child received: 10
Parent | id: 3396
Parent | father id: 2610
Parent | groupd id: 3396
Child received: 11
Received signal: 15
Stopping the program
finished
Message during exit
with kill child
Child received: 41
Parent | id: 3644
Parent | father id: 2610
Parent | groupd id: 3644
Child received: 42
Received signal: 15
Stopping the program
finished
Message during exit
Child stopped, father stopping
finished
Message during exit
*/
\ 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