Last active
August 29, 2015 13:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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