-
-
Save nathanwiegand/ca4777ac4a5e6d58b1e3 to your computer and use it in GitHub Desktop.
This file contains 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 <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