第30章 条件变量

Homework(Code):

        This homework lets you explore some real code that uses locks and condition variables to implement various forms of the producer/consumer queue discussed in the chapter. You’ll look at the real code, run it in various configurations, and use it to learn about what works and what doesn’t, as well as other intricacies. Read the README for details.

Questions:

1. Our first question focuses on main-two-cvs-while.c (the working solution). First, study the code. Do you think you have an understanding of what should happen when you run the program?

main-two-cvs-while.c是四个程序中唯一正确的一个,是对书中Figure 30.14: The Correct Producer/Consumer Synchronization的实现

2. Run with one producer and one consumer, and have the producer produce a few values. Start with a buffer (size 1), and then increase it. How does the behavior of the code change with larger buffers? (or does it?) What would you predict num full to be with different buffer sizes (e.g., -m 10) and different numbers of produced items (e.g., -l 100), when you change the consumer sleep string from default (no sleep) to -C 0,0,0,0,0,0,1?

我们设-l 3不变

只增加缓冲区大小,可以发现程序基本不变化,都是生产者生产一个,消费者立刻消费掉

增加了缓冲区的大小,但每次都只用到一个槽,其余的浪费掉了

./main-two-cvs-while -l 3 -m 1 -p 1 -c 1 -v

./main-two-cvs-while -l 3 -m 5 -p 1 -c 1 -v

./main-two-cvs-while -l 3 -m 10 -p 1 -c 1 -v 

如果按默认情况(不睡眠), 那么生产者生产一个,消费者立马消费掉;生产者再生产一个,消费者又立马消费掉

缓冲区大小为10,但每次只用到1个槽,其余的都空着,跟缓冲区大小为1没啥区别

生产100次,也只是循环了100次

./main-two-cvs-while -l 100 -m 10 -p 1 -c 1 -v

-C 0,0,0,0,0,0,1 意味让消费者线程在c6处睡眠1秒

可以看到生产者不断生产,直到把缓冲区填满;

然后消费者消费1个,生产者又生产一个,消费者再消费1个,循环往复

直到生产者生产完,然后消费者一个一个消费完 

./main-two-cvs-while -l 100 -m 10 -p 1 -c 1 -C 0,0,0,0,0,0,1 -v

3. If possible, run the code on different systems (e.g., a Mac and Linux). Do you see different behavior across these systems?

没有Mac啊······ 

4. Let’s look at some timings. How long do you think the following execution, with one producer, three consumers, a single-entry shared buffer, and each consumer pausing at point c3 for a second, will take? ./main-two-cvs-while -p 1 -c 3 -m 1 -C 0,0,0,1,0,0,0:0,0,0,1,0,0,0:0,0,0,1,0,0,0 -l 10 -v -t

总共用时13.05秒

./main-two-cvs-while -p 1 -c 3 -m 1 -C 0,0,0,1,0,0,0:0,0,0,1,0,0,0:0,0,0,1,0,0,0 -l 10 -v -t

5. Now change the size of the shared buffer to 3 (-m 3). Will this make any difference in the total time?

总共用时12.04秒

./main-two-cvs-while -p 1 -c 3 -m 3 -C 0,0,0,1,0,0,0:0,0,0,1,0,0,0:0,0,0,1,0,0,0 -l 10 -v -t

6. Now change the location of the sleep to c6 (this models a consumer taking something off the queue and then doing something with it), again using a single-entry buffer. What time do you predict in this case? ./main-two-cvs-while -p 1 -c 3 -m 1 -C 0,0,0,0,0,0,1:0,0,0,0,0,0,1:0,0,0,0,0,0,1 -l 10 -v -t

总共用时5.02秒

./main-two-cvs-while -p 1 -c 3 -m 1 -C 0,0,0,0,0,0,1:0,0,0,0,0,0,1:0,0,0,0,0,0,1 -l 10 -v -t

7. Finally, change the buffer size to 3 again (-m 3). What time do you predict now?

总共用时5.02秒

./main-two-cvs-while -p 1 -c 3 -m 3 -C 0,0,0,0,0,0,1:0,0,0,0,0,0,1:0,0,0,0,0,0,1 -l 10 -v -t

8. Now let’s look at main-one-cv-while.c. Can you configure a sleep string, assuming a single producer, one consumer, and a buffer of size 1, to cause a problem with this code? 

main-one-cv-while.c对应于书Figure 30.10: Producer/Consumer: Single CV And While

如果是一个生产者一个消费者,则不会产生问题

./main-one-cv-while -l 3 -p 1 -c 1 -m 1 -v

9. Now change the number of consumers to two. Can you construct sleep strings for the producer and the consumers so as to cause a problem in the code?

不一定能看到书上Figure 30.11: Thread Trace: Broken Solution (v2)的情况,因为不同操作系统线程调度不同,不能确保线程一定按书上这种顺序执行

./main-one-cv-while -l 3 -p 1 -c 2 -m 1 -P 0,0,0,10,0,0,0 -C 0,0,0,10,0,0,0:0,0,0,10,0,0,0 --v

10. Now examine main-two-cvs-if.c. Can you cause a problem to happen in this code? Again consider the case where there is only one consumer, and then the case where there is more than one.

相比于书上Figure 30.8: Producer/Consumer: Single CV And If Statement

这段代码用了两个条件变量,但用if而不是while(控制变量?)

如果是一个生产者一个消费者,则不会产生问题

但如果一个生产者两个消费者,就会产生书上Figure 30.9: Thread Trace: Broken Solution (v1)的问题

./main-two-cvs-if -p 1 -c 2 -m 1 -l 3 -v

 程序错误的原因在于:C0消费完缓冲区后,缓冲区为空;而因为是if循环而不是while循环,C1执行完c3语句后没有再次检查循环条件(num_full == 0),直接访问缓冲区,导致错误

11. Finally, examine main-two-cvs-while-extra-unlock.c. What problem arises when you release the lock before doing a put or a get? Can you reliably cause such a problem to happen, given the sleep strings? What bad thing can happen?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值