I’m working on an assignment that requires me to create a child process then wait for 300 seconds and kill that process, while the parent process should be killed 200 seconds after creating the child process. I’m doing this in C++ in Ubuntu using the Clang++ compiler. I’m very new to C++ and have been using Java for a while. What I have now is probably more like psuedocode than anything else, I really doubt it works. When I compile it gives me errors regarding my kill() calls, I’ve tried things like *this.kill() or this.kill() and neither have worked. How do use the kill command? Also, does this code look like it will do what I want? I’m afraid it is not even close.
#include <iostream>
#include <unistd.h>
using namespace std;
class process{
public:
process(){
main();
}
void main(){
process *parent = new process();
int pid;
pid=fork();
if (pid == 0)
{
sleep(200);
kill();
}
else
{
sleep(100);
kill();
}
}
};
The
killsystem call requires a couple of arguments: a process id for the process to kill, and a specific signal. There is no version of the call with no arguments, to e.g. just kill the current process.See the man page for kill, here’s a linux version of the man page.
Note that frim unistd.h you also have access to the
getpidcall that you can use to get the process id for the calling process, see man getpid.