//定义一个结构体
struct buff_node
{
int node_id;//用来记录当前节点的id
char buff[blockSize];//用来记录数据的数组
buff_node *next;//用来记录下一结构体结点的id
};
//global.h
#include <cerrno>
#include <pthread.h>
#include <fcntl.h>
#define MAXBLOCK 250
#define BUFFNUM 200
#define blockSize 16
#ifndef GLOBAL_HEADER_H
#define GLOBAL_HEADER_H
#include <iostream>
#include <cstdio>
#include <cstdlib>
//定义三个全局变量
extern unsigned long blockIndex;
extern int fileFd;
extern int bufferNum;
//声明结构体相关的指针
extern buff_node *head; //定义指向结构体的头节点指针
extern buff_node *tail; //定义指向结构体的尾节点指针
extern buff_node *temp;
//声明三个函数
extern void bufferInit(void);
extern void *blkProducer(void *);
extern void *blkConsumer(void *);
//声明三个互斥量
extern pthread_mutex_t stderrMutex;
extern pthread_mutex_t stdoutMutex;
extern pthread_mutex_t bufferMutex;
//声明buffer的两个变量
extern pthread_cond_t bufferReadCond;
extern pthread_cond_t bufferWriteCond;
#endif
//global.cpp
#include "global.h"
unsigned long blockIndex= 0;
int fileFd;
int bufferNum = BUFFNUM;
buff_node *head;
buff_node *tail;
buff_node *temp;
void bufferInit(void);
void *blkProducer(void *);
void *blkConsumer(void *);
pthread_mutex_t stderrMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t stdoutMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t bufferMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t bufferReadCond = PTHREAD_COND_INITIALIZER;
pthread_cond_t bufferWriteCond = PTHREAD_COND_INITIALIZER;
//buffer.cpp
#include "global.h"
//对结构体buff_node进行初始化
void bufferInit(void)
{
head = (buff_node *)malloc(sizeof(buff_node));
head->node_id = 0;
head->next = head;
tail = head;//实现循环链表形式
return;
}
//生产者函数
#include <cstring>
#include "global.h"
using std::cout;
using std::cerr;
using std::endl;
void *blkProducer(void *producerNum)
{
int count;
int *nproducers = (int *)producerNum;
for (;;)
{
//对互斥量bufferMutex上锁
pthread_mutex_lock(&bufferMutex);
while (bufferNum == 0)
//等 待
pthread_cond_wait(&bufferWriteCond, &bufferMutex);
if (count > (MAXBLOCK + *nproducers))
{
//对互斥量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
break;
}
if ((temp = (buff_node *)(malloc(sizeof(buff_node)))) == NULL)
{
//对互斥量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
continue;
}
//对temp->buff赋值
memset(temp->buff, '1', blockSize);
temp->node_id = count = ++blockIndex;
temp->next = head;
tail->next = temp;
tail = temp;
--bufferNum;
//释放信号量
pthread_cond_signal(&bufferReadCond);
//对互斥量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
if (count <= MAXBLOCK)
{
//对互斥量stdoutMutex上锁
pthread_mutex_lock(&stdoutMutex);
cout << "post one block, block id is: " << count << endl;
//对互斥量stdoutMutex解锁
pthread_mutex_unlock(&stdoutMutex);
}
}
return NULL;
}
//消费者函数
#include <unistd.h>
#include <fstream>
#include "global.h"
using namespace std;
void *blkConsumer(void *produceNum)
{
buff_node *p;
int *nproducers = (int *)produceNum;
for (;;)
{
//对互斥量 bufferMutex上锁
pthread_mutex_lock(&bufferMutex);
while(bufferNum == BUFFNUM)
//等待
pthread_cond_wait(&bufferReadCond, &bufferMutex);
//改变相应指针
p = head->next;
head->next = p->next;
if (tail == p)
{
tail = head;
}
//bufferNum加一
bufferNum++;
//释放信号量
pthread_cond_signal(&bufferWriteCond);
//对信号量bufferMutex解锁
pthread_mutex_unlock(&bufferMutex);
//得到一个块
if (p->node_id <= MAXBLOCK)
{
pthread_mutex_lock(&stdoutMutex);
cout << "get one block,id is: " << p->node_id<<endl;
pthread_mutex_unlock(&stdoutMutex);
}
if (p->node_id > MAXBLOCK)
{
//释放指针p
free(p);
if (p->node_id == (MAXBLOCK + *nproducers))
//释放头节点
free(head);
}
return NULL;
}
//对块进行写
if (write(fileFd, temp->buff, blockSize) == -1)
{
//对互斥量stderrMutex上锁
pthread_mutex_lock(&stderrMutex);
cerr <<"write error,block id is: "<< p->node_id<<endl;
//对互斥量 stderrMutex解锁
pthread_mutex_unlock(&stderrMutex);
//释放指针p
free(p);
continue;
}
else
{
//对互斥量stdoutMutex上锁
pthread_mutex_lock(&stdoutMutex);
cout <<"wirte block succeed, block id: " <<p->node_id<<endl;
/对互斥量stdoutMutex解锁
pthread_mutex_unlock(&stdoutMutex);
//释放指针p
free(p);
}
}
return NULL;
}
// makefile 源文件
objects = main.o blkConsumer.o blkProducer.o \
buffer.o global.o
targets = parallel_copy
.PHONY:
all clean
all: $(targets)
parallel_copy: main.o blkConsumer.o blkProducer.o \
buffer.o global.o
$(CXX) $(CXXFLAGS) -pg -o $@ $^ -lpthread -pg
-rm *.gch
%.o: %.cpp global.h
$(CXX) $(CPPFLAGS) -pg -O2 -c $^ -D_THREAD_SAFE
clean:
-rm $(objects) $(targets)
//main.cpp
#include <cstring>
#include <unistd.h>
#include <sys/stat.h>
#include "global.h"
using namespace std;
int main(int argc, char **argv)
{
pthread_t *tids;
int cnt = 0;
int nProducers;
int nConsumers;
//记录当前变量Producers的内容
void *producerNum = &nProducers;
//提示输入参数,错误告诉用法
if (argc != 3)
{
cerr << "usage: " << argv[0]
<< " nProducers nConsumers" << endl;
return -1;
}
//自动把参数argv内容转换成整数
nProducers = atoi(argv[1]);
nConsumers = atoi(argv[2]);
//分配相应的内存空间
tids = (pthread_t *) malloc((nProducers + nConsumers) * sizeof(pthread_t));
//用来创建循环链表
bufferInit();
//用来打开文件
fileFd = open("output.res", O_CREAT | O_APPEND | O_TRUNC | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fileFd == -1)
{
perror("open file fialed");
return -1;
}
//用来创建设生产者
for (int i = 0; i < nProducers; ++i)
{
if(pthread_create(&tids[cnt++], NULL, blkProducer, producerNum) == -1)
perror("producer created failed");
}
//用来创建消费者
for (int i = 0; i < nConsumers; ++i)
{
if(pthread_create(&tids[cnt++], NULL, blkConsumer, producerNum) == -1)
perror(" consumer created failed");
}
//互斥量stdoutMutex上锁
pthread_mutex_lock(&stdoutMutex);
cout << "init success!" << endl;
//互斥量解锁
pthread_mutex_unlock(&stdoutMutex);
for (int i = 0; i < nProducers; ++i )
{
//pthread_join()函数,以阻塞的方式等待tids[i]线程结束
pthread_join(tids[i], NULL);
//对互斥量stdoutMutex上所
pthread_mutex_lock(&stdoutMutex);
cout << "the producer " << i << " exit" << endl;
//对互斥量解锁
pthread_mutex_unlock(&stdoutMutex);
}
for (int i = 0; i < nConsumers; ++i )
{
//以等待的方式等待tids[nProducers + i]线程结束
pthread_join(tids[nProducers + i], NULL);
//对互斥量stdoutMutex上锁
pthread_mutex_lock(&stdoutMutex);
cout << "the consumer " << i << " exit" << endl;
//对互斥量stdoutMutex解锁
pthread_mutex_unlock(&stdoutMutex);
}
return 0;
}
07-15
07-15
07-15
07-15
07-15
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交