linux下多线程互斥量实现生产者--消费者问题和哲学家就餐问题

生产者消费者问题,又有界缓冲区问题。两个进程共享一个一个公共的固定大小的缓冲区。其中一个是生产者,将信息放入缓冲区,另一个是消费者,从缓冲区中取信息。

问题的关键在于缓冲区已满,而此时生产者还想往其中放入一个新的数据的情况。其解决办法是让生产者睡眠,待消费者从缓冲区中取出一个或多个数据时再唤醒它,同样的, 当消费者试图从缓冲区中取数据而发现缓冲区空时,消费者就睡眠,直到消费者向其中放一些数据后再将其唤醒。

上述方法可以用互斥量解决,程序代码:

 #include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h> //消费者进程
void *thread_consumer(void *ptr);
//生产者进程
void *thread_producer(void *ptr); #define MAX 100000 /*生产者需要生产的数量*/
pthread_mutex_t the_mutex; /*互斥量*/
pthread_cond_t condc,condp;/*生产者和消费者的线程条件变量*/ //初始时缓冲区中没有数据
int buffer=; int buffer_max=;/*用于记录缓冲区最多被用了多少*/ #define BUFFER_SIZE 10000 /*缓冲区大小*/ /*消费者线程*/
void *thread_consumer(void *arg)
{
int i;
//消费完指定数目 退出线程
for(i=;i<MAX;i++)
{
//互斥量加锁,线程同步
pthread_mutex_lock(&the_mutex);
/*缓冲区空 该进程睡眠 睡眠时pthread_cond_wait函数会对互斥量the_mutex解锁,这样生产者线程可以正常工作*/
while(buffer==)
{
pthread_cond_wait(&condc,&the_mutex);
}
//缓冲区非空 此时pthread_cond_wait又会对互斥量再次加锁
--buffer; //消耗一个元素
printf("consume an element, buffer=%d\n",buffer); //打印缓冲区中剩余元素
//如果缓冲区中元素个数为0 唤醒生产者线程
if(buffer==)
{
pthread_cond_signal(&condp);
}
//释放互斥量
pthread_mutex_unlock(&the_mutex);
}
pthread_exit(NULL);
} /*生产者线程*/
void *thread_producer(void *ptr)
{
int i;
//消费完指定数目 退出线程
for(i=;i<MAX;i++)
{
//互斥量加锁,线程同步
pthread_mutex_lock(&the_mutex);
//缓冲区满, 该进程睡眠 睡眠时pthread_cond_wait函数会对互斥量the_mutex解锁,这样消费者线程可以正常工作*/
while(buffer==BUFFER_SIZE)
{
pthread_cond_wait(&condp,&the_mutex);
}
// 缓冲区未满时 此时pthread_cond_wait又会对互斥量再次加锁
++buffer;//生产者增加一个数据
/*记录缓冲区最大的数据个数*/
if(buffer>buffer_max)
buffer_max=buffer;
printf("produce an element, buffer=%d\n",buffer);
//如果缓冲区中元素个数大于0 唤醒消费者线程
if(buffer>)
{
pthread_cond_signal(&condc);
}
//释放互斥量
pthread_mutex_unlock(&the_mutex);
}
} int main()
{
pthread_t pro,con;
//初始化互斥量
pthread_mutex_init(&the_mutex,);
//初始化线程条件变量
pthread_cond_init(&condc,);
pthread_cond_init(&condp,);
//创建生产者、消费者两个线程
pthread_create(&con,NULL,thread_consumer,NULL);
pthread_create(&pro,NULL,thread_producer,NULL);
//等待两个线程结束
pthread_join(pro,);
pthread_join(con,);
//清除变量
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp);
pthread_mutex_destroy(&the_mutex);
printf("buffer_max=%d",buffer_max);
}

在linux下运行时,可以看到两个线程交替运行,为了看到缓冲区最大能被添加到多少,我把缓冲区大小设置的很大,这样每次运行程序,打印的缓冲区的最大数都是不一样的,这跟实际的线程调度有关。

科学家就餐问题:

问题描述:假设有五位哲学家围坐在一张圆形餐桌旁,做以下两件事情之一:吃饭,或者思考。吃东西的时候,他们就停止思考,思考的时候也停止吃东西。餐桌中间有一大碗意大利面,每两个哲学家之间有一只餐叉。因为用一只餐叉很难吃到意大利面,所以假设哲学家必须用两只餐叉吃东西。他们只能使用自己左右手边的那两只餐叉。哲学家就餐问题有时也用米饭和筷子而不是意大利面和餐叉来描述,因为很明显,吃米饭必须用两根筷子。用下面的图描述很现实

linux下多线程互斥量实现生产者--消费者问题和哲学家就餐问题

问题解决:每个哲学家对应一个线程,程序中定义一个互斥量,对于每个线程进行访问其他哲学家状态时(关键代码)用互斥量进行加锁,这样也就避免了死锁的产生,访问到该哲学家处于饥饿时,同时旁边两位科学家并未处于进餐状态时,他就拿起左右两边的叉子进行吃饭,吃饭一段时间后,就放下叉子进行思考,思考一段时间后处于饥饿状态,重新开始试图拿起叉子吃饭,代码如下:

#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<time.h>
#define N 5 //哲学家数量 #define LEFT(i) (i+N-1)%N //左手边哲学家编号
#define RIGHT(i) (i+1)%N //右手边哲家编号 #define HUNGRY 0 //饥饿
#define THINKING 1 //思考
#define EATING 2 //吃饭 #define U_SECOND 1000000 //1秒对应的微秒数
pthread_mutex_t mutex; //互斥量 int state[N]; //记录每个哲学家状态
//每个哲学家的思考时间,吃饭时间,思考开始时间,吃饭开始时间
clock_t thinking_time[N], eating_time[N], start_eating_time[N], start_thinking_time[N];
//线程函数
void *thread_function(void *arg); int main()
{
pthread_mutex_init(&mutex, NULL); pthread_t a,b,c,d,e;
//为每一个哲学家开启一个线程,传递哲学家编号
pthread_create(&a,NULL,thread_function,"");
pthread_create(&b,NULL,thread_function,"");
pthread_create(&c,NULL,thread_function,"");
pthread_create(&d,NULL,thread_function,"");
pthread_create(&e,NULL,thread_function,"");
//初始化随机数种子
srand((unsigned int)(time(NULL)));
while()
{
;
}
} void *thread_function(void *arg)
{
char *a = (char *)arg;
int num = a[] - ''; //根据传递参数获取哲学家编号
int rand_time;
while()
{
//关键代码加锁
pthread_mutex_lock(&mutex);
//如果该哲学家处于饥饿 并且 左右两位哲学家都没有在吃饭 就拿起叉子吃饭
if(state[num] == HUNGRY && state[LEFT(num)] != EATING && state[RIGHT(num)] != EATING)
{
state[num] = EATING;
start_eating_time[num] = clock(); //记录开始吃饭时间
eating_time[num] = (rand() % + ) * U_SECOND; //随机生成吃饭时间
//输出状态
printf("state: %d %d %d %d %d\n",state[],state[],state[],state[],state[]);
//printf("%d is eating\n",num);
}
else if(state[num] == EATING)
{
//吃饭时间已到 ,开始思考
if(clock() - start_eating_time[num] >= eating_time[num]) //
{
state[num] = THINKING;
//printf("%d is thinking\n",num);
printf("state: %d %d %d %d %d\n",state[],state[],state[],state[],state[]);
start_thinking_time[num] = clock(); //记录开始思考时间
thinking_time[num] = (rand() % + ) * U_SECOND; //随机生成思考时间
}
}
else if(state[num] == THINKING)
{
//思考一定时间后,哲学家饿了,需要吃饭
if(clock() - start_thinking_time[num] >= thinking_time[num])
{
state[num] = HUNGRY;
printf("state: %d %d %d %d %d\n",state[],state[],state[],state[],state[]);
// printf("%d is hungry\n",num);
}
}
pthread_mutex_unlock(&mutex);
}
}
上一篇:FJUT第三周寒假作业《第九集,离间计》栈


下一篇:AngularJS中页面传参方法