标签:
1 #include<stdio.h> 2 #include<sys/mman.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<pthread.h> 6 #include<unistd.h> 7 #include<sys/types.h> 8 #include<fcntl.h> 9 #define Sun 5 10 #define DEF 1024*1024 11 typedef struct Date 12 { 13 14 char* r_begin; 15 char* w_begin; 16 int size; 17 }data; 18 int fid,mid,rid; 19 int count; 20 long long num; 21 char *witeid; 22 char *readid; 23 pthread_mutex_t mutex; 24 void sys_error(char* str) 25 { 26 perror(str); 27 exit(1); 28 } 29 void *mycapy(void * arg) 30 { 31 char* buf= (char*)malloc(sizeof(char)); 32 int n; 33 int i =0; 34 struct Date *dt = (struct Date*)arg; 35 char* readf = dt->r_begin; 36 char* writef = dt->w_begin; 37 for(i;i<dt->size;i++) 38 { 39 strncpy(buf,readf,1); 40 *writef=*buf; 41 pthread_mutex_lock(&mutex); 42 count++; 43 pthread_mutex_unlock(&mutex); 44 readf++; 45 writef++; 46 47 48 } 49 free(dt); 50 free(buf); 51 return(void*)0; 52 } 53 54 void Mypthreadcreate(int sum,int yu,int sleek) 55 { 56 int i; 57 pthread_t *pid =(pthread_t*) malloc(sizeof(pthread_t)*sum); 58 // pthread_t pid ; 59 for(i=0;i<sum;i++) 60 { 61 struct Date *da =(struct Date*)malloc(sizeof(struct Date)); 62 if(i==sum-1) 63 da->size = sleek+yu; 64 else 65 da->size = sleek; 66 da->r_begin = readid + i*sleek; 67 da->w_begin = witeid + i*sleek; 68 int clt =pthread_create(&pid[i],NULL,mycapy,(void*)da); 69 // int clt =pthread_create(&pid,NULL,mycapy,(void*)da); 70 if(clt!=0) 71 { 72 printf("pthread_cread error %s\n",strerror(clt)); 73 } 74 } 75 int k; 76 for(k=0;k<sum;k++) 77 { 78 pthread_join(pid[k],NULL); 79 } 80 81 free(pid); 82 } 83 void Mymmap(int sum,int sleek,int n ,int yuy, int yu) 84 { 85 int ii =0; 86 for(ii;ii<n;ii++) 87 { 88 int NUM = DEF; 89 int NM = DEF; 90 if(num<DEF) 91 NUM = num; 92 NM = NM*ii; 93 if(ii==(n-1)) 94 NUM = NUM+yuy; 95 readid =(char*) mmap(NULL,NUM,PROT_READ, MAP_SHARED,fid,NM); 96 if(readid==MAP_FAILED) 97 { 98 sys_error("mmap readid error"); 99 } 100 witeid= (char*)mmap(NULL,NUM,PROT_WRITE,MAP_SHARED,rid,NM); 101 if(witeid==MAP_FAILED) 102 { 103 sys_error("mmap witeid error"); 104 } 105 printf("wr %d\n",ii); 106 Mypthreadcreate(sum,yu,sleek); 107 munmap(readid,NUM); 108 munmap(witeid,NUM); 109 } 110 } 111 int main(int argv,char* argc[]) 112 { 113 int i; 114 int sleek; 115 int sum; 116 int yu; 117 118 if(argv<3) 119 { 120 printf("input like ./mycpy oldfile newfile"); 121 return 0; 122 } 123 124 125 126 127 fid = open(argc[1],O_RDONLY); 128 if(fid<0) 129 { 130 sys_error("open readfile error"); 131 } 132 num = lseek(fid,0,SEEK_END); 133 rid = open(argc[2],O_RDWR|O_CREAT,0777); 134 if(rid<0) 135 { 136 sys_error("open witefile error"); 137 } 138 long long ret = ftruncate(rid,num); 139 if(ret<0) 140 { 141 sys_error("ftruncate error"); 142 } 143 144 145 146 147 148 if(num <= DEF) 149 { 150 if(argv==4) 151 { int mm =atoi(argc[3]); 152 yu = num%mm; 153 sleek = num/mm; 154 sum = mm; 155 } 156 else 157 { 158 sleek = num/Sun; 159 sum = Sun; 160 yu = num%Sun; 161 } 162 Mymmap(sum,sleek,1,0,yu); 163 printf("r %d\n",999); 164 } 165 else if(num>DEF) 166 { 167 int NUM = DEF; 168 int yuy = num%NUM; 169 int n = num/NUM; 170 int ii =0; 171 printf("n = %d\n",n); 172 if(argv==4) 173 { int mm =atoi(argc[3]); 174 yu = NUM%mm; 175 sleek = NUM/mm; 176 sum = mm; 177 } 178 else 179 { 180 sleek = NUM/Sun; 181 sum = Sun; 182 yu = NUM%Sun; 183 } 184 Mymmap(sum,sleek,n,yuy,yu); 185 } 186 187 188 189 close(fid); 190 191 sleep(2); 192 return 0; 193 } 194 195
标签:
原文地址:http://www.cnblogs.com/lvlup/p/4908561.html