Question: Mutual exclusion and threads synchronization using Semaphore The following are the questions which you have to

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Question: Mutual exclusion and threads synchronization using Semaphore The following are the questions which you have to

Post by answerhappygod »

Question Mutual Exclusion And Threads Synchronization Using Semaphore The Following Are The Questions Which You Have To 1
Question Mutual Exclusion And Threads Synchronization Using Semaphore The Following Are The Questions Which You Have To 1 (106.88 KiB) Viewed 74 times
Question Mutual Exclusion And Threads Synchronization Using Semaphore The Following Are The Questions Which You Have To 2
Question Mutual Exclusion And Threads Synchronization Using Semaphore The Following Are The Questions Which You Have To 2 (95.7 KiB) Viewed 74 times
Question: Mutual exclusion and threads synchronization using Semaphore The following are the questions which you have to provide answer in your assignment report. 1. Imagine a typical scenario that could happen in e-commerce website where two customers are ordering one unit of paracetamol from the same seller at the same time. The initial stock balance of that item is 1 unit, before these customers ordered. It means, logically, only one customer should be able to order this last unit of stock. esk Panadol 650 For Relief offee & Pain Garden tary to U Megan Demam dan kesakitan wegget Tin BO KAPLET Figure 3: [2] For illustration only. The problem can be simulated by using two threads. Assume that: • thread to simulates customer 0 and orders 1 unit of paracetamol. thread t1 simulates customer 1 and orders 1 unit of paracetamol. After a customer ordered the item, the e-commerce webpage checkout cart will first read the stock balance. Assume that the I/O latency consumes 0.1s to do so. Then it will check if there is stock available (balance is larger than 0). If that condition is true, it will deduct one unit of that stock and considered it as a completed order by changing its flag to true. Finally, at the end of main() function, the program will check if flag1 and flag2 are true. If both are true, it means there is an error because both customers managed to order although the stock count was only one unit. Otherwise, there is no error.

A sample source code without semaphore is given. Download the file e- paracetamol.c, compile it (gcc paracetamol.c -o paracetamol -lpthread -lrt) and execute it (./paracetamol) to check if it is working Then, execute ./runner.sh paracetamol to loop the paracetamol program 500 times. The reason why we run this simulation many times is because we want to induce different execution sequence, due to the way a multiprogramming system schedules these two threads. b) Fix the issue by imple nenting semaphore into the paracetamol.c source code. Rename your solation file into paracetamol-solution.c and test the correctness by rur ning your paracetamol-solution program 500 times (i.e. use the runner.sh). Show one variant of possible execution sequences by using a timeline diagram. (20 marks) Hint: An excerpt of correct sample output is shown in the Figure 4.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply