正确的方法关机C应用程序,以确保关键段完成? [英] Correct way to shutdown C application to ensure CRITICAL sections completed?

查看:112
本文介绍了正确的方法关机C应用程序,以确保关键段完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个定义的关闭挂钩处理线程的C应用程序。

I have a shutdown hook handler defined in a SINGLE threaded C application.

int main(int argc, char * argv[]){
    //Shutdown hook for CTRL-C
    (void) signal(SIGINT, shutdownHook);
    ...
    ...
}

因此​​,当用户点击CTRL-C关闭钩启动...

So when the user hits CTRL-C the shutdown hook is initiated...

void shutdownHook(int sig){
    rc = wmq_sender_stop(errorStr);
    if (rc != 0){
        printf("\n%s\n", errorStr);
    }
    while(transactionRunning == TRUE){
        sleep(1);
        printf("Transaction still running\n");
    }
    ....
    ....
}

您可以在上面看到,我称之为wmq_se​​nder_stop程序(在共享库),基本上设置一个变量设置为false,结束在共享库的循环。

You can see above that I call a "wmq_sender_stop" routine (in a shared lib) that essentially sets a variable to FALSE to end a loop in that shared lib.

int wmq_sender_stop(char errorStr[ERROR_STR_LEN]){
       running = FALSE;
       ...
}

和变量跑将停止(希望)在共享库运行主循环。

And that variable "running" will stop (hopefully) the main loop running in that shared lib.

while(running == TRUE){
...
...
}

我遇到的问题是在我的Linux系统中所有的工程100%...但是,当我一个很大的功能强大的服务器上安装应用程序,它调用wmq_se​​nder_stop并设置可变细,但似乎该应用程序在服务器上运行的太快,让,而(运行== TRUE)变量退出.....或者是简单地返回到主应用程序速度过快,虽然(transactionRunning == TRUE)循环刚连续运行...

The problem I am having is on my Linux box all works 100%...but when I install the application on a BIG powerful server it calls the "wmq_sender_stop" and sets the variable fine, but it seems like the application is running too fast on the server to allow the "while(running == TRUE)" variable to exit.....OR it is simply returning to the main application too fast and the "while(transactionRunning == TRUE)" loop just continuously runs...

基本上,大型服务器上,如果我打CRTL-C以下是时间可持续输出到屏幕:

Essentially on the BIG server if I hit CRTL-C the following is continuosly output to the screen:

Transaction still running
Transaction still running
Transaction still running
Transaction still running
Transaction still running
Transaction still running
...

也许,我需要在这里提出的线程,并让它们之间的互通,但他们没有更简单的方法,以优雅的允许结束它的临界code /交易关停即使在极其前的在共享库循环快速的服务器?为什么它在我的Linux笔记本电脑的工作吗?

Perhaps I need to make threads here and have intercommunication between them but is their no easier way to elegantly allow a "loop in a shared lib" to end it's "critical code / transactions" before shutting down even on extremely fast servers? And why does it work on my linux laptop?

如果我需要使用线程什么是他们的comminucate最好最快的方法是什么?目前我使用回调....但我不知道最好的IPC是什么主题?

And if I need to use threads what is the best and FASTEST way for them to comminucate? Currently I am using CALLBACKS....but am not sure what the best IPC is for threads?

感谢您的帮助,很多AP preciated

Thanks for the help, much appreciated

林顿

推荐答案

确保运行(和 transactionRunning ,太)被声明为挥发性。否则,C编译器可能在某些情况下缓存它的价值。现在,想起挥发性的意思是这个值可以通过信号处理程序得到改变。

Make sure that running (and transactionRunning, too) is declared as volatile. Otherwise, the C compiler may cache its value under some circumstances. For now, think of volatile as meaning "this value could get changed by a signal handler".

请注意,你必须要小心code是这样的:

Note that you have to be careful with code like this:

rc = wmq_sender_stop(errorStr);

如果你这样做,在信号处理,你最好希望,这一功能是安全地从信号处理程序调用。大部分功能都没有。 (偶函数是安全的多线程是不安全的,从信号处理程序调用呼叫。)

If you do that in a signal handler, you had better hope that that function is safe to call from a signal handler. Most functions aren't. (Even functions that are safe to call from multiple threads are unsafe to call from a signal handler.)

最后一个问题是,如果你的code是单线程的,但你永远不从信号处理程序返回,怎么你的主循环应该退出?信号处理应用程序中的现有一个线程运行。通常你想在一个信号处理程序做的唯一的事情就是改变标记挥发性,然后返回一个全局变量的值。

The last issue is that if your code is single-threaded, but you never return from your signal handler, how is your main loop supposed to exit? The signal handler runs in one of your application's existing threads. Generally the only thing you want to do in a signal handler is change the value of a global variable that is marked volatile and then return.

这篇关于正确的方法关机C应用程序,以确保关键段完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆