提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、进程间通信
设备分为2部分,一部分为我们的主要程序。另一部分为保护程序。当保护程序和主程序通信成功后,保护程序将主程序拷贝并保存。当保护程序和主程序在规定的时间还通信异常。接直接回退到之前的程序。这样完成程序的升级保护。
1.保护代码
代码如下(示例):
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
unsigned int IpStateCount = 0 ;
int first_open = 0;
unsigned char coppy_sig = 0;
pthread_mutex_t IpStateMu ;
void *IpServerThread(void *param)
{
struct sockaddr_in saddr;
int ssock = -1;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(4205);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if ( (ssock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("ssocket\r\n");
exit(1);
}
if (bind(ssock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
{
perror("1 bind 127.0.0.1\n");
int r = system("ifconfig lo 127.0.0.1");
if (bind(ssock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
{
perror("2 bind 127.0.0.1\n");
exit(1);
}
}
// 接收缓冲区
int nRecvBufLen = 10*1024; //设置为8K
setsockopt(ssock, SOL_SOCKET, SO_RCVBUF, ( const char* )&nRecvBufLen, sizeof( int ) );
//发送缓冲区
int nSendBufLen = 10*1024; //设置为8K
setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, ( const char* )&nSendBufLen, sizeof( int ) );
// struct timeval timeout={10,0};//
//setsockopt(ssock,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));
char rbuf[512];
char sbuf[50];
int j=0,n =0;
int len = sizeof(saddr);
while (1)
{
memset(rbuf,0,512);
n = recvfrom(ssock, rbuf, 512, 0, (struct sockaddr*)&saddr, &len);
if (n>0)
{
if(j%10 == 0)
printf("udprecv:%s\n",rbuf);
if(strcmp(rbuf,"wmts")==0)
{
pthread_mutex_lock(&IpStateMu);
IpStateCount = 0 ;
coppy_sig ++ ;
if(coppy_sig > 10)
coppy_sig = 10;
pthread_mutex_unlock(&IpStateMu);
}
else if(strcmp(rbuf,"wmtscs")==0)
{
pthread_mutex_lock(&IpStateMu);
IpStateCount = 0 ;
pthread_mutex_unlock(&IpStateMu);
}
}
j++;
if(j == 10000)
j = 0;
}
}
int main()
{
char rbuf[2];
pthread_t m_IpServerThread ;
pthread_create(&m_IpServerThread,NULL,IpServerThread,NULL);
pthread_mutex_init(&IpStateMu,NULL);
sleep(8);
while(1)
{
pthread_mutex_lock(&IpStateMu);
IpStateCount ++;
pthread_mutex_unlock(&IpStateMu);
sleep(8);
if(IpStateCount > 2)
printf("CloudradioStateCount=%d\n",IpStateCount);
if(IpStateCount > 10)
{
printf("IpStateCount > 10\n");
system("cd /usr/bin");
system("killall cloudradio");
sleep(1);
system("cp /usr/bin/cloudradio1 /usr/bin/cloudradio");
sleep(1);
system("sync") ;
sleep(1);
system("reboot -f") ;
}
}
}
2.主程序通信代码
代码如下(示例):
static void* appwatchdog(void *pParam)
{
int chick_sock;
struct sockaddr_in chick_saddr;
memset(&chick_saddr,0,sizeof(chick_saddr));
chick_saddr.sin_family = AF_INET;
chick_saddr.sin_port = htons(4205);
chick_saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if ( (chick_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("chick_sock socket failed\n");
}
int wr;
int result;
result = init_watch_dog();
if(result < 0)
{
return NULL;
}
enable_watch_dog();
set_watch_dog_time();
while(1)
{
feed_watch_dog();
usleep(8*1000*1000);
wr = sendto(chick_sock, "wmts", 4, 0, (struct sockaddr *)&chick_saddr, sizeof(chick_saddr));
if(wr < 0)
{
int lo = system("ifconfig lo 127.0.0.1");
printf("0=%d\n",lo);
}
}
}
总结
在主程序的看门狗线程中,加入进程通信代码。保护程序也加入进行通信代码。这样可进行升级保护或者程序保护。