You are here:

C++/catch Ctrl+C and print stats

Advertisement


Question
QUESTION: I am writting a program in which I have an array:

char candidate[];

which is constantly changing throughout the programs execution. What I want to do is catch 'Ctrl+C' and print 'candidate' before the program exits.

#include "signal.h"
#include <iostream>
using namespace std;

void printStats()
{
  cout << candidate << "\n";
}

void terminate(int sig)
{
  cout << "aborting\n";
  printStats();
  exit(0);
}

int main()
{
  //candidate is constantly changing-
  //this is just an example
  char candidate[] = "hindenburg";

  signal(SIGINT, terminate);

return 0;
}

obviously, it wont compile as printStats() says 'candidate' undeclared (first use of this function). How do I pass 'candidate' to printStats()? The structure for signal() is set, so I don't know how to relay 'candidate' from main().


ANSWER: You can't use a local variable and have printStats have access to it.

//candidate is constantly changing-
//this is just an example
static char candidate[] = "hindenburg";

void printStats()
{
cout << candidate << "\n";
}

void terminate(int sig)
{
cout << "aborting\n";
printStats();
exit(0);
}

int main()
{
signal(SIGINT, terminate);

return 0;
}


This:

static char candidate[] = "hindenburg";

is dangerous because you can only store 10 letter words.  You should use something larger:

static char candidate[100] = "hindenburg";

Or whatever is the longest word to be copied to candidate.

Bill

---------- FOLLOW-UP ----------

QUESTION: is there some way I can make 'char candidate' global, or somehow access it without passing it into a function?

I have seen cod in which Ctrl+C was caught and the program returned its current progress and exited. Like 'aborting - 20% complete'.

ANSWER: Sure you can make it global.

Delete static on candidate and move it above function printStats.

Bill

---------- FOLLOW-UP ----------

QUESTION: I got the following from another program. When you hit Ctrl+C, the program prints its current progress and terminates. It seems to access variables used and modified within main() without passing those variables to the printStats() function (length, startclock, totalcombinations).

int main()
{
  // so we can show stats on ctrl+c
  signal(SIGINT, wrapUp);
...
}

// catch CTRL+C, write stats and exit
void wrapUp(int sig)
{
  printf("Aborting...\n");
  writeStats();
  exit(0);
}

void writeStats()
{
  int thread_ID;
  for (thread_ID = 0; thread_ID < c_Threads.size(); thread_ID++)
  {
     crackThread* c_Thread = c_Threads[thread_ID];
     counter += c_Thread->getCounter();
  }
  clock_t currentClock = clock();
  if (currentClock - startClock > 0)
  {
     float fTime = 1.0f * (currentClock - startClock) / CLOCKS_PER_SEC;
     float speedOverall = counter / fTime / 1000000;
     float percentage = (100.0f * counter) / (1.0f * totalCombinations);
     if (percentage > 100.0)
        percentage = 100.0;
     printf("Length %d - %.0f%% in %.2f s (%.2f Mhashes/s)\r", length, percentage, fTime, speedOverall);
  }
  counter = 0;
}

how is this accomplished?

Answer
int length;
clock_t currentClock;
int totalCombinations;

int main()
{
currentClock = clock();
// so we can show stats on ctrl+c
signal(SIGINT, wrapUp);
...
}

// catch CTRL+C, write stats and exit
void wrapUp(int sig)
{
printf("Aborting...\n");
writeStats();
exit(0);
}

void writeStats()
{
int thread_ID;
for (thread_ID = 0; thread_ID < c_Threads.size(); thread_ID++)
{
crackThread* c_Thread = c_Threads[thread_ID];
counter += c_Thread->getCounter();
}
clock_t currentClock = clock();
if (currentClock - startClock > 0)
{
float fTime = 1.0f * (currentClock - startClock) / CLOCKS_PER_SEC;
float speedOverall = counter / fTime / 1000000;
float percentage = (100.0f * counter) / (1.0f * totalCombinations);
if (percentage > 100.0)
percentage = 100.0;
printf("Length %d - %.0f%% in %.2f s (%.2f Mhashes/s)\r", length, percentage, fTime, speedOverall);
}
counter = 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.