[关闭]
@cyysu 2017-10-24T12:40:38.000000Z 字数 3511 阅读 776

嵌入式ARM定时器无故终止

  • 时间:2017年10月23日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
  • 版本:4.0
  • 描述:执行linux定时器时,不知因为什么原因导致程序终止。

嵌入式Linux


定时器代码

  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include "newton_type.h"
  8. #include "ezxml.h"
  9. #include <pthread.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <sys/time.h>
  13. #define THREAD_NUM 2
  14. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  15. #define __DEBUG__ //if release then note this line
  16. #ifdef __DEBUG__
  17. #define printg(format,...) printf(""format" ###File: "__FILE__",Line: %05d\n",##__VA_ARGS__,__LINE__)
  18. #else
  19. #define printg(format,...)
  20. #endif
  21. static void stopHandler(int sign) {
  22. exit(-1);
  23. }
  24. // Newton 监视进程
  25. void NewtonProbe() {
  26. static int countNewton = 0;
  27. char buf[10];
  28. memset(buf, 0, sizeof(buf));
  29. int fd = open("/opt/NEWTON.pid", O_RDWR);
  30. if (fd == -1) {
  31. perror("failed to open NEWTON.pid file");
  32. }
  33. if (read(fd, buf, sizeof(buf) - 1) > 0) {
  34. if (strncmp(buf, "1", 1) == 0)
  35. system("echo \"0\" > /opt/NEWTON.pid");
  36. else {
  37. if (++countNewton > 2) {
  38. countNewton = 0;
  39. printf("newton process is dead\n");
  40. system("bash startall.sh ./newtonconfig-A.xml");
  41. }
  42. }
  43. }
  44. close(fd);
  45. }
  46. // Mqtt 监视进程
  47. void MqttProbe() {
  48. static int countMqtt = 0;
  49. char buf[10];
  50. memset(buf, 0, sizeof(buf));
  51. int fd = open("/opt/MQTT.pid", O_RDWR);
  52. if (fd == -1) {
  53. perror("failed to open MQTT.pid file");
  54. }
  55. if (read(fd, buf, sizeof(buf) - 1) > 0) {
  56. if (strncmp(buf, "1", 1) == 0)
  57. system("echo \"0\" > /opt/MQTT.pid");
  58. else {
  59. if (++countMqtt > 2) {
  60. countMqtt = 0;
  61. printf("mqtt process is dead\n");
  62. system("bash startall.sh ./newtonconfig-A.xml");
  63. }
  64. }
  65. }
  66. close(fd);
  67. }
  68. // OpcUA 监视进程
  69. void OpcuaProbe() {
  70. static int countOpcua = 0;
  71. char buf[10];
  72. memset(buf, 0, sizeof(buf));
  73. int fd = open("/opt/OPCUA.pid", O_RDWR);
  74. if (fd == -1) {
  75. perror("failed to open OPCUA.pid file");
  76. }
  77. if (read(fd, buf, sizeof(buf) - 1) > 0) {
  78. if (strncmp(buf, "1", 1) == 0)
  79. system("echo \"0\" > /opt/OPCUA.pid");
  80. else {
  81. if (++countOpcua > 2) {
  82. countOpcua = 0;
  83. printf("opcua process is dead\n");
  84. system("bash startall.sh ./newtonconfig-A.xml");
  85. }
  86. }
  87. }
  88. close(fd);
  89. }
  90. // Modbus 监视进程
  91. void ModbusProbe() {
  92. static int countModbus=0;
  93. char buf[10];
  94. memset(buf, 0, sizeof(buf));
  95. int fd = open("/opt/MODBUS.pid", O_RDWR);
  96. if (fd == -1) {
  97. perror("failed to open MODBUS.pid file");
  98. }
  99. // 牛顿协议栈进程,首先读取文件内容,其次在修改文件
  100. if (read(fd, buf, sizeof(buf) - 1) > 0) {
  101. printf("%s\n", buf);
  102. if (strncmp(buf, "1", 1) == 0)
  103. system("echo \"0\" > /opt/MODBUS.pid");
  104. else {
  105. if (++countModbus > 2) {
  106. countModbus = 0;
  107. printf("modbus process is dead\n");
  108. system("bash startall.sh ./newtonconfig-A.xml");
  109. }
  110. }
  111. }
  112. close(fd);
  113. }
  114. // 定时器处理程序
  115. void process_Handler()
  116. {
  117. NewtonProbe();
  118. if(strcmp(ezxml_child(ezxml_parse_file("./newtonconfig-A.xml"),"test")->txt,"MOSBUS") == 0)
  119. ModbusProbe();
  120. if(strcmp(ezxml_child(ezxml_parse_file("./newtonconfig-A.xml"),"test")->txt,"MQTT") == 0)
  121. MqttProbe();
  122. if(strcmp(ezxml_child(ezxml_parse_file("./newtonconfig-A.xml"),"test")->txt,"OPCUA") == 0)
  123. OpcuaProbe();
  124. }
  125. // 设置处理信息
  126. void init_sigaction()
  127. {
  128. struct sigaction act;
  129. //设置处理信号的函数
  130. act.sa_handler = process_Handler;
  131. act.sa_flags = 0;
  132. sigemptyset(&act.sa_mask);
  133. sigaction(SIGPROF, &act, NULL);
  134. }
  135. // 初始化配置
  136. void init_time()
  137. {
  138. struct itimerval val;
  139. // 设置定时器时间 5s
  140. val.it_value.tv_sec = 5;
  141. val.it_value.tv_usec = 0;
  142. // 设置定时器间隔
  143. val.it_interval = val.it_value;
  144. setitimer(ITIMER_PROF, &val, NULL);
  145. }
  146. int main(int argc, char **argv) {
  147. signal(SIGINT, stopHandler);
  148. // 运行startall.sh 启动NEWTON 和(OPCUA、MQTT、MODBUS)其中的一个
  149. char Commandbuf[20];
  150. bzero(Commandbuf,sizeof(Commandbuf));
  151. sprintf(Commandbuf,"bash startall.sh %s",argv[1]);
  152. system(Commandbuf);
  153. // 定时器
  154. init_sigaction();
  155. init_time();
  156. while(1);
  157. return 0;
  158. }

我这里就不做详细的出错信息截图了,我就说一下出错状态,会出现Alarm clock然后程序就会终止。这个主要是编译程序时加入了-std=c99选项。第一种解决方法就是去掉这个编译选项,可是有的时候你需要这个编译选项,那么该怎么办呢,你可以用gnu99来代替c99选项。详细如下图

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注