Create hashes with at least a user-selected number of leading zeroes

0

The main program consists of a simple menu with two options:

  • Try to find nonces that can create hashes with at least a user-selected number of leading zeroes
  • Quit When the user chooses to start generating hashes, those calculations will continue until the user presses ctrl+c.
  • Once the user does press ctrl+c, it simply returns to the menu, for the next user selection Hashes must be generated by four threads (all copies of the same function).
  • Whenever any of the threads find a nonce/hash pair, it should display them both on the screen
  • No two threads printing simultaneously must use a mutex/lock to ensure that only one thread may generate output at a time
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <pthread.h>
using namespace std;

static const int NUMTHREADS=4;
volatile bool continuing;
volatile bool continuing1;
volatile bool continuing2;
volatile int occupied;
pthread_mutex_t lock; //Our mutual exclusion lock

void printThirtyTwo(unsigned int word) {
for (int i=0;i<32;i++)
std::cout<<(((0x80000000>>i)&word)?1:0);
std::cout<<std::endl;
}

void printSixtyFour(unsigned long word) {
for (int i=0;i<64;i++)
std::cout<<(((0x8000000000000000>>i)&word)?1:0);
std::cout<<std::endl;
}

//Breaks nonce up into 16 4-bit tokens, to generate hash value
unsigned int calchash(unsigned long nonce) {
unsigned int hash=0;
for (int i=15;i>=0;i--) {
hash=hash*17+((nonce>>(4*i))&0x0F);
}
return hash;
}

void genULong(unsigned long &nonce) {
nonce=0;
for (int i=63;i>=0;i--) {
nonce<<=1;
nonce|=random()%2;
}
}


int leadingZeroes(unsigned int value) {
for (int i=0;i<32;i++)
if ((value>>(31-i))&1) return i;
return 32;
}

//At this level, this header signature can be ignored
void* busywork(void* unnecessary) {
while (continuing) {
usleep(100000);
}
pthread_mutex_lock(&lock);
occupied--;
std::cout<<"Exiting thread."<<std::endl;
pthread_mutex_unlock(&lock);
}

void peek(int sig) {
std::cout<<"Currently processing: "<<(continuing?"Yes":"No")<<std::endl;
}

//We don't really care what 'sig' is for this example
void interrupted(int sig) {
std::cout<<"\nComputations complete.\nHalting now..."<<std::endl;
continuing=false;
continuing1=true;
}
int main() {
pthread_t ct[NUMTHREADS];//our child threads
continuing=true;
continuing2=true;
std::cout<<"About to commence; PID: "<<getpid()<<std::endl;
if (signal(SIGINT,interrupted)==SIG_ERR) {
std::cout<<"Unable to change signal handler."<<std::endl;
return 1;
}
if (signal(SIGUSR1,peek)==SIG_ERR) {
std::cout<<"Unable to change signal handler."<<std::endl;
return 1;
}
for (int i=0;i<NUMTHREADS;i++) {
pthread_mutex_lock(&lock);//reserve lock
pthread_create(&ct[i], NULL, &busywork, NULL);
occupied++;
pthread_mutex_unlock(&lock);//release lock
}
//we don't need the mutex here, because we aren't changing occupied
while (occupied>0)
sleep(150);
std::cout<<"Execution complete."<<std::endl;
int choice;
std::cout<<"Do you want to contuine? (Y or N)";std::cin>>choice;
std::cin>>choice;
if(!choice == "Y"){
break;
}
else{
return 0;
}
}

CSSA BU

Posted 2019-11-25T01:24:37.633

Reputation: 1

Question was closed 2019-11-25T09:22:43.500

2This isn't a question, or about bitcoin.Anonymous 2019-11-25T01:34:07.367

No answers