@cyysu
2017-10-24T12:40:38.000000Z
字数 3511
阅读 776
- 时间:2017年10月23日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
- 版本:4.0
- 描述:执行linux定时器时,不知因为什么原因导致程序终止。
嵌入式Linux
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "newton_type.h"
#include "ezxml.h"
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#define THREAD_NUM 2
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define __DEBUG__ //if release then note this line
#ifdef __DEBUG__
#define printg(format,...) printf(""format" ###File: "__FILE__",Line: %05d\n",##__VA_ARGS__,__LINE__)
#else
#define printg(format,...)
#endif
static void stopHandler(int sign) {
exit(-1);
}
// Newton 监视进程
void NewtonProbe() {
static int countNewton = 0;
char buf[10];
memset(buf, 0, sizeof(buf));
int fd = open("/opt/NEWTON.pid", O_RDWR);
if (fd == -1) {
perror("failed to open NEWTON.pid file");
}
if (read(fd, buf, sizeof(buf) - 1) > 0) {
if (strncmp(buf, "1", 1) == 0)
system("echo \"0\" > /opt/NEWTON.pid");
else {
if (++countNewton > 2) {
countNewton = 0;
printf("newton process is dead\n");
system("bash startall.sh ./newtonconfig-A.xml");
}
}
}
close(fd);
}
// Mqtt 监视进程
void MqttProbe() {
static int countMqtt = 0;
char buf[10];
memset(buf, 0, sizeof(buf));
int fd = open("/opt/MQTT.pid", O_RDWR);
if (fd == -1) {
perror("failed to open MQTT.pid file");
}
if (read(fd, buf, sizeof(buf) - 1) > 0) {
if (strncmp(buf, "1", 1) == 0)
system("echo \"0\" > /opt/MQTT.pid");
else {
if (++countMqtt > 2) {
countMqtt = 0;
printf("mqtt process is dead\n");
system("bash startall.sh ./newtonconfig-A.xml");
}
}
}
close(fd);
}
// OpcUA 监视进程
void OpcuaProbe() {
static int countOpcua = 0;
char buf[10];
memset(buf, 0, sizeof(buf));
int fd = open("/opt/OPCUA.pid", O_RDWR);
if (fd == -1) {
perror("failed to open OPCUA.pid file");
}
if (read(fd, buf, sizeof(buf) - 1) > 0) {
if (strncmp(buf, "1", 1) == 0)
system("echo \"0\" > /opt/OPCUA.pid");
else {
if (++countOpcua > 2) {
countOpcua = 0;
printf("opcua process is dead\n");
system("bash startall.sh ./newtonconfig-A.xml");
}
}
}
close(fd);
}
// Modbus 监视进程
void ModbusProbe() {
static int countModbus=0;
char buf[10];
memset(buf, 0, sizeof(buf));
int fd = open("/opt/MODBUS.pid", O_RDWR);
if (fd == -1) {
perror("failed to open MODBUS.pid file");
}
// 牛顿协议栈进程,首先读取文件内容,其次在修改文件
if (read(fd, buf, sizeof(buf) - 1) > 0) {
printf("%s\n", buf);
if (strncmp(buf, "1", 1) == 0)
system("echo \"0\" > /opt/MODBUS.pid");
else {
if (++countModbus > 2) {
countModbus = 0;
printf("modbus process is dead\n");
system("bash startall.sh ./newtonconfig-A.xml");
}
}
}
close(fd);
}
// 定时器处理程序
void process_Handler()
{
NewtonProbe();
if(strcmp(ezxml_child(ezxml_parse_file("./newtonconfig-A.xml"),"test")->txt,"MOSBUS") == 0)
ModbusProbe();
if(strcmp(ezxml_child(ezxml_parse_file("./newtonconfig-A.xml"),"test")->txt,"MQTT") == 0)
MqttProbe();
if(strcmp(ezxml_child(ezxml_parse_file("./newtonconfig-A.xml"),"test")->txt,"OPCUA") == 0)
OpcuaProbe();
}
// 设置处理信息
void init_sigaction()
{
struct sigaction act;
//设置处理信号的函数
act.sa_handler = process_Handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF, &act, NULL);
}
// 初始化配置
void init_time()
{
struct itimerval val;
// 设置定时器时间 5s
val.it_value.tv_sec = 5;
val.it_value.tv_usec = 0;
// 设置定时器间隔
val.it_interval = val.it_value;
setitimer(ITIMER_PROF, &val, NULL);
}
int main(int argc, char **argv) {
signal(SIGINT, stopHandler);
// 运行startall.sh 启动NEWTON 和(OPCUA、MQTT、MODBUS)其中的一个
char Commandbuf[20];
bzero(Commandbuf,sizeof(Commandbuf));
sprintf(Commandbuf,"bash startall.sh %s",argv[1]);
system(Commandbuf);
// 定时器
init_sigaction();
init_time();
while(1);
return 0;
}
我这里就不做详细的出错信息截图了,我就说一下出错状态,会出现Alarm clock然后程序就会终止。这个主要是编译程序时加入了-std=c99选项。第一种解决方法就是去掉这个编译选项,可是有的时候你需要这个编译选项,那么该怎么办呢,你可以用gnu99来代替c99选项。详细如下图
支付宝 微信