简体   繁体   English

一次结束一个C程序的多个实例?

[英]Ending multiple instances of a C program at once?

I was wondering what the best way is to go about exiting multiple C programs at once? 我想知道最好的方法是立即退出多个C程序吗? I'm running the same C program in two different terminals (Linux) and they are communicating through an interim text file. 我在两个不同的终端(Linux)中运行相同的C程序,并且它们通过临时文本文件进行通信。 When the user ends one of them, most likely using CTRL-C, I want them both to end at once. 当用户结束其中之一(最有可能使用CTRL-C)时,我希望它们都立即结束。 I've read that signals can be used to do capture the user interrupting a program, but how will the program running in the other terminal also know when to end? 我已经读过信号可以用来捕获用户中断的程序,但是在另一个终端中运行的程序又如何知道何时结束?

A conceptual explanation is plenty, or a nudge in the right direction is all I need. 我需要一个概念上的解释就足够了,或者在正确的方向上轻推一下。 Thank you! 谢谢!

Probably you are using threads to receive external connections from one or more remote access. 可能您正在使用线程从一个或多个远程访问接收外部连接。 Your system have to check a common point that we can call System Status (normally we use a separated thread to maintain the system status, information like number of remote users, and other procedures can be under constant validation by this thread), anyway, once you have a thread that are handling a remote user connection this thread can request information from a secondary thread (System Status), once any thread request to System Status Thread to kill the system, this start to disconnect all users by killing each connection thread. 您的系统必须检查一个可以调用系统状态的公共点(通常,我们使用一个单独的线程来维护系统状态,该远程线程的数量等信息以及其他过程可以通过此线程进行不断验证),无论如何,一次您有一个正在处理远程用户连接的线程,该线程可以从辅助线程(系统状态)请求信息,一旦向系统状态线程发出的任何线程请求杀死系统,这就会通过终止每个连接线程来断开所有用户的连接。

I'm not sure if I was clear, anyway, if you need more explanation please let me know a little bit more about your program. 无论如何,我不确定是否很清楚,如果您需要更多说明,请告诉我有关您程序的更多信息。

BR, Henrique Zenoni Machado BR,Henrique Zenoni Machado

Pipes or sockets 管道或插座

What I'd suggest is the “right” thing to do, here, is to stop “communicating” through a text file, and rather, use a pipe, FIFO file, or UNIX-domain socket connection. 我建议做的“正确”的事情是在此处停止通过文本文件的“通信”,而是使用管道,FIFO文件或UNIX域套接字连接。 Check out any “intro to POSIX programming” book in the chapters on “inter-process communications” (IPC), where there should be at least one example of doing that with a pipe, FIFO file, or socket; 查阅“进程间通信”(IPC)章节中的任何“ POSIX编程入门”书,其中至少应有一个使用管道,FIFO文件或套接字进行此操作的示例; the manual page on pipe might give you a head start, as well. pipe上的手册页也可能会带您抢先一步。 Then, you can detect a disconnection by the other process and react accordingly. 然后,您可以检测到其他进程断开连接并做出相应的反应。

Signal Handling 信号处理

Alternatively, you could catch the SIGINT (Signal: Interrupted) in each process, and write some flag to the text file to tell the other end to terminate in your signal handler. 或者,您可以在每个进程中捕获SIGINT(信号:中断),然后将一些标志写入文本文件以告知另一端在信号处理程序中终止。 Since you're working in C, you can refer directly to the manual for signal for the gory details of registering signal handlers. 由于您使用的是C语言,因此可以直接参考signal手册以获取有关注册信号处理程序的详细信息。

Parent/Child 父母/子女

If one of the processes is the parent of the other, you can also detect the death of a child process via SIGCHLD (Signal: Child Terminated), and use one of the wait system calls (eg, waitpid ) to reap it before terminating yourself. 如果其中一个进程是另一个进程的父进程,则还可以通过SIGCHLD(信号:终止子进程)检测子进程的死亡,并在终止自己之前使用wait系统调用之一(例如, waitpid )来获得该进程。 The parent's death is harder to detect, although the child can monitor its own parent process ID to determine if it has been orphaned. 尽管孩子可以监视自己的父进程ID以确定它是否已成为孤儿,但更难检测到父的死亡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM