Skip to content

Instantly share code, notes, and snippets.

@mattmahn
Last active April 14, 2017 03:10
Show Gist options
  • Save mattmahn/274babd879426215b2f67db62f3c7555 to your computer and use it in GitHub Desktop.
Save mattmahn/274babd879426215b2f67db62f3c7555 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <signal.h>
#include <string>
#include <unistd.h>
volatile double sum = 0;
std::chrono::system_clock::time_point start_time;
void sig_handler(int s)
{
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> diff = end - start_time;
std::cout << diff.count() << "s elapsed" << std::endl;
std::cout << sum << std::endl;
}
int
main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Gimme a number! (up to size ull)" << std::endl;
exit(1);
}
unsigned long long number = atoll(argv[1]);
signal(SIGUSR1, sig_handler);
std::cout << "Send SIGUSR1 to this process to print the time elapsed and ";
std::cout << "current sum" << std::endl;
std::cout << "PID: " << ::getpid() << std::endl;
std::cout << "Ready!" << std::endl;
start_time = std::chrono::system_clock::now();
for (unsigned long long i = 1; i < number; ++i)
{
sum += 1.0 / i;
}
auto end_time = std::chrono::system_clock::now();
auto diff = end_time - start_time;
std::cout << "Finished in " << diff.count() << "s" << std::endl;
std::cout << sum << std::endl;
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment