This question is exactly the opposite of Non-blocking version of system()
I want to substitute the current process by another one (not create another one).
I want to start (for instance) notepad
, but in a blocking way (I don't want to get the prompt until notepad
is closed.
In the Windows shell I just do
cmd /c notepad
(notepad
auto-detachs from prompt if not prefixed by cmd /c
)
In C, using system
I just do
system("notepad");
But this is forking behind the scenes. I want to replace the current process by notepad
, and I want it to be blocking.
I have tried:
#include <stdio.h>
#include <unistd.h>
int main(int argc,char **argv)
{
char * const args[] = {"cmd","/c","notepad",NULL};
execvp(args[0],args);
}
notepad
runs, but the console returns to the prompt immediately.(non-blocking). I want it to block, and I don't want to use fork
as it would create another process: I want to replace the current process.
(I have tried with a custom blocking executable, and it doesn't block either. So cmd /c notepad
example is as good as any other one)
So, If I run this executable I just built from a parent process:
- I don't want the process to return to the parent process until the
notepad
process exits - I want to be able to get output from the command (
notepad
isn't a good example in that case) - I don't want to create an extra process (this is part of a massive multiprocessing application, we can't afford to create 2 processes, we need to keep 1 process for each run)
Is it even possible?