简体   繁体   English

在C中留下无限循环

[英]Leaving an infinite while loop in C

I have a loop that constantly reads from my serial port. 我有一个循环,不断从我的串口读取。 The loop is infinite, so the only way I know of stopping the program is by using Ctrl+C . 循环是无限的,所以我知道停止程序的唯一方法是使用Ctrl+C The problem with this solution, which I suspect is causing other problems as well, is that when I use Ctrl+C , my program ends abruptly. 这个解决方案的问题,我怀疑也会导致其他问题,当我使用Ctrl+C ,我的程序突然结束。 At the end of the program, I close the connection to the file descriptor of the serial port I am accessing, but I don't think my program ever reaches that because of me using the Ctrl+C command, which just stops the program exactly where it is. 在程序结束时,我关闭了与我正在访问的串口的文件描述符的连接,但我认为我的程序没有达到那个因为我使用Ctrl+C命令,这只是完全停止程序在哪儿。

Is there a way for me to create the infinite while loop, and exit when I want, but at the same time, maintain the capability to execute the code beneath it? 有没有办法让我创建无限的while循环,并在我想要的时候退出,但同时,保持执行它下面的代码的能力?

Try this and see what happens: 试试这个,看看会发生什么:

#include <unistd.h>
#include <stdio.h>
#include <signal.h>

volatile sig_atomic_t stop;

void
inthand(int signum)
{
    stop = 1;
}

int
main(int argc, char **argv)
{
    signal(SIGINT, inthand);

    while (!stop)
        pause();
    printf("exiting safely\n");

    return 0;
}

Ctrl-C sends a signal (SIGINT) to your process. Ctrl-C向您的进程发送信号(SIGINT)。 The default action for the process is to exit when it gets it, but you can catch the signal instead and handle it gracefully. 该过程的默认操作是在获取它时退出,但您可以捕获信号并优雅地处理它。

You could do something like this: 你可以这样做:

sig_atomic_t volatile g_running = TRUE;

void sig_handler(int signum)
{
  if (signum == SIGINT)
    g_running = FALSE;
}

int main()
{
  signal(SIGINT, &sig_handler);
  while (g_running)
  {
    //your code
  }
  //cleanup code
}

This will catch the SIGINT signal generated by pressing CTRL-C and break the loop by setting g_running to FALSE . 这将捕获按CTRL-C生成的SIGINT信号,并通过将g_running设置为FALSE来中断循环。 Your cleanup code is then executed 然后执行清理代码

Use a variable that controls the while loop, eg while(running) . 使用控制while循环的变量,例如while(running) Just set this variable asynchronously to false to exit the loop. 只需将此变量异步设置为false即可退出循环。

Example: 例:

volatile int running = 1;
while(running) {
    /* Your code */
}

So another code, let's say a callback function, does this 所以另一个代码,比如一个回调函数,就是这样做的

running = 0;

You can set this callback to intercept SIG_TERM (which is Ctrl-C by default) or any signal of your choice (except SIG_KILL which is not sent to the process). 您可以将此回调设置为拦截SIG_TERM(默认情况下为Ctrl-C)或您选择的任何信号(SIG_KILL除外,它不会发送到进程)。

Instead of the infinite while loop, create a while loop which listens/reads to boolean value in a particular file. 而不是无限的while循环,创建一个while循环,它监听/读取特定文件中的boolean值。 You can edit the file to quit the loop. 您可以编辑该文件以退出循环。

Using ctrl + C actually interrupts your program. 使用ctrl + C实际上会中断您的程序。 To avoid that 为了避免这种情况

1) Use a variable and check its state in every iteration of loop. 1)使用变量并在每次循环迭代中检查其状态。

int  shouldExit  = 0;

----
----
---
while (your_condition){
  if(shouldExit == 1) {
     break;
  }

  // Your loop logic here
}

Then assynchronously set the variable 然后异步设置变量

shouldExit = 1 

when you want to exit. 当你想退出。

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

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