• 大小: 11.96MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-12
  • 语言: 其他
  • 标签: 并行  openmp  

资源简介

机械工业出版社 并行程序设计导论 第五章练习题5.6自己写来练手的。

资源截图

代码片段和文件信息


#include “stdafx.h“
#include 
#include 
#include  
#include 
#include
#include
#include
#include  
#include 
#include   
using namespace std;
#define Nr  5
int main(){
queue d;

//string *d=new string[Nr];
/*for(int i=0;i {
d[i]=““;
}*/
//信号灯
int empty=5;//队列中的空闲空间大小
int full=0;//队列中已使用空间的大小

    int nthreads;
int tiditip;
int first;
omp_lock_t lockP;
//当前生产者的数目
int numberOfProductor=0;
//初始化锁
omp_init_lock(&lockP);
//线程总数目为4
omp_set_num_threads(4);
first=1;
//omp_in_parallel();
//omp_set_dynamic(20);


#pragma omp parallel  shared(demptyfullnumberOfProductor) private(tiditip) firstprivate(first) 
{
if(first==1)
{
tid=omp_get_thread_num();
first=0;
nthreads = omp_get_num_threads();
printf(“Number of threads = %d\n“ nthreads);
printf(“Thread %d starting...\n“ tid);
tip= tid % 2;
}
//偶数线程为生产者
if(tip==0)
{   // producator
  //omp_set_lock(&lockP);
stringstream strStream;  
strStream << tid;
string fileName;
string directPath;
 char   buffer[MAX_PATH];   
                    getcwd(buffer MAX_PATH);   
  directPath=buffer;
  fileName=directPath+“\\“+strStream.str()+“.txt“;
 //fileName=strStream.str()+“.txt“;
ifstream fin(fileName);
while(!fin.eof())
{
if(empty>0)
{


        #pragma omp critical
       {
    printf(“线程 %d 生产者 \n“tid);
printf(“生产者产生的数据为:“);
   string str;
   getline(finstr) ;
   d.push(str);
   cout<        empty--;
   full++;
        }
}
else
{
Sleep(1000);
}
}
printf(“线程 %d退出!\n“tid);
#pragma omp critical
{
numberOfProductor++;
 }
 //omp_unset_lock(&lockP);  
}
 else//奇数线程为消费者

  //omp_set_lock(&lockP);
    // consumator
   while(true)
   {
   if(numberOfProductor==2&&d.empty())
   {
   break;
  }
   if(full>0&&!d.empty())
   {
#pragma omp critical
{
printf(“线程 %d 消费者\n“tid);
char sentences[255];
string str;
str=d.front();
//d.pop();
str.copy(sentencesstr.length()0); 
sentences[str.length() + 1] = ‘\0‘;
char *tokenPtr=strtok(sentences“ “);
printf(“消费者分词后的数据为:\n“);
while(tokenPtr!=NULL)
{
 cout<  tokenPtr=strtok(NULL“ “);
}
full--;
empty++;
}


   }
   else
   {
   Sleep(100);
   }
   }
  
 }
 //omp_unset_lock(&lockP);  
//}
}

  
char end=getch();
}



 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\
     文件         246  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\1.txt
     文件         143  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\2.txt
     文件         172  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\3.txt
     文件         148  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\4.txt
     文件      122368  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\OpenMPP-C.exe
     文件     1069528  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\OpenMPP-C.ilk
     文件     1559552  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\Debug\OpenMPP-C.pdb
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\ipch\
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\ipch\openmpp-c-cd764e05\
     文件     2359296  2014-06-10 10:32  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\ipch\openmpp-c-cd764e05\openmpp-c-eb288db.ipch
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\
     文件    33116160  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C.sdf
     文件         894  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C.sln
     文件       18944  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C.suo
     文件         216  2014-06-10 10:18  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\0.txt
     文件         131  2014-06-10 10:17  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\2.txt
     目录           0  2014-06-10 10:36  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\
     文件         246  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\1.txt
     文件         143  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\2.txt
     文件         172  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\3.txt
     文件         148  2014-06-09 20:41  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\4.txt
     文件        3050  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\cl.command.1.tlog
     文件       37912  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\CL.read.1.tlog
     文件        1684  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\CL.write.1.tlog
     文件           2  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\link-cvtres.read.1.tlog
     文件           2  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\link-cvtres.write.1.tlog
     文件           2  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\link.11312-cvtres.read.1.tlog
     文件           2  2014-06-10 10:33  并行openmp程序生产者消费者-渠涧涛-104753130775\OpenMPP-C\OpenMPP-C\Debug\link.11312-cvtres.write.1.tlog
............此处省略33个文件信息

评论

共有 条评论