I just can't stop the daemon from working, which I’m trying to do with the stop parameter. Tell me how best to do this? And why exit(0) does not help?

  int main(int argc, char* argv[]) { pid_t pid, sid; pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } umask(0); sid = setsid(); if (sid < 0) { exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { exit(EXIT_FAILURE); } //..................... //..................... //..................... //..................... if(strcmp(argv[i], "stop") == 0) { cout << "Packets are not sniffed.\n" << endl; exit(0); } return 0; } 

3 answers 3

It's not even a tipi signal that you send, but that you do not understand how it all works ...

Here is what you write:

 pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } 

Those. Your program ends when pid <0 and then when pid> 0. Only one value remains - zero. But this means that we are in a PARTICIPATION process! As a result, the team

 kill(pid, SIGCONT); 

sends a signal to the process with pid == 0. What are you talking about ?! If your child process wants to do a hari-kiri, then this is done using exit (0);

If you want to kill the child from the parent, then you need to send a signal in the branch, where pid> 0. And the signal can be, well, at least SIGINT ... It's the same thing, press Ctrl / C.

I answer the request in the comments: "answer the literal question in the title."

There is only ONE way to kill the process - to send him a signal that cannot be processed. A process (any) cannot process a signal if it has not created a handler for this signal, or this signal is not processed in the printer (-9).

In order to send a signal to a process, you need to know its PID. And here are two possible options: 1) You know his PID because you created it yourself using the fork operation. 2) This process is a properly written daemon and it honestly put its pid file in the / var / run directory, in which its pid is written. In this case, the file name must match the name of the running daemon and is easy to understand.

Thus, your child must write its pid to a pid file in the / ar / run directory. And your process that kills him must read this file, take a pid from it and call kill () with this pid.

Look like that's it...

  • I agree that in many ways I have not figured it out. but I understand that with each new program call I end the parent process and start a new one. so I need it to work until I start the program from the stop command again. here I expect to close the parent if (pid > 0) { exit(EXIT_SUCCESS); } (pid > 0) { exit(EXIT_SUCCESS); } and child (via kill) processes. but if we replace kill with exit , the process will work all the same. - Vova Polischuck
  • Ie, here I did it in such a way that each child process should be called with some kind of command. - Vova Polischuck
  • Could you for people who are from the search engine, thanks to the title, find this question (the main audience of Stack Overflow), answer the literal question in the title or correct the title of the question so that it matches the problem in the body of the question (trying not to change the essence of the question, so that people with similar misconceptions as the author could still find this question). I would have done it myself, but I can’t come up with a brief wording for the title. - jfs

Let's start with the fact that you do not understand the essence of the processes. If you run the program again, a completely new process is generated, and of course, exit will cause the completion of this new process, and not the one that is running as a daemon. As a rule, in programs that support daemon mode, this is done as follows: The PID of the daemon process is stored somewhere in a public place (/ var / run, / tmp), and the process started with the --stop option reads the PID of the daemon process, and kill it with this PID value.

  • I agree. I did not understand. I had no time. Therefore he asked for help. - Vova Polischuck

SIGCONT cannot help in this case, because this signal continues the execution of the stopped process, and if the process is already running, this signal is ignored.