C++ and Linux System Calls // Part 1: Working With Process IDs pid_t getProcessI
ID: 3744131 • Letter: C
Question
C++ and Linux System Calls
// 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)
{
cout<<"I am a child process!"<
return "I am bored of my parent, switching programs now";
}
else
{
cout<<"I just became a parent!"< //wait for 2 seconds
sleep(2);
//wait for child to terminate
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();
// TODO: Add your code here
}
#endif /* TestProg_cpp */
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
#include <iostream>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <unistd.h>
#include<sys/wait.h>
using namespace std;
// 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
int status;
if( id == 0) //child process
{
cout<<"Child process with pid "<<getpid()<<" executes the command : "<<args[0]<<" "<<args[1]<<endl;
execvp( args[0], args );
}
else
{
//parent waits for child process
wait(&status);
cout<<"Child process with pid "<<id<<" exited with status "<<status<<endl;
}
}
int main(int argc,char **argv)
{
char **args;
if(argc < 2)
{
cout<<"Usage : ./execname command options"<<endl;
return -1;
}
//allocate memory for **args
args=(char**)malloc(sizeof(char*)*(argc-1));
for(int i =0 ; i < argc-1;i++)
{
//allocate memory for each args string
args[i] = (char*)malloc(sizeof(char)*strlen(argv[i+1]));
strcpy(args[i],argv[i+1]);
//cout<<"command1: "<<args[i]<<endl;
}
replaceProcess(args);
return 0;
}
-------------------------------------------------
//output
Child process with pid 6 executes the command : ls -la
total 28
drwxrwxrwx 2 999 999 4096 Sep 8 01:34 .
drwxr-xr-x 11 root root 4096 Mar 11 08:49 ..
-rwxrwxrwx 1 root root 14061 Sep 8 01:34 a.out
-rwxrwxrwx 1 root root 1671 Sep 8 01:34 main.cpp
Child process with pid 6 exited with status 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.