Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

5.36 Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to each process. Exercise 4.20 required vou to

image text in transcribed

5.36 Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to each process. Exercise 4.20 required vou to modify vour solutionto Exercise 3.20 by writing a program that created a number of threads that requested and released process identifiers. Now modify your solution to Exercise 4.20 by ensuring that the data structure used to represent the availability of process identifiers is safe from race conditions. Use Pthreads mutex locks, described in Section 5.9.4 The pthread cond-wait) function is used for waiting on a condition variable. The following code illustrates how a thread can wait for the conditionn ab to become true using a Pthread condition variable: pthread mutex.lock(&mutex); while (a != b) pthread_cond_wait (&mutex, &cond var); pthread mutex.unlock (&mutex); The mutex lock associated with the condition variable must be locked before the pthread.cond wait() function is called, since it is used to protect the data in the conditional clause from a possible race condition. Once this lock is acquired, the thread can check the condition. If the condition is not true, the thread then invokes pthread-cond waitO, passing the mutex lock and the condition variable as parameters. Calling pthread cond wait() releases the mutex lock, thereby allowing another thread to access the shared data and possibly update its value so that the condition clause evaluates to true. (To protect against program errors, it is important to place the conditional clause within a loop so that the condition is rechecked after being signaled.) A thread that modifies the shared data can invoke the pthread.cond signal() function, thereby signaling one thread waiting on the condition variable. This is illustrated below: pthread_mutex_lock (&mutex); pthread-cond-signal (&cond-var); pthread_mutex.unlock (&mutex); It is important to note that the call to pthread-cond-signal() does not release the mutex lock. It is the subsequent call to pthread mutex.unlock) that releases the mutex. Once the mutex lock is released, the signaled thread becomes theowner of the mutex lock and returns control fromthe call to pthread_cond wait(

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 Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago