Skip to content

Instantly share code, notes, and snippets.

@matutter
Last active August 29, 2015 13:56
Show Gist options
  • Save matutter/9233337 to your computer and use it in GitHub Desktop.
Save matutter/9233337 to your computer and use it in GitHub Desktop.
a basic example of parent/child that creates zombies and a race condition
#include <stdio.h>
#include <sys/types.h>
void child(int*);
int parent(int);
void main(void)
{
pid_t pid;
int n=0, i=0;
do {
if (pid == 0)
child(&i);
else
n = parent(n);
pid = fork();
}while(n!=i);
}
void child(int* n) {
printf("KID]\tgot %d\n", *n);
printf("KID]\tsays %d+2 = %d\n", *n, *n+2);
*n+=2;
kill(getpid(),9);//child kills itself but makes a ZOMBIE!!! arr
}
int parent(int p) {
int n;
printf("PARENT] enter a # \t\t End = 0\n");
scanf("%d",&n);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment