Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <sys/time.h>
- #define KEY_LENGTH 16
- #define ITERATIONS 16777216
- #define THREAD_COUNT 4
- #define ITERATIONS_PER_THREAD ITERATIONS/THREAD_COUNT
- void ksa(int state[], int key[]) {
- int i,j=0,t;
- for (i=0; i<256; ++i)
- state[i] = i;
- for (i=0; i<256; ++i) {
- j = (j + state[i] + key[i % KEY_LENGTH]) % 256;
- t = state[i];
- state[i] = state[j];
- state[j] = t;
- }
- }
- void prga(int state[], int out[]) {
- int i=0,j=0,x,t;
- for (x=0; x<256; ++x) {
- i = (i+1) % 256;
- j = (j+state[i]) % 256;
- t = state[i];
- state[i] = state[j];
- state[j] = t;
- out[x] = state[(state[i] + state[j]) % 256];
- }
- }
- void makeRandomKey(int key[]) {
- int i;
- for (i=0; i<KEY_LENGTH; ++i)
- key[i] = rand() % 256;
- }
- void populateOccurances(int* occurances) {
- int state[256];
- int out[256];
- int key[KEY_LENGTH];
- int i,j;
- srand(time(NULL));
- for (i=0; i<ITERATIONS_PER_THREAD; ++i) {
- makeRandomKey(key);
- ksa(state, key);
- prga(state, out);
- for (j=0; j<256; ++j)
- ++occurances[j*256+out[j]];
- }
- }
- static void *populateOccurancesThread(void *p) {
- int* occurances = (int*) p;
- populateOccurances(occurances);
- pthread_exit(NULL);
- }
- int main(int argc, char *argv[]) {
- struct timeval time_started, time_now, time_diff;
- gettimeofday(&time_started, NULL);
- int occurances[THREAD_COUNT][256*256];
- int bytePosition,charOccurance;
- int i,k;
- pthread_t threads[THREAD_COUNT];
- for (k=0; k<THREAD_COUNT; ++k)
- for (i=0; i<256*256; ++i)
- occurances[k][i] = 0;
- for (k=0; k<THREAD_COUNT; ++k) {
- int rc = pthread_create(&threads[k], NULL, populateOccurancesThread, (void*)&occurances[k]);
- if (rc) {
- printf("ERROR; return code from pthread_create() is %d\n", rc);
- exit(-1);
- }
- }
- for (k=0; k<THREAD_COUNT; ++k) {
- void * status;
- int rc = pthread_join(threads[k], &status);
- if (rc) {
- printf("ERROR: return code from pthread_join() is %d\n", rc);
- exit(-1);
- }
- }
- for (bytePosition=0; bytePosition<256; ++bytePosition) {
- printf("%d,", bytePosition);
- }
- printf("\n");
- for (charOccurance=0; charOccurance<256; ++charOccurance) {
- for (bytePosition=0; bytePosition<256; ++bytePosition) {
- int occuranceSum = 0;
- for (k=0; k<THREAD_COUNT; ++k) {
- occuranceSum += occurances[k][charOccurance*256+bytePosition];
- }
- printf("%d,", occuranceSum);
- }
- printf("\n");
- }
- gettimeofday(&time_now, NULL);
- timersub(&time_now, &time_started, &time_diff);
- printf("Time taken,%ld.%.6ld s\n", time_diff.tv_sec, time_diff.tv_usec);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement