Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Science Project Ideas In Health Care Volume 1

Authors: Zemelak Goraga

1st Edition

B0CPX2RWPF, 979-8223791072

More Books

Students also viewed these Databases questions

Question

3.What are the Importance / Role of Bank in Business?

Answered: 1 week ago

Question

Evaluate the importance of diversity in the workforce.

Answered: 1 week ago

Question

Identify the legal standards of the recruitment process.

Answered: 1 week ago