Skip to content

Instantly share code, notes, and snippets.

@nathanwiegand
Created February 22, 2010 16:36
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 nathanwiegand/ca4777ac4a5e6d58b1e3 to your computer and use it in GitHub Desktop.
Save nathanwiegand/ca4777ac4a5e6d58b1e3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
void exit(int);
int main() {
int pipeFD[2];
unsigned int reader, writer, i;
FILE *outputFile = fopen("tmpoutput", "w");
pipe(pipeFD);
reader = pipeFD[0]; writer = pipeFD[1];
for(i = 1; ; i++) {
if(i > 5) {
unsigned int outputFD = fileno(outputFile);
if(fork()) {
/* I am the parent. */
char readBuf[20] = {0};
close(writer);
sprintf(readBuf, "%d", reader);
execl("./newbinary", "--hotswapping", readBuf, (char*)0);
exit(0);
} else {
/* I am the child.*/
FILE *outputStream = fdopen(writer, "w");
close(reader);
fprintf(outputStream, "%d\n", i);
fprintf(outputStream, "%u\n", (unsigned int) outputFD);
fclose(outputStream);
exit(0);
}
}
fprintf(outputFile, "Original:\t%d My PID=%d\n", i, getpid());
fflush(outputFile);
printf("Original:\t%d My PID=%d\n", i, getpid());
fflush(stdout);
sleep(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment