My

  

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

Copyright © Windows knowledge All Rights Reserved