I am working on custom wrappers for Unix-specific system calls now. And the last problem I met is about how to create a complete function for creating new processes with another image. And I want this function to return TRUE or FALSE. The last piece of this puzzle is how to get a result of exec*()
from a child process without waiting for it's end in case of exec*()
's success. In other words, I need to get FAIL of SUCCESS result of exec*() quickly and continue execution of a parent process.
And I don't want to use vfork()
and pipes.
My current results:
Using vfork()
and a volatile variable for keeping result made their work.
static int QCreateProcess(char* args, ...)
{
if (processInfoStruct == NULL)
{
return Q_ERROR;
}
volatile int result = TRUE;
pid_t procHandle = vfork();
if (procHandle == 0)
{
char* argsToExec[2] = { args, NULL };
execv(argsToExec[0], argsToExec);
result = FALSE;
_exit(EXIT_FAILURE);
}
else if (procHandle == -1)
{
processInfoStruct->processHandle = NULL;
result = FALSE;
}
else
{
if (result == TRUE)
{
waitpid(procHandle, NULL, WNOHANG);
processInfoStruct->processHandle = procHandle;
}
else
{
processInfoStruct->processHandle = 0;
result = FALSE;
}
}
return result;
}
This code works and returns correct results.
How can this be implemented using fork()
and waitpid()
without the status variable (it won't work with fork()
anyway...) and pipes? I tried to find solutions with different options for the last function (waitpid()
), but a desired combination was not found.
volatile
, as I understood, child does not change the status variable in the way for parent process to see changes – Viktoriia Pashchenko Feb 19 '23 at 12:58