Skip to content

Instantly share code, notes, and snippets.

@monstermunchkin
Created May 18, 2011 18:00
Show Gist options
  • Save monstermunchkin/979124 to your computer and use it in GitHub Desktop.
Save monstermunchkin/979124 to your computer and use it in GitHub Desktop.
Avoiding zombie processes
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#define PIPENAME "foopipe"
#define MAXLEN 100
int main(int argc, char *argv[])
{
char buf[MAXLEN];
FILE *f;
int pid;
/* with sigaction:
struct sigaction act;
act.sa_handler = SIG_IGN;
act.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &act, NULL); */
f = fopen(PIPENAME, "r");
/* with sigignore: */
sigignore(SIGCHLD);
while (1) {
fgets(buf, MAXLEN - 1, f);
pid = fork();
if (pid == 0)
execlp("xmessage", "xmessage", buf, NULL);
if (feof(f) == 0) {
fclose(f);
f = fopen(PIPENAME, "r");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment