Skip to content

Instantly share code, notes, and snippets.

@RyosukeMiyahara
Created July 21, 2014 05:45
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 RyosukeMiyahara/da1daaa9aa5e66cb6092 to your computer and use it in GitHub Desktop.
Save RyosukeMiyahara/da1daaa9aa5e66cb6092 to your computer and use it in GitHub Desktop.
system() VS popen()
#include <iostream> // cout, endl;
#include <stdio.h> // popen, pclose
int main(void) {
std::cout << "Caller starts" << std::endl;
FILE *fp = popen("./writeTest", "r");
pclose(fp);
std::cout << "Caller ends" << std::endl;
return(0);
}
#include <iostream> // cout, endl;
#include <stdlib.h> // system
int main(void) {
std::cout << "Caller starts" << std::endl;
system("./writeTest");
std::cout << "Caller ends" << std::endl;
return(0);
}
#include <iostream> // cout, endl
#include <stdio.h> // fopen, fputs, fclose
#include <unistd.h> // sleep
int main(void) {
std::cout << "Callee starts" << std::endl;
sleep(5);
FILE *fp = fopen("test.txt", "w");
fputs("test\n", fp);
fclose(fp);
std::cout << "Callee ends" << std::endl;
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment