双语:Shared-Memory Systems共享内存系统 生产者消费者简介

Typically, a shared-memory region resides in the address space of the process creating the shared-memory segment.

通常,一个共享内存区域存储在进程地址空间内,该进程创建共享内存段。

 

Other processes that wish to communicate using this shared-memory segment must attach it to their address space.

其他进程想要使用该共享内存段通信,就必须附加该共享内存段到他们的地址空间内。

 

The operating system tries to prevent one process from accessing another process’s memory.

操作系统会尝试阻止一个进程访问另外一个进程的内存。

 

Shared memory requires that two or more processes agree to remove this restriction.

共享内存要求两个或多个进程同意打破这个限制。

 

The form of the data and the location are determined by these processes and are not under the operating system’s control.

数据和存放地址的格式是由这些进程决定的,而不是有操作系统控制的。

 

The processes are also responsible for ensuring that they are not writing to the same location simultaneously.

进程同时需要保证他们不会同时对同一个地址进行写操作。

 

producer–consumer problem, which is a common paradigm for cooperating processes.

生产者和消费者问题是一个常见的进程合作的方式。

 

A producer process produces information that is consumed by a consumer process.

一个生产者进程生产信息,而消费者消进程耗这些信息。

 

For example, a compiler may produce assembly code that is consumed by an assembler.

例如:一个编译器生产汇编语言,由汇编编译器消耗这些汇编语言。

The assembler, in turn, may produce object modules that are consumed by the loader.

汇编编译器又生产对象模块,由加载器消耗这些模块。

 

We generally think of a server as a producer and a client as a consumer.

我们通常认为服务器是一个生产者,而客户端是一个消费者。

 

To allow producer and consumer processes to run concurrently, we must have available a buffer of items that can be filled by the producer and emptied by the consumer.

为了可以让生产者和消费者同时进行,我们必须有足够的缓冲区存储物品(items),可以由生产者填充items,消费者消耗items。

 

A producer can produce one item while the consumer is consuming another item.

生产者可以生产一个item,同时消费者可以消耗另外一个item。

 

The producer and consumer must be synchronized, Two types of buffers can be used: the unbounded buffer and the bounded buffer.

生产者和消费者必须同步,有两个缓冲区类型可以使用:无限缓冲和受限缓冲。

代码:

缓冲区,其实就是数据结果里面的循环列表数据类型,理解其实质之后,要如何方便使用,完全可以由自己编程控制的。

#define BUFFER SIZE 10
typedef struct {
. . .
}item;
item buffer[BUFFER SIZE];
int in = 0;
int out = 0;


生产者:

while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER SIZE) == out)
; /* do nothing */
buffer[in] = next produced;
in = (in + 1) % BUFFER SIZE;
}

消费者:

item next consumed;
while (true) {
while (in == out)
; /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
/* consume the item in next consumed */
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值