SlideShare uma empresa Scribd logo
1 de 9
Theoretical
 A s s i g n m e n t   1
Unbounded-buffer producer-consumer
Problem:
 #define N 100 /*number of slots in the buffer*/
 int count = 0;/*number of items in the buffer*/

 void producer(void)                                  The problem we have here is a race condition.
 {
          int item;

              while(TRUE){                 /*repeated indefinitely*/
              item = produce_item(); /*generates the next item*/
              if(count == N)sleep();      /*If the buffer is full, becomes inactive*/
              insert_item(item);/*put the item in the buffer*/
              count = count + 1;          /*increases the count of items in the buffer*/
              if(count == 1) wakeup(consumer);/*checks whether the buffer was empty*/
      }
 }

 void consumer(void)
 {
           int item;

               while(TRUE) {                       /*checks whether the buffer was empty*/
                     if(count == 0)sleep();       /*if buffer is empty, it goes to inactive state*/
                     item = remove_item();/*removes the element from the buffer*/
                     count = count - 1; /*Decreases the count of items in the buffer*/
                     if(count == N-1)wakeup(producer); /*checks whether the buffer was empty*/
                     consumer_item(item); /*print item*/
          }
 }
Unbounded-buffer producer-consumer

Solution:
  #define N 100 /*number of slots in the buffer*/
  typedef int semaphore; /*semaphores are a kind of int*/
  semaforo mutual = 1;/*controls access to the critical region*/   Solving this problem by semaphores.
  semaforo empty = N;/*count the empty slots*/
  semaforo full = 0;/*count the full slots*/

  void producer(void)
  {
           int item;

      while(TRUE){                /*TRUE is constant 1*/
           item = produce_item(); /* generates something to put in the buffer*/
          down(&empty);
          down(&mutual);
           insert_element(element);
           up(&mutual);
           up(&full);
       }
  }

  void consumer(void)
  {
            int item;

             while(TRUE) {
                 down(&full);
                 down(&mutual);
                 item = remove_consumer();
                  up(&mutual);
                  up(&full);
                   consumer_item(item);
        }
  }
Unbounded-buffer producer-consumer with
locks
 Lock *l;
 Condition *c;
 int avail = 0;
 void consumer(int dummy) {
   while (1) {
                                 This one solution from some ways to
     l->Acquire();
     if (avail == 0) {            solve consumer-producer problem.
       c->Wait(l);
     }
     consume the next unit of data
     avail--;
     l->Release();
   }
 }
 void producer(int dummy) {
   while (1) {
     l->Acquire();
     produce the next unit of data
     avail++;
     c->Signal(l);
     l->Release();
   }
 }
 void main() {
   l = new Lock("l");
   c = new Condition("c");
   Thread *t = new Thread("consumer");
   t->Fork(consumer, 1);
   Thread *t = new Thread("consumer");
   t->Fork(consumer, 2);
   t = new Thread("producer");
   t->Fork(producer, 1);
 }
Eliminate deadlock


                     void do() {
                     l2->Acquire();
                     if (l1->TryAcquire())
 l1->Acquire();
                     {
 l2->Acquire();
                      do something...
 l3->Acquire();
                     } else {
 l4->Acquire();
                     l2->Release();
 l5->Acquire();
                     l1->Acquire();
                     l2->Acquire();
                     do something...
                      l1->Release();
                      }
                     l2->Release();
                     }
How to detect and eliminate deadlock in the
    dining philosophers problem.
  Problem:

Five philosophers are seated around a circular table, each
philosopher has a plate of spaghetti. The spaghetti is so
slippery that a philosopher needs twoforks to eat. Between
each pair of dishes are a fork, when a philosopher is hungry, try
to buy their left and right forks, one at a time, in any order. If
successful in acquiring two forks, eating for a moment, then let
the forks andcontinue to think.
How to detect and eliminate deadlock in the
         dining philosophers problem.
   Solution:
                                                                          void TAKE_FORK(int i) /*i number of philosopher from 0 to N-1*/
#define N           5      /*number of philosophers*/                      {
#define LEFT         (i+N-1)%N /*number of left neighbor*/                              down(&mutex);/*enters the critical region*/
#define RIGHT            (i+1)%N /*number of right neighbor*/                          state[i] = HUNGRY;/*records the philosopher who is eating*/
#define THINKING            0   /*Philosopher is thinking*/                           TRY(i);/*attempts to acquire 2 forks*/
#define HUNGRY 1 /* Philosopher is hungry*/                                            up(&mutex)/*leaves the critical region*/;
#define EATING           2 /*Philosopher is eating*/                                    down(&s[i]);/*crashes if the holders were not acquired*/
typedef int semaphore; /*semaphores are a kind of int*/                   }
int state[N]; /*array that keeps track of the status of all*/             void PUT_FORK(int i) /*i number of philosopher from 0 to N-1*/
semaphore mutex = 1;/*mutual exclusion for critical regions following*/   {
semaphore s[N];/*a semaphore by philosopher*/                                          down(&mutex);/*enters the critical region*/
                                                                                              state[i] = THINKING;/*records the philsopher who is
void philosopher(int i)/*i number of philosopher from 0 to N-1*/          eating*/
{                                                                                      TRY(LEFT);/*attempts to acquire the left fork*/
     while(TRUE){/*repeated indefinitely*/                                              TRY(RIGHT);/*attempts to acquire the right fork*/
           THINKING();/*the philosophers is thinking*/                                 up(&mutex);leaves the critical region*/
           TAKE_FORK(i);/*the philosopher takes the fork*/                }
           EAT();/*the philosopher is eating*/                            void TRY(i) /*i number of philosopher from 0 to N-1*/
           PUT_FORK(i);/*philosopher put the fork*/                       {
     }                                                                    if(state[i] == HUNGRY && state[LEFT] != EATING &&
  }                                                                       state[RIGHT] != EATING){
                                                                          state[i] = EATING;
                                                                          up(&s[i]);
                                                                             }
                                                                          }
Suspend and Resume algorithm


The worst case is when the lock is          The SR algorithm costs twice the
unlocked just after the thread starts the   suspend and resume time. It first spins
suspend.                                    for the suspend and resume time, then
                                            suspends, then gets the lock, then
                                            resume.
 The optimal algorithm just spins until
the lock is unlocked, taking the suspend
and resume time to acquire the lock.
Thanks!


                Our blog:
http://operatingsystems-fime.blogspot.com/

Mais conteúdo relacionado

Último

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Último (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
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
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
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...
 

Assigntment 1 Theoretical

  • 1. Theoretical A s s i g n m e n t 1
  • 2. Unbounded-buffer producer-consumer Problem: #define N 100 /*number of slots in the buffer*/ int count = 0;/*number of items in the buffer*/ void producer(void) The problem we have here is a race condition. { int item; while(TRUE){ /*repeated indefinitely*/ item = produce_item(); /*generates the next item*/ if(count == N)sleep(); /*If the buffer is full, becomes inactive*/ insert_item(item);/*put the item in the buffer*/ count = count + 1; /*increases the count of items in the buffer*/ if(count == 1) wakeup(consumer);/*checks whether the buffer was empty*/ } } void consumer(void) { int item; while(TRUE) { /*checks whether the buffer was empty*/ if(count == 0)sleep(); /*if buffer is empty, it goes to inactive state*/ item = remove_item();/*removes the element from the buffer*/ count = count - 1; /*Decreases the count of items in the buffer*/ if(count == N-1)wakeup(producer); /*checks whether the buffer was empty*/ consumer_item(item); /*print item*/ } }
  • 3. Unbounded-buffer producer-consumer Solution: #define N 100 /*number of slots in the buffer*/ typedef int semaphore; /*semaphores are a kind of int*/ semaforo mutual = 1;/*controls access to the critical region*/ Solving this problem by semaphores. semaforo empty = N;/*count the empty slots*/ semaforo full = 0;/*count the full slots*/ void producer(void) { int item; while(TRUE){ /*TRUE is constant 1*/ item = produce_item(); /* generates something to put in the buffer*/ down(&empty); down(&mutual); insert_element(element); up(&mutual); up(&full); } } void consumer(void) { int item; while(TRUE) { down(&full); down(&mutual); item = remove_consumer(); up(&mutual); up(&full); consumer_item(item); } }
  • 4. Unbounded-buffer producer-consumer with locks Lock *l; Condition *c; int avail = 0; void consumer(int dummy) { while (1) { This one solution from some ways to l->Acquire(); if (avail == 0) { solve consumer-producer problem. c->Wait(l); } consume the next unit of data avail--; l->Release(); } } void producer(int dummy) { while (1) { l->Acquire(); produce the next unit of data avail++; c->Signal(l); l->Release(); } } void main() { l = new Lock("l"); c = new Condition("c"); Thread *t = new Thread("consumer"); t->Fork(consumer, 1); Thread *t = new Thread("consumer"); t->Fork(consumer, 2); t = new Thread("producer"); t->Fork(producer, 1); }
  • 5. Eliminate deadlock void do() { l2->Acquire(); if (l1->TryAcquire()) l1->Acquire(); { l2->Acquire(); do something... l3->Acquire(); } else { l4->Acquire(); l2->Release(); l5->Acquire(); l1->Acquire(); l2->Acquire(); do something... l1->Release(); } l2->Release(); }
  • 6. How to detect and eliminate deadlock in the dining philosophers problem. Problem: Five philosophers are seated around a circular table, each philosopher has a plate of spaghetti. The spaghetti is so slippery that a philosopher needs twoforks to eat. Between each pair of dishes are a fork, when a philosopher is hungry, try to buy their left and right forks, one at a time, in any order. If successful in acquiring two forks, eating for a moment, then let the forks andcontinue to think.
  • 7. How to detect and eliminate deadlock in the dining philosophers problem. Solution: void TAKE_FORK(int i) /*i number of philosopher from 0 to N-1*/ #define N 5 /*number of philosophers*/ { #define LEFT (i+N-1)%N /*number of left neighbor*/ down(&mutex);/*enters the critical region*/ #define RIGHT (i+1)%N /*number of right neighbor*/ state[i] = HUNGRY;/*records the philosopher who is eating*/ #define THINKING 0 /*Philosopher is thinking*/ TRY(i);/*attempts to acquire 2 forks*/ #define HUNGRY 1 /* Philosopher is hungry*/ up(&mutex)/*leaves the critical region*/; #define EATING 2 /*Philosopher is eating*/ down(&s[i]);/*crashes if the holders were not acquired*/ typedef int semaphore; /*semaphores are a kind of int*/ } int state[N]; /*array that keeps track of the status of all*/ void PUT_FORK(int i) /*i number of philosopher from 0 to N-1*/ semaphore mutex = 1;/*mutual exclusion for critical regions following*/ { semaphore s[N];/*a semaphore by philosopher*/ down(&mutex);/*enters the critical region*/ state[i] = THINKING;/*records the philsopher who is void philosopher(int i)/*i number of philosopher from 0 to N-1*/ eating*/ { TRY(LEFT);/*attempts to acquire the left fork*/ while(TRUE){/*repeated indefinitely*/ TRY(RIGHT);/*attempts to acquire the right fork*/ THINKING();/*the philosophers is thinking*/ up(&mutex);leaves the critical region*/ TAKE_FORK(i);/*the philosopher takes the fork*/ } EAT();/*the philosopher is eating*/ void TRY(i) /*i number of philosopher from 0 to N-1*/ PUT_FORK(i);/*philosopher put the fork*/ { } if(state[i] == HUNGRY && state[LEFT] != EATING && } state[RIGHT] != EATING){ state[i] = EATING; up(&s[i]); } }
  • 8. Suspend and Resume algorithm The worst case is when the lock is The SR algorithm costs twice the unlocked just after the thread starts the suspend and resume time. It first spins suspend. for the suspend and resume time, then suspends, then gets the lock, then resume. The optimal algorithm just spins until the lock is unlocked, taking the suspend and resume time to acquire the lock.
  • 9. Thanks! Our blog: http://operatingsystems-fime.blogspot.com/

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n