Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
C Snippets
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
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Björn Eßwein
C Snippets
Commits
2d902207
Commit
2d902207
authored
5 years ago
by
Bjoern Esswein
Browse files
Options
Downloads
Patches
Plain Diff
added signal example
parent
d37c9b62
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
signal.c
+54
-0
54 additions, 0 deletions
signal.c
with
54 additions
and
0 deletions
signal.c
0 → 100644
+
54
−
0
View file @
2d902207
#include
"die.h"
#include
<signal.h>
#include
<sys/types.h>
#include
<sys/wait.h>
#include
<unistd.h>
static
void
sigchldHandler
(
int
sig
);
int
i
;
int
main
()
{
struct
sigaction
handleSIGCHLD
=
{
.
sa_handler
=
sigchldHandler
,
.
sa_flags
=
SA_RESTART
,
};
if
(
sigaction
(
SIGCHLD
,
&
handleSIGCHLD
,
NULL
)
!=
0
)
die
(
"Could not set sigaction"
);
//mask sigchld
sigset_t
newMask_Itr
;
sigset_t
oldMask_Itr
;
sigemptyset
(
&
newMask_Itr
);
sigaddset
(
&
newMask_Itr
,
SIGCHLD
);
if
(
sigprocmask
(
SIG_BLOCK
,
&
newMask_Itr
,
&
oldMask_Itr
)
==
-
1
)
die
(
"sigprocmask"
);
//do critical stuff
//alte signal mask wiederherstellen
if
(
sigprocmask
(
SIG_SETMASK
,
&
oldMask_Itr
,
NULL
)
==
-
1
)
die
(
"sigprocmask"
);
pid_t
pid
=
fork
();
if
(
pid
<
0
)
die
(
"fork"
);
if
(
pid
==
0
)
{
sleep
(
5
);
exit
(
EXIT_SUCCESS
);
}
//passives warten
i
=
1
;
if
(
sigprocmask
(
SIG_BLOCK
,
&
newMask_Itr
,
&
oldMask_Itr
)
==
-
1
)
die
(
"sigprocmask"
);
while
(
i
>
0
)
{
sigsuspend
(
&
oldMask_Itr
);
}
}
static
void
sigchldHandler
(
int
sig
)
{
// Collect zombie processes
(
void
)
sig
;
//unused param
pid_t
pid
;
int
event
;
while
((
pid
=
waitpid
(
-
1
,
&
event
,
WNOHANG
))
>
0
)
{
// collect all zombies (prevent zombies if signal got lost because of masking)
i
--
;
}
}
\ 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