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

/* @file Processes.cpp @author student name, student@uncc.edu @author student na

ID: 3744103 • Letter: #

Question


/*
@file Processes.cpp
@author student name, student@uncc.edu
@author student name, student@uncc.edu
@author student name, student@uncc.edu
@description: <ADD DESCRIPTION>
@course: ITSC 3146
@assignment: in-class activity [n]
*/


#ifndef Processes_cpp
#define Processes_cpp

#include "Processes.h"


using namespace std;


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

return process_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)
{
// 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 */

Part 2:

Modify the createNewProcess() function in the file named Processes.cpp as follows:

The child process must print the message I am a child process!

The child process must then return a string with the message I am bored of my parent, switching programs now

The parent process must print the message I just became a parent!

The parent process must then wait until the child process terminates, at which point it must return a string with the message My child process just terminated! and then terminate itself. Hint: search for “wait for process” in the “System Calls” section of the Linux manual.

IMPORTANT:
Do NOT type the messages, copy-paste them from here. Your output must be exact.

Explanation / Answer

/*

@file Processes.cpp

@author student name, student@uncc.edu

@author student name, student@uncc.edu

@author student name, student@uncc.edu

@description: <ADD DESCRIPTION>

@course: ITSC 3146

@assignment: in-class activity [n]

*/

#ifndef Processes_cpp

#define Processes_cpp 1

#include<stdio.h>

#include<unistd.h>

#include <stdlib.h>

#include<string.h>

#include <sys/types.h>

#include<iostream>

#include<sys/wait.h>

using namespace std;

class Process

{

public:

Process()

{

}

pid_t getProcessID(void)

{

int process_id = getpid();

return process_id;

}

// Driver code

char* createNewProcess()

{

pid_t process_id;

int status;

char* str = (char*)malloc(1000 * sizeof(char));

pid_t id = fork();

process_id = id;

if(id == -1)

{

stpcpy(str,"Error creating process");

return str;

}

else if (id == 0)

{

printf("I am a child process!");

stpcpy(str,"I am bored of my parent, switching programs now");

return str;

}

else

{

printf(" I just became a parent!");

waitpid(-1, &status, 0);

stpcpy(str,"My child process just terminated! and then terminate itself.");

return str;

}

}

};

int main()

{

class Process P;

char *temp=P.createNewProcess();

printf("%s",temp);

return 0;

}

#endif