Параллельное и распределенное программирование на С++
Шрифт:
40 {
41 DIR *DirP;
42 string Temp;
43 string FileName;
44 struct dirent *EntryP;
45 chdir(CurrentDir);
46 cout << «Searching Directory: " << CurrentDir << endl;
47 DirP = opendir(CurrentDir);
48
49 if(DirP == NULL){
50 cout << «could not open file» << endl;
51 return;
52 }
53 EntryP = readdir(DirP);
54 while(EntryP != NULL)
55 {
56 Temp.erase;
57 FileName.erase;
58 Temp = EntryP->d_name;
59 if((Temp != ".») && (Temp != "..»)){
60 FileName.assign(CurrentDir);
61 FileName.append(1,'/');
62 FileName.append(EntryP->d_name);
63 if(isDirectory(FileName)){
64 string NewDirectory;
65 NewDirectory = FileName;
66 depthFirstTraversal(NewDirectory.c_str);
67 }
68 else{
69 if(isRegular(FileName)){
70 int Flag;
71 Flag = FileName.find(".cpp»);
72 if(Flag > 0){
73 pthread_mutex_lock(&CountMutex);
74 FileCount++;
75 pthread_mutex_unlock(&CountMutex);
76 pthread_mutex_lock(&QueueMutex);
77 TextFiles.push(FileName);
78 pthread_mutex_unlock(&QueueMutex);
79 }
80 }
81 }
82
83 }
84 EntryP = readdir(DirP);
85 }
86 closedir(DirP);
87 }
88
89
90
91 void *task(void *X)
92 {
93 char *Directory;
94 Directory = static_cast<char *>(X);
95 depthFirstTraversal(Directory);
96 return(NULL);
97
98 }
Программа 4.6
// Программа 4.6
1 void *keySearch(void *X)
2 {
3 string Temp, Filename;
4 less<string> Comp;
5
6 while(!Keyfile.eof && Keyfile.good)
7 {
8 Keyfile >> Temp;
9 if(!Keyfile.eof){
10 KeyWords.insert(Temp);
11 }
12 }
13 Keyfile.close;
14
15 while(TextFiles.empty)
16 { }
17
18 while(!TextFiles.empty)
19 {
20 pthread_mutex_lock(&QueueMutex);
21 Filename = TextFiles.front;
22 TextFiles.pop;
23 pthread_mutex_unlock(&QueueMutex);
24 Infile.open(Filename.c_str);
25 SearchWords.erase(SearchWords.begin,SearchWords.end);
26
27 while(!Infile.eof && Infile.good)
28 {
29 Infile >> Temp;
30 SearchWords.insert(Temp);
31 }
32
33 Infile.close;
34 if(includes(SearchWords.begin,SearchWords.end,
KeyWords.begin,KeyWords.end,Comp)){
35 Outfile << Filename << endl;
36 pthread_mutex_lock(&CountMutex);
37 FileCount--;
38 pthread_mutex_unlock(&CountMutex);
39 FoundCount++;
40 }
41 }
42 return(NULL);
43
44 }
Программа 4.7
содержит основной поток для потоков модели «изготовитель-потребитель», реализованных в программах 4.5 и 4.6.// Программа 4.7
1 #include <sys/stat.h>
2 #include <fstream>
3 #include <queue>
4 #include <algorithm>
5 #include <pthread.h>
6 #include <iostream>
7 #include <set>
8
9 pthread_mutex_t QueueMutex = PTHREAD_MUTEX_INITIALIZER;
10 pthread_mutex_t CountMutex = PTHREAD_MUTEX_INITIALIZER;
11
12 int FileCount = 0;
13 int FoundCount = 0;
14
15 int keySearch(void);
16 queue<string> TextFiles;
17 set <string,less<string> >KeyWords;
18 set <string,less<string> >SearchWords;
19 ifstream Infile;
20 ofstream Outfile;
21 ifstream Keyfile;
22 string KeywordFile;
23 string OutFilename;
24 pthread_t Thread1;
25 pthread_t Thread2;
26
27 void depthFirstTraversal(const char *CurrentDir);
28 int isDirectory(string FileName);
29 int isRegular(string FileName);
30
31 int main(int argc, char *argv[])
32 {
33 if(argc != 4){
34 cerr << «need more info» << endl;
35 exit (1);
36 }
37
38 Outfile.open(argv[3],ios::app||ios::ate);
39 Keyfile.open(argv[2]);
40 pthread_create(&Thread1,NULL,task,argv[1]);
41 pthread_create(&Thread2,NULL,keySearch,argv[1]);
42 pthread_join(Thread1,NULL);
43 pthread_join(Thread2,NULL);
44 pthread_mutex_destroy(&CountMutex);
45 pthread_mutex_destroy(&QueueMutex);
46
47 cout << argv[1] << " contains " << FoundCount
<< " files that contains all keywords.» << endl;
48 return(0);
49 }
С помощью мьютексов доступ к разделяемой памяти для чтения или записи данных разрешается получить только одному потоку. Для гарантии безопасности работы функций, определенных пользователем, можно использовать и другие механизмы и методы, которые реализуют одну из моделей PRAM:
• EREW (монопольное чтение и монопольная запись)
• CREW (параллельное чтение и монопольная запись)
• ERCW (монопольное чтение и параллельная запись)
• CRCW (параллельное чтение и параллельная запись)
Мьютексы используются для реализации EREW-алгоритмов, которые рассматриваются в главе 5.
Безопасность использования потоков и библиотек
Климан (Klieman), Шах (Shah) и Смаалдерс (Smaalders) утверждали: