16.3 In the famous dining philosophers problem, a bunch of philosophers are sitting around a circular table with one chopstick between each of them. A philosopher needs both chopsticks to eat, and always picks up the left chopstick before the right o…
哲学家进餐问题是这样子的:好几位哲学家围绕着餐桌坐,每一位哲学家要么思考,要么等待,要么就吃饭.为了吃饭,哲学家必须拿起两支筷子(分放于左右两端).不幸的是,筷子的数量和哲学家相等,所以每支筷子必须由两位哲学家共享.图4-1 显现出这种状态.? FAQ 16:我如何避免死锁?…
16.6 You are given a class with synchronized method A and a normal method B. If you have two threads in one instance of a program, can they both execute A at the same time? Can they execute A and B at the same time? 当我们给一个方法加了synchronized关键字,我们确保了两个线…
16.5 Suppose we have the following code:public class Foo { public Foo() { . . . } public void first() { ... } public void second() { ... } public void thirdQ { ... }}The same instance of Foo will be passed to three different threads. ThreadA will cal…
16.4 Design a class which provides a lock only if there are no possible deadlocks. 有很多方法可以避免死锁的发生,一个常用的方法是列出所需要的锁,然后判断锁上这些锁后会不会发生死锁,比如有如下的锁的顺序: A = {1, 2, 3, 4} B = {1, 3, 5} C = {7, 5, 9, 2} 这是有可能产生死锁的,比如当A锁上2等待3,当B锁上3等待5,当C锁上5等待2,我们可以将其看做图,2连上3,3连上…
16.2 How would you measure the time spent in a context switch? 上下文转换发生在两个进程之间,比如让一个等待进程进入执行和让一个运行进程进入等待,这些在多任务中发生.操作系统需要把等待进程的信息放入内存和把当前运行的进程信息保存下来.为了解决这个问题,我们记录在切换进程时的最后和第一个指令的时间点,比如我们有两个进程P1和P2,P1在执行P2在等待,当我们切换P1和P2时,假设发生在P1的第N个指令,我们用tx,k来表示进程x的第k个…
16.1 What's the difference between a thread and a process? 进程Process是程序执行时的一个实例.一个进程是被分配系统资源的独立单元,每个进程在独立的地址空间上执行,如果需要使用其他进程的资源,需要使用进程间通讯,包括管道Pipes,文件Files,套接字Sockets,或者其他形式. 线程Thread存在于进程之中并分享进程的资源(包括堆空间).同一个进程中的多个线程分享同一个堆地址.这是和进程区别很大的地方,进程之间不能直接访问内…
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates…
@ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Functions 函数(3星) 第 4 章 Comments 注释(2星) 第 5 章 Formatting 格式 (1星) 第 6 章 Objects and Data Structures 对象和数据结构(4星) 第 7 章 Error Handling 错误处理(4星) 第 9 章 Unit Tests 单…
The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works with Python 3 or Python 2, and you need Pygame installed as well in order to run it. Click the animated gif to view a larger version. This is a tutorial…