本人学习笔记,参考网上代码完成
makefile
TEST_OBJ := rng_app MAKEOPT := ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- ARM_LINUX_KERNEL := /home/zhangbh/5830/hjf_5830x_7_24/firmware/build_dir/linux-brcm5830_sxxx-prolin2/linux-3.6.5 PWD = $(shell pwd) all: arm-none-linux-gnueabi-gcc -o $(TEST_OBJ) $(TEST_OBJ).c .PHONY: clean clean: rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions Module.symvers .Makefile.swp modules.order $(TEST_OBJ)
rng_app.c
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <assert.h> #include <string.h> #include <stdlib.h> #define RANDOM_NAME "hw_random" int random_fd; typedef struct { char chr; /*tip char*/ char *title; /*tip string*/ int style; /*progress style*/ int max; /*maximum value*/ float offset; char *pro; } progress_t; #define PROGRESS_NUM_STYLE 0 #define PROGRESS_CHR_STYLE 1 #define PROGRESS_BGC_STYLE 2 void progress_init(progress_t *, char *, int, int); void progress_show(progress_t *, int); void progress_destroy(progress_t *); int random_num(int min, int max); void displayProgress(int progress); FILE *fp = NULL; int main(int argc, char **argv) { long long rand_num = 0; long long max_random = 0; int i, j; long long random_size = 100; int count = 0; long long check_cnt = 0; long long a[4096]; long long check_size = 0; long long b[4096]; unsigned cnt = 0; unsigned flag_b = 0; unsigned char *file_name = "./t.txt"; progress_t bar; //progress_init(&bar, "", 50, PROGRESS_NUM_STYLE); progress_init(&bar, "", 100, PROGRESS_CHR_STYLE); //progress_init(&bar, "", 50, PROGRESS_BGC_STYLE); random_fd = open("/dev/hw_random", O_RDONLY); if (random_fd < 0) { printf("open file %s failed! ", RANDOM_NAME); return -1; } printf("sizeof(long int)=%d ", sizeof(long int)); printf("sizeof(long)=%d ", sizeof(long)); printf("sizeof(long long)=%d ", sizeof(long long)); max_random = atoi(argv[1]); check_size = atoi(argv[2]); fp = fopen(file_name, "a"); if (!fp) { perror("fopen"); progress_destroy(&bar); } //rand_num = random_num(0, 10000); check_cnt = 0; while (check_cnt < check_size) { for (j=0; j<random_size; j++) { a[j] = random_num(0, max_random); for (i=0; i<j; i++) { if (a[i] == a[j]) { b[cnt] = a[i]; cnt++; count++; // printf("a[%d]=%d ", j, a[j]); // printf("a[%d]=%d ", i, a[i]); } if (b[0] == a[j]) { flag_b++; } } } check_cnt++; if ((check_cnt*100)%check_size == 0) { // printf("===============>%d%% ", (check_cnt*100)/check_size); // displayProgress((check_cnt*100)/check_size); progress_show(&bar, (check_cnt*100)/check_size/1.0f); } } printf(" +-Done "); printf(" "); // printf("a[50]=%d ", a[50]); printf("count = %d ", count); printf("check_cnt = %d ", check_cnt); printf("flag_b = %d ", flag_b); for (i=0; i<10; i++) { printf("b[%d]=%ld ", i, b[i]); } #if 0 int i; for ( i = 0; i <= 50; i++ ) { progress_show(&bar, i/50.0f); sleep(1); } printf(" +-Done "); #endif progress_destroy(&bar); return 0; } #if 0 int write_to_file(unsigned long data, int tmp) { fprintf(fp, "%ld %d ", msg->user_name, msg->user_data); fflush(fp); return 0; } #endif int random_num(int min, int max) { //static int random_fd; unsigned long int random_value = 0; int bytes_to_read; char *next_random_byte; int ret; assert(max > min); next_random_byte = (char *)&random_value; bytes_to_read = sizeof(random_value); /* 因为是从/dev/random中读取,read可能会被阻塞,一次读取可能只能得到一个字节, * 循环是为了让我们读取足够的字节数来填充random_value. */ do { ret = read(random_fd, next_random_byte, bytes_to_read); bytes_to_read -= ret; next_random_byte += ret; } while (bytes_to_read > 0); return min + (random_value%(max - min + 1)); } void displayProgress(int progress) { int k = 0; for (k=0; k<106; k++) putchar('');//将当前行全部清空,用以显示最新的进度条状态 int j = 0; for (j=0; j<progress; j++) putchar('+');//打印进度条上已经完成的部分,用‘+’表示 for (j=1; j<=100-progress; j++) putchar('-');//打印进度条上还有多少没有完成的 fprintf(stdout, " %3d%%",progress); fflush(stdout); } void progress_init( progress_t *bar, char *title, int max, int style) { bar->chr = '#'; bar->title = title; bar->style = style; bar->max = max; bar->offset = 100 / (float)max; bar->pro = (char *) malloc(max+1); if ( style == PROGRESS_BGC_STYLE ) memset(bar->pro, 0x00, max+1); else { memset(bar->pro, 32, max); memset(bar->pro+max, 0x00, 1); } } void progress_show( progress_t *bar, int bit ) { // int val = (int)(bit * bar->max); int val = bit; switch ( bar->style ) { case PROGRESS_NUM_STYLE: printf("