PDA

View Full Version : [SOLVED] [C/C++] popen: Wait for Subprocess to Finish



dodle
June 14th, 2012, 06:01 AM
I'm using popen to open a subprocess, but I don't know how to get the program to wait until the process finishes to continue. I've searched the forum and googled "c popen wait" but can't find an answer. The system command works for this but I have been warned not to use it.


#include <stdio.h>

int main()
{
popen("sleep 4 && echo done! 2>&1", "r");
printf("Subprocess finished.\n");

return 0;
}

Is there a way to wait for popen to finish, or is there a better way to open a subprocess?

----- Solved -----

Ah, figured it out:


#include <stdio.h>

int main()
{
FILE* test = popen("sleep 4 && echo done! 2>&1", "r");
pclose(test);
printf("Subprocess finished.\n");

return 0;
}

However, I'm having an issue getting popen to print the output of the process to stdout.

Bachstelze
June 14th, 2012, 06:12 AM
Have you read the manual for popen()?

dodle
June 14th, 2012, 06:46 AM
Sorry Bachstelze, I think I edited my post while you were doing yours.

I found the solution that I wanted, thanks to this post (http://www.linuxquestions.org/questions/programming-9/c-c-popen-launch-process-in-specific-directory-620305/#post3053479):


#include <stdio.h>

int main()
{
FILE* test = popen("sleep 4 && echo done!", "r");
if (test == NULL)
{
printf("FAIL!\n");
return 1;
}

char buffer[1028];

while (fgets(buffer, 1028, test) != NULL)
{
printf(buffer);
}

pclose(test);
printf("Success.\n");

return 0;
}