Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Help For the following C++ Code Do not edit the follwing codes: #include <st

ID: 3879195 • Letter: C

Question

C++ Help

For the following C++ Code Do not edit the follwing codes:

#include <stdio.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#include <fcntl.h>
#include "Processes.h"

using namespace std;

pid_t process_id;

/*
* The starting point of the program.
*/
int main(void)
{
// Call the user-defined function that retrieves the process ID
cout << "Part 1: Working With Process IDs" << endl;
cout << "Process ID: " << getProcessID() << endl;
cout << endl << endl;
// Call the user-defined function that creates a new process
cout << "Part 2: Working With Multiple Processes" << endl;
cout << createNewProcess() << endl;
cout << endl << endl;

// Call the function that call an external OS command
if (process_id != 0)
{
cout << "Part 3: Working With External Commands" << endl;
char * args[3] = {(char * )"ls", (char * )"-l", NULL};
replaceProcess(args);  
cout << endl;
}

return 0;

}

And This Code:

// DO NOT MAKE ANY CHANGES IN THIS FILE

#ifndef Processes_h
#define Processes_h

#include <stdio.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#include <fcntl.h>


using namespace std;


extern pid_t process_id;

pid_t getProcessID(void);
string createNewProcess(void);
void replaceProcess(char * args[]);


#endif /* Processes_h */

Edit the following code to get expected outcome:

#ifndef Processes_cpp
#define Processes_cpp

#include "Processes.h"


using namespace std;


// Part 1: Working With Process IDs
pid_t getProcessID(void)
{
// TODO: Add your code here

return -1;
}

// Part 2: Working With Multiple Processes
string createNewProcess(void)
{
pid_t id = fork();
// DO NOT CHANGE THIS LINE OF CODE
process_id = id;
if(id == -1)
{
return "Error creating process";
}
else if (id == 0)
{
// TODO: Add your code here

return "";

}
else
{
// TODO: Add your code here

return "";
}
}

// Part 3: Working With External Commands"

void replaceProcess(char * args[])
{
// Spawn a process to execute the user's command.
pid_t id = fork();
// TODO: Add your code here
}

#endif /* TestProg_cpp */

1. Modify the getProcessID() function in the file named Processes.cpp a. The function must find and store the process's own process id b. The function must return the process id to the calling program. Hint: search for "process id" in the "System Calls" section of the Linux manual.

Explanation / Answer

main.cpp
--------------------------------------------------
#include <stdio.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#include <fcntl.h>
#include "Processes.h"


using namespace std;


pid_t process_id;


/*
* The starting point of the program.
*/
int main(void)
{
   // Call the user-defined function that retrieves the process ID
   cout << "Part 1: Working With Process IDs" << endl;
   cout << "Process ID: " << getProcessID() << endl;
   cout << endl << endl;


   // Call the user-defined function that creates a new process
   cout << "Part 2: Working With Multiple Processes" << endl;
   cout << createNewProcess() << endl;
   cout << endl << endl;

   // Call the function that call an external OS command
   if (process_id != 0)
   {
      cout << "Part 3: Working With External Commands" << endl;
      char * args[3] = {(char * )"ls", (char * )"-l", NULL};
    
      replaceProcess(args);
    
      cout << endl;
   }


   return 0;
}
----------------------------------------------------------------------------------------
Processes.cpp
------------------------------
#ifndef Processes_cpp
#define Processes_cpp

#include "Processes.h"


using namespace std;


// Part 1: Working With Process IDs
pid_t getProcessID(void)
{
pid_t id= getpid();

   return id;
}


// Part 2: Working With Multiple Processes
string createNewProcess(void)
{
   pid_t id = fork();

   // DO NOT CHANGE THIS LINE OF CODE
   process_id = id;


   if(id == -1)
   {
      return "Error creating process";
   }
   else if (id == 0)
   {
      cout << "I am a child process! ";

      return "I am bored of my parent, switching programs now";
   }
   else
   {
      cout << "I just became a parent! ";
      int status=0;
         wait(&status);
    
      return "My child process just terminated!";
   }
}


// Part 3: Working With External Commands"
void replaceProcess(char * args[])
{
   // Spawn a process to execute the user's command.
   pid_t id = fork();


   execvp(args[0], args);


}

#endif /* TestProg_cpp */
---------------------------------------------------------------------------------
Processes.h
-----------------------------------
#ifndef Processes_h
#define Processes_h

#include <stdio.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#include <fcntl.h>


using namespace std;


extern pid_t process_id;

pid_t getProcessID(void);
string createNewProcess(void);
void replaceProcess(char * args[]);


#endif /* Processes_h */