Skip to content

Instantly share code, notes, and snippets.

@esin
Created March 8, 2014 17:05
Show Gist options
  • Save esin/9435078 to your computer and use it in GitHub Desktop.
Save esin/9435078 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void main( int argc, char *argv[ ] )
{
if ( argc != 3 ) exit( 1 );
char *command = argv[ 1 ];
int iwp = atoi( argv[ 2 ] );
if ( iwp > 32767 || iwp < 300) exit( 1 );
if( access( "stop", F_OK) != -1 ) remove( "stop" );
if (getpid() == iwp) {
int th = system( "true" );
}
pid_t p;
while ( 1 ) {
if( access( "stop", F_OK ) != -1 ) exit( EXIT_SUCCESS );
if(fork() == 0) {
p = fork();
if(p == 0) exit( EXIT_FAILURE );
if (p == iwp - 2) {
FILE * F = fopen ( "stop","w" );
if ( F != NULL ) fclose ( F );
int th = system( command );
exit( EXIT_SUCCESS );
}
exit( EXIT_FAILURE );
}
wait( NULL );
}
if( access( "stop", F_OK) != -1 ) remove( "stop" );
exit( 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment