SlideShare a Scribd company logo
1 of 15
Dining philosophers problem
‫خوردن‬ ‫غذا‬ ‫مسئله‬‫ها‬‫فیلسوف‬
‫اشرفی‬ ‫بهنوش‬
‫یک‬‫مسئله‬‫ی‬‫کالسیک‬‫که‬‫اغلب‬‫در‬‫الگوریتم‬‫های‬‫همروند‬(concurrent)‫برای‬‫نشان‬‫دادن‬
‫مشکالت‬‫و‬‫مسائل‬‫همگام‬‫سازی‬(synchronization)‫و‬‫روش‬‫های‬‫حل‬‫ان‬‫به‬‫کار‬‫میرود‬.
‫این‬‫مسئله‬‫برای‬‫این‬‫طراحی‬‫شده‬‫تا‬‫مشکالت‬‫پرهیز‬‫از‬‫بن‬‫بست‬‫را‬‫نشان‬‫دهد‬.
‫این‬‫مسئله‬‫توسط‬dijkstra‫مطرح‬‫شد‬.
‫مسئله‬ ‫شرح‬:
‫هدف‬:
‫میخواهیم‬‫الگوریتمی‬‫بنویسیم‬‫که‬‫ویژگی‬‫های‬‫زیر‬‫را‬‫دارا‬‫باشد‬:
۱.‫دارای‬‫انحصار‬‫متقابل‬‫باشد‬.(‫یعنی‬‫وقتی‬‫یک‬‫چنگال‬‫در‬‫آن‬‫واحد‬‫فقط‬‫در‬
‫اختیار‬‫یک‬‫فیلسوف‬‫باشد‬)
۲.‫هیچ‬‫یک‬‫از‬‫فیلسوف‬‫ها‬‫گرسنه‬‫نمانند‬.
۳.‫دچار‬‫بن‬‫بست‬‫نشویم‬.
‫ها‬ ‫حل‬ ‫راه‬:
1.‫بخوریند‬ ‫غذا‬ ‫چنجال‬ ‫یک‬ ‫با‬ ‫که‬ ‫دهیم‬ ‫یاد‬ ‫ها‬ ‫فیلسوف‬ ‫به‬ ‫میتوانیم‬.
.2‫میتوانیم‬۵‫کنیم‬ ‫اضافه‬ ‫میز‬ ‫به‬ ‫دیگر‬ ‫چنگال‬(‫هست‬ ‫هم‬ ‫تر‬ ‫بهداشتی‬)!!!!
**‫است‬ ‫ثابت‬ ‫منابع‬ ‫که‬ ‫است‬ ‫این‬ ‫بر‬ ‫فرض‬ ‫اینجا‬(‫کرد‬ ‫زیاد‬ ‫نمیتوان‬ ‫را‬ ‫ها‬ ‫چنگال‬ ‫تعداد‬).‫ی‬ ‫نحوه‬ ‫و‬‫کار‬
‫کنیم‬ ‫اضافه‬ ‫ای‬ ‫تازه‬ ‫دستورالعمل‬ ‫نمیتوانیم‬ ‫و‬ ‫است‬ ‫مشخص‬ ‫ها‬ ‫فرایند‬(‫خور‬ ‫غذا‬ ‫برای‬ ‫ها‬ ‫فیلسوف‬‫به‬ ‫دن‬۲
‫کرد‬ ‫عوض‬ ‫را‬ ‫این‬ ‫نمیتوان‬ ‫و‬ ‫دارند‬ ‫نیزا‬ ‫چنگال‬).
۳.‫ساده‬ ‫الگوریتم‬ ‫یک‬:
۱.‫تا‬‫کند‬ ‫فکر‬ ،‫است‬ ‫نگرفته‬ ‫قرار‬ ‫دسترس‬ ‫در‬ ‫چپ‬ ‫سمت‬ ‫چنگال‬ ‫که‬ ‫وقتی‬.‫دسترس‬ ‫در‬ ‫چپ‬ ‫سمت‬ ‫چنگال‬ ‫که‬ ‫وقتی‬‫قرار‬
‫بردارد‬ ‫را‬ ‫آن‬ ،‫گرفت‬.
۲.‫تا‬‫کند‬ ‫فکر‬ ،‫است‬ ‫نگرفته‬ ‫قرار‬ ‫دسترس‬ ‫در‬ ‫راست‬ ‫سمت‬ ‫چنگال‬ ‫که‬ ‫وقتی‬.‫دس‬ ‫در‬ ‫راست‬ ‫سمت‬ ‫چنگال‬ ‫که‬ ‫وقتی‬‫قرار‬ ‫ترس‬
‫بردارد‬ ‫را‬ ‫آن‬ ،‫گرفت‬.
۳.‫وقتی‬‫بخورد‬ ‫غذا‬ ‫ثابتی‬ ‫زمان‬ ‫مدت‬ ‫یک‬ ‫برای‬ ،‫شد‬ ‫برداشته‬ ‫چنگال‬ ‫دو‬ ‫هر‬ ‫که‬.
۴.‫سپس‬‫دهد‬ ‫قرار‬ ‫میز‬ ‫روی‬ ‫بر‬ ‫را‬ ‫راست‬ ‫سمت‬ ‫چنگال‬.
۵.‫سپس‬‫دهد‬ ‫قرار‬ ‫میز‬ ‫روی‬ ‫بر‬ ‫را‬ ‫چپ‬ ‫سمت‬ ‫چنگال‬.
۶.‫این‬‫کند‬ ‫تکرار‬ ‫ابتدا‬ ‫از‬ ‫را‬ ‫مراحل‬.
‫این‬‫روش‬‫به‬‫وضوح‬‫میتواند‬‫دچار‬‫بن‬‫بست‬(deadlock)‫شود‬....
‫اگر‬‫هر‬۵‫فیلسوف‬‫با‬‫هم‬‫اقدام‬‫به‬‫غذا‬‫خوردن‬‫کنند‬‫هر‬۵‫نفر‬‫چنگال‬‫های‬‫سمت‬‫چپشان‬‫را‬
‫برداشته‬‫و‬‫منتظر‬‫چنگال‬‫سمت‬‫راستشان‬‫می‬‫مانند‬‫در‬‫حالی‬‫که‬‫چنگال‬‫سمت‬‫راستش‬‫ان‬‫هیچ‬
‫وقت‬‫ازاد‬‫نمیشود‬.‫پس‬‫همه‬‫ی‬‫فیلسوف‬‫ها‬‫گرسنه‬‫می‬‫مانند‬.
‫این‬‫الگوریتم‬‫با‬‫راهنما‬‫یا‬‫سمافر‬(semaphor)‫پیاده‬‫سازی‬‫میشود‬(‫ص‬۳۵۸‫کتاب‬.‫شکل‬
۱۲-۶)
‫الگوریتم‬ ‫کردن‬ ‫کامل‬:
‫جدید‬ ‫قانون‬:‫مثال‬ ‫فیلسوف‬ ‫اگر‬۱۰‫و‬ ‫بگذارد‬ ‫زمین‬ ‫را‬ ‫چنگالش‬ ‫نشد‬ ‫ازاد‬ ‫راست‬ ‫چنگال‬ ‫و‬ ‫کرد‬ ‫صبر‬ ‫ثانیه‬
۱۰‫کند‬ ‫اقدام‬ ‫خوردن‬ ‫برای‬ ‫دوباره‬ ‫بعد‬ ‫ثانیه‬.
***‫به‬ ‫است‬ ‫ممکن‬ ‫روش‬ ‫این‬livelock‫شود‬ ‫منتهی‬.
‫جای‬ ‫به‬ ‫را‬ ‫زمان‬ ‫کافیست‬ ‫مشکل‬ ‫این‬ ‫حل‬ ‫برای‬۱۰‫تصادفی‬ ‫صورت‬ ‫به‬ ‫ثانیه‬(random)‫کنیم‬ ‫انتخاب‬.‫با‬
‫میشود‬ ‫حل‬ ‫مشکل‬ ‫کار‬ ‫این‬.
۴.‫راهنما‬ ‫از‬ ‫استفاده‬(semaphor):
‫ها‬ ‫فرآیند‬‫ها‬ ‫فیلسوف‬
‫منابع‬‫ها‬ ‫چنگال‬
‫سمافر‬‫پیشخدمت‬
‫به‬ ‫حداکثر‬ ‫پیشخدمت‬۴‫بروند‬ ‫خوری‬ ‫غذا‬ ‫میز‬ ‫به‬ ‫که‬ ‫دهد‬ ‫می‬ ‫اجازه‬ ‫فیلسوف‬.
۵.‫ناظر‬ ‫از‬ ‫استفاده‬(manitor)
.1‫ها‬ ‫فرآیند‬ ‫که‬ ‫میشود‬ ‫باعث‬ ‫چنگال‬ ‫هر‬ ‫برای‬ ‫شرط‬ ‫متغیر‬ ‫از‬ ‫استفاده‬(‫ها‬ ‫فیلسوف‬)‫بتوانن‬‫منابع‬ ‫برای‬ ‫د‬
(‫ها‬ ‫چنگال‬)‫کنند‬ ‫صبر‬.
.2‫بررسی‬ ‫را‬ ‫چنگال‬ ‫هر‬ ‫نبودن‬ ‫یا‬ ‫بودن‬ ‫موجود‬ ‫وضعیت‬ ‫که‬ ‫دارد‬ ‫وجود‬ ‫دودویی‬ ‫بردار‬ ‫یک‬
‫میکند‬(.true/false)
‫است‬ ‫رویه‬ ‫دو‬ ‫شامل‬ ‫ناظر‬ ‫این‬:get_fork/release_forks
Dining philosophers' problem
Dining philosophers' problem
Dining philosophers' problem

More Related Content

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Featured (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Dining philosophers' problem