Interaction with the application while working in C

I have been looking for the answer to this question for a while. I wrote a small, lightweight server and it works just fine. No problem; however, there are some things that I would like to do with the application while it really works. For example: server stopto explicitly stop the server.

Perhaps I should look at creating a daemon for my server? The platform on which it runs is Linux, and I have no intention of porting to platforms other than POSIX, so Windows is not appropriate here.

But yes, to summarize my question: how to get my application to receive “arguments” or “tasks” from the outside (for example, on the command line or in another application)

Welcomes in advance, since I know that StackOverflow is always ready with a solid quick response,

/ Jesse

+3
source share
4 answers

There are many options. One popular solution is to use POSIX signals . Basically you install a signal handler and respond to signals.

If this is too simplistic (because, for example, you need to accept parameters), you can also let your daemon open a socket where it listens for commands. Then you send a small helper program to send commands to the socket. You can also do something similar using a named pipe.

, , Inter-process communication (IPC). .

+3
+1

POSIX . , SIGUSR1, - . :

kill -SIGUSR1 $pid

$pid - pid . , . , , , .

-. - 8080, , ,

http://localhost:8080/

. .

+1

, :

  • Use signal handlers (3). To do this, you need to do something. Command kill(1) will transmit a signal from the outside.
  • Track the file for changes and verify that there, do something depending on what is there
  • Use the IPC protocol, which allows other applications to talk to you. A wide topic widely discussed on the Internet.
  • Make this a client-server architecture in which commands can be sent over TCP / UDP.
+1
source

All Articles