Supports real-time file updates in C language and real-time updates in C language.

Source: Internet
Author: User

Supports real-time file updates in C language and real-time updates in C language.

I. Introduction

In a linux or unix operating system, many services are enabled during system boot. These services are called daemon processes. The daemon is disconnected from the terminal and runs in the background: the daemon is separated from the terminal to prevent the information in the execution process from being displayed on any terminal and the process is not interrupted by any terminal information generated by any terminal. This document describes how to update files in real time using a daemon.

 

Ii. Source Code

File 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);/*** file update Real-Time Detection Program */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); // initialize to Daemon init_daemon (); // execute cyclically, once every second while (1) {// determine whether the file is updated with ret = timespec_compare (& statbuff_file1.st_mtim, & statbuff_file2.st_mtim); if (ret> 0) {system ("cp-~ /Test_file1.txt ~ /Test_file2.txt ");} sleep (1); // sleep for one second}/*** 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 ;}

File 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); // It is the parent process, stop the parent process else if (pid <0) exit (1); // fork failed, exit // is the first sub-process, and the background continues to execute setsid (); // The first sub-process becomes the new session leader and process leader // and is separated from the control terminal if (pid = fork () exit (0); // is the first sub-process, stop the first sub-process else if (pid <0) exit (1); // fork failed, exit // is the second sub-process, continue // the second sub-process is no longer the session leader for (I = 0; I <NOFILE; ++ I) // close the opened file descriptor close (I ); chdir ("/tmp"); umask (0); // reset the file creation mask return ;}

Compile

gcc -o Realtime_Update init.c  Realtime_Update.c

Run

./Realtime_Update

 

3. Download source code

http://files.cnblogs.com/files/274914765qq/Realtime_Update_Daemon.zip

 

Reference: http://www.cnblogs.com/274914765qq/p/4792707.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.