需要用C编写代码 [英] Need to write code in C

查看:80
本文介绍了需要用C编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务3:在生产者和消费者之间发送一系列消息(30分)将代码从任务2复制到名为task3的新目录中。阶段1修改您的生产者,以便它可以处理一系列消息。 1.从stdin读取20个字符的消息,并按照任务2将其写入共享内存段.2。将消息打印到stdout。 3.睡10秒钟。 4.重复步骤1-3直到EOF(文件结束)。 5.分离并销毁该部分。阶段2修改您的使用者,以便它可以读取生产者编写的每条消息。 1.实现忙等待测试可用消息。 2.从共享内存段中读取消息并将其打印到stdout。 3.通过向每个位置写入NULL终止字符来清除段的内容。 4.重复步骤1-3,直到没有其他消息。 5.拆下段并退出。您需要确定消费者在新消息可用时如何检测。如果它过早地阅读该消息,它可能只会得到它的一部分;如果它读得太晚,生产者可能已经开始编写以下消息。此外,您还需要确定消费者何时不知道何时没有消息。由于共享内存段大小为32个字节,并且您只需要读取长度最多为20个字符(字节)的消息,因此您可以使用12个字节来获取其他信息。在报告中描述您的策略。与任务2一样,使用两个终端会话测试您的解决方案。阶段3一旦您的生产者和消费者在彼此之间传递消息,请使用top命令监视两个进程的CPU使用情况。在VirtualBox Ubuntu VM上,您可以执行以下命令,按CPU利用率对进程列表进行排序,并将过滤器列为以当前用户身份运行的进程。这应该可以更容易地监视生产者/消费者。在您的报告中讨论您的观察,并注意到生产者和消费者之间的差异。 $ top -u cnos -o%CPU



我尝试过:



我在编写此代码时遇到问题。我需要一些提示

Task 3: Send a series of messages between producer and consumer (30 marks) Copy your code from Task 2 into a new directory named task3. Stage 1 Modify your producer so it can handle a series of messages. 1. Read a 20 character message from stdin and write it to the shared memory segment as per Task 2. 2. Print the message to stdout. 3. Sleep for 10 seconds. 4. Repeat steps 1–3 until EOF (end of file). 5. Detach and destroy the segment. Stage 2 Modify your consumer so it can read each message written by the producer. 1. Implement busy waiting to test for an available message. 2. Read the message from the shared memory segment and print it to stdout. 3. Clear the contents of the segment by writing a NULL termination character to every location. 4. Repeat steps 1–3 until there are no more messages. 5. Detach the segment and exit. You will need to determine how the consumer can detect when a new message is available. If it reads the message too early, it may only get part of it; if it reads it too late, the producer may have started writing the following message. Furthermore, you also need to determine how the consumer will know when there are no more messages. As the shared memory segment size is 32 bytes and you are only required to read messages up to 20 characters (bytes) in length, you have 12 bytes available to use for other information. Describe your strategy in your report. As with Task 2, test your solution using two terminal sessions. Stage 3 Once your producer and consumer are passing messages between one another, use the top command to monitor the CPU usage of both processes. On the VirtualBox Ubuntu VM, you can execute the following command to sort the list of processes by CPU utilisation and filter to those running as the current user. This should make it easier to monitor the producer/consumer. Discuss your observations in your report, noting the differences between the producer and consumer. $ top -u cnos -o %CPU

What I have tried:

I am having problem on writing this code. I need some hint

推荐答案

top -u cnos -o%CPU



我是什么尝试过:



我在编写此代码时遇到问题。我需要一些提示
top -u cnos -o %CPU

What I have tried:

I am having problem on writing this code. I need some hint


这并不是对你的问题的平均精神回应。然而,它会显得很苛刻。



找另一个学习领域。如果你有任何机会在编写计算机程序方面有任何好处,你就有了解决方案 - 即使它有错误 - 向我们展示你的工作。更重要的是: 向你展示你的作品!



找到你擅长的东西,你喜欢的东西 - 或至少这两个中的一个。

现在帮自己一个忙 - 生活太短暂不能浪费。


This is not meant to be a mean spirited response to your question. It will, however seem harsh.

Find a another field of study. If you had any chance of being any good at all in writing computer programs you'd have a solution - even if it had bugs - to show us your work. More importantly:   to show you your work!

Find something you're good at and that you enjoy - or at least one of these two.
Do yourself a favor, now - life is too short to waste.


我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!首先仔细阅读问题并将其放在课程其余部分的上下文中。由于这似乎是一个考试问题,如果你现在无法弄清楚该做什么,那么要么你在实际课程中没有注意,要么你看得太远,需要缩小你直到你的技能水平提升为止的期望!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! Start by reading the question carefully and put in the context of the rest of your course. Since this would appear to be an exam question, if you can't work out what to do by now, then either you weren't paying any attention during the actual course, or you are looking too far ahead and need to scale back you expectations until your skill level catches up!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于需要用C编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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