91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 516KB
    文件類型: .gz
    金幣: 1
    下載: 0 次
    發布日期: 2021-06-08
  • 語言: 其他
  • 標簽: 多線程??

資源簡介

這個文件包含三個文件夾,有自己寫的兩個版本以及一個網上的版本,操作步驟詳盡,注釋清晰

資源截圖

代碼片段和文件信息

#include
#include
#include
#include

#define?filenumbers?3//表示文件個數
#define?filenamenum?256//表示文件名最長為256
int?cal(char?filename[256]);
void?*thread_function(void?*arg);

int?wordcount=0;
int?sx=0;
char?filenames[filenumbers][filenamenum]={“file1““file2““file3“};

pthread_mutex_t??fileMutex;//定義一個互斥變量
int?main()
{ int?i=0;
int?flag;//用于判斷操作是不是正確
pthread_t?thread[3];
int?count1=0count2=0count3=0;

/*互斥鎖初始化*/
flag=pthread_mutex_init(&fileMutexNULL);
if(flag!=0)
{
perror(“Mutex?initalization?failed!\n“);
return?1;
}

/*create?pthread*/
for(i=0;i<3;i++)
{
flag=pthread_create(&thread[i]NULLthread_function(void*)i);
if(flag!=0)
{
perror(“Thread?creation?failed!\n“);
return?1;
}
}


pthread_join(thread[0](void**)&count1);
pthread_join(thread[1](void**)&count2);
pthread_join(thread[2](void**)&count3);

wordcount=count1+count2+count3;
printf(“總的單詞數:%d\n“wordcount);
return?0;
}





int?cal(char?filename[256])
{
bool?start;
int?count=0;//用于記錄單詞數
char?c;
long?int?offset;
FILE?*fp;
fp=fopen(filename“r“);
if(fp==NULL)
{
printf(“open?filed!\n“);
exit(EXIT_FAILURE);
}

fseek(fp0SEEK_SET);
start=true;//start=0用于表示單詞的開始start=1表示沒有
while(feof(fp)==0)//feof(fp)==0?表示還沒有到文件尾
{
fread(&c11fp);
if(c==‘?‘&&start==1)
{
start=1;
}
else?if(c!=‘?‘&&start==1)
{
start=0;
count++;
}
else?if(c==‘?‘&&start==0)
{
start=1;
}
}
printf(“%s“filename);
printf(“word?count:%d\n“count);
return?count;
}
void?*thread_function(void?*arg)
{
FILE?*fp;
if(((int)arg)==0)
{
fp=fopen(filenames[0]“rb“);
if(fp==NULL)
{
perror(“open?file1?falied!\n“);
//return?(void*)1;
exit(EXIT_FAILURE);
}
return?(void*)cal(“file1“);
}
else?if(((int)arg)==1)
{
fp=fopen(filenames[1]“rb“);
if(fp==NULL)
{
perror(“open?file2?falied!\n“);
//return?(void*)1;
exit(EXIT_FAILURE);
}
return?(void*)cal(“file2“);

}
else?if(((int)arg)==2)
{
fp=fopen(filenames[2]“rb“);
if(fp==NULL)
{
perror(“open?file3?falied!\n“);
//return?(void*)1;
exit(EXIT_FAILURE);
}
return?(void*)cal(“file3“);
}
}

評論

共有 條評論

相關資源