需要有关unix / c程序的帮助 [英] Need help with a unix/c program

查看:54
本文介绍了需要有关unix / c程序的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 程序首先创建一个共享内存缓冲区,其中包含一个20个整数的
数组。

- 缓冲区的每个插槽可以有0或1,其中0代表一个


空槽,1

代表一个占用的槽。


- 最初,缓冲区是空。因此,所有的插槽都被初始化了

和0.


- 然后程序创建了一个由2个子进程组成的粉丝(制作人和

consumer)。


- 两个进程都进入while循环,并在
$ b $中以
共享缓冲区的形式迭代运行b独家方式。


- 使用3个信号量s,n和e;


- s:缓冲区锁定(提供互斥访问)共享

缓冲区)


- n:当前缓冲项数量(迫使消费者在

缓冲区为空时等待)


- e:当前空插槽的数量(当

缓冲区已满时强制生产者等待)


- 另外,使用缓冲区索引''''让生产者知道在哪里将
放入

新项目和索引

''out''让消费者知道在哪里拍摄现有物品。


- 制片人:


- 每次迭代,生产者将新项目(整数1)放入

缓冲区。新项目必须放在

占用项目结尾旁边的插槽中。如果

缓冲区已满,则该过程将被阻止,直到消费者

至少需要一件物品。


- 在添加新项目之后,打印出

缓冲区的更新内容到stderr打印时,它必须经过一个循环,打印

一个
$一次b $ b字符。打印完每个角色后,打电话给我们睡觉(100)


看看它是否被打断(一定不能 - 请注意这个打印

任务也必须是

包含在关键部分,这样它就不会受到其他

流程的干扰。)


- 打印时,请指定谁在打印(制片人或消费者)。并且



缓冲区应按以下方式打印出来(注意空白

插槽代表

作为点)。

[制片人] 1 1 1 1 1 1 1。 。 。 。 。 。 。 。 。 1 1 1 1


- 打印后,拨打usleep(pro_time)来控制

缓冲区增长的速度。


- 消费者:


- 在每次迭代中,消费者从缓冲区中获取现有项目(整数1)

。现有物品必须从

开头占用

物品的插槽中取出。这可以通过简单地在索引处替换为0来模拟。

如果缓冲区

已经为空,则该过程被阻止,直到生产者放置

至少一件商品。


- 在删除商品后,将

缓冲区的更新内容打印到stderr。打印时,它必须经过一个

循环,一次打印一个字符。打印完每个角色后,

打电话给usleep(100)

看看它是否被打断(一定不能 - 请注意这个打印

任务也必须是

包含在关键部分,这样它就不会被

其他过程打断。)


- 打印时,请遵循上面列出的惯例(在制作人

部分)。


- 打印后,调用usleep(con_time)来控制

缓冲区收缩。


- 注意,由于我们使用有界缓冲系统,缓冲区必须是实现为循环 br />
缓冲区(缓冲区的末尾连接到缓冲区的开头)。


- 最初,使con_time的时间是pro_time的三倍,这样


缓冲区可以快速填充(例如,pro_time = 100000,con_time

= 300000)。一旦它被填充了b $ b,就切换它们的速度,这样现在消费者可以快速清空缓冲区(b / b
)再次切换速度,这样缓冲区迅速增长)。重复这个

填充空循环两次,然后

然后终止两个子进程(当缓冲区为空时为第二次

)。父母

进程必须等待两个子进程终止,然后

自行终止。


- 打印示例结果可能如下:


[家长]开始......

....

[监制] 1 1 1 1 。 。 。 。 。 。 。 1 1 1 1 1 1 1 1

[消费者] 1 1 1 1。 。 。 。 。 。 。 。 。 1 1 1 1 1 1 1

....

[监制] 1 1 1 1 1 1 1 1 1 1 1 1 1。 1 1 1 1 1 1

[监制] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[消费者] 1 1 1 1 1 1 1 1 1 1 1 1 1 1。 1 1 1 1 1


....

[消费者]两个周期完成。退出......

[监制]两个周期完成。退出...

[家长]退出...

- The program first creates a shared memory buffer containing an array
of 20 integers.
- Each slot of the buffer can have either 0 or 1, where 0 represents an

empty slot, and 1
represents an occupied one.

- Initially, the buffer is empty. Thus, all the slots are initialized
with 0.

- Then the program creates a fan of 2 child processes (producer and
consumer).

- Both processes enter a while loop, and iteratively operate on the
shared buffer in a mutually
exclusive manner.

- use 3 semaphores s, n, and e;

- s: buffer lock (provides mutual exclusion for accessing the shared
buffer)

- n: current number of buffer items (forces consumer to wait when the
buffer is empty)

- e: current number of empty slots (forces producer to wait when the
buffer is full)

- Also, use the buffer index ''in'' to let the producer know where to
put
the new item, and index
''out'' to let the consumer know where to take an existing item.

- Producer:

- In each iteration, the producer puts a new item (integer 1) to the
buffer. The new item must be placed in the slot next to the end of
occupied items. If the
buffer is already full, then the process is blocked until the consumer
takes at least one item.

- Right after adding a new item, print out the updated contents of the
buffer to stderr When printing, it must go through a loop, printing
one
character at a time. After printing each character, call usleep(100) to

see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by other
process).

- When printing, specify who''s printing (Producer or Consumer). And
the
buffer should be printed out in the following way (note that empty
slots are represented
as dots).
[Producer] 1 1 1 1 1 1 1 . . . . . . . . . 1 1 1 1

- After the printing, call usleep(pro_time) to control the speed of
buffer growing.

- Consumer:

- In each iteration, the consumer takes an existing item (integer 1)
from the buffer. The existing item must be taken from the slot at the
beginning of occupied
items. This is simulated by simply replacing 1 with 0 at the index out.
If the buffer
is already empty, then the process is blocked until the producer puts
at least one item.

- Right after removing an item, print out the updated contents of the
buffer to stderr . When printing, it must go through a
loop, printing one character at a time. After printing each character,
call usleep(100) to
see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by
other process).

- When printing, follow the convention written above (in Producer
section).

- After the printing, call usleep(con_time) to control the speed of
buffer shrinking.

- Note that since we use a bounded buffer system, the buffer must be
implemented as a circular
buffer (the end of the buffer is connected to the start of the buffer).

- Initially, make con_time three times as long as pro_time, so that the

buffer can be quickly filled (for example, pro_time = 100000, con_time
= 300000). Once it is
filled, switch their speed so that now the consumer can quickly empty
the buffer (then
switch their speed again so that the buffer quickly grows). Repeat this
fill-empty cycle twice, and
then terminate both child processes (when the buffer is empty for the
third time). The parent
process must wait for the termination of both child processes, and then
terminate itself.

- An example printing result could be as follows:

[Parent] Starting...
....
[Producer] 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 . . . . . . . . . 1 1 1 1 1 1 1
....
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1 1
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1

....
[Consumer] Two cycles completed. Exiting...
[Producer] Two cycles completed. Exiting...
[Parent] Exiting...

推荐答案

pr ************ @ yahoo.com 写道:

- 程序首先创建一个包含数组的共享内存缓冲区

的20个整数。
- The program first creates a shared memory buffer containing an array
of 20 integers.



这看起来非常像家庭作业....


即使它不是,它也是太平台特定的热门话题,请尝试

comp.unix.programmer(假设它不是作业,它会被反弹

那里)。 />

-

Ian Collins。

This looks very much like homework....

Even if it isn''t, it''s too platform specific to be topical here, try
comp.unix.programmer (assuming it isn''t homework, which will get bounced
there as well).

--
Ian Collins.


Ian Collins写道:
Ian Collins wrote:
pr ************ @ yahoo.com 写道:

- 程序首先创建一个包含数组的共享内存缓冲区

的20个整数。
- The program first creates a shared memory buffer containing an array
of 20 integers.



这看起来非常像家庭作业....


This looks very much like homework....



它是。如果问题有任意要求,例如你必须使用

三个信号量称为s,n和e ......那是'

懒惰混蛋的绝对标志。


我质疑这些人的道德观。通过学校不是存在的全部。如果你在学校外面是一个无用的愚昧人,你最终会被发现并被边缘化。不妨在你上学的时候学习一些东西。


Tom

It is. If the question has arbitrary requirements like "you must use
three semaphores called s, n and e..." that''s your surefire sign of a
lazy bastard.

I question the ethics of people like these. Passing school is not the
be-all of existence. If you''re a useless ignorant outside of school
you eventually get found out and marginalized anyways. Might as well
learn something while you''re in school.

Tom


在文章< 11 ********************** @ m7g2000cwm.googlegroups中。 com>,

Tom St Denis< to ******** @ gmail.comwrote:
In article <11**********************@m7g2000cwm.googlegroups. com>,
Tom St Denis <to********@gmail.comwrote:

> Ian Collins写道:
>Ian Collins wrote:

> pr *** *********@yahoo.com 写道:

- 程序首先创建一个包含数组的共享内存缓冲区

的20个整数。
- The program first creates a shared memory buffer containing an array
of 20 integers.


这看起来非常像家庭作业....


This looks very much like homework....


它是。如果问题有任意要求,例如你必须使用
三个信号量称为s,n和e ......那是一个懒散的混蛋的绝对标志。

我质疑这些人的道德观。通过学校不是存在的全部。如果你是学校外无用的无知
你最终会被发现并被边缘化。不妨在你上学的时候学点东西。


It is. If the question has arbitrary requirements like "you must use
three semaphores called s, n and e..." that''s your surefire sign of a
lazy bastard.

I question the ethics of people like these. Passing school is not the
be-all of existence. If you''re a useless ignorant outside of school
you eventually get found out and marginalized anyways. Might as well
learn something while you''re in school.



虽然我不同意你所说的任何事情,但我总是倾向于b / b
想知道以下内容:在某些学生中,有什么东西本来就更糟糕的试图将一个人放在他的老师身上(通过作弊,例如,通过在Usenet上发布作弊,而不是一些工作的schmoe试图)把一个

放在他的雇员身上(通过作弊等)?


说真的,如果你考虑一下,任何争论可以用来

证明以后的合理性,可以很容易地用来证明前者的合理性。

While I don''t disagree with anything you''ve said, I''m always prone to
wonder the following: Is there anything inherently worse in some
student schmoe trying to put one over on his teachers (by cheating,
e.g., by posting on Usenet) than some working schmoe trying to put one
over on his employeers (by cheating, etc.) ?

Seriously, if you think about it, any arguements that can be used to
justify the later, can just as easily be used to justify the former.


这篇关于需要有关unix / c程序的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆