- job8/pc.c: 使用条件变量解决生产者、计算者、消费者问题
- 题目
- 运行
- 代码
- 解BUG
- bug1
- bug2
- job8/pp.c: 使用条件变量实现 ping-pong 问题
- 题目
- 运行
- 代码
- 系统中有3个线程:生产者、计算者、消费者
- 系统中有2个容量为4的缓冲区:buffer1、buffer2
- 生产者
- 生产’a’、‘b’、‘c’、‘d’、‘e’、‘f’、‘g’、'h’八个字符
- 放入到buffer1
- 打印生产的字符
- 计算者
- 从buffer1取出字符
- 将小写字符转换为大写字符,按照 input:OUTPUT 的格式打印
- 放入到buffer2
- 消费者
- 从buffer2取出字符
- 打印取出的字符
- 程序输出结果(实际输出结果是交织的)
a
b
c
…
a:A
b:B
c:C
…
A
B
C
…
#include
#include
#include
#define CAPACITY 4
int buffer1[CAPACITY],buffer2[CAPACITY];
int in1,in2;
int out1,out2;
int buffer1_is_empty()
{
// printf("buffer1_is_empty:in1:%d,out1:%d\n",in1,out1);
return in1 == out1;
}
int buffer1_is_full()
{
// printf("buffer1_is_full:in1:%d,out1:%d\n",in1,out1);
return (in1 + 1) % CAPACITY == out1;
}
int get_item1()
{
int item;
item = buffer1[out1];
out1 = (out1 +1) %CAPACITY;
return item;
}
void put_item1(int item)
{
buffer1[in1] = item;
in1 = (in1 +1)%CAPACITY;
}
int buffer2_is_empty()
{
// printf("buffer2_is_empty:in2:%d,out2:%d\n",in2,out2);
return in2 == out2;
}
int buffer2_is_full()
{
// printf("buffer2_is_full:in2:%d,out2:%d\n",in2,out2);
return (in2 + 1) % CAPACITY == out2;
}
int get_item2()
{
int item;
item = buffer2[out2];
out2 = (out2 +1) %CAPACITY;
return item;
}
void put_item2(int item)
{
buffer2[in2] = item;
in2 = (in2 +1)%CAPACITY;
}
pthread_mutex_t mutex_pc,mutex_cc;
pthread_cond_t wait_empty_buffer1,wait_empty_buffer2;
pthread_cond_t wait_full_buffer1,wait_full_buffer2;
#define ITEM_COUNT (CAPACITY *2)
void *produce(void *arg)
{
int i;
int item;
for(i=0;i < ITEM_COUNT;i++)
{
pthread_mutex_lock(&mutex_pc);
while(buffer1_is_full())
{
pthread_cond_wait(&wait_empty_buffer1,&mutex_pc);
}
item = 'a' + i;
put_item1(item);
printf("%c\n",item);
pthread_cond_signal(&wait_full_buffer1);
pthread_mutex_unlock(&mutex_pc);
}
return NULL;
}
//a:A
void *commpute(void *arg)
{
int i;
int item,ITEM;
for(i=0;i<ITEM_COUNT;i++)
{
pthread_mutex_lock(&mutex_pc);
while(buffer1_is_empty())
{
pthread_cond_wait(&wait_full_buffer1,&mutex_pc);
}
item = get_item1();
pthread_cond_signal(&wait_empty_buffer1);
pthread_mutex_unlock(&mutex_pc);
pthread_mutex_lock(&mutex_cc);
while(buffer2_is_full())
{
pthread_cond_wait(&wait_empty_buffer2,&mutex_cc);
}
ITEM = item+'A'-'a';
put_item2(ITEM);
printf("\t%c:%c\n",item,ITEM);
pthread_cond_signal(&wait_full_buffer2);
pthread_mutex_unlock(&mutex_cc);
}
return NULL;
}
void *consume(void *arg)
{
int i;
int item;
for(i = 0;i<ITEM_COUNT;i++)
{
pthread_mutex_lock(&mutex_cc);
while(buffer2_is_empty())
{
pthread_cond_wait(&wait_full_buffer2,&mutex_cc);
}
item = get_item2();
printf("\t\t%c\n",item);
pthread_cond_signal(&wait_empty_buffer2);
pthread_mutex_unlock(&mutex_cc);
}
return NULL;
}
int main()
{
pthread_t computer_tid;
pthread_t consumer_tid;
pthread_mutex_init(&mutex_pc,NULL);
pthread_mutex_init(&mutex_cc,NULL);
pthread_cond_init(&wait_empty_buffer1,NULL);
pthread_cond_init(&wait_full_buffer1,NULL);
pthread_cond_init(&wait_empty_buffer2,NULL);
pthread_cond_init(&wait_full_buffer2,NULL);
pthread_create(&computer_tid,NULL,commpute,NULL);
pthread_create(&consumer_tid,NULL,consume,NULL);
produce(NULL);
pthread_join(computer_tid,NULL);
pthread_join(consumer_tid,NULL);
return 0;
}
解BUG
bug1
函数传参全局变量,改不了全局变量,可以直接在函数里面修改全局变量,不要传参了。多写buffer2_is_empty(),buffer2_is_full(),put_item2(),get_item2()函数。
额。。。函数调用的时候忘记加括号了,buffer_is_full ->buffer_is_full()
- 系统中有2个线程:ping 线程和 pong 线程
- ping 线程先执行
- ping 线程执行流程如下
- 打印输出 ping
- 等待 pong 线程输出
- 执行第 1 步
- pong 线程执行流程如下
- 打印输出 pong
- 等待 ping 线程输出
- 执行第 1 步
- 程序输出结果
ping
pong
ping
pong
…
#include
#include
#include
pthread_mutex_t mutex;
pthread_cond_t wait_ping_print;
pthread_cond_t wait_pong_print;
int p = 1;
void *ping(void *arg)
{
int i;
for(i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
while(p==0)
{
pthread_cond_wait(&wait_pong_print,&mutex);
}
p=0;
printf("ping\n");
pthread_cond_signal(&wait_ping_print);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void *pong(void *arg)
{
int i;
for(i=0;i<10;i++)
{
pthread_mutex_lock(&mutex);
while(p==1)
{
pthread_cond_wait(&wait_ping_print,&mutex);
}
p=1;
printf("pong\n");
pthread_cond_signal(&wait_pong_print);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main()
{
pthread_t ping_tid;
pthread_t pong_tid;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&wait_ping_print,NULL);
pthread_cond_init(&wait_pong_print,NULL);
pthread_create(&ping_tid,NULL,ping,NULL);
pthread_create(&pong_tid,NULL,pong,NULL);
pthread_join(ping_tid,NULL);
pthread_join(pong_tid,NULL);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)