Question: #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 cse4733
{
process::process(std::shared_ptr systemCalls,
std::string path,
const std::vector &args)
: processId(-1),
executablePath(std::move(path)),
sysCalls(std::move(systemCalls)),
errorMessage("(none)")
{
for (const auto &arg : args)
{
arguments.push_back(const_cast(arg.c_str()));
}
arguments.push_back(nullptr);
}
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 = sysCalls->Fork();
bool result = false;
if (!pid.has_value())
{
errorMessage = "Failed to create child process.
";
return result;
}
if (pid.value()==0)
{
executeChildProcess();
}
else
{
executeParentProcess(pid.value());
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::executeParentProcess(pid_t pid)
{
// TODO:
// Store the child process ID
// Print the parent process ID
// Print the child process ID
}
void process::handleChildExitStatus(int status, pid_t 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 (e.g., 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::get_id() const -> pid_t
{
return this->processId;
}
auto process::getErrorMessage() const -> std::string
{
return errorMessage;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!