Skip to content

Instantly share code, notes, and snippets.

@znnahiyan
Last active December 18, 2015 15:08
Show Gist options
  • Save znnahiyan/5801851 to your computer and use it in GitHub Desktop.
Save znnahiyan/5801851 to your computer and use it in GitHub Desktop.
Two programs to ensure atomic(?) operations on periodic operations by making an entry that stores the last time the operation was done. See comment below for more information, no formatting in here! To compile, download all three of these files and run `make`
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
int main(int argc, char *argv[])
{
if (argc != 3)
return 2;
std::ifstream log(argv[1], std::fstream::in | std::fstream::binary);
time_t log_time = 0;
log.read((char*) &log_time, sizeof(time_t));
log.close();
if ((time(0) - log_time) > atoi(argv[2]))
return 0;
else
return 1;
}
CC=g++
CPPFLAGS=-m64 -std=c++11 -pedantic -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wno-switch
BIN=$(patsubst %.cpp,bin/%,$(wildcard *.cpp))
all: $(BIN)
bin/%: %.cpp
mkdir -p bin/
$(CC) $(CPPFLAGS) $< -o $@
clean:
rm -rfv bin/
#include <fstream>
#include <time.h>
int main(int argc, char *argv[])
{
if (argc != 2)
return 2;
std::ofstream log(argv[1], std::fstream::out | std::fstream::trunc | std::fstream::binary );
time_t log_time = time(0);
log.write((char*) &log_time, sizeof(time_t));
log.close();
if (log.failbit)
return 3;
return 0;
}
@znnahiyan
Copy link
Author

Formatting problems with description, it was:

Two programs to ensure atomic(?) operations on periodic operations by making an entry that stores the last time the operation was done, use as:

chkentry <entry path> <period in seconds> && your_operations; mkentry <entry path>
  • chkentry return codes:
    • 0: Time in entry was as long enough ago as the argument passed
    • 1: Time in entry wasn't long enough ago.
    • 2: Incorrect number of arguments passed.
  • mkentry return codes:
    • 0: Writing new entry was successful
    • 2: Incorrect number of arguments passed.
    • 3: There was an error relating to the file. Please check permissions and stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment