Hi, I am struggling with this program in C language. Create a program that creat
ID: 3696079 • Letter: H
Question
Hi, I am struggling with this program in C language.
Create a program that
creates a child process and the child process prints when it is created
and then goes into a infinite loop to wait for a signal. In the main function for the parent process,
list several signals the the user can choose from to send to the child process, get the number for
the signal from the user, and send it to the child process. When the child process receives a
signal, print out that it has and which signal it received. Have the parent process wait on the
child to process to finish and then print out "All done!".
Sample Runs:
Child process created!
Enter the number for the signal you want to send.
SIGHUP 1
SIGINT 2
SIGFPE 8
SIGSEGV 11
SIGTERM 15
Number:
1
Child process received signal: SIGHUP
All done!
Child process created!
Enter the number for the signal you want to send.
SIGHUP 1
SIGINT 2
SIGFPE 8
SIGSEGV 11
SIGTERM 15
Number:
8
Child process received signal: SIGFPE
All done!
Explanation / Answer
Ans;
#include "executor.h"
int exec(std::string path) {
pid_t PID = 0;
int result = 0, filedes[2], status;
/* Create child process: */
PID = fork();
if (PID == -1) {
return 1;
}
result = pipe(filedes);
if (PID != 0) {
std::cout << "Created child process. PID: " << PID << " ";
}
if (result == -1) {
return 1;
}
if (PID == 0) { // This is the child process
std::string prog = "./" + path;
execvp(prog.c_str(), NULL); // Execute the program
} else { // This is the parent process
waitpid(PID, &status, 0); // Wait for the child process to return.
std::cout << "Process returned " << WEXITSTATUS(status) << ". ";
std::cout << "Press enter. ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.