测试NAND Flash稳定性小程序

下面的程序可以用来测试NAND Flash 稳定性,主要思想是反复读写NAND Flash上的文件及对比读写是否一致,最后统计结果。

test.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>   
#include <sys/types.h> 
#include <memory.h>


int main(int argc, char **argv)
{
if(argc < 4)
{
printf("argc is not enough, exit ! \n");
printf("Run as: ./test srcFile destFile loop \n");
exit(1);
}
     
int i = 0;
pid_t status;
int cmd1_fork_err = 0;
int cmd1_err = 0;
int cmd2_fork_err = 0;
int cmd2_err = 0;
int cmd22_fork_err = 0;
int cmd22_err = 0;
    int diff_count = 0;
int cmd3_fork_err = 0;
int cmd3_err = 0;


char *src = argv[1];
char *dest = argv[2];
    int len1 = strlen(src);
int len2= strlen(dest);
int loop = atoi(argv[3]);


char *cp = "cp ";
char *rm = "rm ";
char *md5sum = "md5sum ";
char *to = " > ";
char *log1 = "1.txt";
char *log2 = "2.txt";


char tmp1[200] = {'\0'};
char tmp2[200] = {'\0'};
char tmp22[200] = {'\0'};
    char tmp3[200] = {'\0'};
char *cmd1 = tmp1;
char *cmd2 = tmp2;
char *cmd22 = tmp22;
char *cmd3 = tmp3;


    char tmpBuf[40] = {'\0'};
    char tmpBuf1[40] = {'\0'};
    char *md51 = tmpBuf;
    char *md52 = tmpBuf1;
    FILE *fp;
        
strcpy(tmp1, cp);
strcpy(&tmp1[3], src);
tmp1[3 + len1] = ' ';
strcpy(&tmp1[4 + len1], dest);
printf("cmd1 %s \n", cmd1);

strcpy(tmp2, md5sum);
strcpy(&tmp2[7], src);
strcpy(&tmp2[7 + len1], to);
    strcpy(&tmp2[7 + len1 + 3], log1);
printf("cmd2 %s \n", cmd2);
    
strcpy(tmp22, md5sum);
strcpy(&tmp22[7], dest);
strcpy(&tmp22[7 + len2], to);
strcpy(&tmp22[7 + len2 + 3], log2);
printf("cmd22 %s \n", cmd22);


    strcpy(tmp3, rm);
strcpy(&tmp3[3], dest);
printf("cmd3 %s \n", cmd3);
      
for(i = 0; i < loop; i++)
{
status = system(cmd1);
if (-1 == status) 
{   
printf("%s fork fail \n", cmd1);
cmd1_fork_err++;
printf("system error! \n");  
}
else
{
//printf("%s exit status value = [0x%x]\n", cmd1, status);
if (WIFEXITED(status))
{
if (0 == WEXITSTATUS(status))  
{  
printf("%s run shell script successfully.\n", cmd1);            
}  
else  
{  
cmd1_err++;
                 printf("%s run shell script fail, script exit code: %d\n", cmd1, WEXITSTATUS(status));
}
}
else 
{
cmd1_err++;
printf("%s exit status = [%d]\n", cmd1, WEXITSTATUS(status));
}
}
sleep(3);
printf("\n");
        
status = system(cmd2);
if (-1 == status)
{
printf("%s fork fail \n", cmd2);
cmd2_fork_err++;
printf("system error! \n");
}
else
{
//printf("%s exit status value = [0x%x]\n", cmd2, status);
if (WIFEXITED(status))
{
if (0 == WEXITSTATUS(status))
{
printf("%s run shell script successfully.\n", cmd2);
}
else
{
cmd2_err++;
printf("%s run shell script fail, script exit code: %d\n", cmd2, WEXITSTATUS(status));
}
}
else 
{
cmd2_err++;
printf("%s exit status = [%d]\n", cmd2, WEXITSTATUS(status));
}
}
   sleep(3);
        printf("\n");


status = system(cmd22);
if (-1 == status)
{
printf("%s fork fail \n", cmd22);
cmd22_fork_err++;
printf("system error! \n");
}
else
{
//printf("%s exit status value = [0x%x]\n", cmd22, status);
if (WIFEXITED(status))
{
if (0 == WEXITSTATUS(status))
{
printf("%s run shell script successfully.\n", cmd22);
}
else
{
cmd22_err++;
printf("%s run shell script fail, script exit code: %d\n", cmd22, WEXITSTATUS(status));
}
}
else
{
cmd22_err++;
printf("%s exit status = [%d]\n", cmd22, WEXITSTATUS(status));
}
}
sleep(3);
printf("\n");

     if((fp = fopen(log1, "rb"))==NULL)
     {
     printf("Can not open file %s \n", log1);
     return 1;
     }
        fread(tmpBuf, sizeof(char), 32, fp);
        fclose(fp);
        printf("md51: %s \n", md51);
        
        if((fp = fopen(log2, "rb"))==NULL)
     {
     printf("Can not open file %s \n", log2);
     return 1;
     }
        fread(tmpBuf1, sizeof(char), 32, fp);
        fclose(fp);
        printf("md52: %s \n", md52);
        
        if(0 != memcmp(tmpBuf, tmpBuf1, 32))
        {
            diff_count++;
        }


status = system(cmd3);
if (-1 == status)
{
printf("%s fork fail \n", cmd3);
cmd3_fork_err++;
printf("system error! \n");
}
else 
{
//printf("%s exit status value = [0x%x]\n", cmd3, status);
if (WIFEXITED(status))
{
  if (0 == WEXITSTATUS(status))
   {
printf("%s run shell script successfully.\n", cmd3);
}
else
{
cmd3_err++;
printf("%s run shell script fail, script exit code: %d\n", cmd3, WEXITSTATUS(status));
}
}
else
{
cmd3_err++;
printf("%s exit status = [%d]\n", cmd3, WEXITSTATUS(status));
}
}
sleep(3);


printf("%d complete! \n", (i+1));
}
printf("\n");


printf("Result: %s: fork error = %d, shell error = %d \n", cmd1, cmd1_fork_err, cmd1_err);
printf("Result: %s: fork error = %d, shell error = %d \n", cmd2, cmd2_fork_err, cmd2_err);
    printf("Result: %s: fork error = %d, shell error = %d \n", cmd22, cmd22_fork_err, cmd22_err);
    printf("Result: diff_count = %d  \n", diff_count);
printf("Result: %s: fork error = %d, shell error = %d \n", cmd3, cmd3_fork_err, cmd3_err);


return 0;
}


Makefile:

test: test.c
mips-linux-gnu-gcc -EL test.c -o test


本文链接:https://my.lmcjl.com/post/9078.html

展开阅读全文

4 评论

留下您的评论.