I was hoping if someone could help me understand how to make it so that the pipe PUBLIC is automatically removed when the server gets killed through Ctrl-C. I believe this involves using an interrupt handler but not sure how to do this.
The client is described first (in the article) followed by the server. When ready to launch, I first start the server in the background by using ./server & followed by running the client. The CMD prompt allows the user to type anything and it goes through the server. As of now, when Ctrl-C is used it kills the client but the server is still running with the PUBLIC pipe. Any help would be greatly appreciated.
You have to register signal handler function using POSIX api.
Another possibility is to use RAII
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class pipe {
char* filename;
public:
pipe(constchar* filename) : filename(filename)
{
// create pipe here
}
~pipe()
{
// call file remove function for this->filename;
}
};
int main(void)
{
pipe public_pipe("/path/to/PUBLIC");
return run_program();
}
Now when program exists public_pipe variable is destroyed and destructor handles removal of the pipe. Of course that pipe class is far from functional and you need to add a way for your program to have access to that pipe.