在C中留下一个无限的while循环

时间:2022-09-06 17:10:53

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. 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. 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。这个解决方案的问题是,当我使用Ctrl+C时,我的程序突然终止。在程序结束时,我关闭了与正在访问的串行端口的文件描述符的连接,但我认为我的程序从来没有达到这个连接,因为我使用了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循环,并在我需要时退出,但同时保持执行下面代码的能力?

5 个解决方案

#1


10  

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. The default action for the process is to exit when it gets it, but you can catch the signal instead and handle it gracefully.

Ctrl-C向您的进程发送一个信号(SIGINT)。进程的默认操作是在它获得信号时退出,但是您可以捕获信号并优雅地处理它。

#2


6  

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. Your cleanup code is then executed

这将捕获按CTRL-C生成的SIGINT信号,并通过将g_running设置为FALSE来中断循环。然后执行清理代码

#3


0  

Instead of the infinite while loop, create a while loop which listens/reads to boolean value in a particular file. You can edit the file to quit the loop.

创建一个while循环来监听/读取特定文件中的布尔值,而不是无限的while循环。您可以编辑该文件以退出循环。

#4


0  

Using ctrl + C actually interrupts your program. To avoid that

使用ctrl + C实际上会中断程序。为了避免这种

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.

当你想要退出时。

#5


0  

Use a variable that controls the while loop, e.g. while(running). Just set this variable asynchronously to false to exit the loop.

使用一个变量来控制while循环,例如while(运行)。只需将该变量异步设置为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)。

#1


10  

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. The default action for the process is to exit when it gets it, but you can catch the signal instead and handle it gracefully.

Ctrl-C向您的进程发送一个信号(SIGINT)。进程的默认操作是在它获得信号时退出,但是您可以捕获信号并优雅地处理它。

#2


6  

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. Your cleanup code is then executed

这将捕获按CTRL-C生成的SIGINT信号,并通过将g_running设置为FALSE来中断循环。然后执行清理代码

#3


0  

Instead of the infinite while loop, create a while loop which listens/reads to boolean value in a particular file. You can edit the file to quit the loop.

创建一个while循环来监听/读取特定文件中的布尔值,而不是无限的while循环。您可以编辑该文件以退出循环。

#4


0  

Using ctrl + C actually interrupts your program. To avoid that

使用ctrl + C实际上会中断程序。为了避免这种

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.

当你想要退出时。

#5


0  

Use a variable that controls the while loop, e.g. while(running). Just set this variable asynchronously to false to exit the loop.

使用一个变量来控制while循环,例如while(运行)。只需将该变量异步设置为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)。