#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <mntent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <stdarg.h>
#include <regex.h>
#include <termios.h>
#include <stdint.h>
#include <stddef.h>
#include <netdb.h>
#include <features.h>
#include <ctype.h>
#include <errno.h>
#include <utime.h>
#include <assert.h>
/*for process*/
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <arpa/inet.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1*1024*1024
pthread_t thread11;
pthread_t thread22;
pthread_mutex_t mut;
long long get_ustime(void)
{
static char firsttimehere=1;
static struct timeval timeorigin;
struct timeval now;
// the first time, set the origin.
if (firsttimehere) {
gettimeofday(&timeorigin,NULL);
firsttimehere=0;
}
gettimeofday(&now,NULL);
return
(long long)(
(
(long long)now.tv_sec
-(long long)timeorigin.tv_sec
)*1000000LL
+
(
(long long)now.tv_usec
-(long long)timeorigin.tv_usec
)
);
}
/**测试写速度*/
void *thread1(void *args)
{
printf("thread1 : I'm write \n");
long long _SIZE_=1024*1024;
long long count=0,timeb=0,timee=0;
char *buf=NULL;
buf=(char *)malloc(_SIZE_*10);
memset(buf,0,sizeof(buf));
FILE *fp=NULL;
fp=fopen("testfile.c","a+");
timeb=get_ustime();
int i=0;
while(i<10)
{
i++;
fwrite(buf,_SIZE_,1,fp);
count+=_SIZE_;
if(count%(1024*1024)==0)
printf("%ld MB\n",count/(1024*1024));
}
if(fp)
fclose(fp);
timee=get_ustime();
printf("write rate=%3.2f Mbps,_SIZE_=%ld\n",(double)(count*8/(1024*1024))/((double)(timee-timeb)/1000000),_SIZE_);
printf("thread1 :主函数在等我完成任务吗?\n");
pthread_exit(NULL);
}
/**测试读速度*/
void *thread2(void *args)
{
printf("thread2 : I'm reading \n");
char *buf=NULL;
FILE *fp=NULL;
long long count=0,timeb=0,timee=0;
int _SIZE_=0;
//printf("long long size=%ld\n",sizeof(long long));
timeb=get_ustime();
if((fp=fopen(args,"rb"))==NULL)
{
//printf("A:Open :%s err \n",argv[1]);
return 0;
}
_SIZE_=1024*1024; //atoi(argv[2]);
buf=(char *)malloc(_SIZE_*sizeof(char));
printf("timeb=%lld,bufsize=%ld Bytes\n",timeb,_SIZE_);
while(!feof(fp))
{
memset(buf,0,sizeof(buf));
fread(buf,_SIZE_,1,fp);
count+=_SIZE_;
if(count%(1024*1024)==0)
printf("%ld MB\n",count/(1024*1024));
}
if(fp)
fclose(fp);
timee=get_ustime();
printf("fread file over,timee=%lld,filesize=%lld\n",timee,count);
printf("read rate=%3.2f Mbps,_SIZE_=%ld\n",(double)(count*8/(1024*1024))/((double)(timee-timeb)/1000000),_SIZE_);
if(buf!=NULL){
free(buf);
buf=NULL;
}
pthread_exit(NULL);
}
void thread_wait(void)
{
/*等待线程结束*/
// if(thread[0] !=0) { //comment4
pthread_join(thread11,NULL);
printf("线程1已经结束\n");
// }
//if(thread[1] !=0) { //comment5
pthread_join(thread22,NULL);
printf("线程2已经结束\n");
// }
}
int main(int argc, char *argv[])
{
/*用默认属性初始化互斥锁*/
//pthread_mutex_init(&mut,NULL);
printf("我是主函数哦,我正在创建线程,呵呵\n");
//thread_create();
int temp;
// memset(thread, 0, sizeof(thread)); //comment1
/*创建线程*/
if((pthread_create(&thread11, NULL, thread1, NULL)) != 0) { //comment2
printf("线程1创建失败!\n");
return 1;
}
else {
printf("线程1被创建\n");
}
if((temp = pthread_create(&thread22, NULL, thread2, (char*)argv[1])) != 0) { //comment3
printf("线程2创建失败");
}
else {
printf("线程2被创建\n");
}
printf("我是主函数哦,我正在等待线程完成任务阿,呵呵\n");
thread_wait();
return 0;
}