Skip to content
Snippets Groups Projects
Commit 2d902207 authored by Bjoern Esswein's avatar Bjoern Esswein
Browse files

added signal example

parent d37c9b62
No related branches found
No related tags found
No related merge requests found
signal.c 0 → 100644
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment