2014-01-25 3 views
0

MB를 3 매개 변수로 사용하고 처리 시간을 제공하는 프로그램을 작성했습니다.버퍼 NULL !: 메모리를 할당 할 수 없습니다.

int main(int argc,char* argv[]){ 
double kb = 0; 
int time_taken = 0; 
double throughput = 0; 
int i = 0; 
int w_rw = 0; 
int fd = -1; 
int bw = -1; 
unsigned long long nob = 0; 
char ip; 
char* file_name = "myfile"; 
char* buff; 
struct stat info; 
struct timeval start_time,end_time; 
int mb = 1024 * 1024; 

/* Check the nos for W/RW and MBs*/ 
if(1 == argc){ 
     printf("\n Missing 2 params R/RW & MB,Eg: execute ./a.out 2 100 \n"); 
     return 1; 
} 
if(argc < 4){ 
     w_rw = atoi(argv[1]); 
     if(w_rw > 2){ 
       w_rw = 2; 
     } 
     nob = (atoi(argv[2]) * mb); 
     printf("\n W/RW : %d BYTES : %u \n",w_rw,nob); 
} 
else{ 
     // Do Nothing 
} 
/* Allocate Buffer */ 
buff = (char *)malloc(nob * sizeof(char)); 
if(NULL == buff){ 
     perror("Buffer NULL!"); 
     return -1; 
} 

printf("\n File - Create,Open,Write,Close \n"); 

for(i = 0; i < w_rw; i++){ 
     if(i == 0){ 
       printf("\n --- Write IO Performance--- \n"); 
     } 
     else{ 
       printf("\n --- Re-write IO Performance--- \n"); 
       printf("\n Press any char to continue : \n"); 
       ip = getchar(); 
     } 

     /* Open file*/ 
     if((fd = open(file_name,O_CREAT | O_RDWR))< 0){ 
       perror("Open failed!"); 
       return -1; 
     } 

     /*Calculating the start and end time of write*/ 
     gettimeofday(&start_time,NULL); 
     if((bw = write(fd,buff,nob)) < 0){ 
       perror("Write failed!"); 
       return -1; 
     } 
     gettimeofday(&end_time,NULL); 

     /*Calculating the throughput*/ 
     time_taken = (end_time.tv_sec - start_time.tv_sec); 
     kb = bw/1024; 
     throughput = kb/time_taken; 

     /* Start */ 
     if(-1 == stat(file_name,&info)){ 
       perror("STAT Failed"); 
       return -1; 
     } 

     printf("\n Inode no: %d \n Blocks : %d \n",info.st_ino,info.st_blocks); 
     printf("\n Start sec : %u \n",start_time.tv_sec); 
     printf("\n End sec : %u \n",end_time.tv_sec); 
     printf("\n Bytes written : %d bytes \n",bw); 
     printf("\n Time_taken : %d secs \n",time_taken); 
     printf("\n Throughput : %f kb/sec \n",throughput); 

     close(fd); 
} 

unlink(file_name); 
return 0; 
} 

O/P :

[RW GFS @ 루트] # 2 ./a.out 76800

W/RW : 2 바이트 : 3221225472 버퍼는 NULL! :

메모리를 할당 할 수
+0

은 시스템에 따라 다릅니다. –

+0

할당과 관련이있는 경우에는 'size_t'를 사용하는 것이 더 좋습니다. –

+0

또한'malloc'의 결과를 캐스팅하지 마십시오 : https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc –

답변

2

표준 (데스크톱/랩톱) 컴퓨터에서 예상대로 메모리 블록 (78GB!)과 malloc이 실패합니다.

는 사실, 당신은 :

int mb = 1024 * 1024; 
// ... 
nob = (atoi(argv[2]) * mb); 
// ... 
buff = (char *)malloc(nob * sizeof(char)); 

그래서 nob는 귀하의 경우 1024 * 1024 * 76800입니다.

작은 크기로 시도하면 효과가 있습니다.

+0

그러나 나는 약 100G의 더 큰 크기로 시험 할 필요가있다. 2TB 용량의 파일 시스템이 있습니다. 왜 그렇게 큰 크기를 만들 수 없습니까? 서명되지 않은 int 및 signed int는 문제가되지 않습니다. – Angus

+4

Nope. 요점은 ** 당신이 78GB의 RAM **을 요청하고 있다는 것입니다. 78GB 이상의 RAM이 있거나 컴퓨터가 가상 메모리에서 78G를 처리 할 수 ​​있음을 강력히 압니다. 요청한 크기의 합리적인 submultiple 인 버퍼 (예 : 32MB)를 할당 한 다음'for' 루프를 사용하여'n '번 쓰기로 목표를 달성 할 수 있습니다. –

관련 문제