#include<stdio.h>
#include<pthread.h>
int MAX;
ptrhead_mutex_t the_mutex;
pthread_cond_t condc,condp1,condp2;
int buffer1=0,buffer2=0;
void*producer1(void *ptr)
{
int i;
for(i=1;i<MAX;i++)
{
pthread_mutex_lock(&the_mutex);
while(buffer!=0)pthread_cond_wait(&cndp1,&the_mutex);
buffer=i;
pthread_cond_signal(&condc);
pthread_mutex_unlock(&the_mutex);
}//end for
pthread_exit(0);
}
void*producer2(void *ptr)
{
int i;
for(i=MAX;i>0;i--)
{
pthread_mutex_lock(&the_mutex);
while(buffer!=0)pthread_cond_wait(&cndp2,&the_mutex);
buffer=i;
pthread_cond_signal(&condc);
pthread_mutex_unlock(&the_mutex);
}//end for
pthread_exit(0);
}
void*consumer(void*ptr)
{
int i;
for(i=1;i<=MAX;i++)
{
int a1=0,a2=0;
pthread_mutex_lock(&the_mutex);
while(buffer1==0) pthread_cond_wait(&condc,&the_mutex);
a1=buffer1;
buffer1=0;
while(buffer2==0) pthread_cond_wait(&condc,&the_mutex);
a2=buffer2;
buffer2=0;
pthread_cond_signal(&condp1);
pthread_cond_signal(&condp2);
pthread_mutex_unlock(&the_mutex);
}//end for
pthread_exit(0);
}
int main(int argc,char **agrv)
{
int max;
scanf("%d",&max);
MAX=max;
pthread_t pro1,pro2,con;
pthread_mutex_init(&the_mutex,0);
pthread_cond_init(&condc,0);
pthread_cond_init(&condp1,0);
pthread_cond_init(&condp2,0);
pthread_create(&con,0,consumer,0);
pthread_create(&pro1,0,producer1,0);
pthread_create(&pro2,0,producer2,0);
pthread_join(pro1,0);
pthread_join(pro2,0);
pthread_join(con,0);
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp2);
pthread_cond_destroy(&condp1);
pthread_mutex_destroy(&the_mutex);
}
分享到:
相关推荐
相当经典的主题 世界罕有 爱的跳动.nth 寰宇旷幻.nth 深蓝夜空.nth 小时候.nth
35个NOKIA官方nth主题.zip
mynokia.nth 诺基亚1681c主题
RouterOS 3.X中NTH的取值及相关定义
诺基亚nth主题 大家快来下载啊
RouterOS v3 NTH的负载均衡 中文 图文 教程
解决为何ROS3.X的NTH配置无法负载
ROS3.30 ADSL双线PCC及NTH负载均衡教程
ros3.22 adsl 5线nth负载均衡,自动限速完整版.rar )
今天我们就来“前瞻”一下CSS3的一个伪类选择器“:nth-child()”。 语法: :nth-child(an+b) 为什么选择她,因为我认为,这个选择器是最多学问的一个了。很可惜,据我所测,目前能较好地支持她的只有Opera9+和Safari...
ROS,5线NTH接近完美--脚本版本
\adsl 5线nth负载均衡,自动限速完整版
我是第1个p标签 我是第2个p标签</p> <!-- 希望这个变红 --> 然后两个选择器相对应的CSS代码如下: ...p:nth-child(2) { color: red;...对于:nth-child选择器,在简单白话文中,意味着选择一个元素:
手机主题 nth格式 s40 版 手机主题 nth格式 s40 版
在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type。 但是它们到底有什么区别呢?下面这篇文章主要就给介绍了CSS3中nth-child与nth-of-type的区别,需要的朋友可以参考...
ROS3.30 ADSL双线PCC及NTH负载均衡图文教程
nth-child和nth-of-type是css的两个伪选择符。应用中,这两者常常容易混淆。这里把它们拿出来仔细做个对比,看看这两者是怎么查找元素的。 nth-child(n) —— 寻找第n个子元素 nth-of-type(n) —— 寻找同一类型...
Ros双ADSL、NTH负载均衡实际操作设置(脚本化操作).pdf