C中的哲学问题代码 [英] Philosopher problem code in C

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

问题描述

我想要用于餐饮哲学家问题的c代码....请帮助



我尝试了什么:



寻找餐饮哲学家的代码

i want the c code for dining philosopher problem....plz help

What I have tried:

finding code for dining philosopher

推荐答案

对于这个问题找代码绝对没用。你根本不知道你面对的是什么。这是用于强调线程同步的整个问题区域的基本研究问题。



因此,这甚至不是应该解决的问题。首先,它应该遇到麻烦,目的是说明一种非常潜意识的死锁。与通常的死锁不同,当两个物体彼此等待时,所有的哲学家都可以进入状态(例如,通过随机定时来同步),它们在循环中等待彼此。而且,很容易看出,只有所有的哲学家聚集在一起才会发生(更准确地说,如果有多少哲学家和晚餐按照坐着的顺序来吃饭)。请参阅:用餐哲学家问题 - 维基百科,免费的百科全书 [ ^ ]。



难怪,这个问题可以看作是为学生们提供有益的学习练习。代表哲学家和分叉的方法有很多,真的很多种。只是提到范围:在典型的方法中,fork由具有互斥访问权限的共享资源表示(顺便说一下,互斥同步无法从死锁中保存这一事实的很好说明);但在其他方法中,每个分支以及哲学家都被视为一项单独的任务。在模型的重要条件是,逻辑的任何部分都不应该基于哲学家或分支的身份;无论如何都不应该识别它们。



研究问题通常假设您使用线程或进程创建这种情况的计算机模型,并且至少说明死锁。如果你说明一种或多种方法来避免死锁并证明它们的正确性会很好,但这并不是那么重要。说明问题的概率性质是很好的,这在实践中非常重要。很容易干扰哲学家在没有死锁的情况下为许多耳朵活着的方式进行计时,这种情况一下子就会发生。



我解释过在实际的编程部分,你需要做什么才能完成这项研究。但代码在哪里?



是的,我使用几种不同的环境和工具解决了这个问题,包括我自己实现的多线程系统(没有餐饮哲学家)任何概念证明都不能认真考虑:-)),但我不会为你写任何东西。 如果您使用其他人的代码,或者您在任何地方找到了这样的代码,那么它将等同于您的完全失败。这样,问题的整个目的就会被打败。要真正理解,你需要做的工作。这是一生一次的机会,因为只有一个餐饮哲学家的问题。 如果您找到并使用其他人创建的解决方案,请向餐饮哲学家说再见,或者说再见多线程



-SA
It's absolutely useless to "find code" for this problem. You just have no idea what you are facing. This is the fundamental study problem used to emphasize the whole problematic area of thread synchronization.

As such, this is not even a problem which is supposed to be "solved". First of all, it is supposed to run into a trouble, with the purpose of illustration of a very subliminal kind of deadlock. Unlike a "usual" deadlock, when two objects wait for each other, all of the philosophers can come the into state (say, by coming in sync as a result of random timing) that they wait for each other in cycle. Moreover, it's easy to see that it can only happen if all the philosophers come together (more exactly, if there are as many philosophers as the number of dinner come to dinner in the order of sitting). Please see: Dining philosophers problem — Wikipedia, the free encyclopedia[^].

No wonder, this problem can be considered as a useful study exercise for the students. There are many, really many different ways to represent philosophers and forks. Just to mention the range: in a typical approach, a fork is represented by a shared resource with mutual exclusion access (by the way, great illustration of the fact that mutual exclusion synchronization cannot "save" from deadlocks); but in other approach, each fork, as well as a philosopher, is considered as a separate task. On important condition of the model is that no part of the logic should be based on identity of a philosopher or a fork; they should not be identified anyhow.

The study problem usually assume that you create the computer model of this situation using threads or processes, and, at least, illustrate the deadlock. It would be good if you illustrate one or more ways to avoid deadlock and prove their correctness, but this is not so important. It would be good to illustrate the probabilistic nature of the problem, which is very important in practice. It's quite easy to interfere with timing the way that the philosophers "live" for many ears without the deadlock, which will still happen, all of a sudden.

I explained you what's required to perform this study, on a practical, programming part. But where is the code?

Yes, I solved the problem several times, using several different environment and tools, including my own implementation of a multithreading system (without dining philosopher, any proof of concepts cannot be considered seriously :-)), but I'm not going write anything for you. If you use someone else's code, or if you find some such code anywhere, it will be equivalent to your total failure. That way, the whole purpose of the problem would be defeated. To get real understanding, you need to do the work. This is a once of lifetime chance, because there is only one dining philosophers problem. And if you find and use some solution created by anyone else, say bye-bye to dining philosophers, and perhaps say bye-bye to multithreading.

—SA


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



亲自尝试,你可能会发现它不是和你想的一样困难!
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!


这篇关于C中的哲学问题代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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