C语言实现文件实时更新
一、简介
在linux或者unix操作系统中在系统引导的时候会开启很多服务,这些服务就叫做守护进程。 守护进程脱离了终端并且在后台运行:守护进程脱离于终端是为了避免进程在执行过程中的信息在任何终端上显示并且进程也不会被任何终端所产生的终端信息所打断。 本文介绍使用守护进程实现文件实时更新的方法步骤。
二、源码
文件1:Realtime_Update.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h> void init_daemon(void);
static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs); /**
* 文件更新实时检测程序
*/
main()
{
int ret;
struct stat statbuff_file1;
struct stat statbuff_file2;
char *file1 = "~/test_file1.txt";
char *file2 = "~/test_file2.txt"; stat(file1, &statbuff_file1);
stat(file2, &statbuff_file2); //初始化为Daemon
init_daemon(); //循环执行,每秒一次
while(1)
{
//判断文件是否更新
ret = timespec_compare(&statbuff_file1.st_mtim, &statbuff_file2.st_mtim);
if(ret > 0)
{
system("cp -a ~/test_file1.txt ~/test_file2.txt");
} sleep(1);//睡眠一秒钟
}
} /**
* lhs < rhs: return <0
* lhs == rhs: return 0
* lhs > rhs: return >0
*/
static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs)
{
if (lhs->tv_sec < rhs->tv_sec)
return -1;
if (lhs->tv_sec > rhs->tv_sec)
return 1;
return lhs->tv_nsec - rhs->tv_nsec;
}
文件2:init.c
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h> void init_daemon(void)
{
int pid;
int i; if(pid=fork())
exit(0);//是父进程,结束父进程
else if(pid< 0)
exit(1);//fork失败,退出 //是第一子进程,后台继续执行
setsid();//第一子进程成为新的会话组长和进程组长
//并与控制终端分离
if(pid=fork())
exit(0);//是第一子进程,结束第一子进程
else if(pid< 0)
exit(1);//fork失败,退出 //是第二子进程,继续
//第二子进程不再是会话组长
for(i=0; i< NOFILE; ++i) //关闭打开的文件描述符
close(i);
chdir("/tmp");
umask(0);//重设文件创建掩模
return;
}
编译
gcc -o Realtime_Update init.c Realtime_Update.c
运行
./Realtime_Update
三、源码下载
C语言实现文件实时更新的更多相关文章
- 使用PhpStrom等IDE编辑 远程linux服务器上的文件 实时更新
习惯了在linux上 vim进行PHP的开发,突然忘了IDE的感觉,所以想到了.通过PhpStrom等IDE直接编辑 samba共享到 mac或者windows本地的磁盘进行编辑. 为了忘却的纪念,有 ...
- 〖Linux〗实时更新 hosts 文件的脚本
适用场景: 下载了一个smarthosts的hosts文件,但hosts文件过旧导致一些ip地址已失效无法访问网络. 脚本使用: ./hostsupdate # 直接从 /etc/hosts 中获得需 ...
- 【Python】分享使用的插件文件链接(实时更新)
链接:https://pan.baidu.com/s/1o7AgHtw Python工具实时更新.
- facebook充值实时更新接口文档翻译 希望对做facebook充值的小伙伴有帮助
Realtime Updates for Payments are an essential method by which you are informed of changes to orders ...
- sphinx通过增量索引实现近实时更新
一.sphinx增量索引实现近实时更新设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少. 例如.原来的数据 ...
- 数据可视化:Echart中k图实现动态阈值报警及实时更新数据
1 目标 使用Echart的k图展现上下阈值,并且当真实值超过上阈值或低于下阈值时候,标红报警. 2 实现效果 如下:
- sphinx 增量索引 实现近实时更新
一.sphinx增量索引的设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少.例如.原来的数据有几百万条 ...
- unison + inotify 实现文件实时双向同步部署步骤
unison + inotify 实现文件实时双向同步部署步骤 一. Unison简介 Unison是Windows.Linux以及其他Unix平台下都可以使用的文件同步工具,它能使两个文件夹(本地或 ...
- Core 2.0 的dll实时更新、https、依赖包变更问题及解决
今天所有开发环境已经迁移到mac OS下的Visual Studio Code + 命令行编译发布,而运行服务器是CentOS7,和windows没什么关联了. 只要你Relese编译并在本地有一个与 ...
随机推荐
- LeetCode OJ:Path Sum II(路径和II)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode OJ:Combination Sum III(组合之和III)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- lamp script
centos6 ,不区分32位,64位, 要求机器可以上外网. 支持lamp 和 lnmp, mysql支持5.1和5.6两个版本,php支持5.3和5.6两个版本,apache2.2,nginx1. ...
- HDU - 5730 :Shell Necklace(CDQ分治+FFT)
Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n b ...
- POJ2411Mondriaan's Dream(DP+状态压缩 or 插头DP)
问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after prod ...
- lower_bound()函数与quicksort()函数的简单掌握
lower_bound 这个序列中可能会有很多重复的元素,也可能所有的元素都相同,为了充分考虑这种边界条件,STL中的lower_bound算法总体上是才用了二分查找的方法,但是由于是查找序列中的第一 ...
- 寻找数组中第K大数
1.寻找数组中的第二大数 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- 【2】HashMap
http://www.cnblogs.com/xwdreamer/archive/2012/06/03/2532832.html 一:java的数据结构 在Java编程语言中,最基本的结构就是两种,一 ...
- 横向排列两个多个div盒子的方法(CSS浮动清除float-clear/inline)/办法
最近在做一个div css切割,昨晚发现了长期以来一直无记录下来的问题!关于兼容IE跟FF的float属性.趁现在还清醒赶紧记下笔记先:一.并排在一行的两个div样式有这种情况:ie或者ff下对于子d ...
- Logstash详解之——input模块
原文地址 Logstash由三个组件构造成,分别是input.filter以及output.我们可以吧Logstash三个组件的工作流理解为:input收集数据,filter处理数据,output输出 ...