under Unix Copy a large text file, the contents of the file are prepared by yourself. The request is text, 10M or more.
(You can write a piece of text, then cat repeatedly appends the expanded text)
Form of the program: Enter the command and parameters $my_copy from_name to_name bufsize(number)
Depending on the The bufsize statistics, the running time of my_copy, give the analysis results and analyze the reasons.
When running my_copy, you need to synchronously collect the output of vmstat 1 and redirect it to log.txt.
bufsize:1 3264 1282565121024
Hint: This program cannot be developed with windows.
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/time. h>#include <unistd.h>
void tv_sub(struct timeval *end, struct timeval *begin) //calculate the time difference and store the result in the end {if((end->tv_usec-= Begin->tv_usec)<0){--end->tv_sec;end->tv_usec += 1000000;}end->tv_sec -= begin->tv_sec;}
int Main(int argc, char *argv[]){typedef struct timeval TIME;TIME begin, end;gettimeofday(&begin, NULL);
int f_read, f_write, num;const int BUFSIZE = atoi( Argv[3]);char buf[BUFSIZE];
if (argc != 4){printf("usage: my_read filename1 filename2 bufsize\ ");exit(1);}
if ((f_read = open(argv[1], O_RDONLY)) < 0){printf("cannot open file %s\ ", argv[1]);exit(1);}
if ((f_write = open(argv[2], O_WRONLY|
O_CREAT, 0644/*S_IREAD|
S_IWRITE|
S_IEXEC*/)) < 0) {printf("cannot open file %s\ ", argv[2]);exit(1);}
while ((num = read(f_read, Buf, BUFSIZE)) > 0){if (write(f_write, buf, num) != num){printf("write error!\ ");close(f_write);exit(1);}}if (num < 0){printf("read error!\ ");exit(1);}gettimeofday(&end, NULL);tv_sub(&end, &begin);printf("BUFSIZE =%d\\ttime cost: %u:%u\ ", BUFSIZE, end.tv_sec, end.tv_usec);
return 0;}
//***************************************
#makefile
#Note that the command line should start with TAB, here is the space
OBJ = main.omy_copy : $(OBJ)cc -o $@ $(OBJ)main.o : main.ccc -c Main.c
clean:rm my_copy $(OBJ)
//************************* *************
#shell example program, copy 000.txt to 001.txt
#!/bin/sh#vmstat.sh
echo "begin vmstat 1..."vmstat 1 >> log.txt &echo "begin read file..."
i=1while [ $i - Le 1024 ]do#echo "BUFSIZE is $i"./my_copy 000. Txt 001.txt $ii=`expr $i \\* 2`donepid=`ps |
Grep vmstat |
Awk '{print $1}'`kill $pid
According to statcounter statistics, the global operating system market share in March is still Win7
“ A real programmer does not need an IDE. They all use a text editor with a plugin to write c
If you have used Linux for a while and feel that the hard disk is not enough, you
ACL is an abbreviation of Access Control List. The main purpose is to provide detailed permission t
Linux primary and secondary device number introduction
Centos network card eth1 becomes eth0 modification method
Basic tutorial on the installation and use of SNMP under centos
Azure High Availability Architecture Design (LB and Traffic Manager)
Ubuntu founder: Enterprise Linux is not only Red Hat
Introduction to hashlimit module in iptables
How to open telnet function in Linux?
Shell environment variable setting tutorial
Overview of the Role of the Linux Kernel
Win8 system taskbar program icon color error error solution
Win10 RS2 Preview 14905 update fix content and known issues summary
Browser to solve the problem of web pages not showing pictures
What should I do if the Win8 desktop icon turns white? Win8 desktop icon whitening 2 solutions
Windows 7 tips Tips for booting items
Win7 notebook touchpad off graphic tutorial
Win7 system inserted into the U disk pop-up "scan and repair" is how it happened?
Retrieve lost icons in the Windows 8 system Metro interface
What should I do if the Win10 boot prompts a critical error?