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
icon
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}
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
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
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.
Similar questions
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education