📄 write.c
字号:
#include "apue.h"#include <unistd.h>#include <fcntl.h>#include <sys/times.h>#define FILENAMESIZE 80#define SIZE 1024#define TESTTIMES 8int main( int argc, char *argv[] ){ struct tms begin, end; clock_t Begin, End; char outfilename[FILENAMESIZE], * buffer; long filesize, loops, buffersize, n; pid_t outfile; double sys_click, stime, utime, total; if( argc == 2 ) { strcpy( outfilename, argv[1] ); if( ( outfile = open( outfilename, O_WRONLY | O_CREAT | O_TRUNC, 0644 ) ) == -1 ) err_sys( "Open outfile error!\n" ); } else if( argc == 3 ) { if( strcmp( argv[2], "sync" ) != 0 ) err_sys( "Arguments error!\n" ); else { strcpy( outfilename, argv[1] ); if( ( outfile = open( outfilename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, 0644 ) ) == -1 ) err_sys( "Open outfile error!\n" ); } } else err_sys( "Argument error!\n" ); if( ( filesize = lseek( STDIN_FILENO, 0, SEEK_END ) ) == -1 ) err_sys( "SEEK_END file error!\n" ); if( ( buffer = (char *)malloc( filesize * sizeof(char) + 1 ) ) == NULL ) err_sys( "Malloc error!\n" ); if( lseek( STDIN_FILENO, 0, SEEK_SET ) == -1 ) err_sys( "SEEK_SET file error!\n" ); if( read( STDIN_FILENO, buffer, filesize ) != filesize ) err_sys( "Read file error!\n" ); buffer[filesize] = '\0' ; sys_click = sysconf( _SC_CLK_TCK ); printf(" BUFFERSIZE \tUsercpu_time(s)\t Syscpu_time(s)\t Clock_time(s)\t Loop_times \n"); for( buffersize = SIZE, n = 0; n < TESTTIMES; buffersize *= 2, n++ ) { loops = 0; Begin = times( &begin ); lseek( outfile, 0, SEEK_SET ); while( loops * buffersize < filesize ) { if( write( outfile, buffer + loops * buffersize, buffersize ) == -1 ) err_sys( "Write error!\n" ); loops++; } End = times( &end ); utime = (double)( end.tms_utime - begin.tms_utime ) / sys_click; stime = (double)( end.tms_stime - begin.tms_stime ) / sys_click; total = (double)( End - Begin ) / sys_click; printf( " %11d\t %4.6f\t %4.6f\t %4.6f\t%10d\n", buffersize, utime, stime, total, loops ); } free( buffer ); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -