olution for the producer-consumer problem using semaphores. (a) Which lines of the code in the Figure are considered critical sections? , (b) what would you change in the code to make a deadlock happens
olution for the producer-consumer problem using semaphores. (a) Which lines of the code in the Figure are considered critical sections? , (b) what would you change in the code to make a deadlock happens
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
c-1. The producer-consumer is one of the classical synchronization problems. The Figure shows
a code of a solution for the producer-consumer problem using semaphores.
(a) Which lines of the code in the Figure are considered critical sections? ,
(b) what would you change in the code to make a deadlock happens?,
(c) explain each semaphore used in the code (meaning why each of them is added to the code?
![1#include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <semaphore.h>
6 int buffer[5];
7 int count = 0;
8 sem_t sem1;
9 sem_t sem2;
10 sem_t sem3;
11
12 void producer() {
13
14
15
16
17
18
19
20
21
22
26
27
28
29
30
31
32
33
34
while (1) {
}
23 }
24 void* consumer() {
25
while (1) {
// Produce
}
23445
int x = rand() % 100;
sem_wait (&sem2);
sem_wait (&sem1);
buffer[count] = x;
count++;
sem_post(&sem1);
sem_post(&sem3);
int y = -404;
// Consume
sem_wait (&sem3);
sem_wait (&sem1);
y = buffer[count - 1];
count--;
sem_post(&sem1);
sem_post(&sem2);
printf("Consumed %d\n", y);
35
36}
37
38 int main() {
39
sem_init(&sem1, 0, 1);
40
sem_init(&sem2, 0, 5);
41 sem_init(&sem3, 0, 0);
42 pthread_t th[2];
pthread_create(&th [0], NULL, &producer, NULL);
pthread_create(&th [1], NULL, &consumer, NULL);
45 pthread_join(th[0], NULL);
pthread_join(th[1], NULL);
sem_destroy(&sem1);
46
47
48
sem_destroy (&sem2);
49
sem_destroy(&sem3);
50 return 0;
51}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fd54873c3-f85b-45f8-bf73-b983f4eca4a4%2F615e126f-5fe6-42af-802a-a97f566c6c0c%2F4nwxxk_processed.png&w=3840&q=75)
Transcribed Image Text:1#include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <semaphore.h>
6 int buffer[5];
7 int count = 0;
8 sem_t sem1;
9 sem_t sem2;
10 sem_t sem3;
11
12 void producer() {
13
14
15
16
17
18
19
20
21
22
26
27
28
29
30
31
32
33
34
while (1) {
}
23 }
24 void* consumer() {
25
while (1) {
// Produce
}
23445
int x = rand() % 100;
sem_wait (&sem2);
sem_wait (&sem1);
buffer[count] = x;
count++;
sem_post(&sem1);
sem_post(&sem3);
int y = -404;
// Consume
sem_wait (&sem3);
sem_wait (&sem1);
y = buffer[count - 1];
count--;
sem_post(&sem1);
sem_post(&sem2);
printf("Consumed %d\n", y);
35
36}
37
38 int main() {
39
sem_init(&sem1, 0, 1);
40
sem_init(&sem2, 0, 5);
41 sem_init(&sem3, 0, 0);
42 pthread_t th[2];
pthread_create(&th [0], NULL, &producer, NULL);
pthread_create(&th [1], NULL, &consumer, NULL);
45 pthread_join(th[0], NULL);
pthread_join(th[1], NULL);
sem_destroy(&sem1);
46
47
48
sem_destroy (&sem2);
49
sem_destroy(&sem3);
50 return 0;
51}
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education