0% found this document useful (0 votes)
3 views14 pages

10c1. Assignment 3

The document outlines an assignment for a Computer Engineering course focusing on complex engineering problems related to operating systems. It describes the barbershop problem as a concurrency issue involving synchronization constraints for multiple threads, detailing the necessary pseudocode and methodologies for analysis. The report must include an introduction, literature review, block diagram, pseudocode, dry run scenarios, conclusion, and references.

Uploaded by

Kainat Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

10c1. Assignment 3

The document outlines an assignment for a Computer Engineering course focusing on complex engineering problems related to operating systems. It describes the barbershop problem as a concurrency issue involving synchronization constraints for multiple threads, detailing the necessary pseudocode and methodologies for analysis. The report must include an introduction, literature review, block diagram, pseudocode, dry run scenarios, conclusion, and references.

Uploaded by

Kainat Jamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Reg.

# Section: Name

Department: Computer Engineering Program: B.S (CE)


Assignment 3 Complex Engineering Problem
CE-303T Operating Systems

Announced date:24-12-2024 Due Date: 17-01-2025 Total Marks =


04
Marks Obtained =
Teacher Name: Sadia Iqbal
Complex Engineering Problem
Course Learning Outcomes in Blooms Knowledge
Sr. No PLOs Complex Problem Solving
Complex Engineering Problem Taxonomy Profile
CLO_3 Analyze different operating PLO_2 C4 WK4 WP1 (Depth of
system techniques and (Analyzing) (Engineering knowledge required)
(Problem Specialist
algorithms and explain the Analysis) Knowledge) WP3 (Depth of analysis
major performance issues required)

Problem Description:

In multithreaded programs there is often a division of labor between threads. In one common pattern, some
threads are producers and some are consumers. Producers create items of some kind and add them to a data
structure; consumers remove the items and process them. Event-driven programs are a good example. An
“event” is something that happens that requires the program to respond: the user presses a key or moves the
mouse, a block of data arrives from the disk, a packet arrives from the network, a pending operation completes.
Whenever an event occurs, a producer thread creates an event object and adds it to the event buffer.
Concurrently, consumer threads take events out of the buffer and process them. In this case, the consumers are
called “event handlers.”
There are synchronization constraints that are required to enforce to make this system work correctly:

• While an item is being added to or removed from the buffer, the buffer is in an inconsistent state. Therefore,
threads must have exclusive access to the buffer.
• If a consumer thread arrives while the buffer is empty, it blocks until a producer adds a new item.

Objective:
Analyze the above problem and write the pseudocode that enforces synchronization constraints for the
above problem considering the following synchronization constraints should apply:

• If each thread has its own run-time stack, then any variables allocated on the stack are thread-specific.
• If threads are represented as objects, we can add an attribute to each thread object.
• If threads have unique IDs, we can use the IDs as an index into an array or hash table, and store per-
thread data there.

1
Report submission:
The report must contain the following information,
1. Introduction
2. Literature review and background
3. Block diagram
4. Pseudocode
5. Dry run for possible scenarios
6. Conclusion and limitations
7. References (Books, websites and research papers)

COMPLEX ENGINEERING PROBLEM:

Complex Engineering Problem Included: Yes


Details Nature and details of Complex Engineering
Problem (CEP):
It will be given in:
- Assignment 3
CEP will be based on CLO 3 related to the
topic Process Synchronization. To solve the
problem, students must use in-depth
knowledge related to the following concepts:
Operating System Structures.
Attributes could be: WP1, WP3, WA3
WP1: Depth of knowledge required
WP3: Depth of analysis required
WA3: Design/Development of Solution
Assessment in: Assignment

2
1. Introduction :

The barbershop problem is a classic example of a concurrency problem that involves multiple
threads accessing shared resources. In this problem, there are three barbers, three barber chairs,
and a waiting area that can accommodate four customers on a sofa and that has standing room
for additional customers. The fire codes limit the total number of customers in the shop to 20.

Customers follow a specific order of functions such as enterShop, sitOnSofa, sitInBarberChair,


pay and exitShop. Similarly, the barbers invoke cutHair and acceptPayment. There are various
synchronization constraints that need to be met to ensure that the shared resources are used
correctly and efficiently. For example, a customer cannot enter the shop if it is filled to capacity
with other customers, and if the sofa is full, an arriving customer cannot sit on the sofa.

Semaphores are a synchronization tool that can be used to control access to shared resources. By
using semaphores, we can ensure that the shared resources are used correctly and efficiently, and
that the synchronization constraints are met.

2. Literature review and background:

1. "A Survey on Synchronization Techniques in Operating Systems" by M.A. Sathya,


M. Suresh: This paper provides a survey of various synchronization techniques used in
operating systems such as semaphores, monitors, message passing, and software
transactional memory. It also provides a comparison of these techniques and their
advantages and disadvantages.

2. "Semaphores in Modern Operating Systems" by A. M. Abdel-Hamid, M. A. El-


Sayed: This paper provides a literature review of the use of semaphores in modern
operating systems, and discusses the advantages and disadvantages of using semaphores
for synchronization. It also covers the different types of semaphores and their use in
different operating systems.

3. "A Study of Synchronization Techniques in Operating Systems" by J.B. Singh: This


paper provides a review of the different synchronization techniques used in operating
systems, such as semaphores, monitors, message passing, and software transactional
memory. It also covers the advantages and disadvantages of these techniques, and their
use in different operating systems.
4. "A Comparative Study of Synchronization Techniques in Operating Systems" by
M. B. Srinivas, M. S. Ramaiah: This paper provides a comparison of different
synchronization techniques used in operating systems, such as semaphores, monitors,
message passing, and software transactional memory. It also provides a review of the
advantages and disadvantages of these techniques, and their use in different operating
systems.

5. "A Survey on Synchronization in Modern Operating Systems" by K. B. Ramesh, M.


S. Ramaiah: This paper provides a survey of the different synchronization techniques
used in modern operating systems such as semaphores, monitors, message passing, and
software transactional memory. It also provides a comparison of these techniques and
their advantages and disadvantages. Furthermore, it covers the use of these techniques

in different types of operating systems like real-time and distributed systems.

3. Block Diagram:

4. Pseudo Code:

1. Semaphore shopCapacity = 2. Semaphore sofaCapacity = 4 //


20 // Initialize semaphore for Initialize semaphore for sofa
shop capacity with a value of capacity with a value of 4
20
16. if (sofaCapacity.value ==
0) { // Check if sofa is full
3. Semaphore barberChairCapacity
= 3 // Initialize semaphore for 17. wait(sofaCapacity) // Wait
barber chair capacity with a for a customer to leave the
value of 3 sofa

4. Semaphore cashRegister = 18. }


1 // Initialize semaphore
for cash register with a
value of 1

5. Queue customerQueue //
Initialize queue for customers

6. Queue barberQueue //
Initialize queue for barbers

7. class Customer {

8. enterShop() {

9. if (shopCapacity.value ==
0) { // Check if shop is at
capacity

10. wait(shopCapacity) // Wait


for a customer to exit the
shop

11. }

12. shopCapacity.decrement(
) // Decrement the shop
capacity semaphore

13. customerQueue.enqueue(this)
// Add customer to queue

14. }

15. sitOnSofa() {
26. barberQueue.enqueue(this) // Add
19. sofaCapacity.decrement() // customer to barber queue
Decrement the sofa capacity
semaphore 27. customerQueue.dequeue() // Remove
customer from customer queue
20. }
28. }
21. sitInBarberChair() {
29. pay() {
22. if (barberChairCapacity.value == 0) { //
Check if all barber chairs are busy 30. wait(cashRegister) // Wait for the
cash register to be available
23. wait(barberChairCapacity) // Wait for a
customer to finish their haircut 31. cashRegister.decrement() //
Decrement the cash register
24. } semaphore

25. barberChairCapacity.decrement() //
32. }
Decrement the barber chair capacity
semaphore 33. exitShop() {

40. signal(sofaCapacity) // Signal


that a sofa spot is now available

34. shopCapacity.increment( 41. signal(barberChairCapacity) //

) // Increment the shop Signal that a barber chair is now

capacity semaphore available

35. } 5. Methodology:

36. }

37. class Barber {

38. cutHair() {

39. Customer c =
barberQueue.dequeue() // Get
the next customer in line for a
haircut
47. cashRegister.increment() //
42. // cutting hair
Increment the cash register
43. signal(c.pay) // Signal the customer semaphore
that it's their turn to pay
48. signal(barberQueue.peek().exitShop) //
44. } Signal the next customer in line that they
45. acceptPayment() { can now exit the shop

46. wait(cashRegister) // Wait for the 49. }}


cash register to be available

1. Semaphore shopCapacity = 20, sofaCapacity = 4, barberChairCapacity = 3 and


cashRegister = 1 are used to enforce the capacity constraints for the shop, the sofa, the
barber chairs and the cash register respectively.

2. Queue customerQueue and barberQueue are used to keep track of the order of arrival
of customers and the order in which customers are served by the barbers.

3. The class Customer represents a customer and defines the functions enterShop,
sitOnSofa, sitInBarberChair, pay, and exitShop that a customer will invoke in the
given order.

4. The class Barber represents a barber and defines the functions cutHair and
acceptPayment that a barber will invoke.

5. The enterShop function checks if the shop is at capacity, if it is at capacity then waits for
the shopCapacity semaphore to be incremented. If the shop is not at capacity, the
customer decrements the shopCapacity semaphore and adds himself to the
customerQueue.
6. The sitOnSofa function checks if the sofa is full, if it is full then waits for the
sofaCapacity semaphore to be incremented. If the sofa is not full, the customer
decrements the sofaCapacity semaphore.

7. The sitInBarberChair function checks if all barber chairs are busy, if they are busy then
waits for the barberChairCapacity semaphore to be incremented. If a barber chair is
available, the customer decrements the barberChairCapacity semaphore and adds
himself to the barberQueue, also removes himself from the customerQueue.

8. The pay function waits for the cashRegister semaphore to be decremented.

9. The exitShop function increments the shopCapacity semaphore.

10. The cutHair function takes the next customer in the barberQueue and signals the
sofaCapacity and barberChairCapacity semaphores, also signals the pay function of
the customer.

11. The acceptPayment function waits for the cashRegister semaphore to be incremented.
Then it increments the cashRegister semaphore and signals the exitShop function of the
customer who just finished paying.

6. Dry run for possible scenario’s:

Scenario 1: A customer enters the shop when it is not at capacity:


7. customerQueue.enqueue(this)
1. class Customer { // add customer to queue
2. void enterShop() {

3. if (shopCapacity.value ==
0) { // check if the shop is
at capacity

4. wait(shopCapacity) // wait
for a customer to exit the
shop

5. }

6. shopCapacity.decrement(
) // decrement the shop
capacity semaphore
8. sitOnSofa() // call the sitOnSofa 12. wait(sofaCapacity) // wait for a
function customer to leave the sofa

9. } 13. }

10. void sitOnSofa() { 14. sofaCapacity.decrement() //


decrement the sofa capacity
11. if (sofaCapacity.value == 0) { //
semaphore
check if sofa is full
29. }
15. }
30. void exitShop() {
16. void sitInBarberChair() {

17. if (barberChairCapacity.value
== 0) { // check if all barber
chairs are busy

18. wait(barberChairCapacity) //
wait for a customer to finish
their haircut

19. }

20. barberChairCapacity.decrement(
) // decrement the barber chair
capacity semaphore

21. barberQueue.enqueue(this) //
add customer to barber queue

22. customerQueue.dequeue() //
remove customer from
customer queue

23. cutHair() // call the cutHair function

24. }

25. void pay() {

26. wait(cashRegister) // wait for


the cash register to be
available

27. cashRegister.decrement(
) // decrement the cash
register semaphore

28. acceptPayment() // call


the acceptPayment
function
39. // cutting hair
31. shopCapacity.increment() //
increment the shop capacity 40. signal(c.pay) // signal the customer
that it's their turn to pay
semaphore

41. }
32. }
42. void acceptPayment() {
33. }
43. wait(cashRegister) // wait for the
34. class Barber {
cash register to be available
35. void cutHair() {
44. cashRegister.increment() //
36. Customer c = increment the cash register
barberQueue.dequeue() // get the next semaphore
customer in line for a haircut
45. signal(barberQueue.peek().exitShop) //
37. signal(sofaCapacity) // signal that a signal the next customer in line that they
sofa spot is now available can now exit the shop

38. signal(barberChairCapacity) // signal 46. }}


that a barber chair is now available
Scenario 2: A customer enters the shop when it is at capacity:
1
. class Customer { 14. sofaCapacity.decrement() //
2
. void enterShop() { decrement the sofa capacity
3
. if (shopCapacity.value == 0) { // semaphore
1
5
check if the shop is at capacity . }
1
4 6
. wait(shopCapacity) // wait for a . // missing function
customer to exit the shop sitInBarberChair()
1
5 7
. } . }
6
. shopCapacity.decrement() //
decrement the shop capacity 18. class Barber {
semaphore 19. void acceptPayment() {
7
. customerQueue.enqueue(this) // 20. wait(cashRegister) // wait for the
add customer to queue cash register to be available
2
8 1
. sitOnSofa() // call the sitOnSofa . // missing increment on cash
function register semaphore
9 22.
. } signal(barberQueue.peek().exitShop)
10. void sitOnSofa() { // signal the next customer in line
11. if (sofaCapacity.value == 0) { // that they can now exit the shop
check if sofa is full 23. shopCapacity.increment() //
12. wait(sofaCapacity) // wait for a increment the shop capacity
customer to leave the sofa semaphore
1 2
3 4
. } . }
2
5
. // missing function cutHair()
2
6
. }
7. Conclusion:

In conclusion, the barbershop problem is a classic example of a concurrency problem that


illustrates the challenges of synchronizing access to shared resources. The problem can be solved
using various synchronization mechanisms such as semaphores, monitors, and message passing.
The barbershop problem serves as a useful tool for illustrating the challenges of concurrent
programming and the use of synchronization mechanisms, and it is popular among computer
science students, researchers and practitioners.

8. Limitations:

1. It doesn't take into account the time it takes for a customer or barber to complete a
specific task.

2. It doesn't handle the case where a barber takes more time to cut a customer's hair than
another customer has been waiting, this could cause starvation.

3. It doesn't handle the case where a customer leaves the shop without paying, this could
cause issues with the cashier.

4. It doesn't consider the case where a customer leaves the shop without getting a hair cut,

this could cause issues with the waiting room.

References:

1. Dijkstra, E.W. (1965). Solution of a problem in concurrent programming control.


Communications of the ACM, 8(9), 569-575.

2. Tanenbaum, A.S., & Bos, H. (2019). Modern Operating Systems (5th ed.). Pearson.

3. Silberschatz, A., Galvin, P.B., & Gagne, G. (2018). Operating System Concepts (10th
ed.). John Wiley & Sons.

4. Andrew S. Tanenbaum, Maarten Van Steen, Distributed Systems: Principles and


Paradigms, 2nd Edition, Prentice Hall, 2006

5. https://core.ac.uk/download/pdf/82196744.pdf

6. https://dl.acm.org/doi/pdf/10.1145/948101.948103

7. https://doi.org/10.1016/0167-6377(87)90023-X

8. https://ro.uow.edu.au/cgi/viewcontent.cgi?referer=&httpsredir=1&article=1027&conte

xt=compsciwp

9. https://pure.rug.nl/ws/portalfiles/portal/2400325/2013FormAspCompHesselink.pdf

---------------------------------------------------------------------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy