Skip to content

Instantly share code, notes, and snippets.

@zhuowei
Created March 19, 2014 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhuowei/9653033 to your computer and use it in GitHub Desktop.
Save zhuowei/9653033 to your computer and use it in GitHub Desktop.
Use brute force and ignorance to run a program with a specified PID
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int targetPid = atoi(argv[1]);
for(;;) {
int newPid = fork();
if (newPid == 0) { //child
newPid = getpid();
if (newPid == targetPid) {
execv(argv[2], argv + 3);
} else {
exit(0);
}
} else {
if (newPid == targetPid) {
exit(0);
} else {
wait(NULL);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment