C++ and Linux System Calls /* @file Processes.cpp @author student name, student@
ID: 3744100 • Letter: C
Question
C++ and Linux System Calls
/*
@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: Working With Multiple Processes (10 points)
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.
Part 3: Working With External Commands (15 points)
Modify the replaceProcess() function in the file named Processes.cpp as follows:
The parent process will use the fork() function to create a child process. This step has been done for you.
The child process must then change its memory image to a different program by using the execvp system call (http://linux.die.net/man/3/execvp). The parameter args that has been passed to the replaceProcess() function is the array of parameters to be passed to execvp, telling it which program to execute and what parameters to pass to that program.
For example, the test code provided to you executes the “ls” (directory list) program with the parameter “-al” by setting the args array as follows:
char * args[3] = {(char * )"ls", (char * )"-al", NULL};
IMPORTANT:
Although the test code executes the “ls” program, we must be able to change the command that execvp executes. So, DO NOT hardcode the command to be passed to execvp. Simply use the args array provided.
Finally, in the parent process, you must make sure to invoke the necessary system call to wait for the child process to terminate. Once the child terminates, exit the program.
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
#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)
{
int status = 0;
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
printf("I am a child process!");
return "I am bored of my parent, switching programs now";
}
else
{
// TODO: Add your code here
printf("I just became a parent!");
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();
int status = 0;
// TODO: Add your code here
if (id == 0)
{
execvp(*args, args);
}
else
{
wait(&status);
exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.