正在加载图片...
Bounded-Buffer-Shared-Memory Solution Insert()Method while (true)( /Produce an item/ while (((in 1)%BUFFER SIZE)==out) :/*do nothing--no free buffers"/ buffer[in]item; in =(in +1)%BUFFER SIZE; Shared variables reside in a Remove()Method shared region while(true)( #define BUFFER_SIZE 10 while (in =out) typedef struct{ /do nothing--nothing to consume /remove an item from the buffer 】item: item buffer[out]: item buffer[BUFFER SIZE]: out =(out +1)%BUFFER SIZE; return item; int in =0;/index of the next empty buffer int out =0,//index of the next full buffer oSolution is correct,but can only use BUFFER SIZE-1 elements all empty?VS.all full? 陈话兰xlanchen@ustc.edu:cn http/staff.u0117401 Operating System操作系统原理 March29,20198/57. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bounded-Buffer – Shared-Memory Solution n-1 0 1 Shared variables reside in a shared region #define BUFFER_SIZE 10 typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in = 0; // index of the next empty buffer int out = 0; // index of the next full buffer Insert() Method while (true) { /* Produce an item */ while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing −− no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; } Remove() Method while (true) { while (in == out) ; // do nothing −− nothing to consume // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; } Solution is correct, but can only use BUFFER_SIZE-1 elements ▶ all empty? VS. all full? 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 8 / 57
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有