Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

typedef struct { pthread_mutex_t mutex; /* Add other variables */ } SmartLock; void init_lock (SmartLock* lock) { pthread_mutex_init(&(lock->mutex), NULL); } int lock (SmartLock* lock) {

typedef struct {

pthread_mutex_t mutex;

/* Add other variables */

} SmartLock;

void init_lock(SmartLock* lock) {

pthread_mutex_init(&(lock->mutex), NULL);

}

int lock(SmartLock* lock) {

pthread_mutex_lock(&(lock->mutex)); return 1;

}

void unlock(SmartLock* lock) {

pthread_mutex_unlock(&(lock->mutex)); }

void cleanup() { }

/* * Cleanup any dynamic allocated memory for SmartLock to avoid memory leak * You can assume that cleanup will always be the last function call * in main function of the test cases. */

As shown above, SmartLock will internally hold a pthread_mutex_t instance for correct locking and unlocking mechanism. You need to update init_lock(), lock(), unlock() and cleanup() functions to incorporate the deadlock prevention logic in SmartLock. While APIs for init_lock() and unlock() appear straightforward, the lock() function returns an int value. This value should either be 0or 1 where 1 indicates that the lock got acquired and 0 indicates that the lock was not acquired (in order to prevent deadlocks). This means, even if the program calls lock, it is not always guaranteed to acquire the lock and hence, it must first check the return value before proceeding, as shown below

while(lock(&mySmartLock) == 0);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions