Last active
December 18, 2015 15:08
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Formatting problems with description, it was: