Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include namespace cse 4 7 3 3 { process::process ( std::shared _ ptr systemCalls, std::string path, const std::vector &args ) : processId ( -
#include
#include
namespace cse
process::processstd::sharedptr systemCalls,
std::string path,
const std::vector &args
: processId
executablePathstd::movepath
sysCallsstd::movesystemCalls
errorMessagenone
for const auto &arg : args
arguments.pushbackconstcastargcstr;
arguments.pushbacknullptr;
auto process::execute bool
TODO:
Call the system fork function to create a child process
IF the fork fails, display an error message and return false
IF the fork succeeds, check if this is the child process or the parent process
IF the child:
Execute the child process logic
Return false to indicate that this is the child process
ELSE the parent:
Execute the parent process logic
Return true to indicate that this is the parent process and the child process was created successfully
auto pid sysCallsFork;
bool result false;
if pid.hasvalue
errorMessage "Failed to create child process.
;
return result;
if pidvalue
executeChildProcess;
else
executeParentProcesspidvalue;
result true;
return result;
void process::executeChildProcess
TODO:
Print the child process ID
Print the parent process ID
Execute the given program with the provided arguments
Should Execvp return if it does? then print an error message and exit
void process::executeParentProcesspidt pid
TODO:
Store the child process ID
Print the parent process ID
Print the child process ID
void process::handleChildExitStatusint status, pidt pid
TODO:
Check if the child process terminated normally
IF the child process terminated normally
Print a message with the child process ID with the exit status.
ELSE IF the child process did not terminate normally
Print an error message with the child process ID
auto process::wait std::optional
Declare a variable to hold the status of the child process
Use the Waitpid system call to wait for the child process to change state eg to terminate
Check if the Waitpid system call succeeded
If the Waitpid system call failed, print an error message and return std::nullopt
Else if the Waitpid system call succeeded, handle the child's exit status
Return the exit status of the child process
return std::nullopt;
auto process::getid const pidt
return thisprocessId;
auto process::getErrorMessage const std::string
return errorMessage;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started